show lock scaling table
[tridge/junkcode.git] / tserver / cgi.c
index 4888f4287e0eec65aebfda46a8f83714c208adf0..b4cf0d6ecc5d9e21b58a4b10ff4453a7cdd38cb3 100644 (file)
@@ -197,7 +197,7 @@ static int load_one_part(struct cgi_state *cgi, FILE *f, int *len, char *boundar
                    memcmp("--", &content[content_len-boundary_len-2], 2) == 0) {
                        content_len -= boundary_len+4;
                        if (name) {
-                               if (raw_data) {
+                               if (raw_data || filename) {
                                        put(cgi, name, filename?filename:"");
                                        cgi->variables->content = content;
                                        cgi->variables->content_len = content_len;
@@ -381,7 +381,7 @@ static void download(struct cgi_state *cgi, const char *path)
        mtype = cgi->http_header(cgi, path);
 
        if (mtype == MIME_TYPE_TEXT_HTML) {
-               cgi->tmpl->process(cgi->tmpl, path);
+               cgi->tmpl->process(cgi->tmpl, path, 1);
                return;
        }
 
@@ -392,16 +392,43 @@ static void download(struct cgi_state *cgi, const char *path)
        }
 }
 
-/*
-  read and parse the http request
- */
-static int setup(struct cgi_state *cgi)
+
+/* we're running under a web server as cgi */
+static int setup_cgi(struct cgi_state *cgi)
+{
+       char *p;
+
+       if ((p = getenv("CONTENT_LENGTH"))) {
+               cgi->content_length = atoi(p);
+       }
+       if ((p = getenv("REQUEST_METHOD"))) {
+               cgi->got_request = 1;
+               if (strcasecmp(p, "POST") == 0) {
+                       cgi->request_post = 1;
+               }
+       }
+       if ((p = getenv("QUERY_STRING"))) {
+               cgi->query_string = strdup(p);
+       }
+       if ((p = getenv("SCRIPT_NAME"))) {
+               cgi->url = strdup(p);
+               cgi->pathinfo = cgi->url;
+       }
+       if ((p = getenv("CONTENT_TYPE"))) {
+               cgi->content_type = strdup(p);
+       }
+       return 0;
+}
+
+
+
+/* we are a mini-web server. We need to read the request from stdin */
+static int setup_standalone(struct cgi_state *cgi)
 {
        char line[1024];
        char *url=NULL;
        char *p;
 
-       /* we are a mini-web server. We need to read the request from stdin */
        while (fgets(line, sizeof(line)-1, stdin)) {
                trim_tail(line, CRLF);
                if (line[0] == 0) break;
@@ -428,6 +455,7 @@ static int setup(struct cgi_state *cgi)
        if (!url) {
                cgi->http_error(cgi, "400 Bad Request", "",
                                "You must specify a GET or POST request");
+               exit(1);
        }
 
        /* trim the URL */
@@ -442,12 +470,26 @@ static int setup(struct cgi_state *cgi)
        }
 
        cgi->url = url;
-
        cgi->pathinfo = url;
+       return 0;
+}
+
+/*
+  read and parse the http request
+ */
+static int setup(struct cgi_state *cgi)
+{
+       int ret;
 
-       while (*cgi->pathinfo == '/') cgi->pathinfo++;
+       if (getenv("GATEWAY_INTERFACE")) {
+               ret = setup_cgi(cgi);
+       } else {
+               ret = setup_standalone(cgi);
+       }
 
-       return 0;
+       while (cgi->pathinfo && *cgi->pathinfo == '/') cgi->pathinfo++;
+
+       return ret;
 }
 
 /*