From bb60fe2ccf61fb1b7d1578c8b64d7c091092e89a Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Sun, 19 Jul 2020 21:42:50 +0200 Subject: [PATCH] vweb: implement cookie expiration date (#5873) --- vlib/vweb/vweb.v | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index d17d3c4431..0092e01103 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -60,7 +60,7 @@ pub mut: pub struct Cookie { name string value string - exprires time.Time + expires time.Time secure 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) { - secure := if cookie.secure { "Secure;" } else { "" } - http_only := if cookie.http_only { "HttpOnly" } else { "" } - ctx.add_header('Set-Cookie', '$cookie.name=$cookie.value; $secure $http_only') + mut cookie_data := []string{} + mut secure := if cookie.secure { "Secure;" } else { "" } + 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) {