libvmod-cookie; simpler cookie handling in Varnish VCL

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:

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

7 Responses to libvmod-cookie; simpler cookie handling in Varnish VCL

  1. Jeff Williams says:

    Hi Lasse,
    Just looking at your module. What is the VMOD_COOKIE_MAGIC used for?

  2. Jeff Williams says:

    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.

  3. pwaterz says:

    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)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s