-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib] make cookies module modern #17116
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0c6393
fix js unsigned integer
ringabout 9a40fbf
better
ringabout 9319749
Merge remote-tracking branch 'upstream/devel' into a90
ringabout 5f5c862
Use `std` prefix for standard library modules
ringabout 67e5353
Merge remote-tracking branch 'upstream/devel' into a94
ringabout 358bdde
update cookies module
ringabout f405a42
use unsafeGet
ringabout 6a94301
introduce sameSite.Default
ringabout 6275281
better
ringabout 057be01
docuemnt sameSite
ringabout 7332598
fix a typo
ringabout 9916307
Update lib/pure/cookies.nim
ringabout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,25 @@ | |
|
||
## This module implements helper procs for parsing Cookies. | ||
|
||
import strtabs, times | ||
import std/[strtabs, times, options] | ||
|
||
|
||
type | ||
SameSite* {.pure.} = enum ## The SameSite cookie attribute. | ||
## `Default` means that `setCookie` | ||
## proc will not set `SameSite` attribute. | ||
Default, None, Lax, Strict | ||
|
||
proc parseCookies*(s: string): StringTableRef = | ||
timotheecour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## parses cookies into a string table. | ||
## Parses cookies into a string table. | ||
## | ||
## The proc is meant to parse the Cookie header set by a client, not the | ||
## "Set-Cookie" header set by servers. | ||
## | ||
## Example: | ||
## | ||
## .. code-block::Nim | ||
## doAssert parseCookies("a=1; foo=bar") == {"a": 1, "foo": "bar"}.newStringTable | ||
runnableExamples: | ||
import std/strtabs | ||
let cookieJar = parseCookies("a=1; foo=bar") | ||
assert cookieJar["a"] == "1" | ||
assert cookieJar["foo"] == "bar" | ||
|
||
result = newStringTable(modeCaseInsensitive) | ||
var i = 0 | ||
|
@@ -39,9 +46,10 @@ proc parseCookies*(s: string): StringTableRef = | |
|
||
proc setCookie*(key, value: string, domain = "", path = "", | ||
expires = "", noName = false, | ||
secure = false, httpOnly = false): string = | ||
secure = false, httpOnly = false, | ||
maxAge = none(int), sameSite = SameSite.Default): string = | ||
## Creates a command in the format of | ||
## ``Set-Cookie: key=value; Domain=...; ...`` | ||
## `Set-Cookie: key=value; Domain=...; ...` | ||
result = "" | ||
if not noName: result.add("Set-Cookie: ") | ||
result.add key & "=" & value | ||
|
@@ -50,12 +58,19 @@ proc setCookie*(key, value: string, domain = "", path = "", | |
if expires != "": result.add("; Expires=" & expires) | ||
if secure: result.add("; Secure") | ||
if httpOnly: result.add("; HttpOnly") | ||
if maxAge.isSome: result.add("; Max-Age=" & $maxAge.unsafeGet) | ||
|
||
if sameSite != SameSite.Default: | ||
if sameSite == SameSite.None: | ||
doAssert httpOnly, "Cookies with SameSite=None must specify the Secure attribute!" | ||
ringabout marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result.add("; SameSite=" & $sameSite) | ||
|
||
proc setCookie*(key, value: string, expires: DateTime|Time, | ||
domain = "", path = "", noName = false, | ||
secure = false, httpOnly = false): string = | ||
secure = false, httpOnly = false, | ||
maxAge = none(int), sameSite = SameSite.Default): string = | ||
## Creates a command in the format of | ||
## ``Set-Cookie: key=value; Domain=...; ...`` | ||
return setCookie(key, value, domain, path, | ||
## `Set-Cookie: key=value; Domain=...; ...` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pre-existing but this overload (with differently placed params) seems like a bad design, instead we should expose a proc (eg setCookie(..., t.toGmtString, ...) |
||
result = setCookie(key, value, domain, path, | ||
format(expires.utc, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'"), | ||
noname, secure, httpOnly) | ||
noname, secure, httpOnly, maxAge, sameSite) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std/compilesettings uses lower-case enum values, this is inconsistent; we should specify in nep1 which one is the "best practice"
followup: timotheecour#622 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already specified in nep1: Non-pure enum values should use camelCase whereas pure enum values should use PascalCase.