Search code examples
regexvarnishvarnish-vcl

Regex match anything that is not sub-pattern


I have cookies in my HTTP header like so:

Set-Cookie: frontend=ovsu0p8khivgvp29samlago1q0; adminhtml=6df3s767g199d7mmk49dgni4t7; external_no_cache=1; ZDEDebuggerPresent=php,phtml,php3

and I need to extract the 26 character string that comes after frontend (e.g. ovsu0p8khivgvp29samlago1q0). The following regular expression matches that for me:

(?<=frontend=)(.*)(?=;)

However, I am using Varnish Cache and can only use a regex replace. Therefore, to extract that cookie value (26 character frontend string) I need to match all characters that do not match that pattern (so I can replace them with '').

I've done a fair bit of Googling but so far have drawn a blank. I've tried the following

  • Match characters that do not match the pattern I want: [^((?<=frontend=)[A-Za-z0-9]{26}(?=;))] which matches random characters, including the ones I want to preserve

I'd be grateful if someone could point me in the right direction, or note where I might have gone wrong.


Solution

  • The Set-Cookie response header is a bit magical in Varnish, since the backends tend to send multiple headers with the same name. This is prohibited by the RFC, but the defacto way to do it.

    If you are using Varnish 3.0 you can use the Header VMOD, it can parse the response and extract what you need:

    https://github.com/varnish/libvmod-header