From 8459bebceb9533948193774371cbd9fd571b78ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 16 Oct 2019 09:48:31 +0200 Subject: Cowboy 2.7.0 --- docs/en/cowboy/2.2/guide/cookies/index.html | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'docs/en/cowboy/2.2/guide/cookies/index.html') diff --git a/docs/en/cowboy/2.2/guide/cookies/index.html b/docs/en/cowboy/2.2/guide/cookies/index.html index 5c682860..96012abf 100644 --- a/docs/en/cowboy/2.2/guide/cookies/index.html +++ b/docs/en/cowboy/2.2/guide/cookies/index.html @@ -71,7 +71,7 @@

Cookies may be sent repeatedly. This is often useful to update the expiration time and avoid losing a cookie.

Setting cookies

By default cookies are defined for the duration of the session:

-
@@ -79,7 +79,7 @@ http://www.gnu.org/software/src-highlite --> Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0).

They can also be set for a duration in seconds:

-
@@ -88,7 +88,7 @@ http://www.gnu.org/software/src-highlite --> #{max_age => 3600}).

To delete cookies, set max_age to 0:

-
@@ -97,7 +97,7 @@ http://www.gnu.org/software/src-highlite --> #{max_age => 0}).

To restrict cookies to a specific domain and path, the options of the same name can be used:

-
@@ -106,7 +106,7 @@ http://www.gnu.org/software/src-highlite -->

Cookies will be sent with requests to this domain and all its subdomains, and to resources on this path or deeper in the path hierarchy.

To restrict cookies to secure channels (typically resources available over HTTPS):

-
@@ -115,7 +115,7 @@ http://www.gnu.org/software/src-highlite --> #{secure => true}).

To prevent client-side scripts from accessing a cookie:

-
@@ -128,7 +128,7 @@ http://www.gnu.org/software/src-highlite -->

The client only ever sends back the cookie name and value. All other options that can be set are never sent back.

Cowboy provides two functions for reading cookies. Both involve parsing the cookie header(s) and so should not be called repeatedly.

You can get all cookies as a key/value list:

-
@@ -136,14 +136,14 @@ http://www.gnu.org/software/src-highlite --> {_, Lang} = lists:keyfind(<<"lang">>, 1, Cookies).

Or you can perform a match against cookies and retrieve only the ones you need, while at the same time doing any required post processing using constraints. This function returns a map:

-
#{id := ID, lang := Lang} = cowboy_req:match_cookies([id, lang], Req).

You can use constraints to validate the values while matching them. The following snippet will crash if the id cookie is not an integer number or if the lang cookie is empty. Additionally the id cookie value will be converted to an integer term:

-
@@ -151,7 +151,7 @@ http://www.gnu.org/software/src-highlite -->

Note that if two cookies share the same name, then the map value will be a list of the two cookie values.

A default value can be provided. The default will be used if the lang cookie is not found. It will not be used if the cookie is found but has an empty value:

-
@@ -215,6 +215,8 @@ http://www.gnu.org/software/src-highlite --> +
  • 2.7
  • +
  • 2.6
  • 2.5
  • -- cgit v1.2.3