comparison src/c/urweb.c @ 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
comparison
equal deleted inserted replaced
1065:217eb87dde31 1066:740b85ef4352
382 size_t n_transactionals, used_transactionals; 382 size_t n_transactionals, used_transactionals;
383 383
384 global *globals; 384 global *globals;
385 size_t n_globals; 385 size_t n_globals;
386 386
387 char *current_url;
388
387 char error_message[ERROR_BUF_LEN]; 389 char error_message[ERROR_BUF_LEN];
388 }; 390 };
389 391
390 extern int uw_inputs_len, uw_timeout; 392 extern int uw_inputs_len, uw_timeout;
391 393
434 ctx->transactionals = malloc(0); 436 ctx->transactionals = malloc(0);
435 ctx->n_transactionals = ctx->used_transactionals = 0; 437 ctx->n_transactionals = ctx->used_transactionals = 0;
436 438
437 ctx->globals = malloc(0); 439 ctx->globals = malloc(0);
438 ctx->n_globals = 0; 440 ctx->n_globals = 0;
441
442 ctx->current_url = "";
439 443
440 return ctx; 444 return ctx;
441 } 445 }
442 446
443 void uw_set_db(uw_context ctx, void *db) { 447 void uw_set_db(uw_context ctx, void *db) {
3046 uw_write_header(ctx, mimeType); 3050 uw_write_header(ctx, mimeType);
3047 uw_write_header(ctx, "\r\nContent-Length: "); 3051 uw_write_header(ctx, "\r\nContent-Length: ");
3048 buf_check(&ctx->outHeaders, INTS_MAX); 3052 buf_check(&ctx->outHeaders, INTS_MAX);
3049 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len); 3053 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
3050 ctx->outHeaders.front += len; 3054 ctx->outHeaders.front += len;
3051 uw_write_header(ctx, "\r\n"); 3055 uw_write_header(ctx, "\r\n");
3052 3056
3053 buf_append(&ctx->page, b.data, b.size); 3057 buf_append(&ctx->page, b.data, b.size);
3054 3058
3055 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) 3059 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
3056 cl->func(cl->arg); 3060 cl->func(cl->arg);
3061 } 3065 }
3062 3066
3063 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) { 3067 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
3064 cleanup *cl; 3068 cleanup *cl;
3065 int len; 3069 int len;
3070 char *s;
3066 3071
3067 ctx->returning_indirectly = 1; 3072 ctx->returning_indirectly = 1;
3073 buf_reset(&ctx->page);
3074 buf_check(&ctx->page, buf_used(&ctx->outHeaders)+1);
3075 memcpy(ctx->page.start, ctx->outHeaders.start, buf_used(&ctx->outHeaders));
3076 ctx->page.start[buf_used(&ctx->outHeaders)] = 0;
3068 buf_reset(&ctx->outHeaders); 3077 buf_reset(&ctx->outHeaders);
3069 buf_reset(&ctx->page);
3070 3078
3071 uw_write_header(ctx, on_redirect); 3079 uw_write_header(ctx, on_redirect);
3080
3081 s = strchr(ctx->page.start, '\n');
3082 if (s) {
3083 char *s2;
3084 for (++s; s2 = strchr(s, '\n'); s = s2+1) {
3085 *s2 = 0;
3086 printf("Line: %s\n", s);
3087 if (!strncmp(s, "Set-Cookie: ", 12)) {
3088 uw_write_header(ctx, s);
3089 uw_write_header(ctx, "\n");
3090 }
3091 }
3092 }
3093
3072 uw_write_header(ctx, "Location: "); 3094 uw_write_header(ctx, "Location: ");
3073 uw_write_header(ctx, url); 3095 uw_write_header(ctx, url);
3074 uw_write_header(ctx, "\r\n\r\n"); 3096 uw_write_header(ctx, "\r\n\r\n");
3075 3097
3076 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) 3098 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
3226 } 3248 }
3227 3249
3228 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) { 3250 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
3229 return toupper(c); 3251 return toupper(c);
3230 } 3252 }
3253
3254 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
3255 return ctx->current_url;
3256 }
3257
3258 void uw_set_currentUrl(uw_context ctx, char *s) {
3259 ctx->current_url = s;
3260 }