From c1816939cb921097620b88c213d181d0bdba7f29 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 26 Nov 2009 14:20:00 -0500 Subject: More fun with cookies --- CHANGELOG | 7 +++++++ demo/cookie.ur | 27 +++++++++++++++++++++++++-- doc/manual.tex | 3 ++- include/urweb.h | 4 +++- lib/ur/basis.urs | 6 +++++- src/c/urweb.c | 29 +++++++++++++++++++++++++++-- src/demo.sml | 2 +- src/monoize.sml | 34 +++++++++++++++++++++++++++++----- src/settings.sml | 1 + 9 files changed, 100 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 33f9bc9c..25a9b851 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +======== +Next +======== + +- Extended cookie interface (breaks backward compatibility for 'setCookie') +- Bug fixes + ======== 20091124 ======== diff --git a/demo/cookie.ur b/demo/cookie.ur index ad4e19ec..7e011157 100644 --- a/demo/cookie.ur +++ b/demo/cookie.ur @@ -1,15 +1,30 @@ cookie c : {A : string, B : float, C : int} fun set r = - setCookie c {A = r.A, B = readError r.B, C = readError r.C}; + setCookie c {Value = {A = r.A, B = readError r.B, C = readError r.C}, + Expires = None, + Secure = False}; return Cookie set. +fun setExp r = + setCookie c {Value = {A = r.A, B = readError r.B, C = readError r.C}, + Expires = Some (readError "2012-11-6 00:00:00"), + Secure = False}; + return Cookie set robustly. + +fun delete () = + clearCookie c; + return Cookie cleared. + fun main () = ro <- getCookie c; return {case ro of None => No cookie set. - | Some v => Cookie: A = {[v.A]}, B = {[v.B]}, C = {[v.C]}} + | Some v => + Cookie: A = {[v.A]}, B = {[v.B]}, C = {[v.C]}
+
+
}

