- VMOD blob - Utilities for the VCL blob t·续
- STRING transcode(ENUM decoding, ENUM encoding, ENUM case, INT length, STRING encoded)#
- BOOL same(BLOB, BLOB)#
- BOOL equal(BLOB, BLOB)#
- INT length(BLOB)#
- BLOB sub(BLOB, BYTES length, BYTES offset=0)#
- new xblob = blob.blob(ENUM decoding, STRING encoded)#
- BLOB xblob.get()#
- STRING xblob.encode(ENUM encoding, ENUM case)#
- ERRORS#
- LIMITATIONS#
- SEE ALSO#
- COPYRIGHT#
VMOD blob - Utilities for the VCL blob t·续
STRING transcode(ENUM decoding, ENUM encoding, ENUM case, INT length, STRING encoded)#
STRING transcode(
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} decoding=IDENTITY,
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} encoding=IDENTITY,
ENUM {LOWER, UPPER, DEFAULT} case=DEFAULT,
INT length=0,
STRING encoded
)
Translates from one encoding to another, by first decoding the string encoded according to the scheme decoding , and then returning the encoding of the resulting blob according to the scheme encoding. case determines the case of hex digits for the HEX and URL encodings, and is ignored for other encodings.
As with blob.decode(): If length > 0, only decode the first length characters of the encoded string, otherwise decode the entire string. The default value of length is 0.
decoding and encoding default to IDENTITY, and case defaults to DEFAULT. DEFAULT is interpreted as LOWER for the HEX and URL encodings, and is the required value for the other encodings.
Example:
set resp.http.Hex2Base64-1
= blob.transcode(HEX, BASE64, encoded="666f6f");
# same with named parameters
set resp.http.Hex2Base64-2
= blob.transcode(encoded="666f6f",
encoding=BASE64, decoding=HEX);
# URL decode -- recall that IDENTITY is the default encoding.
set resp.http.urldecoded
= blob.transcode(encoded="foo%20bar", decoding=URL);
# URL encode
set resp.http.urlencoded
= blob.transcode(encoded="foo bar", encoding=URL);
BOOL same(BLOB, BLOB)#
Returns true if and only if the two BLOB arguments are the same object, i.e. they specify exactly the same region of memory, or both are empty.
If the BLOBs are both empty (length is 0 and/or the internal pointer is NULL), then blob.same() returns true. If any non-empty BLOB is compared to an empty BLOB, then blob.same() returns false.
BOOL equal(BLOB, BLOB)#
Returns true if and only if the two BLOB arguments have equal contents (possibly in different memory regions).
As with blob.same(): If the BLOBs are both empty, then blob.equal() returns true. If any non-empty BLOB is compared to an empty BLOB, then blob.equal() returns false.
INT length(BLOB)#
Returns the length of the BLOB.
BLOB sub(BLOB, BYTES length, BYTES offset=0)#
Returns a new BLOB formed from length bytes of the BLOB argument starting at offset bytes from the start of its memory region. The default value of offset is 0B.
blob.sub() fails and returns NULL if the BLOB argument is empty, or if offset + length requires more bytes than are available in the BLOB.
new xblob = blob.blob(ENUM decoding, STRING encoded)#
new xblob = blob.blob(
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} decoding=IDENTITY,
STRING encoded
)
Creates an object that contains the BLOB derived from the string encoded according to the scheme decoding.
Example:
new theblob1 = blob.blob(BASE64, encoded="YmxvYg==");
# same with named arguments
new theblob2 = blob.blob(encoded="YmxvYg==", decoding=BASE64);
# string as a blob
new stringblob = blob.blob(encoded="bazz");
BLOB xblob.get()#
Returns the BLOB created by the constructor.
Example:
set resp.http.The-Blob1 =
blob.encode(blob=theblob1.get());
set resp.http.The-Blob2 =
blob.encode(blob=theblob2.get());
set resp.http.The-Stringblob =
blob.encode(blob=stringblob.get());
STRING xblob.encode(ENUM encoding, ENUM case)#
STRING xblob.encode(
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} encoding=IDENTITY,
ENUM {LOWER, UPPER, DEFAULT} case=DEFAULT
)
Returns an encoding of BLOB created by the constructor, according to the scheme encoding. case determines the case of hex digits for the HEX and URL encodings, and MUST be set to DEFAULT for the other encodings.
Example:
# blob as text
set resp.http.The-Blob = theblob1.encode();
# blob as base64
set resp.http.The-Blob-b64 = theblob1.encode(BASE64);
For any blob.blob() object, encoding and case, encodings via the xblob.encode() method and the blob.encode() function are equal:
# Always true:
blob.encode(ENC, CASE, blob.get()) == blob.encode(ENC, CASE)
But the xblob.encode() object method is more efficient – the encoding is computed once and cached (with allocation in heap memory), and the cached encoding is retrieved on every subsequent call. The blob.encode() function computes the encoding on every call, allocating space for the string in Varnish workspaces.
So if the data in a BLOB are fixed at VCL initialization time, so that its encodings will always be the same, it is better to create a blob.blob() object. The VMOD’s functions should be used for data that are not known until runtime.
ERRORS#
The encoders, decoders and blob.sub() may fail if there is insufficient space to create the new blob or string. Decoders may also fail if the encoded string is an illegal format for the decoding scheme. Encoders will fail for the IDENTITY and BASE64* encoding schemes if the case ENUM is not set to DEFAULT.
If any of the VMOD’s methods, functions or constructor fail, then VCL failure is invoked, just as if return(fail) had been called in the VCL source. This means that:
- If the blob.blob() object constructor fails, or if any methods or functions fail during
vcl_init{}, then the VCL program will fail to load, and the VCC compiler will emit an error message. - If a method or function fails in any other VCL subroutine besides
vcl_synth{}, then control is directed tovcl_synth{}. The response status is set to 503 with the reason string"VCL failed", and an error message will be written to the VSL using the tagVCL_Error. - If the failure occurs during
vcl_synth{}, thenvcl_synth{}is aborted. The response line"503 VCL failed"is returned, and theVCL_Errormessage is written to the log.
LIMITATIONS#
The VMOD allocates memory in various ways for new blobs and strings. The blob.blob() object and its methods allocate memory from the heap, and hence they are only limited by available virtual memory.
The blob.encode(), blob.decode() and blob.transcode() functions allocate Varnish workspace, as does blob.sub() for the newly created BLOB. If these functions are failing, as indicated by “out of space” messages in the Varnish log (with the VCL_Error tag), then you will need to increase the varnishd parameters workspace_client and/or workspace_backend.
The blob.transcode() function also allocates space on the stack for a temporary BLOB. If this function causes stack overflow, you may need to increase the varnishd parameter thread_pool_stack.
SEE ALSO#
COPYRIGHT#
This document is licensed under the same conditions as Varnish itself.
See LICENSE for details.
SPDX-License-Identifier: BSD-2-Clause
Authors: Nils Goroll <nils.goroll@uplex.de>
Geoffrey Simmons <geoffrey.simmons@uplex.de>
