Secure and HttpOnly cookies

This commit is contained in:
Erwan Ameil 2014-08-30 21:38:47 +02:00
commit a249a12da7

View file

@ -29,7 +29,8 @@ proc parseCookies*(s: string): PStringTable =
inc(i) # skip ';' inc(i) # skip ';'
proc setCookie*(key, value: string, domain = "", path = "", proc setCookie*(key, value: string, domain = "", path = "",
expires = "", noName = false): string = expires = "", noName = false,
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=...; ...``
result = "" result = ""
@ -38,16 +39,20 @@ proc setCookie*(key, value: string, domain = "", path = "",
if domain != "": result.add("; Domain=" & domain) if domain != "": result.add("; Domain=" & domain)
if path != "": result.add("; Path=" & path) if path != "": result.add("; Path=" & path)
if expires != "": result.add("; Expires=" & expires) if expires != "": result.add("; Expires=" & expires)
if secure: result.add("; secure")
if httpOnly: result.add("; HttpOnly")
proc setCookie*(key, value: string, expires: TTimeInfo, proc setCookie*(key, value: string, expires: TTimeInfo,
domain = "", path = "", noName = false): string = domain = "", path = "", noName = false,
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``. ## **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 'UTC'"), noname) format(expires, "ddd',' dd MMM yyyy HH:mm:ss 'UTC'"),
noname, secure, httpOnly)
when isMainModule: when isMainModule:
var tim = TTime(int(getTime()) + 76 * (60 * 60 * 24)) var tim = TTime(int(getTime()) + 76 * (60 * 60 * 24))
@ -55,5 +60,3 @@ when isMainModule:
echo(setCookie("test", "value", tim.getGMTime())) echo(setCookie("test", "value", tim.getGMTime()))
echo parseCookies("uid=1; kp=2") echo parseCookies("uid=1; kp=2")