| 1 |
= cowboy_req:parse_cookies(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:parse_cookies - Parse cookie headers |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
parse_cookies(Req :: cowboy_req:req()) -> parse_cookies(Req, #{}) |
| 12 |
parse_cookies(Req :: cowboy_req:req(), Opts) -> [{Name, Value}] |
| 13 |
|
| 14 |
Name :: binary() %% case sensitive |
| 15 |
Value :: binary() %% case sensitive |
| 16 |
Opts :: #{max_cookies => non_neg_integer()} |
| 17 |
---- |
| 18 |
|
| 19 |
Parse cookie headers. |
| 20 |
|
| 21 |
When the cookie header is missing or empty, `[]` is returned. |
| 22 |
|
| 23 |
The `max_cookies` option limits the maximum number of |
| 24 |
cookies. It defaults to 100. |
| 25 |
|
| 26 |
This function will crash on invalid cookie data. Because |
| 27 |
invalid cookies are fairly common when dealing with browsers |
| 28 |
(because of the string interface that the Javascript API provides), |
| 29 |
it is recommended to filter the cookie header value before |
| 30 |
attempting to parse it. This can be accomplished by calling |
| 31 |
the function link:man:cowboy_req:filter_cookies(3)[cowboy_req:filter_cookies(3)] |
| 32 |
first. This does not guarantee that parsing succeeds. If it |
| 33 |
still fails it is recommended to send an error response or |
| 34 |
redirect with instructions to delete the relevant cookies: |
| 35 |
|
| 36 |
.Recover from cookie parsing errors |
| 37 |
[source,erlang] |
| 38 |
---- |
| 39 |
Req1 = cowboy_req:filter_cookies([session_id, token], Req0), |
| 40 |
try cowboy_req:parse_cookies(Req1) of |
| 41 |
Cookies -> |
| 42 |
do_something(Req1, Cookies) |
| 43 |
catch _:_ -> |
| 44 |
%% We can't parse the cookies we need, unset them |
| 45 |
%% otherwise the browser will continue sending them. |
| 46 |
Req2 = cowboy_req:set_resp_cookie(<<"session_id">>, |
| 47 |
<<>>, Req1, #{max_age => 0}), |
| 48 |
Req = cowboy_req:set_resp_cookie(<<"token">>, |
| 49 |
<<>>, Req2, #{max_age => 0}), |
| 50 |
cowboy_req:reply(500, Req) |
| 51 |
end. |
| 52 |
---- |
| 53 |
|
| 54 |
== Arguments |
| 55 |
|
| 56 |
Req:: |
| 57 |
|
| 58 |
The Req object. |
| 59 |
|
| 60 |
Opts:: |
| 61 |
|
| 62 |
The `max_cookies` option limits the maximum number of |
| 63 |
cookies. It defaults to 100. |
| 64 |
|
| 65 |
== Return value |
| 66 |
|
| 67 |
The cookies are returned as a list of key/values. Keys and |
| 68 |
values are case sensitive binary strings. |
| 69 |
|
| 70 |
== Changelog |
| 71 |
|
| 72 |
* *2.18*: Function `parse_cookies/2` introduced with a `max_cookies` option. |
| 73 |
* *2.0*: Only the parsed header value is returned, it is no longer wrapped in a tuple. |
| 74 |
* *2.0*: Function introduced. Replaces `cookie/2,3` and `cookies/1`. |
| 75 |
|
| 76 |
== Examples |
| 77 |
|
| 78 |
.Look for a specific cookie |
| 79 |
[source,erlang] |
| 80 |
---- |
| 81 |
Cookies = cowboy_req:parse_cookies(Req), |
| 82 |
{_, Token} = lists:keyfind(<<"token">>, 1, Cookies). |
| 83 |
---- |
| 84 |
|
| 85 |
== See also |
| 86 |
|
| 87 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 88 |
link:man:cowboy_req:parse_header(3)[cowboy_req:parse_header(3)], |
| 89 |
link:man:cowboy_req:filter_cookies(3)[cowboy_req:filter_cookies(3)], |
| 90 |
link:man:cowboy_req:match_cookies(3)[cowboy_req:match_cookies(3)] |