Skip to content

Commit

Permalink
Fix parse_set_cookie/1 and format_set_cookie/1 functions
Browse files Browse the repository at this point in the history
1. According to the RFCs 2109 and 2965, multiple cookies can be set in a
single 'Set-Cookie' header. So, yaws_api:parse_set_cookie/1 now returns a
list of #setcookie{} records. If no cookie was found or if an error occurred,
it returns []. The parsing is also improved.
Note that this fix breaks the compatibility with previous versions.

2. In yaws_api:format_set_cookie/1, options are now always formated as
quoted-strings.

3. 2 new functions are added, yaws_api:parse_cookie/1 and
yaws_api:format_cookie/1, to parse and format 'Cookie' headers. To let these
functions to work, the #cookie{} record was introduced.

Documentation and testsuite are updated accordingly.
  • Loading branch information
capflam committed Jul 4, 2012
1 parent be0babd commit 3a8e071
Show file tree
Hide file tree
Showing 4 changed files with 636 additions and 161 deletions.
16 changes: 13 additions & 3 deletions include/yaws_api.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,29 @@
-record(setcookie,{
key,
value,
quoted,
quoted = false,
comment,
comment_url,
discard,
discard = false,
domain,
max_age,
expires,
path,
port,
secure,
secure = false,
version}).


-record(cookie,{
key,
value,
quoted = false,
version = "0",
domain,
path,
port}).


-record(redir_self, {
host, % string() - our own host
scheme, % http | https
Expand Down
64 changes: 62 additions & 2 deletions man/yaws_api.5
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,76 @@ Sets a cookie to the browser.
\fBfind_cookie_val(Cookie, Header)\fR
This function can be used to search for a cookie that was previously
set by \fBsetcookie/2-6\fR. For example if we set a cookie
as \fByaws_api:setcookie("sid",SomeRandomSid) \fR, then on subsequent requests
as \fByaws_api:setcookie("sid",SomeRandomSid)\fR, then on subsequent requests
from the browser we can call:
\fBfind_cookie("sid",(Arg#arg.headers)#headers.cookie)\fR

The function returns [] if no cookie was found, otherwise the actual cookie
is returned as a string.

.TP
\fBparse_set_cookie(Str)\fR
This function parses the value of a \fBSet-Cookie\fR header. Because multiple
cookies can be set in a single \fBSet-Cookie\fR header, this function returns a
list of \fI#setcookie{}\fR records. If no cookie was found or if an error
occurred, it returns [].

\fI#setcookie{}\fR record is defined in \fIyaws_api.hrl\fR:
\fI
.nf

-record(setcookie, {
key,
value,
quoted = false,
comment,
comment_url,
discard = false,
domain,
max_age,
expires,
path,
port,
secure = false,
version
}).
.fi
\fR

.TP
\fBparse_cookie(Str)\fR
This function does the same thing than \fBparse_set_cookie/1\fR but for the
value of a \fBCookie\fR header. It returns a list of \fI#cookie{}\fR records. If
no cookie was found or if an error occurred, it returns [].

\fI#cookie{}\fR record is defined in \fIyaws_api.hrl\fR:
\fI
.nf

-record(cookie, {
key,
value,
quoted = false,
version = "0",
domain,
path,
port}).
}).
.fi
\fR

.TP
\fBformat_set_cookie(Str)\fR
Build a cookie string from a \fI#setcookie{}\fR record like returned by
\fBparse_set_cookie/1\fR.

.TP
\fBformat_cookie(Str)\fR
Build a cookie string from a \fI#cookie{}\fR record like returned by
\fBparse_cookie/1\fR.

.TP
\fBredirect(Url\fR
\fBredirect(Url)\fR
This function generates a redirect to the browser.
It will clear any previously set headers. So to generate
a redirect \fBand\fR set a cookie, we need to set the cookie after
Expand Down
Loading

0 comments on commit 3a8e071

Please sign in to comment.