vmod_blob / vmod_blobobj for encoding/decoding to/from blobs?

Nils Goroll slink at schokola.de
Tue Jun 16 19:21:36 CEST 2015


ever since we have VCL BLOBs, I wanted to write a new digest module which takes
blobs as arguments and return values and leaves all the encode/decode stuff to
some encode/decode module.

So the encode/decode module is first to be done and I have been pondering how to
best design the interface, but none of the ideas I could come up with quite
matched my expectations.

So I re-interated and got to the idea of a functional-style vmod_blob to create
blobs from encoded stings and vice versa and an oo-style vmod_blobobj to do the
same oo-style.

The only disadvantage I can see with this approach is that the simple "decode
this string as a string" function looks clumsy.

As I'd be about to "claim" the quite generic name "blob" and because
encoding/decoding is so fundamental, I'd really like to hear opinions about this

vcc interfaces and some sample code mocks follow, see
http://etherpad.wikimedia.org/p/libvmod_code.vcc for my full WIP document


/* functional */
$Module blob
$Function BLOB from(ENUM { IDENTITY, BASE64, URL, HEX, ... }, STRING_LIST)

/* oo */
$Module blobobj
$Object variable()
$Object const(ENUM { IDENTITY, BASE64, URL, HEX, ... }, STRING_LIST)

// oo getters
$Method BLOB .get()
$Method STRING .as(ENUM { IDENTITY, BASE64, URL, HEXLC, ... });

// oo setters for variable() objects - for const log warning and have no effect
$Method VOID .set(BLOB)
$Method VOID .set_from(ENUM { IDENTITY, BASE64, URL, HEX, ... }, STRING_LIST)
// functional


// functional
set req.http.x-digest = blob.as(BASE64, digest4.hmac(MD5,
    blob.from(HEX, "2193763978af5623bc42"), # key
    blob.from(HEX, req.http.value)));

// a clumsy way of writing ..: = decode.from(URL, req.url);
set req.http.x-decoded-url = blob.as(IDENTITY, blob.from(URL, req.url));

// oo
sub vcl_init {
        new tohash = blobobj.variable();
        new secret_key = blobobj.const(HEX, "2193763978af5623bc42");
        new hashval = blobobj.variable();

sub vcl_recv {
        // could also write encoded.set(blob.from(....))
        tohash.set_from(HEX, req.http.value);
        hashval.set(digest4.hmac(MD5, secret_key.get, tohash.get));
        set req.http.x-digest = hashval.as(BASE64);

More information about the varnish-dev mailing list