diff 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
line wrap: on
line diff
--- a/src/c/urweb.c	Sun Jul 29 12:54:17 2012 -0400
+++ b/src/c/urweb.c	Thu Aug 02 16:33:25 2012 -0400
@@ -420,6 +420,9 @@
   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 @@
   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 @@
   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_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 @@
     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 @@
   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 : "";
 }