.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "VMOD_BLOB" 3 "" "" .SH NAME vmod_blob \- Utilities for the VCL blob type, encoding and decoding .\" . .\" NB: This file is machine generated, DO NOT EDIT! . .\" . .\" Edit ./vmod_blob.vcc and run make instead . .\" . .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .EX import blob [as name] [from \(dqpath\(dq] BLOB decode(ENUM decoding, INT length, STRING encoded) STRING encode(ENUM encoding, ENUM case, BLOB blob) 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) new xblob = blob.blob(ENUM decoding, STRING encoded) BLOB xblob.get() STRING xblob.encode(ENUM encoding, ENUM case) .EE .UNINDENT .UNINDENT .SH DESCRIPTION .sp This VMOD provides utility functions and an object for the VCL data type \fBBLOB\fP, which may contain arbitrary data of any length. .sp Examples: .INDENT 0.0 .INDENT 3.5 .sp .EX sub vcl_init { # Create blob objects from encodings such as base64 or hex. new myblob = blob.blob(BASE64, \(dqZm9vYmFy\(dq); new yourblob = blob.blob(encoded=\(dq666F6F\(dq, decoding=HEX); } sub vcl_deliver { # The .get() method retrieves the BLOB from an object. set resp.http.MyBlob\-As\-Hex = blob.encode(blob=myblob.get(), encoding=HEX); # The .encode() method efficiently retrieves an encoding. set resp.http.YourBlob\-As\-Base64 = yourblob.encode(BASE64); # decode() and encode() functions convert blobs to text and # vice versa at runtime. set resp.http.Base64\-Encoded = blob.encode(BASE64, blob=blob.decode(HEX, encoded=req.http.Hex\-Encoded)); } sub vcl_recv { # transcode() converts from one encoding to another. # case=UPPER specifies upper\-case hex digits A\-F. set req.http.Hex\-Encoded = blob.transcode(decoding=BASE64, encoding=HEX, case=UPPER, encoded=\(dqYmF6\(dq); # transcode() from URL to IDENTITY effects a URL decode. set req.url = blob.transcode(encoded=req.url, decoding=URL); # transcode() from IDENTITY to URL effects a URL encode. set req.http.url_urlcoded = blob.transcode(encoded=req.url, encoding=URL); } .EE .UNINDENT .UNINDENT .SS ENCODING SCHEMES .sp Binary\-to\-text encoding schemes are specified by ENUMs in the VMOD\(aqs constructor, methods and functions. Decodings convert a (possibly concatenated) string into a blob, while encodings convert a blob into a string. .sp ENUM values for an encoding scheme can be one of: .INDENT 0.0 .IP \(bu 2 \fBIDENTITY\fP .IP \(bu 2 \fBBASE64\fP .IP \(bu 2 \fBBASE64URL\fP .IP \(bu 2 \fBBASE64URLNOPAD\fP .IP \(bu 2 \fBBASE64CF\fP .IP \(bu 2 \fBHEX\fP .IP \(bu 2 \fBURL\fP .UNINDENT .sp Empty strings are decoded into a \(dqnull blob\(dq (of length 0), and conversely a null blob is encoded as the empty string. .sp For encodings with \fBHEX\fP or \fBURL\fP, you may also specify a \fIcase\fP ENUM with one of the values \fBLOWER\fP, \fBUPPER\fP or \fBDEFAULT\fP to produce a string with lower\- or uppercase hex digits (in \fB[a\-f]\fP or \fB[A\-F]\fP). The default value for \fIcase\fP is \fBDEFAULT\fP, which for \fBHEX\fP and \fBURL\fP means the same as \fBLOWER\fP\&. .sp The \fIcase\fP ENUM is not relevant for decodings; \fBHEX\fP or \fBURL\fP strings to be decoded as BLOBs may have hex digits in either case, or in mixed case. .sp The \fIcase\fP ENUM MUST be set to \fBDEFAULT\fP for the other encodings (\fBBASE64*\fP and \fBIDENTITY\fP). You cannot, for example, produce an uppercase string by using the \fBIDENTITY\fP scheme with \fBcase=UPPER\fP\&. To change the case of a string, use the \fBstd.toupper()\fP or \fBstd.tolower()\fP functions from \fIvmod_std(3)\fP\&. .SS IDENTITY .sp The simplest encoding converts between the BLOB and STRING data types, leaving the contents byte\-identical. .sp Note that a BLOB may contain a null byte at any position before its end; if such a BLOB is decoded with \fBIDENTITY\fP, the resulting STRING will have a null byte at that position. Since VCL strings, like C strings, are represented with a terminating null byte, the string will be truncated, appearing to contain less data than the original blob. For example: .INDENT 0.0 .INDENT 3.5 .sp .EX # Decode from the hex encoding for \(dqfoo\e0bar\(dq. # The header will be seen as \(dqfoo\(dq. set resp.http.Trunced\-Foo1 = blob.encode(IDENTITY, blob=blob.decode(HEX, encoded=\(dq666f6f00626172\(dq)); .EE .UNINDENT .UNINDENT .sp \fBIDENTITY\fP is the default encoding and decoding. So the above can also be written as: .INDENT 0.0 .INDENT 3.5 .sp .EX # Decode from the hex encoding for \(dqfoo\e0bar\(dq. # The header will be seen as \(dqfoo\(dq. set resp.http.Trunced\-Foo2 = blob.encode(blob=blob.decode(HEX, encoded=\(dq666f6f00626172\(dq)); .EE .UNINDENT .UNINDENT .sp The \fIcase\fP ENUM MUST be set to \fBDEFAULT\fP for \fBIDENTITY\fP encodings. .SS BASE64* .sp The base64 encoding schemes use 4 characters to encode 3 bytes. There are no newlines or maximal line lengths \-\- whitespace is not permitted. .sp The \fBBASE64\fP encoding uses the alphanumeric characters, \fB+\fP and \fB/\fP; and encoded strings are padded with the \fB=\fP character so that their length is always a multiple of four. .sp The \fBBASE64URL\fP encoding also uses the alphanumeric characters, but \fB\-\fP and \fB_\fP instead of \fB+\fP and \fB/\fP, so that an encoded string can be used safely in a URL. This scheme also uses the padding character \fB=\fP\&. .sp The \fBBASE64URLNOPAD\fP encoding uses the same alphabet as \fBBASE6URL\fP, but leaves out the padding. Thus the length of an encoding with this scheme is not necessarily a multiple of four. .sp The \fBBASE64CF\(ga is similar to \(ga\(gaBASE64URL\fP, with the following changes to \fBBASE64\fP: \fB+\fP replaced with \fB\-\fP, \fB/\fP replaced with \fB~\fP and \fB_\fP as the padding character. It is used by a certain CDN provider who also inspired the name. .sp The \fIcase\fP ENUM MUST be set to \fBDEFAULT\fP for for all of the \fBBASE64*\fP encodings. .SS HEX .sp The \fBHEX\fP encoding scheme converts hex strings into blobs and vice versa. For encodings, you may use the \fIcase\fP ENUM to specify upper\- or lowercase hex digits \fBA\fP through \fBf\fP (default \fBDEFAULT\fP, which means the same as \fBLOWER\fP). A prefix such as \fB0x\fP is not used for an encoding and is illegal for a decoding. .sp If a hex string to be decoded has an odd number of digits, it is decoded as if a \fB0\fP is prepended to it; that is, the first digit is interpreted as representing the least significant nibble of the first byte. For example: .INDENT 0.0 .INDENT 3.5 .sp .EX # The concatenated string is \(dqabcdef0\(dq, and is decoded as \(dq0abcdef0\(dq. set resp.http.First = \(dqabc\(dq; set resp.http.Second = \(dqdef0\(dq; set resp.http.Hex\-Decoded = blob.encode(HEX, blob=blob.decode(HEX, encoded=resp.http.First + resp.http.Second)); .EE .UNINDENT .UNINDENT .SS URL .sp The \fBURL\fP decoding replaces any \fB%<2\-hex\-digits>\fP substrings with the binary value of the hexadecimal number after the \fB%\fP sign. .sp The \fBURL\fP encoding implements \(dqpercent encoding\(dq as per RFC3986. The \fIcase\fP ENUM determines the case of the hex digits, but does not affect alphabetic characters that are not percent\-encoded. .SS BLOB decode(ENUM decoding, INT length, STRING encoded) .INDENT 0.0 .INDENT 3.5 .sp .EX BLOB decode( ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} decoding=IDENTITY, INT length=0, STRING encoded ) .EE .UNINDENT .UNINDENT .sp Returns the BLOB derived from the string \fIencoded\fP according to the scheme specified by \fIdecoding\fP\&. .sp If \fIlength\fP > 0, only decode the first \fIlength\fP characters of the encoded string. If \fIlength\fP <= 0 or greater than the length of the string, then decode the entire string. The default value of \fIlength\fP is 0. .sp \fIdecoding\fP defaults to IDENTITY. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX blob.decode(BASE64, encoded=\(dqZm9vYmFyYmF6\(dq); # same with named parameters blob.decode(encoded=\(dqZm9vYmFyYmF6\(dq, decoding=BASE64); # convert string to blob blob.decode(encoded=\(dqfoo\(dq); .EE .UNINDENT .UNINDENT .SS STRING encode(ENUM encoding, ENUM case, BLOB blob) .INDENT 0.0 .INDENT 3.5 .sp .EX STRING encode( ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} encoding=IDENTITY, ENUM {LOWER, UPPER, DEFAULT} case=DEFAULT, BLOB blob ) .EE .UNINDENT .UNINDENT .sp Returns a string representation of the BLOB \fIblob\fP as specified by \fIencoding\fP\&. \fIcase\fP determines the case of hex digits for the \fBHEX\fP and \fBURL\fP encodings, and is ignored for the other encodings. .sp \fIencoding\fP defaults to \fBIDENTITY\fP, and \fIcase\fP defaults to \fBDEFAULT\fP\&. \fBDEFAULT\fP is interpreted as \fBLOWER\fP for the \fBHEX\fP and \fBURL\fP encodings, and is the required value for the other encodings. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX set resp.http.encode1 = blob.encode(HEX, blob=blob.decode(BASE64, encoded=\(dqZm9vYmFyYmF6\(dq)); # same with named parameters set resp.http.encode2 = blob.encode(blob=blob.decode(encoded=\(dqZm9vYmFyYmF6\(dq, decoding=BASE64), encoding=HEX); # convert blob to string set resp.http.encode3 = blob.encode(blob=blob.decode(encoded=\(dqfoo\(dq)); .EE .UNINDENT .UNINDENT .SS STRING transcode(ENUM decoding, ENUM encoding, ENUM case, INT length, STRING encoded) .INDENT 0.0 .INDENT 3.5 .sp .EX 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 ) .EE .UNINDENT .UNINDENT .sp Translates from one encoding to another, by first decoding the string \fIencoded\fP according to the scheme \fIdecoding\fP, and then returning the encoding of the resulting blob according to the scheme \fIencoding\fP\&. \fIcase\fP determines the case of hex digits for the \fBHEX\fP and \fBURL\fP encodings, and is ignored for other encodings. .sp As with \fI\%blob.decode()\fP: If \fIlength\fP > 0, only decode the first \fIlength\fP characters of the encoded string, otherwise decode the entire string. The default value of \fIlength\fP is 0. .sp \fIdecoding\fP and \fIencoding\fP default to IDENTITY, and \fIcase\fP defaults to \fBDEFAULT\fP\&. \fBDEFAULT\fP is interpreted as \fBLOWER\fP for the \fBHEX\fP and \fBURL\fP encodings, and is the required value for the other encodings. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX set resp.http.Hex2Base64\-1 = blob.transcode(HEX, BASE64, encoded=\(dq666f6f\(dq); # same with named parameters set resp.http.Hex2Base64\-2 = blob.transcode(encoded=\(dq666f6f\(dq, encoding=BASE64, decoding=HEX); # URL decode \-\- recall that IDENTITY is the default encoding. set resp.http.urldecoded = blob.transcode(encoded=\(dqfoo%20bar\(dq, decoding=URL); # URL encode set resp.http.urlencoded = blob.transcode(encoded=\(dqfoo bar\(dq, encoding=URL); .EE .UNINDENT .UNINDENT .SS BOOL same(BLOB, BLOB) .sp Returns \fBtrue\fP 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. .sp If the BLOBs are both empty (length is 0 and/or the internal pointer is \fBNULL\fP), then \fI\%blob.same()\fP returns \fBtrue\fP\&. If any non\-empty BLOB is compared to an empty BLOB, then \fI\%blob.same()\fP returns \fBfalse\fP\&. .SS BOOL equal(BLOB, BLOB) .sp Returns true if and only if the two BLOB arguments have equal contents (possibly in different memory regions). .sp As with \fI\%blob.same()\fP: If the BLOBs are both empty, then \fI\%blob.equal()\fP returns \fBtrue\fP\&. If any non\-empty BLOB is compared to an empty BLOB, then \fI\%blob.equal()\fP returns \fBfalse\fP\&. .SS INT length(BLOB) .sp Returns the length of the BLOB. .SS BLOB sub(BLOB, BYTES length, BYTES offset=0) .sp Returns a new BLOB formed from \fIlength\fP bytes of the BLOB argument starting at \fIoffset\fP bytes from the start of its memory region. The default value of \fIoffset\fP is \fB0B\fP\&. .sp \fI\%blob.sub()\fP fails and returns NULL if the BLOB argument is empty, or if \fBoffset + length\fP requires more bytes than are available in the BLOB. .SS new xblob = blob.blob(ENUM decoding, STRING encoded) .INDENT 0.0 .INDENT 3.5 .sp .EX new xblob = blob.blob( ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} decoding=IDENTITY, STRING encoded ) .EE .UNINDENT .UNINDENT .sp Creates an object that contains the BLOB derived from the string \fIencoded\fP according to the scheme \fIdecoding\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX new theblob1 = blob.blob(BASE64, encoded=\(dqYmxvYg==\(dq); # same with named arguments new theblob2 = blob.blob(encoded=\(dqYmxvYg==\(dq, decoding=BASE64); # string as a blob new stringblob = blob.blob(encoded=\(dqbazz\(dq); .EE .UNINDENT .UNINDENT .SS BLOB xblob.get() .sp Returns the BLOB created by the constructor. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX 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()); .EE .UNINDENT .UNINDENT .SS STRING xblob.encode(ENUM encoding, ENUM case) .INDENT 0.0 .INDENT 3.5 .sp .EX STRING xblob.encode( ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF, HEX, URL} encoding=IDENTITY, ENUM {LOWER, UPPER, DEFAULT} case=DEFAULT ) .EE .UNINDENT .UNINDENT .sp Returns an encoding of BLOB created by the constructor, according to the scheme \fIencoding\fP\&. \fIcase\fP determines the case of hex digits for the \fBHEX\fP and \fBURL\fP encodings, and MUST be set to \fBDEFAULT\fP for the other encodings. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .EX # blob as text set resp.http.The\-Blob = theblob1.encode(); # blob as base64 set resp.http.The\-Blob\-b64 = theblob1.encode(BASE64); .EE .UNINDENT .UNINDENT .sp For any \fI\%blob.blob()\fP object, \fIencoding\fP and \fIcase\fP, encodings via the \fI\%xblob.encode()\fP method and the \fI\%blob.encode()\fP function are equal: .INDENT 0.0 .INDENT 3.5 .sp .EX # Always true: blob.encode(ENC, CASE, blob.get()) == blob.encode(ENC, CASE) .EE .UNINDENT .UNINDENT .sp But the \fI\%xblob.encode()\fP 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 \fI\%blob.encode()\fP function computes the encoding on every call, allocating space for the string in Varnish workspaces. .sp 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 \fI\%blob.blob()\fP object. The VMOD\(aqs functions should be used for data that are not known until runtime. .SH ERRORS .sp The encoders, decoders and \fI\%blob.sub()\fP 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 \fBIDENTITY\fP and \fBBASE64*\fP encoding schemes if the \fIcase\fP ENUM is not set to \fBDEFAULT\fP\&. .sp If any of the VMOD\(aqs methods, functions or constructor fail, then VCL failure is invoked, just as if \fBreturn(fail)\fP had been called in the VCL source. This means that: .INDENT 0.0 .IP \(bu 2 If the \fI\%blob.blob()\fP object constructor fails, or if any methods or functions fail during \fBvcl_init{}\fP, then the VCL program will fail to load, and the VCC compiler will emit an error message. .IP \(bu 2 If a method or function fails in any other VCL subroutine besides \fBvcl_synth{}\fP, then control is directed to \fBvcl_synth{}\fP\&. The response status is set to 503 with the reason string \fB\(dqVCL failed\(dq\fP, and an error message will be written to the \fIvsl(7)\fP using the tag \fBVCL_Error\fP\&. .IP \(bu 2 If the failure occurs during \fBvcl_synth{}\fP, then \fBvcl_synth{}\fP is aborted. The response line \fB\(dq503 VCL failed\(dq\fP is returned, and the \fBVCL_Error\fP message is written to the log. .UNINDENT .SH LIMITATIONS .sp The VMOD allocates memory in various ways for new blobs and strings. The \fI\%blob.blob()\fP object and its methods allocate memory from the heap, and hence they are only limited by available virtual memory. .sp The \fI\%blob.encode()\fP, \fI\%blob.decode()\fP and \fI\%blob.transcode()\fP functions allocate Varnish workspace, as does \fI\%blob.sub()\fP for the newly created BLOB. If these functions are failing, as indicated by \(dqout of space\(dq messages in the Varnish log (with the \fBVCL_Error\fP tag), then you will need to increase the varnishd parameters \fBworkspace_client\fP and/or \fBworkspace_backend\fP\&. .sp The \fI\%blob.transcode()\fP 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 \fBthread_pool_stack\fP\&. .SH SEE ALSO .INDENT 0.0 .IP \(bu 2 \fIvarnishd(1)\fP .IP \(bu 2 \fIvcl(7)\fP .IP \(bu 2 \fIvsl(7)\fP .IP \(bu 2 \fIvmod_std(3)\fP .UNINDENT .SH COPYRIGHT .INDENT 0.0 .INDENT 3.5 .sp .EX This document is licensed under the same conditions as Varnish itself. See LICENSE for details. SPDX\-License\-Identifier: BSD\-2\-Clause Authors: Nils Goroll Geoffrey Simmons .EE .UNINDENT .UNINDENT .\" Generated by docutils manpage writer. .