annotate src/c/request.c @ 1104:72670131dace

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