diff src/c/urweb.c @ 736:796e42c93c48

Cookie signatures for RPCs
author Adam Chlipala <adamc@hcoop.net>
date Thu, 23 Apr 2009 16:13:02 -0400
parents f2a2be93331c
children d049d31a1966
line wrap: on
line diff
--- a/src/c/urweb.c	Thu Apr 23 14:10:10 2009 -0400
+++ b/src/c/urweb.c	Thu Apr 23 16:13:02 2009 -0400
@@ -300,7 +300,7 @@
 
   const char *script_header, *url_prefix;
 
-  int needs_push;
+  int needs_push, needs_sig;
 
   size_t n_deltas, used_deltas;
   delta *deltas;
@@ -336,6 +336,7 @@
   ctx->script_header = "";
   ctx->url_prefix = "/";
   ctx->needs_push = 0;
+  ctx->needs_sig = 0;
   
   ctx->error_message[0] = 0;
 
@@ -589,6 +590,10 @@
   ctx->needs_push = n;
 }
 
+void uw_set_needs_sig(uw_context ctx, int n) {
+  ctx->needs_sig = n;
+}
+
 
 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
   if (b->back - b->front < extra) {
@@ -717,16 +722,30 @@
   }
 }
 
+extern uw_Basis_string uw_cookie_sig(uw_context);
+
 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
-  if (ctx->client == NULL)
-    return "";
-  else {
-    char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix));
-    sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;listener();",
+  if (ctx->client == NULL) {
+    if (ctx->needs_sig) {
+      char *sig = uw_cookie_sig(ctx);
+      char *r = uw_malloc(ctx, strlen(sig) + 8);
+      sprintf(r, "sig=\"%s\";", sig);
+      return r;
+    }
+    else
+      return "";
+  } else {
+    char *sig = ctx->needs_sig ? uw_cookie_sig(ctx) : "";
+    char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix)
+                        + (ctx->needs_sig ? strlen(sig) + 7 : 0));
+    sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
             ctx->client->id,
             ctx->client->pass,
             ctx->url_prefix,
-            ctx->timeout);
+            ctx->timeout,
+            ctx->needs_sig ? "sig=\"" : "",
+            sig,
+            ctx->needs_sig ? "\";" : "");
     return r;
   }
 }
@@ -1998,8 +2017,6 @@
   return r;
 }
 
-extern uw_Basis_string uw_cookie_sig(uw_context);
-
 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
   return uw_cookie_sig(ctx);
 }