Merge pull request #7483 from GULPF/cookies-timezones

Don't assume UTC in cookies.setCookie
This commit is contained in:
Dominik Picheta 2018-04-04 22:37:37 +01:00 • committed by GitHub
commit 06a4505305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 12 deletions

View file

@ -7,6 +7,8 @@
- ``re.split`` for empty regular expressions now yields every character in - ``re.split`` for empty regular expressions now yields every character in
the string which is what other programming languages chose to do. the string which is what other programming languages chose to do.
- ``cookies.setCookie` no longer assumes UTC for the expiration date.
#### Breaking changes in the compiler #### Breaking changes in the compiler
### Library additions ### Library additions

View file

@ -51,26 +51,26 @@ proc setCookie*(key, value: string, domain = "", path = "",
if secure: result.add("; Secure") if secure: result.add("; Secure")
if httpOnly: result.add("; HttpOnly") if httpOnly: result.add("; HttpOnly")
proc setCookie*(key, value: string, expires: DateTime, proc setCookie*(key, value: string, expires: DateTime|Time,
domain = "", path = "", noName = false, domain = "", path = "", noName = false,
secure = false, httpOnly = false): string = secure = false, httpOnly = false): string =
## Creates a command in the format of ## Creates a command in the format of
## ``Set-Cookie: key=value; Domain=...; ...`` ## ``Set-Cookie: key=value; Domain=...; ...``
##
## **Note:** UTC is assumed as the timezone for ``expires``.
return setCookie(key, value, domain, path, return setCookie(key, value, domain, path,
format(expires, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'"), format(expires.utc, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'"),
noname, secure, httpOnly) noname, secure, httpOnly)
when isMainModule: when isMainModule:
var tim = fromUnix(getTime().toUnix + 76 * (60 * 60 * 24)) let expire = fromUnix(0) + 1.seconds
let cookie = setCookie("test", "value", tim.utc) let cookies = [
when not defined(testing): setCookie("test", "value", expire),
echo cookie setCookie("test", "value", expire.local),
let start = "Set-Cookie: test=value; Expires=" setCookie("test", "value", expire.utc)
assert cookie[0..start.high] == start ]
let expected = "Set-Cookie: test=value; Expires=Thu, 01 Jan 1970 00:00:01 GMT"
doAssert cookies == [expected, expected, expected]
let table = parseCookies("uid=1; kp=2") let table = parseCookies("uid=1; kp=2")
assert table["uid"] == "1" doAssert table["uid"] == "1"
assert table["kp"] == "2" doAssert table["kp"] == "2"