annotate src/c/request.c @ 1302:d008c4c43a0a

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