@@ -150,8 +150,7 @@ Example::
150
150
151
151
$Function STRING get(PRIV_TASK, STRING cookiename)
152
152
153
- Get the value of ``cookiename``, as stored in internal vmod storage. If
154
- ``cookiename`` does not exist an empty string is returned.
153
+ Get the value of ``cookiename``, as stored in internal vmod storage.
155
154
156
155
Example::
157
156
@@ -161,6 +160,38 @@ Example::
161
160
std.log("cookie1 value is: " + cookie.get("cookie1"));
162
161
}
163
162
163
+ If ``cookiename`` does not exist, the `NULL` string is returned. Note
164
+ that a `NULL` string is converted to an empty string when assigned to
165
+ a header. This means that the following is correct::
166
+
167
+ if (req.http.Cookie) {
168
+ cookie.parse(req.http.Cookie);
169
+ set req.http.X-tmp = cookie.get("a_cookie");
170
+ } else {
171
+ set req.http.X-tmp = "";
172
+ }
173
+ if (req.http.X-tmp != "") {
174
+ // do something with the X-tmp header...
175
+ } else {
176
+ // fallback case
177
+ }
178
+
179
+ However, using `cookie.isset()` is often a better way to check if a
180
+ particular cookie is present, like this::
181
+
182
+ unset req.http.X-tmp; # unnecessary if no fallback is needed
183
+ if (req.http.Cookie) {
184
+ cookie.parse(req.http.Cookie);
185
+ if (cookie.isset("a_cookie")) {
186
+ set req.http.X-tmp = cookie.get("a_cookie");
187
+ # do something with the X-tmp header...
188
+ }
189
+ }
190
+ # if necessary, do something when a_cookie is not there
191
+ if (!req.http.X-tmp) {
192
+ # fallback case
193
+ }
194
+
164
195
$Function STRING get_re(PRIV_TASK, REGEX expression)
165
196
166
197
Get the value of the first cookie in internal vmod storage that matches
0 commit comments