annotate src/c/cgi.c @ 1936:6745eafff617

Start SQL transactions as read-only when possible, based on conservative program analysis
author Adam Chlipala <adam@chlipala.net>
date Thu, 12 Dec 2013 17:42:48 -0500
parents 0da75c01a993
children d02c1a0d8082
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
adamc@856 63 int main(int argc, char *argv[]) {
adam@1446 64 uw_context ctx = uw_request_new_context(0, &uw_application, NULL, log_error, log_debug);
adamc@856 65 uw_request_context rc = uw_new_request_context();
adamc@856 66 request_result rr;
adamc@856 67 char *method = getenv("REQUEST_METHOD"),
adamc@856 68 *path = getenv("SCRIPT_NAME"), *path_info = getenv("PATH_INFO"),
adamc@856 69 *query_string = getenv("QUERY_STRING");
adamc@856 70 char *body = malloc(1);
adamc@856 71 ssize_t body_len = 1, body_pos = 0, res;
adamc@856 72
adamc@856 73 uppercased = malloc(6);
adamc@856 74
adamc@856 75 if (!method) {
adamc@856 76 log_error(NULL, "REQUEST_METHOD not set\n");
adamc@856 77 exit(1);
adamc@856 78 }
adamc@856 79
adamc@856 80 if (!path) {
adamc@856 81 log_error(NULL, "SCRIPT_NAME not set\n");
adamc@856 82 exit(1);
adamc@856 83 }
adamc@856 84
adamc@856 85 if (path_info) {
adamc@856 86 char *new_path = malloc(strlen(path) + strlen(path_info) + 1);
adamc@856 87 sprintf(new_path, "%s%s", path, path_info);
adamc@856 88 path = new_path;
adamc@856 89 }
adamc@856 90
adamc@856 91 if (!query_string)
adamc@856 92 query_string = "";
adamc@856 93
adamc@856 94 while ((res = read(0, body + body_pos, body_len - body_pos)) > 0) {
adamc@856 95 body_pos += res;
adamc@856 96
adamc@856 97 if (body_pos == body_len) {
adamc@856 98 body_len *= 2;
adamc@856 99 body = realloc(body, body_len);
adamc@856 100 }
adamc@856 101 }
adamc@856 102
adamc@856 103 if (res < 0) {
adamc@856 104 log_error(NULL, "Error reading stdin\n");
adamc@856 105 exit(1);
adamc@856 106 }
adamc@856 107
adamc@856 108 uw_set_on_success("");
adamc@856 109 uw_set_headers(ctx, get_header, NULL);
adam@1799 110 uw_set_env(ctx, get_env, NULL);
adamc@1094 111 uw_request_init(&uw_application, NULL, log_error, log_debug);
adamc@856 112
adamc@856 113 body[body_pos] = 0;
adamc@856 114 rr = uw_request(rc, ctx, method, path, query_string, body, body_pos,
adamc@856 115 on_success, on_failure,
adamc@856 116 NULL, log_error, log_debug,
adamc@863 117 -1, NULL, NULL);
adamc@856 118 uw_print(ctx, 1);
adamc@856 119
adamc@856 120 if (rr == SERVED)
adamc@856 121 return 0;
adamc@856 122 else
adamc@856 123 return 1;
adamc@856 124 }
adamc@1121 125
adamc@1121 126 void *uw_init_client_data() {
adamc@1121 127 return NULL;
adamc@1121 128 }
adamc@1121 129
adamc@1121 130 void uw_free_client_data(void *data) {
adamc@1121 131 }
adamc@1121 132
adamc@1121 133 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 134 }
adamc@1121 135
adamc@1121 136 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1936 137 uw_ensure_transaction(ctx);
adamc@1121 138 uw_get_app(ctx)->expunger(ctx, cli);
adam@1672 139 uw_commit(ctx);
adamc@1121 140 }
adamc@1121 141
adamc@1121 142 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 143 }
adam@1320 144
adam@1843 145 int uw_supports_direct_status = 0;