1VMOD_COOKIE(3)                                                  VMOD_COOKIE(3)
2
3
4

NAME

6       vmod_cookie - Varnish Cookie Module
7

SYNOPSIS

9          import cookie [as name] [from "path"]
10
11          VOID clean()
12
13          VOID delete(STRING cookiename)
14
15          VOID filter(STRING filterstring)
16
17          VOID filter_re(REGEX expression)
18
19          VOID keep(STRING filterstring)
20
21          VOID keep_re(REGEX expression)
22
23          STRING format_date(TIME now, DURATION timedelta)
24
25          STRING get(STRING cookiename)
26
27          STRING get_re(REGEX expression)
28
29          STRING get_string()
30
31          BOOL isset(STRING cookiename)
32
33          VOID parse(STRING cookieheader)
34
35          VOID set(STRING cookiename, STRING value)
36

DESCRIPTION

38       Handle HTTP cookies more easily in Varnish VCL.
39
40       Parses  a  cookie  header into an internal data store, where per-cookie
41       get/set/delete functions are available. A keep() function  removes  all
42       but  a set comma-separated list of cookies. A filter() function removes
43       a comma- separated list of cookies.
44
45       Regular expressions can be used for either selecting cookies,  deleting
46       matching cookies and deleting non-matching cookie names.
47
48       A convenience function for formatting the Set-Cookie Expires date field
49       is also included.
50
51       The state loaded with cookie.parse() has a lifetime of the current  re‐
52       quest  or backend request context. To pass variables to the backend re‐
53       quest, store the contents as fake bereq headers.
54
55       Filtering example:
56
57          import cookie;
58
59          sub vcl_recv {
60              if (req.http.cookie) {
61                  cookie.parse(req.http.cookie);
62                  # Either delete the ones you want to get rid of:
63                  cookie.delete("cookie2");
64                  # or delete all but a few:
65                  cookie.keep("SESSIONID,PHPSESSID");
66
67                  # Store it back into req so it will be passed to the backend.
68                  set req.http.cookie = cookie.get_string();
69
70                  # If empty, unset so the builtin VCL can consider it for caching.
71                  if (req.http.cookie == "") {
72                      unset req.http.cookie;
73                  }
74              }
75          }
76
77   VOID clean()
78       Clean up previously parsed cookies. It is not necessary to run  clean()
79       in normal operations.
80
81       Example:
82
83          sub vcl_recv {
84              cookie.clean();
85          }
86
87   VOID delete(STRING cookiename)
88       Delete cookiename from internal vmod storage if it exists.
89
90       Example:
91
92          sub vcl_recv {
93              cookie.parse("cookie1=value1; cookie2=value2");
94              cookie.delete("cookie2");
95              # get_string() will now yield "cookie1=value1"
96          }
97
98   VOID filter(STRING filterstring)
99       Delete  all  cookies  from  internal  vmod  storage  that  are  in  the
100       comma-separated argument cookienames.
101
102       Example:
103
104          sub vcl_recv {
105              cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
106              cookie.filter("cookie1,cookie2");
107              # get_string() will now yield "cookie3=value3"
108          }
109
110   VOID filter_re(REGEX expression)
111       Delete all cookies from internal vmod storage that matches the  regular
112       expression expression.
113
114       Example:
115
116          sub vcl_recv {
117              cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
118              cookie.filter_re("^cookie[12]$");
119              # get_string() will now yield "cookie3=value3"
120          }
121
122   VOID keep(STRING filterstring)
123       Delete  all  cookies  from  internal  vmod  storage  that is not in the
124       comma-separated argument cookienames.
125
126       Example:
127
128          sub vcl_recv {
129              cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
130              cookie.keep("cookie1,cookie2");
131              # get_string() will now yield "cookie1=value1; cookie2=value2"
132          }
133
134   VOID keep_re(REGEX expression)
135       Delete all cookies from internal vmod storage that does not  match  ex‐
136       pression expression.
137
138       Example:
139
140          sub vcl_recv {
141              cookie.parse("cookie1=value1; cookie2=value2; cookie3=value3");
142              cookie.keep_re("^cookie[12]$");
143              # get_string() will now yield "cookie1=value1; cookie2=value2"
144          }
145
146   STRING format_date(TIME now, DURATION timedelta)
147       Get  a  RFC1123  formatted  date  string  suitable  for  inclusion in a
148       Set-Cookie response header.
149
150       Care should be taken if the response has multiple  Set-Cookie  headers.
151       In that case the header vmod should be used.
152
153       Example:
154
155          sub vcl_deliver {
156              # Set a userid cookie on the client that lives for 5 minutes.
157              set resp.http.Set-Cookie = "userid=" + req.http.userid +
158                  "; Expires=" + cookie.format_date(now, 5m) + "; httpOnly";
159          }
160
161   STRING get(STRING cookiename)
162       Get the value of cookiename, as stored in internal vmod storage.
163
164       Example:
165
166          import std;
167          sub vcl_recv {
168              cookie.parse("cookie1=value1; cookie2=value2");
169              std.log("cookie1 value is: " + cookie.get("cookie1"));
170          }
171
172       If  cookiename does not exist, the NULL string is returned. Note that a
173       NULL string is converted to an empty string when assigned to a  header.
174       This means that the following is correct:
175
176          if (req.http.Cookie) {
177                  cookie.parse(req.http.Cookie);
178                  set req.http.X-tmp = cookie.get("a_cookie");
179          } else {
180                  set req.http.X-tmp = "";
181          }
182          if (req.http.X-tmp != "") {
183                  // do something with the X-tmp header...
184          } else {
185                  // fallback case
186          }
187
188       However,  using cookie.isset() is often a better way to check if a par‐
189       ticular cookie is present, like this:
190
191          unset req.http.X-tmp; # unnecessary if no fallback is needed
192          if (req.http.Cookie) {
193                  cookie.parse(req.http.Cookie);
194                  if (cookie.isset("a_cookie")) {
195                          set req.http.X-tmp = cookie.get("a_cookie");
196                          # do something with the X-tmp header...
197                  }
198          }
199          # if necessary, do something when a_cookie is not there
200          if (!req.http.X-tmp) {
201                  # fallback case
202          }
203
204   STRING get_re(REGEX expression)
205       Get the value of the first cookie in internal vmod storage that matches
206       the  regular expression expression. If nothing matches, the NULL string
207       is returned.
208
209       Example:
210
211          import std;
212          sub vcl_recv {
213              cookie.parse("cookie1=value1; cookie2=value2");
214              std.log("cookie1 value is: " + cookie.get_re("^cookie1$"));
215          }
216
217   STRING get_string()
218       Get a Cookie string value with all cookies in  internal  vmod  storage.
219       Does not modify internal storage.
220
221       Example:
222
223          sub vcl_recv {
224              cookie.parse(req.http.cookie);
225              cookie.keep("SESSIONID,PHPSESSID");
226              set req.http.cookie = cookie.get_string();
227          }
228
229   BOOL isset(STRING cookiename)
230       Check if cookiename is set in the internal vmod storage.
231
232       Example:
233
234          import std;
235          sub vcl_recv {
236              cookie.parse("cookie1=value1; cookie2=value2");
237              if (cookie.isset("cookie2")) {
238                  std.log("cookie2 is set.");
239              }
240          }
241
242   VOID parse(STRING cookieheader)
243       Parse  the  cookie  string  in  cookieheader.  If state already exists,
244       clean() will be run first.
245
246       Example:
247
248          sub vcl_recv {
249              cookie.parse(req.http.Cookie);
250          }
251
252   VOID set(STRING cookiename, STRING value)
253       Set the internal vmod storage for cookiename to value.
254
255       Example:
256
257          sub vcl_recv {
258              cookie.set("cookie1", "value1");
259              std.log("cookie1 value is: " + cookie.get("cookie1"));
260          }
261

DEPRECATED

263   ALIAS format_rfc1123()
264       Deprecated alias for format_date().
265
267          This document is licensed under the same conditions as Varnish itself.
268          See LICENSE for details.
269
270          SPDX-License-Identifier: BSD-2-Clause
271
272
273
274
275                                                                VMOD_COOKIE(3)
Impressum