Appending value to set-cookie header in beresp. in Varnish 2.1

1.8k Views Asked by At

The case is I'd like to add two headers with the same name, but different values while processing response from the backend server. More precisely under certain circumstances I'd like to append (not set) another Set-Cookie header to the original response from my Apache server to eventually return two cookies to the client. It seems that "set" action overrides all occurrences of the same header.

Is that possible?

2

There are 2 best solutions below

0
On

You can use regsub() or regsuball() for the manipulation of HTTP headers.

https://www.varnish-cache.org/docs/2.1/reference/vcl.html

Edit: off the top of my head, it should look similar to the following

set beresp.http.Set-Cookie = regsub(beresp.http.Set-Cookie, "^(.+)$", "\1; secure; httponly");
0
On

VCL doesn't have an explicit string concatenation operator. However, you can concatenate values by setting them to a variable:

set req.http._message = "one string", " a second string"

which can then be assigned to other values or used however.

In your particular case, you should be able to do something like this:

if (some-condition) {
    set beresp.http.Set-Cookie = beresp.http.Set-Cookie, "my second cookie";
}