changeset 1066:740b85ef4352

Basis.currentUrl, plus redirect debugging
author Adam Chlipala <adamc@hcoop.net>
date Thu, 10 Dec 2009 15:20:36 -0500
parents 217eb87dde31
children 50dd937c4cb9
files include/urweb.h lib/ur/basis.urs src/c/request.c src/c/urweb.c
diffstat 4 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu Dec 10 13:32:09 2009 -0500
+++ b/include/urweb.h	Thu Dec 10 15:20:36 2009 -0500
@@ -248,4 +248,7 @@
 uw_Basis_char uw_Basis_tolower(uw_context, uw_Basis_char);
 uw_Basis_char uw_Basis_toupper(uw_context, uw_Basis_char);
 
+uw_Basis_string uw_Basis_currentUrl(uw_context);
+void uw_set_currentUrl(uw_context, char *);
+
 #endif
--- a/lib/ur/basis.urs	Thu Dec 10 13:32:09 2009 -0500
+++ b/lib/ur/basis.urs	Thu Dec 10 15:20:36 2009 -0500
@@ -565,6 +565,7 @@
 val checkUrl : string -> option url
 val url : transaction page -> url
 val redirect : t ::: Type -> url -> transaction t
+val currentUrl : transaction url
 
 val dyn : ctx ::: {Unit} -> use ::: {Type} -> bind ::: {Type} -> [ctx ~ body] => unit
           -> tag [Signal = signal (xml (body ++ ctx) use bind)] (body ++ ctx) [] use bind
--- a/src/c/request.c	Thu Dec 10 13:32:09 2009 -0500
+++ b/src/c/request.c	Thu Dec 10 15:20:36 2009 -0500
@@ -169,6 +169,8 @@
   size_t boundary_len;
   char *inputs;
 
+  uw_set_currentUrl(ctx, path);
+
   if (!strcmp(method, "POST")) {
     char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
     if (!clen_s) {
--- a/src/c/urweb.c	Thu Dec 10 13:32:09 2009 -0500
+++ b/src/c/urweb.c	Thu Dec 10 15:20:36 2009 -0500
@@ -384,6 +384,8 @@
   global *globals;
   size_t n_globals;
 
+  char *current_url;
+
   char error_message[ERROR_BUF_LEN];
 };
 
@@ -437,6 +439,8 @@
   ctx->globals = malloc(0);
   ctx->n_globals = 0;
 
+  ctx->current_url = "";
+
   return ctx;
 }
 
@@ -3048,7 +3052,7 @@
   buf_check(&ctx->outHeaders, INTS_MAX);
   sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
   ctx->outHeaders.front += len;
-  uw_write_header(ctx, "\r\n");  
+  uw_write_header(ctx, "\r\n");
 
   buf_append(&ctx->page, b.data, b.size);
 
@@ -3063,12 +3067,30 @@
 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
   cleanup *cl;
   int len;
+  char *s;
 
   ctx->returning_indirectly = 1;
+  buf_reset(&ctx->page);
+  buf_check(&ctx->page, buf_used(&ctx->outHeaders)+1);
+  memcpy(ctx->page.start, ctx->outHeaders.start, buf_used(&ctx->outHeaders));
+  ctx->page.start[buf_used(&ctx->outHeaders)] = 0;
   buf_reset(&ctx->outHeaders);
-  buf_reset(&ctx->page);
 
   uw_write_header(ctx, on_redirect);
+
+  s = strchr(ctx->page.start, '\n');
+  if (s) {
+    char *s2;
+    for (++s; s2 = strchr(s, '\n'); s = s2+1) {
+      *s2 = 0;
+      printf("Line: %s\n", s);
+      if (!strncmp(s, "Set-Cookie: ", 12)) {
+        uw_write_header(ctx, s);
+        uw_write_header(ctx, "\n");
+      }
+    }
+  }
+
   uw_write_header(ctx, "Location: ");
   uw_write_header(ctx, url);
   uw_write_header(ctx, "\r\n\r\n");
@@ -3228,3 +3250,11 @@
 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
   return toupper(c);
 }
+
+uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
+  return ctx->current_url;
+}
+
+void uw_set_currentUrl(uw_context ctx, char *s) {
+  ctx->current_url = s;
+}