vweb: add cookies with expire date

pull/5573/head
Louis Schmieder 2020-06-29 21:14:36 +02:00 committed by GitHub
parent 7565fe595b
commit 616b07204d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -179,3 +179,11 @@ pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_
}
}
}
// This is just a TEMPORARY function for cookies and their expire dates
pub fn (time Time) utc_string() string {
day_str := time.weekday_str()
month_str := time.smonth()
utc_string := '$day_str, $time.day $month_str $time.year ${time.hour:02d}:${time.minute:02d}:${time.second:02d} UTC'
return utc_string
}

View File

@ -82,3 +82,7 @@ fn test_get_fmt_str() {
// combinations.
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
}
fn test_utc_string() {
assert 'Fri, 11 Jul 1980 21:23:42 UTC' == time_to_test.utc_string()
}

View File

@ -115,6 +115,10 @@ pub fn (mut ctx Context) set_content_type(typ string) {
ctx.content_type = typ
}
pub fn (mut ctx Context) set_cookie_with_expire_date(key, val string, expire_date time.Time) {
ctx.add_header('Set-Cookie', '$key=$val; Secure; HttpOnly; expires=${expire_date.utc_string()}')
}
pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
mut cookie_header := ctx.get_header('cookie')
if cookie_header == '' {