comparison src/c/urweb.c @ 1799:3d922a28370b

Basis.getenv
author Adam Chlipala <adam@chlipala.net>
date Thu, 02 Aug 2012 16:33:25 -0400
parents f57983ba2a36
children 5c51ae0d643b
comparison
equal deleted inserted replaced
1798:10a2cb93d175 1799:3d922a28370b
418 int id; 418 int id;
419 419
420 char *(*get_header)(void *, const char *); 420 char *(*get_header)(void *, const char *);
421 void *get_header_data; 421 void *get_header_data;
422 422
423 char *(*get_env)(void *, const char *);
424 void *get_env_data;
425
423 uw_buffer outHeaders, page, heap, script; 426 uw_buffer outHeaders, page, heap, script;
424 int allowed_to_return_indirectly, returning_indirectly; 427 int allowed_to_return_indirectly, returning_indirectly;
425 input *inputs, *subinputs, *cur_container; 428 input *inputs, *subinputs, *cur_container;
426 size_t sz_inputs, n_subinputs, used_subinputs; 429 size_t sz_inputs, n_subinputs, used_subinputs;
427 430
481 ctx->app = NULL; 484 ctx->app = NULL;
482 ctx->id = id; 485 ctx->id = id;
483 486
484 ctx->get_header = NULL; 487 ctx->get_header = NULL;
485 ctx->get_header_data = NULL; 488 ctx->get_header_data = NULL;
489
490 ctx->get_env = NULL;
491 ctx->get_env_data = NULL;
486 492
487 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1); 493 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1);
488 ctx->outHeaders.start[0] = 0; 494 ctx->outHeaders.start[0] = 0;
489 uw_buffer_init(uw_page_max, &ctx->page, 1); 495 uw_buffer_init(uw_page_max, &ctx->page, 1);
490 ctx->page.start[0] = 0; 496 ctx->page.start[0] = 0;
651 } 657 }
652 658
653 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) { 659 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
654 ctx->get_header = get_header; 660 ctx->get_header = get_header;
655 ctx->get_header_data = get_header_data; 661 ctx->get_header_data = get_header_data;
662 }
663
664 void uw_set_env(uw_context ctx, char *(*get_env)(void *, const char *), void *get_env_data) {
665 ctx->get_env = get_env;
666 ctx->get_env_data = get_env_data;
656 } 667 }
657 668
658 static void uw_set_error(uw_context ctx, const char *fmt, ...) { 669 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
659 va_list ap; 670 va_list ap;
660 va_start(ap, fmt); 671 va_start(ap, fmt);
3474 return s; 3485 return s;
3475 else 3486 else
3476 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s)); 3487 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s));
3477 } 3488 }
3478 3489
3490 static int envVar_format(const char *s) {
3491 for (; *s; ++s)
3492 if (!isalnum((int)*s) && *s != '_' && *s != '.')
3493 return 0;
3494
3495 return 1;
3496 }
3497
3479 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) { 3498 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) {
3480 if (!mime_format(s)) 3499 if (!envVar_format(s))
3481 return NULL; 3500 return NULL;
3482 3501
3483 if (ctx->app->check_responseHeader(s)) 3502 if (ctx->app->check_responseHeader(s))
3484 return s; 3503 return s;
3485 else 3504 else
3486 return NULL; 3505 return NULL;
3487 } 3506 }
3488 3507
3508 uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) {
3509 if (!envVar_format(s))
3510 uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
3511
3512 if (ctx->app->check_envVar(s))
3513 return s;
3514 else
3515 uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s));
3516 }
3517
3518 uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) {
3519 if (!mime_format(s))
3520 return NULL;
3521
3522 if (ctx->app->check_envVar(s))
3523 return s;
3524 else
3525 return NULL;
3526 }
3527
3489 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) { 3528 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) {
3490 return uw_Basis_requestHeader(ctx, name); 3529 return uw_Basis_requestHeader(ctx, name);
3491 } 3530 }
3492 3531
3493 static int mime_value_format(const char *s) { 3532 static int mime_value_format(const char *s) {
3506 uw_write_header(ctx, ": "); 3545 uw_write_header(ctx, ": ");
3507 uw_write_header(ctx, value); 3546 uw_write_header(ctx, value);
3508 uw_write_header(ctx, "\r\n"); 3547 uw_write_header(ctx, "\r\n");
3509 3548
3510 return uw_unit_v; 3549 return uw_unit_v;
3550 }
3551
3552 uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) {
3553 return ctx->get_env(ctx->get_env_data, name);
3511 } 3554 }
3512 3555
3513 uw_Basis_string uw_unnull(uw_Basis_string s) { 3556 uw_Basis_string uw_unnull(uw_Basis_string s) {
3514 return s ? s : ""; 3557 return s ? s : "";
3515 } 3558 }