annotate src/c/request.c @ 1149:15767031950c

AC_USE_SYSTEM_EXTENSIONS replaces the need for hacky #defines at starts of C files
author Adam Chlipala <adamc@hcoop.net>
date Sat, 06 Feb 2010 13:46:17 -0500
parents b08b73591d2c
children e6eabf0c5ccc
rev   line source
adamc@853 1 #include <stdio.h>
adamc@853 2 #include <string.h>
adamc@853 3 #include <stdlib.h>
adamc@853 4 #include <sys/types.h>
adamc@853 5 #include <sys/socket.h>
adamc@853 6 #include <netinet/in.h>
adamc@853 7 #include <unistd.h>
adamc@853 8 #include <signal.h>
adamc@853 9
adamc@853 10 #include <pthread.h>
adamc@853 11
adamc@853 12 #include <mhash.h>
adamc@853 13
adamc@853 14 #include "urweb.h"
adamc@853 15
adamc@853 16 #define MAX_RETRIES 5
adamc@853 17
adamc@856 18 static int try_rollback(uw_context ctx, void *logger_data, uw_logger log_error) {
adamc@853 19 int r = uw_rollback(ctx);
adamc@853 20
adamc@853 21 if (r) {
adamc@856 22 log_error(logger_data, "Error running SQL ROLLBACK\n");
adamc@853 23 uw_reset(ctx);
adamc@1115 24 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@853 25 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@853 26 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@1115 27 uw_set_error_message(ctx, "Database error; you are probably out of storage space.");
adamc@853 28 }
adamc@853 29
adamc@853 30 return r;
adamc@853 31 }
adamc@853 32
adamc@1094 33 uw_context uw_request_new_context(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
adamc@853 34 uw_context ctx = uw_init();
adamc@853 35 int retries_left = MAX_RETRIES;
adamc@1094 36 uw_set_app(ctx, app);
adamc@853 37
adamc@853 38 while (1) {
adamc@853 39 failure_kind fk = uw_begin_init(ctx);
adamc@853 40
adamc@853 41 if (fk == SUCCESS) {
adamc@856 42 log_debug(logger_data, "Database connection initialized.\n");
adamc@853 43 break;
adamc@853 44 } else if (fk == BOUNDED_RETRY) {
adamc@853 45 if (retries_left) {
adamc@856 46 log_debug(logger_data, "Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@853 47 --retries_left;
adamc@853 48 } else {
adamc@856 49 log_error(logger_data, "Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@853 50 uw_free(ctx);
adamc@853 51 return NULL;
adamc@853 52 }
adamc@853 53 } else if (fk == UNLIMITED_RETRY)
adamc@856 54 log_debug(logger_data, "Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@853 55 else if (fk == FATAL) {
adamc@856 56 log_error(logger_data, "Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@853 57 uw_free(ctx);
adamc@853 58 return NULL;
adamc@853 59 } else {
adamc@856 60 log_error(logger_data, "Unknown uw_begin_init return code!\n");
adamc@853 61 uw_free(ctx);
adamc@853 62 return NULL;
adamc@853 63 }
adamc@853 64 }
adamc@853 65
adamc@853 66 return ctx;
adamc@853 67 }
adamc@853 68
adamc@1094 69 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
adamc@853 70 uw_context ctx;
adamc@853 71 failure_kind fk;
adamc@853 72
adamc@853 73 uw_global_init();
adamc@1094 74 uw_app_init(app);
adamc@853 75
adamc@1094 76 ctx = uw_request_new_context(app, logger_data, log_error, log_debug);
adamc@853 77
adamc@853 78 if (!ctx)
adamc@853 79 exit(1);
adamc@853 80
adamc@853 81 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@856 82 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx));
adamc@1094 83 uw_rollback(ctx);
adamc@853 84 uw_reset(ctx);
adamc@853 85 }
adamc@853 86
adamc@853 87 if (fk != SUCCESS) {
adamc@856 88 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx));
adamc@1094 89 uw_rollback(ctx);
adamc@853 90 exit(1);
adamc@853 91 }
adamc@853 92
adamc@853 93 uw_free(ctx);
adamc@853 94 }
adamc@853 95
adamc@853 96
adamc@853 97 typedef struct uw_rc {
adamc@853 98 size_t path_copy_size;
adamc@853 99 char *path_copy;
adamc@853 100 } *uw_request_context;
adamc@853 101
adamc@853 102 uw_request_context uw_new_request_context(void) {
adamc@853 103 uw_request_context r = malloc(sizeof(struct uw_rc));
adamc@853 104 r->path_copy_size = 0;
adamc@853 105 r->path_copy = malloc(0);
adamc@853 106 return r;
adamc@853 107 }
adamc@853 108
adamc@853 109 void uw_free_request_context(uw_request_context r) {
adamc@853 110 free(r->path_copy);
adamc@853 111 free(r);
adamc@853 112 }
adamc@853 113
adamc@854 114 request_result uw_request(uw_request_context rc, uw_context ctx,
adamc@854 115 char *method, char *path, char *query_string,
adamc@854 116 char *body, size_t body_len,
adamc@856 117 void (*on_success)(uw_context), void (*on_failure)(uw_context),
adamc@856 118 void *logger_data, uw_logger log_error, uw_logger log_debug,
adamc@863 119 int sock,
adamc@863 120 int (*send)(int sockfd, const void *buf, size_t len),
adamc@863 121 int (*close)(int fd)) {
adamc@853 122 int retries_left = MAX_RETRIES;
adamc@853 123 failure_kind fk;
adamc@1134 124 int is_post = 0;
adamc@853 125 char *boundary = NULL;
adamc@1134 126 size_t boundary_len = 0;
adamc@854 127 char *inputs;
adamc@1094 128 const char *prefix = uw_get_url_prefix(ctx);
adamc@853 129
adamc@1066 130 uw_set_currentUrl(ctx, path);
adamc@1066 131
adamc@854 132 if (!strcmp(method, "POST")) {
adamc@853 133 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
adamc@853 134 if (!clen_s) {
adamc@1037 135 clen_s = "0";
adamc@1037 136 /*log_error(logger_data, "No Content-length with POST\n");
adamc@1037 137 return FAILED;*/
adamc@853 138 }
adamc@853 139 int clen = atoi(clen_s);
adamc@853 140 if (clen < 0) {
adamc@856 141 log_error(logger_data, "Negative Content-length with POST\n");
adamc@853 142 return FAILED;
adamc@853 143 }
adamc@853 144
adamc@854 145 if (body_len < clen) {
adamc@856 146 log_error(logger_data, "Request doesn't contain all POST data (according to Content-Length)\n");
adamc@853 147 return FAILED;
adamc@853 148 }
adamc@853 149
adamc@853 150 is_post = 1;
adamc@853 151
adamc@853 152 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
adamc@853 153 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
adamc@853 154 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
adamc@856 155 log_error(logger_data, "Bad multipart boundary spec");
adamc@853 156 return FAILED;
adamc@853 157 }
adamc@853 158
adamc@853 159 boundary = clen_s + 28;
adamc@853 160 boundary[0] = '-';
adamc@853 161 boundary[1] = '-';
adamc@853 162 boundary_len = strlen(boundary);
adamc@853 163 }
adamc@854 164 } else if (strcmp(method, "GET")) {
adamc@856 165 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method);
adamc@853 166 return FAILED;
adamc@853 167 }
adamc@853 168
adamc@1094 169 if (!strncmp(path, prefix, strlen(prefix))
adamc@1094 170 && !strcmp(path + strlen(prefix), ".msgs")) {
adamc@853 171 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@853 172 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@853 173
adamc@853 174 if (sock < 0) {
adamc@856 175 log_error(logger_data, ".msgs requested, but not socket supplied\n");
adamc@853 176 return FAILED;
adamc@853 177 }
adamc@853 178
adamc@853 179 if (id && pass) {
adamc@853 180 unsigned idn = atoi(id);
adamc@864 181 uw_client_connect(idn, atoi(pass), sock, send, close, logger_data, log_error);
adamc@856 182 log_error(logger_data, "Processed request for messages by client %u\n\n", idn);
adamc@853 183 return KEEP_OPEN;
adamc@853 184 }
adamc@853 185 else {
adamc@856 186 log_error(logger_data, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
adamc@853 187 return FAILED;
adamc@853 188 }
adamc@853 189 }
adamc@853 190
adamc@853 191 if (boundary) {
adamc@854 192 char *part = body, *after_sub_headers, *header, *after_header;
adamc@853 193 size_t part_len;
adamc@853 194
adamc@853 195 part = strstr(part, boundary);
adamc@853 196 if (!part) {
adamc@856 197 log_error(logger_data, "Missing first multipart boundary\n");
adamc@853 198 return FAILED;
adamc@853 199 }
adamc@853 200 part += boundary_len;
adamc@853 201
adamc@853 202 while (1) {
adamc@853 203 char *name = NULL, *filename = NULL, *type = NULL;
adamc@853 204
adamc@853 205 if (part[0] == '-' && part[1] == '-')
adamc@853 206 break;
adamc@853 207
adamc@853 208 if (*part != '\r') {
adamc@856 209 log_error(logger_data, "No \\r after multipart boundary\n");
adamc@853 210 return FAILED;
adamc@853 211 }
adamc@853 212 ++part;
adamc@853 213 if (*part != '\n') {
adamc@856 214 log_error(logger_data, "No \\n after multipart boundary\n");
adamc@853 215 return FAILED;
adamc@853 216 }
adamc@853 217 ++part;
adamc@853 218
adamc@853 219 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
adamc@856 220 log_error(logger_data, "Missing end of headers after multipart boundary\n");
adamc@853 221 return FAILED;
adamc@853 222 }
adamc@853 223 after_sub_headers[2] = 0;
adamc@853 224 after_sub_headers += 4;
adamc@853 225
adamc@1134 226 for (header = part; (after_header = strstr(header, "\r\n")); header = after_header + 2) {
adamc@853 227 char *colon, *after_colon;
adamc@853 228
adamc@853 229 *after_header = 0;
adamc@853 230 if (!(colon = strchr(header, ':'))) {
adamc@856 231 log_error(logger_data, "Missing colon in multipart sub-header\n");
adamc@853 232 return FAILED;
adamc@853 233 }
adamc@853 234 *colon++ = 0;
adamc@853 235 if (*colon++ != ' ') {
adamc@856 236 log_error(logger_data, "No space after colon in multipart sub-header\n");
adamc@853 237 return FAILED;
adamc@853 238 }
adamc@853 239
adamc@853 240 if (!strcasecmp(header, "Content-Disposition")) {
adamc@853 241 if (strncmp(colon, "form-data; ", 11)) {
adamc@856 242 log_error(logger_data, "Multipart data is not \"form-data\"\n");
adamc@853 243 return FAILED;
adamc@853 244 }
adamc@853 245
adamc@1134 246 for (colon += 11; (after_colon = strchr(colon, '=')); colon = after_colon) {
adamc@853 247 char *data;
adamc@853 248 after_colon[0] = 0;
adamc@853 249 if (after_colon[1] != '"') {
adamc@856 250 log_error(logger_data, "Disposition setting is missing initial quote\n");
adamc@853 251 return FAILED;
adamc@853 252 }
adamc@853 253 data = after_colon+2;
adamc@853 254 if (!(after_colon = strchr(data, '"'))) {
adamc@856 255 log_error(logger_data, "Disposition setting is missing final quote\n");
adamc@853 256 return FAILED;
adamc@853 257 }
adamc@853 258 after_colon[0] = 0;
adamc@853 259 ++after_colon;
adamc@853 260 if (after_colon[0] == ';' && after_colon[1] == ' ')
adamc@853 261 after_colon += 2;
adamc@853 262
adamc@853 263 if (!strcasecmp(colon, "name"))
adamc@853 264 name = data;
adamc@853 265 else if (!strcasecmp(colon, "filename"))
adamc@853 266 filename = data;
adamc@853 267 }
adamc@853 268 } else if (!strcasecmp(header, "Content-Type")) {
adamc@853 269 type = colon;
adamc@853 270 }
adamc@853 271 }
adamc@853 272
adamc@854 273 part = memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
adamc@853 274 if (!part) {
adamc@856 275 log_error(logger_data, "Missing boundary after multipart payload\n");
adamc@853 276 return FAILED;
adamc@853 277 }
adamc@853 278 part[-2] = 0;
adamc@853 279 part_len = part - after_sub_headers - 2;
adamc@853 280 part[0] = 0;
adamc@853 281 part += boundary_len;
adamc@853 282
adamc@853 283 if (filename) {
adamc@853 284 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
adamc@853 285
adamc@853 286 if (uw_set_file_input(ctx, name, f)) {
adamc@856 287 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 288 return FAILED;
adamc@853 289 }
adamc@853 290 } else if (uw_set_input(ctx, name, after_sub_headers)) {
adamc@856 291 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 292 return FAILED;
adamc@853 293 }
adamc@853 294 }
adamc@853 295 }
adamc@853 296 else {
adamc@854 297 inputs = is_post ? body : query_string;
adamc@853 298
adamc@853 299 if (inputs) {
adamc@853 300 char *name, *value;
adamc@853 301
adamc@853 302 while (*inputs) {
adamc@853 303 name = inputs;
adamc@1134 304 if ((inputs = strchr(inputs, '&')))
adamc@853 305 *inputs++ = 0;
adamc@853 306 else
adamc@853 307 inputs = strchr(name, 0);
adamc@853 308
adamc@1134 309 if ((value = strchr(name, '='))) {
adamc@853 310 *value++ = 0;
adamc@853 311 if (uw_set_input(ctx, name, value)) {
adamc@856 312 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 313 return FAILED;
adamc@853 314 }
adamc@853 315 }
adamc@853 316 else if (uw_set_input(ctx, name, "")) {
adamc@856 317 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 318 return FAILED;
adamc@853 319 }
adamc@853 320 }
adamc@853 321 }
adamc@853 322 }
adamc@853 323
adamc@856 324 log_debug(logger_data, "Serving URI %s....\n", path);
adamc@853 325
adamc@853 326 while (1) {
adamc@853 327 size_t path_len = strlen(path);
adamc@853 328
adamc@856 329 on_success(ctx);
adamc@853 330
adamc@853 331 if (path_len + 1 > rc->path_copy_size) {
adamc@853 332 rc->path_copy_size = path_len + 1;
adamc@853 333 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
adamc@853 334 }
adamc@853 335 strcpy(rc->path_copy, path);
adamc@853 336 fk = uw_begin(ctx, rc->path_copy);
adamc@1065 337 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
adamc@853 338 uw_commit(ctx);
adamc@1131 339 if (uw_has_error(ctx)) {
adamc@1131 340 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
adamc@1131 341
adamc@1131 342 uw_reset_keep_error_message(ctx);
adamc@1131 343 on_failure(ctx);
adamc@1131 344 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@1131 345 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@1131 346 uw_write(ctx, "Fatal error: ");
adamc@1131 347 uw_write(ctx, uw_error_message(ctx));
adamc@1131 348 uw_write(ctx, "\n</body></html>");
adamc@1131 349
adamc@1131 350 return FAILED;
adamc@1131 351 } else
adamc@1131 352 return SERVED;
adamc@853 353 } else if (fk == BOUNDED_RETRY) {
adamc@853 354 if (retries_left) {
adamc@856 355 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@853 356 --retries_left;
adamc@853 357 }
adamc@853 358 else {
adamc@856 359 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@853 360
adamc@856 361 try_rollback(ctx, logger_data, log_error);
adamc@853 362
adamc@853 363 uw_reset_keep_error_message(ctx);
adamc@856 364 on_failure(ctx);
adamc@853 365 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@853 366 uw_write(ctx, "Fatal error (out of retries): ");
adamc@853 367 uw_write(ctx, uw_error_message(ctx));
adamc@853 368 uw_write(ctx, "\n");
adamc@853 369
adamc@853 370 return FAILED;
adamc@853 371 }
adamc@853 372 } else if (fk == UNLIMITED_RETRY)
adamc@856 373 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@853 374 else if (fk == FATAL) {
adamc@856 375 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
adamc@853 376
adamc@856 377 try_rollback(ctx, logger_data, log_error);
adamc@853 378
adamc@853 379 uw_reset_keep_error_message(ctx);
adamc@856 380 on_failure(ctx);
adamc@853 381 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@853 382 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@853 383 uw_write(ctx, "Fatal error: ");
adamc@853 384 uw_write(ctx, uw_error_message(ctx));
adamc@853 385 uw_write(ctx, "\n</body></html>");
adamc@853 386
adamc@853 387 return FAILED;
adamc@853 388 } else {
adamc@856 389 log_error(logger_data, "Unknown uw_handle return code!\n");
adamc@853 390
adamc@856 391 try_rollback(ctx, logger_data, log_error);
adamc@853 392
adamc@853 393 uw_reset_keep_request(ctx);
adamc@856 394 on_failure(ctx);
adamc@853 395 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@853 396 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@853 397
adamc@853 398 return FAILED;
adamc@853 399 }
adamc@853 400
adamc@856 401 if (try_rollback(ctx, logger_data, log_error))
adamc@853 402 return FAILED;
adamc@853 403
adamc@853 404 uw_reset_keep_request(ctx);
adamc@853 405 }
adamc@853 406 }
adamc@853 407
adamc@856 408 typedef struct {
adamc@1094 409 uw_app *app;
adamc@856 410 void *logger_data;
adamc@856 411 uw_logger log_error, log_debug;
adamc@856 412 } loggers;
adamc@856 413
adamc@853 414 void *client_pruner(void *data) {
adamc@856 415 loggers *ls = (loggers *)data;
adamc@1094 416 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug);
adamc@853 417
adamc@853 418 if (!ctx)
adamc@853 419 exit(1);
adamc@853 420
adamc@853 421 while (1) {
adamc@853 422 uw_prune_clients(ctx);
adamc@853 423 sleep(5);
adamc@853 424 }
adamc@853 425 }