Pull cookie values without regular expressions


#1

Fastly shipped a pretty cool way to allow you to process cookies (and other headers) without using big ugly regular expressions.

From a example Cookie value of loggedin=true; serviceValue=xyz; testHash=as2d24g41 you can isolate specific values with the following syntax:

  • req.http.Cookie:loggedin returns the string true
  • req.http.Cookie:serviceValue returns the string xyz
  • req.http.Cookie:testHash returns the string as2d24g41

This will work with any header with multiple values, such as: Cookie, Set-Cookie, Cache-Control, or a custom header.


#2

That’s digital gold, Austin! cleans-up quite a bit of VCL code for anyone dealing with cookies.

Did you measure how much extra processing this is costing your infrastructure? Any measurable added latency at all?


#3

Did you measure how much extra processing this is costing your infrastructure? Any measurable added latency at all?

There’s not really any difference. VCL stuff like this doesn’t impact delivery performance. In some odd cases, a badly formed regular expression may take more CPU than an elegant one, but it’s a small fractional difference, and something we can optimize for under the hood. If anything, this method is more performant in our backend systems that cookie regexes. But none of that is surfaced to end performance. It’s just something we plan for in low level varnish architecture.


#4

Supplying the cookie name via a table lookup doesn’t seem to work though? Example:

req.http.Cookie:table.lookup(relaunch, "cookieName")
* fastly_service_v1.qa: [ERR] Invalid configuration for Fastly Service (5hYeq6hHppZbxco247CSKE): Syntax error: Expected ',' got '('
at: (input Line 253 Pos 35)
      req.http.Cookie:table.lookup(relaunch, "cookieName"),
----------------------------------#------------------------

#5

Can this be used to unset only specific values. For example does unset req.http.Cookie:testHash remove the testHash value from the Cookie string?


#6

Late to the game, but yes that works. I didn’t find it doc’d anywhere, but it worked when I tried it out.