From fff6ed4e0a656a89c787a6461cd8a29eec3d0a71 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 4 Mar 2023 04:02:14 +0100 Subject: http.c: Use persistent cookies Cookies without "Expires" are considered non-persistent and thus can be removed by the web browser. Instead, slcl now sets persistent cookies that last for 1 year. --- http.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/http.c b/http.c index dfb335d..71a872e 100644 --- a/http.c +++ b/http.c @@ -10,6 +10,7 @@ #include #include #include +#include struct http_ctx { @@ -1503,12 +1504,51 @@ static int http_read(struct http_ctx *const h, bool *const close) return -1; } +static int append_expire(struct dynstr *const d) +{ + time_t t = time(NULL); + + if (t == (time_t)-1) + { + fprintf(stderr, "%s: time(3): %s\n", __func__, strerror(errno)); + return -1; + } + + t += 365 * 24 * 60 * 60; + + struct tm tm; + + if (!localtime_r(&t, &tm)) + { + fprintf(stderr, "%s: localtime_r(3): %s\n", __func__, strerror(errno)); + return -1; + } + + char s[sizeof "Thu, 01 Jan 1970 00:00:00 GMT"]; + + if (!strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S GMT", &tm)) + { + fprintf(stderr, "%s: strftime(3) failed\n", __func__); + return -1; + } + + dynstr_append_or_ret_nonzero(d, "; Expires=%s", s); + return 0; +} + char *http_cookie_create(const char *const key, const char *const value) { struct dynstr d; dynstr_init(&d); dynstr_append_or_ret_null(&d, "%s=%s; HttpOnly", key, value); + + if (append_expire(&d)) + { + dynstr_free(&d); + return NULL; + } + return d.str; } -- cgit v1.2.3