annotate src/c/cgi.c @ 2268:bc1ef958d801

Thread state through addCaching more carefully.
author Ziv Scully <ziv@mit.edu>
date Wed, 14 Oct 2015 23:10:10 -0400
parents c93fbd139732
children
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adamc@856 3 #include <stdio.h>
adam@1799 4 #include <stdlib.h>
adamc@856 5 #include <ctype.h>
adamc@856 6 #include <string.h>
adamc@856 7 #include <stdlib.h>
adamc@856 8 #include <unistd.h>
adamc@856 9 #include <stdarg.h>
adamc@856 10
adamc@1094 11 #include "urweb.h"
adamc@856 12 #include "request.h"
adamc@856 13
adamc@1094 14 extern uw_app uw_application;
adamc@1094 15
adamc@856 16 static char *uppercased;
adamc@856 17 static size_t uppercased_len;
adamc@856 18
adamc@856 19 static char *get_header(void *data, const char *h) {
adamc@856 20 size_t len = strlen(h);
adamc@856 21 char *s, *r;
adamc@856 22 const char *saved_h = h;
adamc@856 23
adamc@856 24 if (len > uppercased_len) {
adamc@856 25 uppercased_len = len;
adamc@856 26 uppercased = realloc(uppercased, len + 6);
adamc@856 27 }
adamc@856 28
adamc@856 29 strcpy(uppercased, "HTTP_");
adamc@856 30 for (s = uppercased+5; *h; ++h)
adamc@1154 31 *s++ = *h == '-' ? '_' : toupper((int)*h);
adamc@856 32 *s = 0;
adamc@856 33
adamc@1134 34 if ((r = getenv(uppercased)))
adamc@856 35 return r;
adamc@856 36 else if (!strcasecmp(saved_h, "Content-length")
adamc@856 37 || !strcasecmp(saved_h, "Content-type"))
adamc@856 38 return getenv(uppercased + 5);
adamc@856 39 else
adamc@856 40 return NULL;
adamc@856 41 }
adamc@856 42
adam@1799 43 static char *get_env(void *data, const char *name) {
adam@1799 44 return getenv(name);
adam@1799 45 }
adam@1799 46
adamc@856 47 static void on_success(uw_context ctx) { }
adamc@856 48
adamc@856 49 static void on_failure(uw_context ctx) {
adamc@856 50 uw_write_header(ctx, "Status: 500 Internal Server Error\r\n");
adamc@856 51 }
adamc@856 52
adamc@856 53 static void log_error(void *data, const char *fmt, ...) {
adamc@856 54 va_list ap;
adamc@856 55 va_start(ap, fmt);
adamc@856 56
adamc@856 57 vfprintf(stderr, fmt, ap);
adamc@856 58 }
adamc@856 59
adamc@856 60 static void log_debug(void *data, const char *fmt, ...) {
adamc@856 61 }
adamc@856 62
grrwlf@1997 63 static uw_loggers ls = {NULL, log_error, log_debug};
grrwlf@1997 64
adamc@856 65 int main(int argc, char *argv[]) {
grrwlf@1997 66 uw_context ctx = uw_request_new_context(0, &uw_application, &ls);
adamc@856 67 uw_request_context rc = uw_new_request_context();
adamc@856 68 request_result rr;
adamc@856 69 char *method = getenv("REQUEST_METHOD"),
adamc@856 70 *path = getenv("SCRIPT_NAME"), *path_info = getenv("PATH_INFO"),
adamc@856 71 *query_string = getenv("QUERY_STRING");
adamc@856 72 char *body = malloc(1);
adamc@856 73 ssize_t body_len = 1, body_pos = 0, res;
adamc@856 74
adamc@856 75 uppercased = malloc(6);
adamc@856 76
adamc@856 77 if (!method) {
adamc@856 78 log_error(NULL, "REQUEST_METHOD not set\n");
adamc@856 79 exit(1);
adamc@856 80 }
adamc@856 81
adamc@856 82 if (!path) {
adamc@856 83 log_error(NULL, "SCRIPT_NAME not set\n");
adamc@856 84 exit(1);
adamc@856 85 }
adamc@856 86
adamc@856 87 if (path_info) {
adamc@856 88 char *new_path = malloc(strlen(path) + strlen(path_info) + 1);
adamc@856 89 sprintf(new_path, "%s%s", path, path_info);
adamc@856 90 path = new_path;
adamc@856 91 }
adamc@856 92
adamc@856 93 if (!query_string)
adamc@856 94 query_string = "";
adamc@856 95
adamc@856 96 while ((res = read(0, body + body_pos, body_len - body_pos)) > 0) {
adamc@856 97 body_pos += res;
adamc@856 98
adamc@856 99 if (body_pos == body_len) {
adamc@856 100 body_len *= 2;
adamc@856 101 body = realloc(body, body_len);
adamc@856 102 }
adamc@856 103 }
adamc@856 104
adamc@856 105 if (res < 0) {
adamc@856 106 log_error(NULL, "Error reading stdin\n");
adamc@856 107 exit(1);
adamc@856 108 }
adamc@856 109
adamc@856 110 uw_set_on_success("");
adamc@856 111 uw_set_headers(ctx, get_header, NULL);
adam@1799 112 uw_set_env(ctx, get_env, NULL);
grrwlf@1997 113 uw_request_init(&uw_application, &ls);
adamc@856 114
adamc@856 115 body[body_pos] = 0;
adamc@856 116 rr = uw_request(rc, ctx, method, path, query_string, body, body_pos,
adamc@856 117 on_success, on_failure,
adamc@856 118 NULL, log_error, log_debug,
adamc@863 119 -1, NULL, NULL);
adamc@856 120 uw_print(ctx, 1);
adamc@856 121
adamc@856 122 if (rr == SERVED)
adamc@856 123 return 0;
adamc@856 124 else
adamc@856 125 return 1;
adamc@856 126 }
adamc@1121 127
adamc@1121 128 void *uw_init_client_data() {
adamc@1121 129 return NULL;
adamc@1121 130 }
adamc@1121 131
adamc@1121 132 void uw_free_client_data(void *data) {
adamc@1121 133 }
adamc@1121 134
adamc@1121 135 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 136 }
adamc@1121 137
adamc@1121 138 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1941 139 uw_ensure_transaction(ctx);
adam@1941 140 uw_get_app(ctx)->expunger(ctx, cli);
adam@1941 141
adam@1941 142 if (uw_commit(ctx))
adam@1941 143 uw_error(ctx, UNLIMITED_RETRY, "Rerunning expunge transaction");
adamc@1121 144 }
adamc@1121 145
adamc@1121 146 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 147 }
adam@1320 148
adam@1843 149 int uw_supports_direct_status = 0;