I’ve written a Varnish module (VMOD) to simplify handling of HTTP cookies in Varnish VCL.
In essence it does the usual stuff you’d usually do with regular expressions, but with much simpler syntax and readability.
It works by parsing the req.http.Cookie into a small data structure. Afterwards you can add/delete/filter the list, and finally ask for a combined string back again.
Here is the list of functions:
- cookie.parse(cookiestring) – parse the http.req.Cookie string.
- cookie.get(cookiename) – get the value of a single parsed cookie.
- cookie.set(cookiename, cookievalue) – set the value of a cookie.
- cookie.delete(cookiename) – delete a single cookie.
- cookie.filter_except(filterstring). Delete all cookies but the ones matching the names in the comma-separated filterstring (“cookie1,cookie2,cookie3”.) This should be quite useful.
- cookie.get_string() – return the parsed string.
You can find it here:
Hi Lasse,
Just looking at your module. What is the VMOD_COOKIE_MAGIC used for?
Jeff
Actually, I can see how it is used, but why use the pthread_setspecific/pthread_getspecific instead of the varnish workspace?
Hi Jeff.
Thread local storage is there because we don’t have any known place in any workspace (ws) where the parsed cookies start.
We only use this for a pointer into the WS, where the parsed cookies are kept.
cookie.get doesn’t not work with varnish 3. I get this error if I try and use that method.
Expected an action, ‘if’, ‘{‘ or ‘}’
(‘input’ Line 86 Pos 5)
cookie.get(“cookie1”);
I meant varnish 3.0.4
Hi Patrick.
Try to assign the resulting value to something, I think that will work better.
Very oddly when I tried it this morning, it worked
if(cookie.get(“login”) != “”) {
return (pass);
}