@@ -17,5 +32,13 @@ fun main () = B:
C:
+
+ +
+ Version that expires on November 6, 2012:
+ A:
+ B:
+ C:
+
diff --git a/doc/manual.tex b/doc/manual.tex index 5a46552d..866b9585 100644 --- a/doc/manual.tex +++ b/doc/manual.tex @@ -1288,7 +1288,8 @@ $$\begin{array}{l} \\ \mt{con} \; \mt{http\_cookie} :: \mt{Type} \to \mt{Type} \\ \mt{val} \; \mt{getCookie} : \mt{t} ::: \mt{Type} \to \mt{http\_cookie} \; \mt{t} \to \mt{transaction} \; (\mt{option} \; \mt{t}) \\ - \mt{val} \; \mt{setCookie} : \mt{t} ::: \mt{Type} \to \mt{http\_cookie} \; \mt{t} \to \mt{t} \to \mt{transaction} \; \mt{unit} + \mt{val} \; \mt{setCookie} : \mt{t} ::: \mt{Type} \to \mt{http\_cookie} \; \mt{t} \to \{\mt{Value} : \mt{t}, \mt{Expires} : \mt{option} \; \mt{time}, \mt{Secure} : \mt{bool}\} \to \mt{transaction} \; \mt{unit} \\ + \mt{val} \; \mt{clearCookie} : \mt{t} ::: \mt{Type} \to \mt{http\_cookie} \; \mt{t} \to \mt{transaction} \; \mt{unit} \end{array}$$ There are also an abstract $\mt{url}$ type and functions for converting to it, based on the policy defined by \texttt{[allow|deny] url} directives in the project file. diff --git a/include/urweb.h b/include/urweb.h index 55068966..9884a3ca 100644 --- a/include/urweb.h +++ b/include/urweb.h @@ -185,7 +185,8 @@ uw_Basis_string uw_Basis_requestHeader(uw_context, uw_Basis_string); void uw_write_header(uw_context, uw_Basis_string); uw_Basis_string uw_Basis_get_cookie(uw_context, uw_Basis_string c); -uw_unit uw_Basis_set_cookie(uw_context, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v); +uw_unit uw_Basis_set_cookie(uw_context, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure); +uw_unit uw_Basis_clear_cookie(uw_context, uw_Basis_string prefix, uw_Basis_string c); uw_Basis_channel uw_Basis_new_channel(uw_context, uw_unit); uw_unit uw_Basis_send(uw_context, uw_Basis_channel, uw_Basis_string); @@ -210,6 +211,7 @@ uw_Basis_int uw_Basis_blobSize(uw_context, uw_Basis_blob); __attribute__((noreturn)) void uw_return_blob(uw_context, uw_Basis_blob, uw_Basis_string mimeType); uw_Basis_time uw_Basis_now(uw_context); +extern const uw_Basis_time uw_Basis_minTime; void uw_register_transactional(uw_context, void *data, uw_callback commit, uw_callback rollback, uw_callback free); diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index 47bc3d48..31aa4cdd 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -115,6 +115,7 @@ val current : t ::: Type -> signal t -> transaction t (** * Time *) val now : transaction time +val minTime : time (** HTTP operations *) @@ -123,7 +124,10 @@ val requestHeader : string -> transaction (option string) con http_cookie :: Type -> Type val getCookie : t ::: Type -> http_cookie t -> transaction (option t) -val setCookie : t ::: Type -> http_cookie t -> t -> transaction unit +val setCookie : t ::: Type -> http_cookie t -> {Value : t, + Expires : option time, + Secure : bool} -> transaction unit +val clearCookie : t ::: Type -> http_cookie t -> transaction unit (** JavaScript-y gadgets *) 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); } diff --git a/src/demo.sml b/src/demo.sml index 4e2caa99..c5480a93 100644 --- a/src/demo.sml +++ b/src/demo.sml @@ -430,7 +430,7 @@ fun make {prefix, dirname, guided} = TextIO.closeOut outf; - Compiler.compile (OS.Path.base fname) + Compiler.compiler (OS.Path.base fname) end; TextIO.output (demosOut, "\n\n"); diff --git a/src/monoize.sml b/src/monoize.sml index 5ac9d46b..25b7d9c3 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -1338,19 +1338,43 @@ fun monoExp (env, st, fm) (all as (e, loc)) = val s = (L'.TFfi ("Basis", "string"), loc) val un = (L'.TRecord [], loc) val t = monoType env t - val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t) + val rt = (L'.TRecord [("Value", t), + ("Expires", (L'.TOption (L'.TFfi ("Basis", "time"), + loc), loc)), + ("Secure", (L'.TFfi ("Basis", "bool"), loc))], loc) + + fun fd x = (L'.EField ((L'.ERel 1, loc), x), loc) + val (e, fm) = urlifyExp env fm (fd "Value", t) in - ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc), - (L'.EAbs ("v", t, (L'.TFun (un, un), loc), + ((L'.EAbs ("c", s, (L'.TFun (rt, (L'.TFun (un, un), loc)), loc), + (L'.EAbs ("r", rt, (L'.TFun (un, un), loc), (L'.EAbs ("_", un, un, (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String (Settings.getUrlPrefix ())), loc), (L'.ERel 2, loc), - e]), loc)), + e, + fd "Expires", + fd "Secure"]) + , loc)), loc)), loc)), loc), + fm) + end + + | L.ECApp ((L.EFfi ("Basis", "clearCookie"), _), t) => + let + val s = (L'.TFfi ("Basis", "string"), loc) + val un = (L'.TRecord [], loc) + in + ((L'.EAbs ("c", s, (L'.TFun (un, un), loc), + (L'.EAbs ("_", un, un, + (L'.EFfiApp ("Basis", "clear_cookie", + [(L'.EPrim (Prim.String + (Settings.getUrlPrefix ())), + loc), + (L'.ERel 1, loc)]), loc)), loc)), loc), fm) - end + end | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc), diff --git a/src/settings.sml b/src/settings.sml index 300bbf2c..009e2b0a 100644 --- a/src/settings.sml +++ b/src/settings.sml @@ -80,6 +80,7 @@ fun mayClientToServer x = S.member (!clientToServer, x) val effectfulBase = basis ["dml", "nextval", "set_cookie", + "clear_cookie", "new_client_source", "get_client_source", "set_client_source", -- cgit v1.2.3