Commit 4aa14a09 authored by Nils Goroll's avatar Nils Goroll

default to encoding=IDENTITY and decoding=IDENTITY

Update examples to reflect the new defaults

Add vtc to test all examples from the documentation
parent 02556fed
# looks like -*- perl -*-
varnishtest "test all examples from the vcc/rst"
server s1 {
rxreq
txresp
} -start
varnish v1 -arg "-p http_max_hdr=128" -vcl+backend {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
import std;
############################################################
# top Usage section
sub vcl_init {
# Create blob objects from encodings such as base64 or hex.
new myblob = blobcode.blob(BASE64, "Zm9vYmFy");
new yourblob = blobcode.blob(encoded="666F6F", decoding=HEX);
}
sub vcl_deliver {
# The .get() method retrieves the BLOB from an object.
set resp.http.MyBlob-As-Hex
= blobcode.encode(blob=myblob.get(), encoding=HEXLC);
# 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
= blobcode.encode(BASE64,
blobcode.decode(HEX, req.http.Hex-Encoded));
}
sub vcl_recv {
# transcode() converts from one encoding to another.
set req.http.Hex-Encoded
= blobcode.transcode(decoding=BASE64, encoding=HEXUC, encoded="YmF6");
# transcode() can replace other specific encoding/deconding
# vmods - e.g. vmod_urlcode
set req.url = blobcode.transcode(encoded=req.url, decoding=URL);
set req.http.url_urlcoded
= blobcode.transcode(encoded=req.url, encoding=URLLC);
}
# get output from recv
sub vcl_deliver {
set resp.http.url_urlcoded = req.http.url_urlcoded;
}
############################################################
# IDENTITY
sub vcl_deliver {
set resp.http.Trunced-Foo1
= blobcode.encode(IDENTITY, blobcode.decode(HEX, "666f6f00626172"));
set resp.http.Trunced-Foo2
= blobcode.encode(blob=blobcode.decode(HEX, "666f6f00626172"));
}
############################################################
# HEX
sub vcl_deliver {
set resp.http.First = "abc";
set resp.http.Second = "def0";
set resp.http.Hex-Decoded = blobcode.encode(
HEXLC,
blobcode.decode(HEX, resp.http.First + resp.http.Second));
}
############################################################
# encode - also contains decode examples
sub vcl_deliver {
set resp.http.encode1
= blobcode.encode(HEXLC, blobcode.decode(BASE64, "Zm9vYmFyYmF6"));
# same with named parameters
set resp.http.encode2
= blobcode.encode(blob=blobcode.decode(encoded="Zm9vYmFyYmF6",
decoding=BASE64),
encoding=HEXLC);
# convert blob to string
set resp.http.encode3
= blobcode.encode(blob=blobcode.decode(encoded="foo"));
}
############################################################
# transcode
sub vcl_deliver {
set resp.http.Hex2Base64-1 = blobcode.transcode(HEX, BASE64, "666f6f");
# same with named parameters
set resp.http.Hex2Base64-2
= blobcode.transcode(encoded="666f6f",
encoding=BASE64, decoding=HEX);
# replacement for urlcode.decode("foo%20bar")
set resp.http.urldecoded
= blobcode.transcode(encoded="foo%20bar", decoding=URL);
# replacement for urlcode.encode("foo bar")
set resp.http.urlencoded
= blobcode.transcode(encoded="foo bar", encoding=URLLC);
}
############################################################
# version
sub vcl_deliver {
std.log("Using VMOD blobcode version " + blobcode.version());
}
############################################################
# blob init + .get + .encode
sub vcl_init {
new theblob1 = blobcode.blob(BASE64, "YmxvYg==");
# same with named arguments
new theblob2 = blobcode.blob(encoded="YmxvYg==", decoding=BASE64);
# string as a blob
new stringblob = blobcode.blob(encoded="bazz");
}
sub vcl_deliver {
# .get
set resp.http.The-Blob1 =
blobcode.encode(blob=theblob1.get());
set resp.http.The-Blob2 =
blobcode.encode(blob=theblob2.get());
set resp.http.The-Stringblob =
blobcode.encode(blob=stringblob.get());
# .encode
# blob as text
set resp.http.The-Blob = theblob1.encode();
# blob as base64
set resp.http.The-Blob-b64 = theblob1.encode(BASE64);
}
} -start
logexpect l1 -v v1 {
expect * 1001 VCL_Log "^Using VMOD blobcode version 0.1"
} -start
client c1 {
txreq -url "/foo%20bar"
rxresp
expect resp.status == 200
expect resp.http.MyBlob-As-Hex == "666f6f626172"
expect resp.http.YourBlob-As-Base64 == "Zm9v"
expect resp.http.Base64-Encoded == "YmF6"
expect resp.http.url_urlcoded == "%2ffoo%20bar"
expect resp.http.Trunced-Foo1 == "foo"
expect resp.http.Trunced-Foo2 == "foo"
expect resp.http.First == "abc"
expect resp.http.Second == "def0"
expect resp.http.Hex-Decoded == "0abcdef0"
expect resp.http.encode1 == "666f6f62617262617a"
expect resp.http.encode2 == "666f6f62617262617a"
expect resp.http.encode3 == "foo"
expect resp.http.Hex2Base64-1 == "Zm9v"
expect resp.http.Hex2Base64-2 == "Zm9v"
expect resp.http.urldecoded == "foo bar"
expect resp.http.urlencoded == "foo%20bar"
expect resp.http.The-Blob1 == "blob"
expect resp.http.The-Blob2 == "blob"
expect resp.http.The-Stringblob == "bazz"
expect resp.http.The-Blob == "blob"
expect resp.http.The-Blob-b64 == "YmxvYg=="
} -run
logexpect l1 -wait
......@@ -27,11 +27,11 @@ varnish v1 -vcl {
set req.http.b64allurl =
{"AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTTUUUUVVVVWWWWXXXXYYYYZZZZaaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrrssssttttuuuuvvvvwwwwxxxxyyyyzzzz0000111122223333444455556666777788889999----____"};
set resp.http.id2id
= blobcode.transcode(IDENTITY, IDENTITY, "Hello world");
set resp.http.id2id
= blobcode.transcode(encoded="Hello world");
set resp.http.id2b64
= blobcode.transcode(IDENTITY, BASE64, req.http.hobbes);
= blobcode.transcode(encoded=req.http.hobbes, encoding=BASE64);
set resp.http.id2b64nopad
= blobcode.transcode(IDENTITY, BASE64URLNOPAD, req.http.hobbes);
......
#-
# Copyright (c) 2015 UPLEX Nils Goroll Systemoptimierung
# Copyright (c) 2015-2016 UPLEX Nils Goroll Systemoptimierung
# All rights reserved
#
# Authors: Nils Goroll <nils.goroll@uplex.de>
......@@ -14,27 +14,35 @@ $Module blobcode 3 binary-to-text encodings and decodings for the VCL blob type
sub vcl_init {
# Create blob objects from encodings such as base64 or hex.
new myblob = blobcode.blob(decoding=BASE64, encoded="Zm9vYmFy");
new yourblob = blobcode.blob(decoding=HEX, encoded="666F6F");
new myblob = blobcode.blob(BASE64, "Zm9vYmFy");
new yourblob = blobcode.blob(encoded="666F6F", decoding=HEX);
}
sub vcl_recv {
sub vcl_deliver {
# The .get() method retrieves the BLOB from an object.
set req.http.MyBlob-As-Hex
= blobcode.encode(encoding=HEX, blob=myblob.get());
set resp.http.MyBlob-As-Hex
= blobcode.encode(blob=myblob.get(), encoding=HEXLC);
# The .encode() method efficiently retrieves an encoding.
set req.http.YourBlob-As-Base64 = yourblob.encode(BASE64);
set resp.http.YourBlob-As-Base64 = yourblob.encode(BASE64);
# decode() and encode() functions convert blobs to text and
# vice versa at runtime.
set req.http.Base64-Encoded
set resp.http.Base64-Encoded
= blobcode.encode(BASE64,
blobcode.decode(HEX, req.http.Hex-Encoded));
}
sub vcl_recv {
# transcode() converts from one encoding to another.
set req.http.Hex-Encoded
= blobcode.transcode(decoding=BASE64, encoding=HEX, "YmF6");
= blobcode.transcode(decoding=BASE64, encoding=HEXUC, encoded="YmF6");
# transcode() can replace other specific encoding/deconding
# vmods - e.g. vmod_urlcode
set req.url = blobcode.transcode(encoded=req.url, decoding=URL);
set req.http.url_urlcoded
= blobcode.transcode(encoded=req.url, encoding=URLLC);
}
......@@ -92,9 +100,16 @@ blob. For example::
# Decode from the hex encoding for "foo\0bar".
# The header will be seen as "foo".
set req.http.Trunced-Foo
= blobcode.encode(encoding=IDENTITY,
blobcode.decode(decoding=HEX, "666f6f00626172"));
set resp.http.Trunced-Foo1
= blobcode.encode(IDENTITY, blobcode.decode(HEX, "666f6f00626172"));
Because the IDENTITY is the default encoding and decoding, the above
can also be written as::
# Decode from the hex encoding for "foo\0bar".
# The header will be seen as "foo".
set resp.http.Trunced-Foo2
= blobcode.encode(blob=blobcode.decode(HEX, "666f6f00626172"));
BASE64*
-------
......@@ -131,11 +146,11 @@ interpreted as representing the least significant nibble of the first
byte. For example::
# The concatenated string is "abcdef0", and is decoded as "0abcdef0".
set req.http.First = "abc";
set req.http.Second = "def0";
set req.http.Hex-Decoded
= blobcode.encode(IDENTITY, blobcode.decode(HEX,
req.http.First + req.http.Second));
set resp.http.First = "abc";
set resp.http.Second = "def0";
set resp.http.Hex-Decoded
= blobcode.encode(HEXLC, blobcode.decode(HEX,
resp.http.First + resp.http.Second));
URL*
----
......@@ -143,18 +158,27 @@ URL*
*TODO*
$Function BLOB decode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEX, URL} decoding, STRING_LIST encoded)
HEX, URL} decoding="IDENTITY", STRING_LIST encoded)
Description
Returns the BLOB derived from the string ``encoded``
according to the scheme specified by ``decoding``.
Example
``blobcode.decode(decoding=BASE64, encoded="Zm9vYmFyYmF6")``
``decoding`` defaults to IDENTITY
Example::
blobcode.decode(BASE64, "Zm9vYmFyYmF6");
# same with named parameters
blobcode.decode(encoded="Zm9vYmFyYmF6", decoding=BASE64);
# convert string to blob
blobcode.decode(encoded="foo");
$Function BLOB decode_n(INT n,
ENUM {IDENTITY, BASE64, BASE64URL,
BASE64URLNOPAD, HEX, URL} decoding,
BASE64URLNOPAD, HEX, URL} decoding="IDENTITY",
STRING_LIST encoded)
Description
......@@ -163,37 +187,66 @@ Description
the string, then return the same result as ``decode()``.
$Function STRING encode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEXUC, HEXLC, URLUC, URLLC} encoding, BLOB blob)
HEXUC, HEXLC, URLUC, URLLC} encoding="IDENTITY",
BLOB blob)
Description
Returns a string representation of the BLOB ``blob`` as
specifed by ``encoding``.
Example
``set req.http.Hex = blobcode.encode(encoding=HEXLC,``
``blob=blobcode.decode(BASE64, "Zm9vYmFyYmF6");``
``encoding`` defaults to IDENTITY
Example::
set resp.http.encode1
= blobcode.encode(HEXLC, blobcode.decode(BASE64, "Zm9vYmFyYmF6"));
# same with named parameters
set resp.http.encode2
= blobcode.encode(blob=blobcode.decode(encoded="Zm9vYmFyYmF6",
decoding=BASE64),
encoding=HEXLC);
# convert blob to string
set resp.http.encode3
= blobcode.encode(blob=blobcode.decode(encoded="foo"));
$Function STRING transcode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEX, URL} decoding,
HEX, URL} decoding="IDENTITY",
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEXUC, HEXLC, URLUC, URLLC} encoding,
HEXUC, HEXLC, URLUC, URLLC} encoding="IDENTITY",
STRING_LIST encoded)
Description
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``.
according to the scheme ``encoding``.
``decoding`` and ``encoding`` default to IDENTITY
Example::
set resp.http.Hex2Base64-1 = blobcode.transcode(HEX, BASE64, "666f6f");
Example
``set req.http.Hex2Base64 = blobcode.transcode(decoding=HEX,``
``encoding=BASE64, encoded="666f6f");``
# same with named parameters
set resp.http.Hex2Base64-2
= blobcode.transcode(encoded="666f6f",
encoding=BASE64, decoding=HEX);
# replacement for urlcode.decode("foo%20bar")
set resp.http.urldecoded
= blobcode.transcode(encoded="foo%20bar", decoding=URLLC);
# replacement for urlcode.encode("foo bar")
set resp.http.urlencoded
= blobcode.transcode(encoded="foo bar", encoding=URL);
$Function STRING transcode_n(INT n,
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEX, URL} decoding,
HEX, URL} decoding="IDENTITY",
ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEXUC, HEXLC, URLUC, URLLC} encoding,
HEXUC, HEXLC, URLUC, URLLC} encoding="IDENTITY",
STRING_LIST encoded)
Description
......@@ -205,41 +258,62 @@ $Function STRING version()
Description
Returns the version string for this VMOD.
Example
``std.log("Using VMOD blobcode version " + blobcode.version());``
Example::
std.log("Using VMOD blobcode version " + blobcode.version());
$Object blob(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, HEX,
URL} decoding,
URL} decoding="IDENTITY",
STRING_LIST encoded)
Prototype
Prototype::
new OBJ = blobcode.blob(ENUM decoding, STRING_LIST encoded)
Description
Creates an object that contains the BLOB derived from the
string ``encoded`` according to the scheme ``decoding``.
Example
``new theblob = blobcode.blob(BASE64, "YmxvYg==");``
Example::
new theblob1 = blobcode.blob(BASE64, "YmxvYg==");
# same with named arguments
new theblob2 = blobcode.blob(encoded="YmxvYg==", decoding=BASE64);
# string as a blob
new stringblob = blobcode.blob(encoded="bazz");
$Method BLOB .get()
Description
Returns the BLOB created by the constructor.
Example
``set resp.http.The-Blob``
``= blobcode.encode(IDENTITY, theblob.get());``
Example::
set resp.http.The-Blob1 =
blobcode.encode(blob=theblob1.get());
set resp.http.The-Blob2 =
blobcode.encode(blob=theblob2.get());
set resp.http.The-Stringblob =
blobcode.encode(blob=stringblob.get());
$Method STRING .encode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, HEXUC,
HEXLC, URLUC, URLLC} encoding)
HEXLC, URLUC, URLLC} encoding="IDENTITY")
Description
Returns an encoding of BLOB created by the constructor,
according to the scheme ``encoding``.
Example
``set req.http.The-Blob = theblob.encode(IDENTITY);``
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`` object and encoding ``ENC``, encodings via the ``.encode()``
method and the ``encode()`` function are equal::
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment