Skip to content

Commit 7e46a24

Browse files
hermunnbsdphk
authored andcommitted
Docfix for issue 3634
Fixes: #3634
1 parent 556007b commit 7e46a24

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

vmod/vmod_cookie.vcc

+33-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ Example::
150150

151151
$Function STRING get(PRIV_TASK, STRING cookiename)
152152

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.
155154

156155
Example::
157156

@@ -161,6 +160,38 @@ Example::
161160
std.log("cookie1 value is: " + cookie.get("cookie1"));
162161
}
163162

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+
164195
$Function STRING get_re(PRIV_TASK, REGEX expression)
165196

166197
Get the value of the first cookie in internal vmod storage that matches

0 commit comments

Comments
 (0)