A quick memo about useful informations I found on Varnish this afternoon after digging in the VCL system. I prefer writing them somewhere before forgetting…
The list of variables available in VCL for each context is available in libvcl/vcc_obj.c which seems to be generated by lib/libvcl/vcc_gen_fixed_token.tcl.
In vcc_gen_fixed_token.vcl the following code :
{ client.ip IP
RO
all
"const struct sess *"
}
Results in the following C code:
{ "client.ip", IP, 9,
"VRT_r_client_ip(sp)",
NULL,
V_RO, 0,
VCL_MET_RECV | VCL_MET_PIPE | VCL_MET_PASS | VCL_MET_HASH
| VCL_MET_MISS | VCL_MET_HIT | VCL_MET_FETCH | VCL_MET_DELIVER
| VCL_MET_ERROR
},
“client.ip” is the directive available in a VCL file, “RO” means Read Only – “RW” (Read Write is available as well) – ‘all’ means it is available in all contexts in the VCL file :
VCL_MET_RECV | VCL_MET_PIPE | VCL_MET_PASS | VCL_MET_HASH | VCL_MET_MISS | VCL_MET_HIT | VCL_MET_FETCH | VCL_MET_DELIVER | VCL_MET_ERROR
The list of functions one can use directly (between C{ … }C tags) in VCL seems is available in include/vrt_obj.h VRT_SetHdr and VRT_GetHdr are available in include/vrt.h (for the declaration) and in bin/varnishd/cache_vrt.c.
It is possible to change the TTL of an item directly by using a few lines of C code, like for example :
if (obj.http.My-Header)
{
C{
char *ttl;
ttl = VRT_GetHdr(sp, HDR_OBJ, "\012My-Header:");
VRT_l_obj_ttl(sp, atoi(ttl));
}C
remove obj.http.My-Header;
}
}
The “\012″ prepended to “My-Header” is the octal version of the length of the “My-Header” string.
The “sp” variable is the variable describing the current session.
HDR_OBJ is a constant available in in include/vrt.h :
enum gethdr_e { HDR_REQ, HDR_RESP, HDR_OBJ, HDR_BEREQ, HDR_BERESP };
It is possible to define custom HTTP headers by using the VRT_SetHdr function available in bin/varnishd/cache_vrt.c.
For example :
C{
const char *contentMD5 = "12345";
VRT_SetHdr(sp, HDR_RESP, "\014Content-MD5:", contentMD5, vrt_magic_string_end);
}C
Reading the VCC code generated from a VCL file is available by running :
varnishd -d -f path/to/file.vcl -C
Useful links:
- Octal/Decimal converter (Yes, it is good to be lazy :p)












