From 616b07204de2379dde82ca0053851f7bdf70be3d Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Mon, 29 Jun 2020 21:14:36 +0200 Subject: [PATCH] vweb: add cookies with expire date --- vlib/time/format.v | 8 ++++++++ vlib/time/format_test.v | 4 ++++ vlib/vweb/vweb.v | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/vlib/time/format.v b/vlib/time/format.v index b7c92a0e47..4d7191d105 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -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 +} diff --git a/vlib/time/format_test.v b/vlib/time/format_test.v index 2978a3f4ee..eae8b0e6a1 100644 --- a/vlib/time/format_test.v +++ b/vlib/time/format_test.v @@ -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() +} diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index b984f78657..bff260c56b 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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 == '' {