Remove all but specific Set-Cookie headers

Ruslan Sivak russ at vshift.com
Thu Oct 28 00:10:02 CEST 2010


http://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies

sub vcl_recv {
  if (!req.http.Cookie) {
    return (pass);
  }
  if (req.http.Cookie) {
    set req.http.Cookie = ";" req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie,
";(COOKIE1|COOKIE2)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
    return(lookup);
   }
}


What would be the equivalent sub vcl_fetch?  This is what I have, but it
seems to only work for COOKIE1.  I think because the Cookie header comes
from the browser all in one line, but there are multiple Set-Cookie
headers. 

sub vcl_fetch {
  set beresp.ttl=5d;
  if (beresp.http.set-cookie) {
    set beresp.http.set-cookie = ";" beresp.http.set-cookie;
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, ";
+", ";");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie,
";(COOKIE1|COOKIE2)=", "; \1=");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, ";[^
][^;]*", "");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "^[;
]+|[; ]+$", "");

    if (beresp.http.set-cookie == "") {
        remove beresp.http.set-cookie;
    }
    return (deliver);
   }
}

Russ




More information about the varnish-misc mailing list