From aa64e9fd2d19f09da954248f523b4715c8a01529 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 29 Jul 2015 16:31:30 +0200 Subject: [PATCH] ZakCgiMain::set_cookie setted up only name, value and expires (closes #951). --- src/main.c | 38 +++++++++++++++++++++++++++++--------- tests/cookies.c | 5 +++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 75a6f05..c5decf5 100644 --- a/src/main.c +++ b/src/main.c @@ -327,24 +327,44 @@ gchar gboolean secure, gboolean http_only) { - char *ret; + gchar *ret; + GString *str; char *cur = g_strdup (setlocale (LC_TIME, NULL)); setlocale (LC_NUMERIC, "C"); - ret = g_strdup_printf ("Set-Cookie: %s=%s%s", - name, - value, - expires != NULL ? g_date_time_format (expires, "; expires=%a, %d-%b-%Y %H:%M:%S GMT") : "", - domain != NULL ? g_strdup_printf ("; domain=%s", domain) : "", - path != NULL ? g_strdup_printf ("; path=%s", path) : "", - secure ? g_strdup ("; secure") : "", - http_only ? g_strdup ("; httponly") : ""); + str = g_string_new ("Set-Cookie: "); + g_string_append_printf (str, "%s=%s", + name, + value); + if (expires != NULL) + { + g_string_append_printf (str, "%s", g_date_time_format (expires, "; expires=%a, %d-%b-%Y %H:%M:%S GMT")); + } + if (domain != NULL) + { + g_string_append_printf (str, "; domain=%s", domain); + } + if (path != NULL) + { + g_string_append_printf (str, "; path=%s", path); + } + if (secure) + { + g_string_append (str, "; secure"); + } + if (http_only) + { + g_string_append (str, "; httponly"); + } setlocale (LC_TIME, cur); g_free (cur); + ret = g_strdup (str->str); + g_string_free (str, TRUE); + return ret; } diff --git a/tests/cookies.c b/tests/cookies.c index 8a85366..732d777 100644 --- a/tests/cookies.c +++ b/tests/cookies.c @@ -47,11 +47,12 @@ main (int argc, char *argv[]) g_date_time_add_months (g_date_time_new_now_utc (), 3), NULL, NULL, FALSE, FALSE)); g_string_append_printf (header, "\n%s", - zak_cgi_main_set_cookie ("SECONDO", "il secondo cookie", NULL, NULL, NULL, FALSE, FALSE)); + zak_cgi_main_set_cookie ("SECONDO", "il secondo cookie", NULL, NULL, "/", FALSE, TRUE)); + + syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "header: %s", header->str); zak_cgi_main_out (header->str, str->str); g_string_free (str, TRUE); return 0; } - -- 2.49.0