From fdc6c1310be60fbeb597400618473861f78676de Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 2 Aug 2012 16:33:25 -0400 Subject: Basis.getenv --- src/c/cgi.c | 6 ++++++ src/c/fastcgi.c | 7 +++++++ src/c/http.c | 5 +++++ src/c/urweb.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) (limited to 'src/c') diff --git a/src/c/cgi.c b/src/c/cgi.c index 6d6e5252..d3ec32c7 100644 --- a/src/c/cgi.c +++ b/src/c/cgi.c @@ -1,6 +1,7 @@ #include "config.h" #include +#include #include #include #include @@ -39,6 +40,10 @@ static char *get_header(void *data, const char *h) { return NULL; } +static char *get_env(void *data, const char *name) { + return getenv(name); +} + static void on_success(uw_context ctx) { } static void on_failure(uw_context ctx) { @@ -102,6 +107,7 @@ int main(int argc, char *argv[]) { uw_set_on_success(""); uw_set_headers(ctx, get_header, NULL); + uw_set_env(ctx, get_env, NULL); uw_request_init(&uw_application, NULL, log_error, log_debug); body[body_pos] = 0; diff --git a/src/c/fastcgi.c b/src/c/fastcgi.c index 5677af83..9e3c8d7e 100644 --- a/src/c/fastcgi.c +++ b/src/c/fastcgi.c @@ -229,6 +229,12 @@ static char *get_header(void *data, const char *h) { return search_nvps(hs->nvps, hs->uppercased); } +static char *get_env(void *data, const char *h) { + headers *hs = (headers *)data; + + return search_nvps(hs->nvps, h); +} + static int read_funny_len(unsigned char **buf, int *len) { if (*len <= 0) return -1; @@ -471,6 +477,7 @@ static void *worker(void *data) { query_string = ""; uw_set_headers(ctx, get_header, &hs); + uw_set_env(ctx, get_env, &hs); { request_result rr; diff --git a/src/c/http.c b/src/c/http.c index 9af86070..aa045d7a 100644 --- a/src/c/http.c +++ b/src/c/http.c @@ -40,6 +40,10 @@ static char *get_header(void *data, const char *h) { return NULL; } +static char *get_env(void *data, const char *name) { + return getenv(name); +} + static void on_success(uw_context ctx) { uw_write_header(ctx, "HTTP/1.1 200 OK\r\n"); } @@ -193,6 +197,7 @@ static void *worker(void *data) { } uw_set_headers(ctx, get_header, headers); + uw_set_env(ctx, get_env, NULL); printf("Serving URI %s....\n", path); rr = uw_request(rc, ctx, method, path, query_string, body, back - body, diff --git a/src/c/urweb.c b/src/c/urweb.c index 8d44088d..2eb1c9fe 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -420,6 +420,9 @@ struct uw_context { char *(*get_header)(void *, const char *); void *get_header_data; + char *(*get_env)(void *, const char *); + void *get_env_data; + uw_buffer outHeaders, page, heap, script; int allowed_to_return_indirectly, returning_indirectly; input *inputs, *subinputs, *cur_container; @@ -484,6 +487,9 @@ uw_context uw_init(int id, void *logger_data, uw_logger log_debug) { ctx->get_header = NULL; ctx->get_header_data = NULL; + ctx->get_env = NULL; + ctx->get_env_data = NULL; + uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1); ctx->outHeaders.start[0] = 0; uw_buffer_init(uw_page_max, &ctx->page, 1); @@ -655,6 +661,11 @@ void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), v ctx->get_header_data = get_header_data; } +void uw_set_env(uw_context ctx, char *(*get_env)(void *, const char *), void *get_env_data) { + ctx->get_env = get_env; + ctx->get_env_data = get_env_data; +} + static void uw_set_error(uw_context ctx, const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -3476,8 +3487,16 @@ uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s)); } +static int envVar_format(const char *s) { + for (; *s; ++s) + if (!isalnum((int)*s) && *s != '_' && *s != '.') + return 0; + + return 1; +} + uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) { - if (!mime_format(s)) + if (!envVar_format(s)) return NULL; if (ctx->app->check_responseHeader(s)) @@ -3486,6 +3505,26 @@ uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) return NULL; } +uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) { + if (!envVar_format(s)) + uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s)); + + if (ctx->app->check_envVar(s)) + return s; + else + uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s)); +} + +uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) { + if (!mime_format(s)) + return NULL; + + if (ctx->app->check_envVar(s)) + return s; + else + return NULL; +} + uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) { return uw_Basis_requestHeader(ctx, name); } @@ -3510,6 +3549,10 @@ uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string return uw_unit_v; } +uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) { + return ctx->get_env(ctx->get_env_data, name); +} + uw_Basis_string uw_unnull(uw_Basis_string s) { return s ? s : ""; } -- cgit v1.2.3