vweb: implement cookie expiration date (#5873)

pull/5888/head
Louis Schmieder 2020-07-19 21:42:50 +02:00 committed by GitHub
parent 8a855ccae1
commit bb60fe2ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -60,7 +60,7 @@ pub mut:
pub struct Cookie { pub struct Cookie {
name string name string
value string value string
exprires time.Time expires time.Time
secure bool secure bool
http_only bool http_only bool
} }
@ -120,9 +120,15 @@ pub fn (mut ctx Context) not_found() Result {
} }
pub fn (mut ctx Context) set_cookie(cookie Cookie) { pub fn (mut ctx Context) set_cookie(cookie Cookie) {
secure := if cookie.secure { "Secure;" } else { "" } mut cookie_data := []string{}
http_only := if cookie.http_only { "HttpOnly" } else { "" } mut secure := if cookie.secure { "Secure;" } else { "" }
ctx.add_header('Set-Cookie', '$cookie.name=$cookie.value; $secure $http_only') secure += if cookie.http_only { " HttpOnly" } else { " " }
cookie_data << secure
if cookie.expires.unix > 0 {
cookie_data << 'expires=${cookie.expires.utc_string()}'
}
data := cookie_data.join(' ')
ctx.add_header('Set-Cookie', '$cookie.name=$cookie.value; $data')
} }
pub fn (mut ctx Context) set_cookie_old(key, val string) { pub fn (mut ctx Context) set_cookie_old(key, val string) {