In Varnish, how can I read the Set-Cookie response header?

Posted by Adam Friedman on Stack Overflow See other posts from Stack Overflow or by Adam Friedman
Published on 2012-10-09T20:09:07Z Indexed on 2012/10/09 21:38 UTC
Read the original article Hit count: 225

I am trying to detect if my application has set a cookie that holds an "alert message" for the user on the next page, where the Javascript displays it if detected.

In my vcl_fetch(), I need to detect if the specific cookie value "alert_message" appears anywhere in the Set-Cookie header (presumably in the VCL variable beresp.http.Set-Cookie). If detected, then I do not want to cache that next page (since Varnish strips the Set-Cookie header by default, which would obliterate the alert message before it makes it back to the browser).

So here is my simple test:

if(beresp.http.Set-Cookie ~ "alert_message") {
    set req.http.no_cache = 1;
}

Strangely, it fails to evaluate to true.

So I throw the variable into the Server header to see what it looks like:

set beresp.http.Server = " MyApp Varnish implementation - test reading set-cookie: "+beresp.http.Set-Cookie;

But for some reason this only displays the FIRST Set-Cookie line in the response headers.

Here are the relevant response headers:

Server: MyApp Varnish implementation - test reading cookie: elstats_session=7d7279kjmsnkel31lre3s0vu24; expires=Wed, 10-Oct-2012 00:03:32 GMT; path=/; HttpOnly
Set-Cookie:app_session=7d7279kjmsnkel31lre3s0vu24; expires=Wed, 10-Oct-2012 00:03:32 GMT; path=/; HttpOnly
Set-Cookie:alert_message=Too+many+results.; expires=Tue, 09-Oct-2012 20:13:32 GMT; path=/; domain=.my.app.com
Set-Cookie:alert_key=flash_error; expires=Tue, 09-Oct-2012 20:13:32 GMT; path=/; domain=.my.app.com
Vary:Accept-Encoding

How do I read and run string detection on ALL Set-Cookie header lines?

© Stack Overflow or respective owner

Related posts about cookies

Related posts about http-headers