From 20a451e11a5c10d4751beebcf09e0c86568ac5d2 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 26 Nov 2009 14:20:00 -0500 Subject: More fun with cookies --- src/c/urweb.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/c/urweb.c') diff --git a/src/c/urweb.c b/src/c/urweb.c index 344ef2ad..cbe065c3 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -672,7 +672,7 @@ static input *check_input_space(uw_context ctx, size_t len) { } int uw_set_input(uw_context ctx, const char *name, char *value) { - printf("Input name %s\n", name); + //printf("Input name %s\n", name); if (!strcasecmp(name, ".b")) { int n = uw_input_num(value); @@ -2680,18 +2680,41 @@ uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { return NULL; } -uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) { +uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure) { uw_write_header(ctx, "Set-Cookie: "); uw_write_header(ctx, c); uw_write_header(ctx, "="); uw_write_header(ctx, v); uw_write_header(ctx, "; path="); uw_write_header(ctx, prefix); + if (expires) { + char formatted[30]; + struct tm tm; + + gmtime_r(expires, &tm); + + strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm); + + uw_write_header(ctx, "; expires="); + uw_write_header(ctx, formatted); + } + if (secure) + uw_write_header(ctx, "; secure"); uw_write_header(ctx, "\r\n"); return uw_unit_v; } +uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) { + uw_write_header(ctx, "Set-Cookie: "); + uw_write_header(ctx, c); + uw_write_header(ctx, "=; path="); + uw_write_header(ctx, prefix); + uw_write_header(ctx, "; expires=Mon, 01-01-1970 00:00:00 GMT\r\n"); + + return uw_unit_v; +} + static delta *allocate_delta(uw_context ctx, unsigned client) { unsigned i; delta *d; @@ -3077,6 +3100,8 @@ uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) { return r; } +const uw_Basis_time minTime = 0; + uw_Basis_time uw_Basis_now(uw_context ctx) { return time(NULL); } -- cgit v1.2.3 From b9982e3f5bb8c01d2b2b787b39d3a0b6796d0e8d Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 26 Nov 2009 14:58:03 -0500 Subject: Port rest of demo to new cookie signature; fix parsing of multiple incoming cookies --- demo/cookieSec.ur | 2 +- src/c/urweb.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src/c/urweb.c') diff --git a/demo/cookieSec.ur b/demo/cookieSec.ur index 447d38ad..98615c78 100644 --- a/demo/cookieSec.ur +++ b/demo/cookieSec.ur @@ -25,7 +25,7 @@ fun main () = and set r = - setCookie username r.User; + setCookie username {Value = r.User, Expires = None, Secure = False}; main () and imHere () = diff --git a/src/c/urweb.c b/src/c/urweb.c index cbe065c3..aaa1278f 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2667,10 +2667,19 @@ uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { } if (p = uw_Basis_requestHeader(ctx, "Cookie")) { + char *p2; + while (1) { - if (!strncmp(p, c, len) && p[len] == '=') - return p + 1 + len; - else if (p = strchr(p, ';')) + if (!strncmp(p, c, len) && p[len] == '=') { + if (p2 = strchr(p, ';')) { + size_t n = p2 - (p + len); + char *r = uw_malloc(ctx, n); + memcpy(r, p + 1 + len, n-1); + r[n-1] = 0; + return r; + } else + return p + 1 + len; + } else if (p = strchr(p, ';')) p += 2; else return NULL; -- cgit v1.2.3 From 6fe3b9b161042130f6df278defee7dfd8ae6850d Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Fri, 27 Nov 2009 09:12:46 -0500 Subject: Fix handling of clearCookie/getCookie in same page gen --- src/c/urweb.c | 16 +++++++++++----- tests/vlad3.ur | 27 +++++++++++++++++++++++++++ tests/vlad3.urp | 2 ++ tests/vlad3.urs | 1 + 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/vlad3.ur create mode 100644 tests/vlad3.urp create mode 100644 tests/vlad3.urs (limited to 'src/c/urweb.c') diff --git a/src/c/urweb.c b/src/c/urweb.c index aaa1278f..d1a5d320 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2645,6 +2645,8 @@ uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, siz return b; } +#define THE_PAST "expires=Mon, 01-01-1970 00:00:00 GMT" + uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { int len = strlen(c); char *p = ctx->outHeaders.start; @@ -2658,10 +2660,14 @@ uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { size_t sz = strcspn(p2+1, ";\r\n"); if (!strncasecmp(p, c, p2 - p)) { - char *ret = uw_malloc(ctx, sz + 1); - memcpy(ret, p2+1, sz); - ret[sz] = 0; - return ret; + if (sz == 0 && strstr(p2+2, THE_PAST)) + return NULL; + else { + char *ret = uw_malloc(ctx, sz + 1); + memcpy(ret, p2+1, sz); + ret[sz] = 0; + return ret; + } } } } @@ -2719,7 +2725,7 @@ uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_s uw_write_header(ctx, c); uw_write_header(ctx, "=; path="); uw_write_header(ctx, prefix); - uw_write_header(ctx, "; expires=Mon, 01-01-1970 00:00:00 GMT\r\n"); + uw_write_header(ctx, "; " THE_PAST "\r\n"); return uw_unit_v; } diff --git a/tests/vlad3.ur b/tests/vlad3.ur new file mode 100644 index 00000000..3d1e812b --- /dev/null +++ b/tests/vlad3.ur @@ -0,0 +1,27 @@ +cookie user : {EMail : string} + +fun main () = + ro <- getCookie user; + case ro of + Some u => welcome u + | _ => login () + +and welcome u = return + Welcome {[u.EMail]}. Logout + + +and logout () = + clearCookie user; + main () + +and login () = return +
E-mail: +
+ +and signin r = + setCookie user {Value = {EMail = r.EMail}, + Expires = None, (* Some (readError "2012-11-6 +00:00:00"), *) + Secure = False + }; + main () diff --git a/tests/vlad3.urp b/tests/vlad3.urp new file mode 100644 index 00000000..3a0fa1f3 --- /dev/null +++ b/tests/vlad3.urp @@ -0,0 +1,2 @@ + +vlad3 diff --git a/tests/vlad3.urs b/tests/vlad3.urs new file mode 100644 index 00000000..6ac44e0b --- /dev/null +++ b/tests/vlad3.urs @@ -0,0 +1 @@ +val main : unit -> transaction page -- cgit v1.2.3