1VMOD_COOKIE(3) VMOD_COOKIE(3)
2
3
4
6 vmod_cookie - Varnish Cookie Module
7
9 import cookie [from "path"] ;
10
11 VOID clean()
12
13 VOID delete(STRING cookiename)
14
15 VOID filter(STRING filterstring)
16
17 VOID filter_except(STRING filterstring)
18
19 STRING format_rfc1123(TIME now, DURATION timedelta)
20
21 STRING get(STRING cookiename)
22
23 STRING get_string()
24
25 BOOL isset(STRING cookiename)
26
27 VOID parse(STRING cookieheader)
28
29 VOID set(STRING cookiename, STRING value)
30
32 Handle HTTP cookies easier in Varnish VCL. (without regex)
33
34 Parses a cookie header into an internal data store, where per-cookie
35 get/set/delete functions are available. A filter_except() method
36 removes all but a set comma-separated list of cookies. A filter()
37 method removes a comma- separated list of cookies.
38
39 A convenience function for formatting the Set-Cookie Expires date field
40 is also included. If there are multiple Set-Cookie headers vmod-header
41 should be used.
42
43 The state loaded with cookie.parse() has a lifetime of the current
44 request or backend request context. To pass variables to the backend
45 request, store the contents as fake bereq headers.
46
47 Filtering example:
48
49 vcl 4.0;
50
51 import cookie;
52
53 backend default { .host = "192.0.2.11"; .port = "8080"; }
54
55 sub vcl_recv {
56 if (req.http.cookie) {
57 cookie.parse(req.http.cookie);
58 # Either delete the ones you want to get rid of:
59 cookie.delete("cookie2");
60 # or filter everything but a few:
61 cookie.filter_except("SESSIONID,PHPSESSID");
62
63 # Store it back into req so it will be passed to the backend.
64 set req.http.cookie = cookie.get_string();
65
66 # If empty, unset so the builtin VCL can consider it for caching.
67 if (req.http.cookie == "") {
68 unset req.http.cookie;
69 }
70 }
71 }
72
73 VOID clean()
74 Description
75 Clean up previously parsed cookies. It is not necessary to run
76 clean() in normal operations.
77
78 Example
79
80 sub vcl_recv {
81 cookie.clean();
82 }
83
84 VOID delete(STRING cookiename)
85 Description
86 Delete cookiename from internal vmod storage if it exists.
87
88 Example
89
90 sub vcl_recv {
91 cookie.parse("cookie1: value1; cookie2: value2;");
92 cookie.delete("cookie2");
93 // get_string() will now yield "cookie1: value1";
94 }
95
96 VOID filter(STRING filterstring)
97 Description
98 Delete all cookies from internal vmod storage that are in the
99 comma-separated argument cookienames.
100
101 Example
102
103 sub vcl_recv {
104 cookie.parse("cookie1: value1; cookie2: value2; cookie3: value3");
105 cookie.filter("cookie1,cookie2");
106 // get_string() will now yield
107 // "cookie3: value3";
108 }
109
110 VOID filter_except(STRING filterstring)
111 Description
112 Delete all cookies from internal vmod storage that is not in the
113 comma-separated argument cookienames.
114
115 Example
116
117 sub vcl_recv {
118 cookie.parse("cookie1: value1; cookie2: value2; cookie3: value3");
119 cookie.filter_except("cookie1,cookie2");
120 // get_string() will now yield
121 // "cookie1: value1; cookie2: value2;";
122 }
123
124 STRING format_rfc1123(TIME now, DURATION timedelta)
125 Description
126 Get a RFC1123 formatted date string suitable for inclusion in a
127 Set-Cookie response header.
128
129 Care should be taken if the response has multiple Set-Cookie
130 headers. In that case the header vmod should be used.
131
132 Example
133
134 sub vcl_deliver {
135 # Set a userid cookie on the client that lives for 5 minutes.
136 set resp.http.Set-Cookie = "userid=" + req.http.userid + "; Expires=" + cookie.format_rfc1123(now, 5m) + "; httpOnly";
137 }
138
139 STRING get(STRING cookiename)
140 Description
141 Get the value of cookiename, as stored in internal vmod storage.
142 If cookiename does not exist an empty string is returned.
143
144 Example
145
146 import std;
147 sub vcl_recv {
148 cookie.parse("cookie1: value1; cookie2: value2;");
149 std.log("cookie1 value is: " + cookie.get("cookie1"));
150 }
151
152 STRING get_string()
153 Description
154 Get a Cookie string value with all cookies in internal vmod
155 storage. Does not modify internal storage.
156
157 Example
158
159 sub vcl_recv {
160 cookie.parse(req.http.cookie);
161 cookie.filter_except("SESSIONID,PHPSESSID");
162 set req.http.cookie = cookie.get_string();
163 }
164
165 BOOL isset(STRING cookiename)
166 Description
167 Check if cookiename is set in the internal vmod storage.
168
169 Example
170
171 import std;
172 sub vcl_recv {
173 cookie.parse("cookie1: value1; cookie2: value2;");
174 if (cookie.isset("cookie2")) {
175 std.log("cookie2 is set.");
176 }
177 }
178
179 VOID parse(STRING cookieheader)
180 Description
181 Parse the cookie string in cookieheader. If state already
182 exists, clean() will be run first.
183
184 Example
185
186 sub vcl_recv {
187 cookie.parse(req.http.Cookie);
188 }
189
190 VOID set(STRING cookiename, STRING value)
191 Description
192 Set the internal vmod storage for cookiename to value.
193
194 Example
195
196 sub vcl_recv {
197 cookie.set("cookie1", "value1");
198 std.log("cookie1 value is: " + cookie.get("cookie1"));
199 }
200
201
202
203
204 VMOD_COOKIE(3)