aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/src/guide/cookies.asciidoc27
-rw-r--r--doc/src/manual/cowboy_req.set_resp_cookie.asciidoc36
-rw-r--r--examples/cookie/src/toppage_handler.erl2
-rw-r--r--src/cowboy_req.erl6
-rw-r--r--test/handlers/resp_h.erl3
-rw-r--r--test/http_SUITE_data/http_set_resp.erl2
6 files changed, 34 insertions, 42 deletions
diff --git a/doc/src/guide/cookies.asciidoc b/doc/src/guide/cookies.asciidoc
index 33a1940..4825031 100644
--- a/doc/src/guide/cookies.asciidoc
+++ b/doc/src/guide/cookies.asciidoc
@@ -33,24 +33,21 @@ update the expiration time and avoid losing a cookie.
=== Setting cookies
-// @todo So I am not particularly happy about set_resp_cookie/4
-// having Opts as a *third* argument, instead of the *last* like
-// all other functions that come with an Opts argument. We will
-// probably need to change this before 2.0.
-
By default cookies are defined for the duration of the session:
[source,erlang]
+----
SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0).
+----
They can also be set for a duration in seconds:
[source,erlang]
----
SessionID = generate_session_id(),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
- #{max_age => 3600}, Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
+ #{max_age => 3600}).
----
To delete cookies, set `max_age` to 0:
@@ -58,8 +55,8 @@ To delete cookies, set `max_age` to 0:
[source,erlang]
----
SessionID = generate_session_id(),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
- #{max_age => 0}, Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
+ #{max_age => 0}).
----
To restrict cookies to a specific domain and path, the options
@@ -67,8 +64,8 @@ of the same name can be used:
[source,erlang]
----
-Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>,
- #{domain => "my.example.org", path => "/account"}, Req0).
+Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>, Req0,
+ #{domain => "my.example.org", path => "/account"}).
----
Cookies will be sent with requests to this domain and all
@@ -81,8 +78,8 @@ available over HTTPS):
[source,erlang]
----
SessionID = generate_session_id(),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
- #{secure => true}, Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
+ #{secure => true}).
----
To prevent client-side scripts from accessing a cookie:
@@ -90,8 +87,8 @@ To prevent client-side scripts from accessing a cookie:
[source,erlang]
----
SessionID = generate_session_id(),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
- #{http_only => true}, Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
+ #{http_only => true}).
----
Cookies may also be set client-side, for example using
diff --git a/doc/src/manual/cowboy_req.set_resp_cookie.asciidoc b/doc/src/manual/cowboy_req.set_resp_cookie.asciidoc
index f4643ff..2831059 100644
--- a/doc/src/manual/cowboy_req.set_resp_cookie.asciidoc
+++ b/doc/src/manual/cowboy_req.set_resp_cookie.asciidoc
@@ -11,7 +11,7 @@ cowboy_req:set_resp_cookie - Set a cookie
set_resp_cookie(Name, Value, Req :: cowboy_req:req())
-> set_resp_cookie(Name, Value, [], Req)
-set_resp_cookie(Name, Value, Opts, Req :: cowboy_req:req())
+set_resp_cookie(Name, Value, Req :: cowboy_req:req(), Opts)
-> Req
Name :: binary() %% case sensitive
@@ -33,14 +33,14 @@ Value::
Cookie value.
-Opts::
-
-Optional cookie options.
-
Req::
The Req object.
+Opts::
+
+Cookie options.
+
== Return value
A new Req object is returned.
@@ -66,44 +66,38 @@ Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0).
.Set a cookie with an expiration time
[source,erlang]
----
-Req = cowboy_req:set_resp_cookie(<<"lang">>, <<"fr-FR">>, [
- {max_age, 3600}
-], Req0).
+Req = cowboy_req:set_resp_cookie(<<"lang">>, <<"fr-FR">>,
+ Req0, #{max_age => 3600}).
----
.Delete a cookie
[source,erlang]
----
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, <<>>, [
- {max_age, 0}
-], Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, <<>>,
+ Req0, #{max_age => 0}).
----
.Set a cookie for a specific domain and path
[source,erlang]
----
-Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>, [
- {domain, "my.example.org"},
- {path, "/account"}
-], Req0).
+Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>,
+ Req0, #{domain => "my.example.org", path => "/account"}).
----
.Restrict a cookie to HTTPS
[source,erlang]
----
SessionID = base64:encode(crypto:strong_rand_bytes(32)),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, [
- {secure, true}
-], Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
+ Req0, #{secure => true}).
----
.Restrict a cookie to HTTP
[source,erlang]
----
SessionID = base64:encode(crypto:strong_rand_bytes(32)),
-Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, [
- {http_only, true}
-], Req0).
+Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
+ Req0, #{http_only => true}).
----
== See also
diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl
index 3df6045..38952ef 100644
--- a/examples/cookie/src/toppage_handler.erl
+++ b/examples/cookie/src/toppage_handler.erl
@@ -8,7 +8,7 @@
init(Req0, Opts) ->
NewValue = integer_to_list(rand:uniform(1000000)),
Req1 = cowboy_req:set_resp_cookie(<<"server">>, NewValue,
- #{path => <<"/">>}, Req0),
+ Req0, #{path => <<"/">>}),
#{client := ClientCookie, server := ServerCookie}
= cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req1),
{ok, Body} = toppage_dtl:render([
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 84bfee5..ca31105 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -548,7 +548,7 @@ stream_multipart(Req=#{multipart := {Boundary, Buffer}}, _) ->
-spec set_resp_cookie(iodata(), iodata(), Req)
-> Req when Req::req().
set_resp_cookie(Name, Value, Req) ->
- set_resp_cookie(Name, Value, #{}, Req).
+ set_resp_cookie(Name, Value, Req, #{}).
%% The cookie name cannot contain any of the following characters:
%% =,;\s\t\r\n\013\014
@@ -556,9 +556,9 @@ set_resp_cookie(Name, Value, Req) ->
%% The cookie value cannot contain any of the following characters:
%% ,; \t\r\n\013\014
%% @todo Fix the cookie_opts() type.
--spec set_resp_cookie(binary(), iodata(), cookie_opts(), Req)
+-spec set_resp_cookie(binary(), iodata(), Req, cookie_opts())
-> Req when Req::req().
-set_resp_cookie(Name, Value, Opts, Req) ->
+set_resp_cookie(Name, Value, Req, Opts) ->
Cookie = cow_cookie:setcookie(Name, Value, maps:to_list(Opts)),
RespCookies = maps:get(resp_cookies, Req, #{}),
Req#{resp_cookies => RespCookies#{Name => Cookie}}.
diff --git a/test/handlers/resp_h.erl b/test/handlers/resp_h.erl
index 5e6696e..694f04a 100644
--- a/test/handlers/resp_h.erl
+++ b/test/handlers/resp_h.erl
@@ -20,7 +20,8 @@ do(<<"set_resp_cookie3">>, Req0, Opts) ->
end,
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
do(<<"set_resp_cookie4">>, Req0, Opts) ->
- Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", #{path => cowboy_req:path(Req0)}, Req0),
+ Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0,
+ #{path => cowboy_req:path(Req0)}),
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
do(<<"set_resp_header">>, Req0, Opts) ->
Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
diff --git a/test/http_SUITE_data/http_set_resp.erl b/test/http_SUITE_data/http_set_resp.erl
index 6ac4c8e..e575aab 100644
--- a/test/http_SUITE_data/http_set_resp.erl
+++ b/test/http_SUITE_data/http_set_resp.erl
@@ -12,7 +12,7 @@ init(Req, Opts) ->
end, Req, maps:to_list(Headers)),
Req3 = cowboy_req:set_resp_body(Body, Req2),
Req4 = cowboy_req:set_resp_header(<<"x-cowboy-test">>, <<"ok">>, Req3),
- Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, [], Req4),
+ Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, Req4),
case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req5) of
false -> {ok, Req5, Opts};
true ->