comparison src/c/urweb.c @ 854:158d980889ac

Further refactoring of request.c to work with CGI
author Adam Chlipala <adamc@hcoop.net>
date Tue, 23 Jun 2009 15:40:35 -0400
parents 74a1e3bdf430
children 86ec89baee01
comparison
equal deleted inserted replaced
853:19fdeef40ada 854:158d980889ac
308 void *data; 308 void *data;
309 uw_callback commit, rollback, free; 309 uw_callback commit, rollback, free;
310 } transactional; 310 } transactional;
311 311
312 struct uw_context { 312 struct uw_context {
313 char *headers, *headers_end; 313 char *(*get_header)(void *, const char *);
314 void *get_header_data;
314 315
315 buf outHeaders, page, heap, script; 316 buf outHeaders, page, heap, script;
316 input *inputs, *subinputs, *cur_container; 317 input *inputs, *subinputs, *cur_container;
317 size_t n_subinputs, used_subinputs; 318 size_t n_subinputs, used_subinputs;
318 319
346 extern int uw_inputs_len, uw_timeout; 347 extern int uw_inputs_len, uw_timeout;
347 348
348 uw_context uw_init() { 349 uw_context uw_init() {
349 uw_context ctx = malloc(sizeof(struct uw_context)); 350 uw_context ctx = malloc(sizeof(struct uw_context));
350 351
351 ctx->headers = ctx->headers_end = NULL; 352 ctx->get_header = NULL;
353 ctx->get_header_data = NULL;
352 354
353 buf_init(&ctx->outHeaders, 0); 355 buf_init(&ctx->outHeaders, 0);
354 buf_init(&ctx->page, 0); 356 buf_init(&ctx->page, 0);
355 buf_init(&ctx->heap, 0); 357 buf_init(&ctx->heap, 0);
356 buf_init(&ctx->script, 1); 358 buf_init(&ctx->script, 1);
456 uw_db_init(ctx); 458 uw_db_init(ctx);
457 459
458 return r; 460 return r;
459 } 461 }
460 462
461 void uw_set_headers(uw_context ctx, char *headers) { 463 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
462 char *s = headers, *s2; 464 ctx->get_header = get_header;
463 ctx->headers = headers; 465 ctx->get_header_data = get_header_data;
464
465 while (s2 = strchr(s, '\r')) {
466 s = s2;
467
468 if (s[1] == 0)
469 break;
470
471 *s = 0;
472 s += 2;
473 }
474
475 ctx->headers_end = s;
476 }
477
478 void uw_headers_moved(uw_context ctx, char *headers) {
479 ctx->headers_end = headers + (ctx->headers_end - ctx->headers);
480 ctx->headers = headers;
481 } 466 }
482 467
483 int uw_db_begin(uw_context); 468 int uw_db_begin(uw_context);
484 469
485 static void uw_set_error(uw_context ctx, const char *fmt, ...) { 470 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
521 ctx->cleanup_front->arg = arg; 506 ctx->cleanup_front->arg = arg;
522 ++ctx->cleanup_front; 507 ++ctx->cleanup_front;
523 } 508 }
524 509
525 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { 510 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
526 int len = strlen(h); 511 return ctx->get_header(ctx->get_header_data, h);
527 char *s = ctx->headers, *p;
528
529 while (p = strchr(s, ':')) {
530 if (p - s == len && !strncasecmp(s, h, len)) {
531 return p + 2;
532 } else {
533 if ((s = strchr(p, 0)) && s < ctx->headers_end)
534 s += 2;
535 else
536 return NULL;
537 }
538 }
539
540 return NULL;
541 } 512 }
542 513
543 void uw_login(uw_context ctx) { 514 void uw_login(uw_context ctx) {
544 if (ctx->needs_push) { 515 if (ctx->needs_push) {
545 char *id_s, *pass_s; 516 char *id_s, *pass_s;
2375 return b; 2346 return b;
2376 } 2347 }
2377 2348
2378 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { 2349 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
2379 int len = strlen(c); 2350 int len = strlen(c);
2380 char *s = ctx->headers, *p = ctx->outHeaders.start; 2351 char *p = ctx->outHeaders.start;
2381 2352
2382 while (p = strstr(p, "\nSet-Cookie: ")) { 2353 while (p = strstr(p, "\nSet-Cookie: ")) {
2383 char *p2; 2354 char *p2;
2384 p += 13; 2355 p += 13;
2385 p2 = strchr(p, '='); 2356 p2 = strchr(p, '=');
2394 return ret; 2365 return ret;
2395 } 2366 }
2396 } 2367 }
2397 } 2368 }
2398 2369
2399 while (p = strchr(s, ':')) { 2370 if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
2400 if (!strncasecmp(s, "Cookie: ", 8)) { 2371 while (1) {
2401 p += 2; 2372 if (!strncmp(p, c, len) && p[len] == '=')
2402 while (1) { 2373 return p + 1 + len;
2403 if (!strncmp(p, c, len) 2374 else if (p = strchr(p, ';'))
2404 && p + len < ctx->headers_end && p[len] == '=') 2375 p += 2;
2405 return p + 1 + len;
2406 else if (p = strchr(p, ';'))
2407 p += 2;
2408 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
2409 s += 2;
2410 break;
2411 }
2412 else
2413 return NULL;
2414 }
2415 } else {
2416 if ((s = strchr(p, 0)) && s < ctx->headers_end)
2417 s += 2;
2418 else 2376 else
2419 return NULL; 2377 return NULL;
2420 } 2378 }
2421 } 2379 }
2422 2380