annotate src/c/request.c @ 1515:8c65218920cf

Stop using -fno-inline by default
author Adam Chlipala <adam@chlipala.net>
date Sat, 23 Jul 2011 09:42:34 -0400
parents 36f7d1debb37
children 4d0b80dd4c37
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 "urweb.h"
adamc@853 15
adamc@853 16 #define MAX_RETRIES 5
adamc@853 17
adamc@1152 18 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2);
adamc@1152 19
adam@1327 20 static int try_rollback(uw_context ctx, int will_retry, void *logger_data, uw_logger log_error) {
adam@1327 21 int r = uw_rollback(ctx, will_retry);
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@1115 26 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
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@1115 29 uw_set_error_message(ctx, "Database error; you are probably out of storage space.");
adamc@853 30 }
adamc@853 31
adamc@853 32 return r;
adamc@853 33 }
adamc@853 34
adam@1446 35 uw_context uw_request_new_context(int id, uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
adam@1446 36 uw_context ctx = uw_init(id, logger_data, log_debug);
adamc@853 37 int retries_left = MAX_RETRIES;
adamc@1094 38 uw_set_app(ctx, app);
adamc@853 39
adamc@853 40 while (1) {
adamc@853 41 failure_kind fk = uw_begin_init(ctx);
adamc@853 42
adamc@853 43 if (fk == SUCCESS) {
adamc@856 44 log_debug(logger_data, "Database connection initialized.\n");
adamc@853 45 break;
adamc@853 46 } else if (fk == BOUNDED_RETRY) {
adamc@853 47 if (retries_left) {
adamc@856 48 log_debug(logger_data, "Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@853 49 --retries_left;
adamc@853 50 } else {
adamc@856 51 log_error(logger_data, "Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@853 52 uw_free(ctx);
adamc@853 53 return NULL;
adamc@853 54 }
adamc@853 55 } else if (fk == UNLIMITED_RETRY)
adamc@856 56 log_debug(logger_data, "Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@853 57 else if (fk == FATAL) {
adamc@856 58 log_error(logger_data, "Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@853 59 uw_free(ctx);
adamc@853 60 return NULL;
adamc@853 61 } else {
adamc@856 62 log_error(logger_data, "Unknown uw_begin_init return code!\n");
adamc@853 63 uw_free(ctx);
adamc@853 64 return NULL;
adamc@853 65 }
adamc@853 66 }
adamc@853 67
adamc@853 68 return ctx;
adamc@853 69 }
adamc@853 70
adam@1308 71 static void *ticker(void *data) {
adam@1308 72 while (1) {
adam@1308 73 usleep(100000);
adam@1308 74 ++uw_time;
adam@1308 75 }
adam@1308 76
adam@1308 77 return NULL;
adam@1308 78 }
adam@1308 79
adam@1349 80 typedef struct {
adam@1349 81 uw_app *app;
adam@1349 82 void *logger_data;
adam@1349 83 uw_logger log_error, log_debug;
adam@1349 84 } loggers;
adam@1349 85
adam@1349 86 typedef struct {
adam@1446 87 int id;
adam@1349 88 loggers *ls;
adam@1349 89 uw_periodic pdic;
adam@1349 90 } periodic;
adam@1349 91
adam@1349 92 static void *periodic_loop(void *data) {
adam@1349 93 periodic *p = (periodic *)data;
adam@1446 94 uw_context ctx = uw_request_new_context(p->id, p->ls->app, p->ls->logger_data, p->ls->log_error, p->ls->log_debug);
adam@1349 95
adam@1349 96 if (!ctx)
adam@1349 97 exit(1);
adam@1349 98
adam@1349 99 while (1) {
adam@1418 100 int retries_left = MAX_RETRIES;
adam@1418 101
adam@1349 102 failure_kind r;
adam@1349 103 do {
adam@1418 104 uw_reset(ctx);
adam@1349 105 r = uw_runCallback(ctx, p->pdic.callback);
adam@1418 106 if (r == BOUNDED_RETRY)
adam@1418 107 --retries_left;
adam@1418 108 else if (r == UNLIMITED_RETRY)
adam@1418 109 p->ls->log_debug(p->ls->logger_data, "Error triggers unlimited retry in periodic: %s\n", uw_error_message(ctx));
adam@1418 110 else if (r == BOUNDED_RETRY)
adam@1418 111 p->ls->log_debug(p->ls->logger_data, "Error triggers bounded retry in periodic: %s\n", uw_error_message(ctx));
adam@1418 112 else if (r == FATAL)
adam@1418 113 p->ls->log_error(p->ls->logger_data, "Fatal error: %s\n", uw_error_message(ctx));
adam@1418 114 if (r == FATAL || r == BOUNDED_RETRY || r == UNLIMITED_RETRY)
adam@1437 115 if (try_rollback(ctx, 0, p->ls->logger_data, p->ls->log_error))
adam@1437 116 return NULL;
adam@1418 117 } while (r == UNLIMITED_RETRY || (r == BOUNDED_RETRY && retries_left > 0));
adam@1418 118
adam@1418 119 if (r != FATAL && r != BOUNDED_RETRY)
adam@1418 120 uw_commit(ctx);
adam@1349 121
adam@1349 122 sleep(p->pdic.period);
adam@1349 123 };
adam@1349 124 }
adam@1349 125
adamc@1094 126 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
adamc@853 127 uw_context ctx;
adamc@853 128 failure_kind fk;
adam@1349 129 uw_periodic *ps;
adam@1349 130 loggers *ls = malloc(sizeof(loggers));
adam@1446 131 int id;
adam@1349 132
adam@1349 133 ls->app = app;
adam@1349 134 ls->logger_data = logger_data;
adam@1349 135 ls->log_error = log_error;
adam@1349 136 ls->log_debug = log_debug;
adamc@853 137
adamc@853 138 uw_global_init();
adamc@1094 139 uw_app_init(app);
adamc@853 140
adam@1308 141 {
adam@1308 142 pthread_t thread;
adam@1308 143
adam@1308 144 if (uw_time_max && pthread_create(&thread, NULL, ticker, NULL)) {
adam@1308 145 fprintf(stderr, "Error creating ticker thread\n");
adam@1308 146 exit(1);
adam@1308 147 }
adam@1308 148 }
adam@1308 149
adam@1446 150 ctx = uw_request_new_context(0, app, logger_data, log_error, log_debug);
adamc@853 151
adamc@853 152 if (!ctx)
adamc@853 153 exit(1);
adamc@853 154
adamc@853 155 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@856 156 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx));
adam@1327 157 uw_rollback(ctx, 1);
adamc@853 158 uw_reset(ctx);
adamc@853 159 }
adamc@853 160
adamc@853 161 if (fk != SUCCESS) {
adamc@856 162 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx));
adam@1327 163 uw_rollback(ctx, 0);
adamc@853 164 exit(1);
adamc@853 165 }
adamc@853 166
adamc@853 167 uw_free(ctx);
adam@1349 168
adam@1446 169 id = 1;
adam@1349 170 for (ps = app->periodics; ps->callback; ++ps) {
adam@1349 171 pthread_t thread;
adam@1349 172 periodic *arg = malloc(sizeof(periodic));
adam@1446 173 arg->id = id++;
adam@1349 174 arg->ls = ls;
adam@1349 175 arg->pdic = *ps;
adam@1349 176
adam@1349 177 if (pthread_create(&thread, NULL, periodic_loop, arg)) {
adam@1349 178 fprintf(stderr, "Error creating periodic thread\n");
adam@1349 179 exit(1);
adam@1349 180 }
adam@1349 181 }
adamc@853 182 }
adamc@853 183
adamc@853 184
adamc@853 185 typedef struct uw_rc {
adam@1370 186 size_t path_copy_size, queryString_size;
adam@1370 187 char *path_copy, *queryString;
adamc@853 188 } *uw_request_context;
adamc@853 189
adamc@853 190 uw_request_context uw_new_request_context(void) {
adamc@853 191 uw_request_context r = malloc(sizeof(struct uw_rc));
adam@1371 192 r->path_copy_size = 0;
adam@1371 193 r->queryString_size = 1;
adamc@853 194 r->path_copy = malloc(0);
adam@1371 195 r->queryString = malloc(1);
adamc@853 196 return r;
adamc@853 197 }
adamc@853 198
adamc@853 199 void uw_free_request_context(uw_request_context r) {
adamc@853 200 free(r->path_copy);
adam@1370 201 free(r->queryString);
adamc@853 202 free(r);
adamc@853 203 }
adamc@853 204
adamc@854 205 request_result uw_request(uw_request_context rc, uw_context ctx,
adamc@854 206 char *method, char *path, char *query_string,
adamc@854 207 char *body, size_t body_len,
adamc@856 208 void (*on_success)(uw_context), void (*on_failure)(uw_context),
adamc@856 209 void *logger_data, uw_logger log_error, uw_logger log_debug,
adamc@863 210 int sock,
adamc@863 211 int (*send)(int sockfd, const void *buf, size_t len),
adamc@863 212 int (*close)(int fd)) {
adamc@853 213 int retries_left = MAX_RETRIES;
adamc@853 214 failure_kind fk;
adamc@1134 215 int is_post = 0;
adamc@853 216 char *boundary = NULL;
adamc@1134 217 size_t boundary_len = 0;
adamc@854 218 char *inputs;
adamc@1094 219 const char *prefix = uw_get_url_prefix(ctx);
adamc@1169 220 char *s;
adam@1294 221 int had_error = 0;
adam@1294 222 char errmsg[ERROR_BUF_LEN];
adamc@1169 223
adam@1417 224 uw_reset(ctx);
adam@1446 225
adam@1371 226 rc->queryString[0] = 0;
adam@1371 227
adamc@1169 228 for (s = path; *s; ++s) {
adamc@1169 229 if (s[0] == '%' && s[1] == '2' && s[2] == '7') {
adamc@1169 230 s[0] = '\'';
adamc@1169 231 memmove(s+1, s+3, strlen(s+3)+1);
adamc@1169 232 }
adamc@1169 233 }
adamc@853 234
adamc@1066 235 uw_set_currentUrl(ctx, path);
adamc@1066 236
adamc@854 237 if (!strcmp(method, "POST")) {
adamc@853 238 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
adamc@853 239 if (!clen_s) {
adamc@1037 240 clen_s = "0";
adamc@1037 241 /*log_error(logger_data, "No Content-length with POST\n");
adamc@1037 242 return FAILED;*/
adamc@853 243 }
adamc@853 244 int clen = atoi(clen_s);
adamc@853 245 if (clen < 0) {
adamc@856 246 log_error(logger_data, "Negative Content-length with POST\n");
adamc@853 247 return FAILED;
adamc@853 248 }
adamc@853 249
adamc@854 250 if (body_len < clen) {
adamc@856 251 log_error(logger_data, "Request doesn't contain all POST data (according to Content-Length)\n");
adamc@853 252 return FAILED;
adamc@853 253 }
adamc@853 254
adamc@853 255 is_post = 1;
adam@1386 256 uw_isPost(ctx);
adamc@853 257
adamc@853 258 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
adamc@853 259 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
adamc@853 260 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
adamc@856 261 log_error(logger_data, "Bad multipart boundary spec");
adamc@853 262 return FAILED;
adamc@853 263 }
adamc@853 264
adamc@853 265 boundary = clen_s + 28;
adamc@853 266 boundary[0] = '-';
adamc@853 267 boundary[1] = '-';
adamc@853 268 boundary_len = strlen(boundary);
adam@1347 269 } else if (clen_s && strcasecmp(clen_s, "application/x-www-form-urlencoded")) {
adam@1347 270 uw_Basis_postBody pb = {clen_s, body};
adam@1347 271 uw_postBody(ctx, pb);
adamc@853 272 }
adamc@854 273 } else if (strcmp(method, "GET")) {
adamc@856 274 log_error(logger_data, "Not ready for non-GET/POST command: %s\n", method);
adamc@853 275 return FAILED;
adamc@853 276 }
adamc@853 277
adamc@1094 278 if (!strncmp(path, prefix, strlen(prefix))
adamc@1094 279 && !strcmp(path + strlen(prefix), ".msgs")) {
adamc@853 280 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@853 281 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@853 282
adamc@853 283 if (sock < 0) {
adamc@856 284 log_error(logger_data, ".msgs requested, but not socket supplied\n");
adamc@853 285 return FAILED;
adamc@853 286 }
adamc@853 287
adamc@853 288 if (id && pass) {
adamc@853 289 unsigned idn = atoi(id);
adamc@864 290 uw_client_connect(idn, atoi(pass), sock, send, close, logger_data, log_error);
adam@1320 291 log_debug(logger_data, "Processed request for messages by client %u\n\n", idn);
adamc@853 292 return KEEP_OPEN;
adamc@853 293 }
adamc@853 294 else {
adamc@856 295 log_error(logger_data, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
adamc@853 296 return FAILED;
adamc@853 297 }
adamc@853 298 }
adamc@853 299
adamc@853 300 if (boundary) {
adamc@854 301 char *part = body, *after_sub_headers, *header, *after_header;
adamc@853 302 size_t part_len;
adamc@853 303
adamc@853 304 part = strstr(part, boundary);
adamc@853 305 if (!part) {
adamc@856 306 log_error(logger_data, "Missing first multipart boundary\n");
adamc@853 307 return FAILED;
adamc@853 308 }
adamc@853 309 part += boundary_len;
adamc@853 310
adamc@853 311 while (1) {
adamc@853 312 char *name = NULL, *filename = NULL, *type = NULL;
adamc@853 313
adamc@853 314 if (part[0] == '-' && part[1] == '-')
adamc@853 315 break;
adamc@853 316
adamc@853 317 if (*part != '\r') {
adamc@856 318 log_error(logger_data, "No \\r after multipart boundary\n");
adamc@853 319 return FAILED;
adamc@853 320 }
adamc@853 321 ++part;
adamc@853 322 if (*part != '\n') {
adamc@856 323 log_error(logger_data, "No \\n after multipart boundary\n");
adamc@853 324 return FAILED;
adamc@853 325 }
adamc@853 326 ++part;
adamc@853 327
adamc@853 328 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
adamc@856 329 log_error(logger_data, "Missing end of headers after multipart boundary\n");
adamc@853 330 return FAILED;
adamc@853 331 }
adamc@853 332 after_sub_headers[2] = 0;
adamc@853 333 after_sub_headers += 4;
adamc@853 334
adamc@1134 335 for (header = part; (after_header = strstr(header, "\r\n")); header = after_header + 2) {
adamc@853 336 char *colon, *after_colon;
adamc@853 337
adamc@853 338 *after_header = 0;
adamc@853 339 if (!(colon = strchr(header, ':'))) {
adamc@856 340 log_error(logger_data, "Missing colon in multipart sub-header\n");
adamc@853 341 return FAILED;
adamc@853 342 }
adamc@853 343 *colon++ = 0;
adamc@853 344 if (*colon++ != ' ') {
adamc@856 345 log_error(logger_data, "No space after colon in multipart sub-header\n");
adamc@853 346 return FAILED;
adamc@853 347 }
adamc@853 348
adamc@853 349 if (!strcasecmp(header, "Content-Disposition")) {
adamc@853 350 if (strncmp(colon, "form-data; ", 11)) {
adamc@856 351 log_error(logger_data, "Multipart data is not \"form-data\"\n");
adamc@853 352 return FAILED;
adamc@853 353 }
adamc@853 354
adamc@1134 355 for (colon += 11; (after_colon = strchr(colon, '=')); colon = after_colon) {
adamc@853 356 char *data;
adamc@853 357 after_colon[0] = 0;
adamc@853 358 if (after_colon[1] != '"') {
adamc@856 359 log_error(logger_data, "Disposition setting is missing initial quote\n");
adamc@853 360 return FAILED;
adamc@853 361 }
adamc@853 362 data = after_colon+2;
adamc@853 363 if (!(after_colon = strchr(data, '"'))) {
adamc@856 364 log_error(logger_data, "Disposition setting is missing final quote\n");
adamc@853 365 return FAILED;
adamc@853 366 }
adamc@853 367 after_colon[0] = 0;
adamc@853 368 ++after_colon;
adamc@853 369 if (after_colon[0] == ';' && after_colon[1] == ' ')
adamc@853 370 after_colon += 2;
adamc@853 371
adamc@853 372 if (!strcasecmp(colon, "name"))
adamc@853 373 name = data;
adamc@853 374 else if (!strcasecmp(colon, "filename"))
adamc@853 375 filename = data;
adamc@853 376 }
adamc@853 377 } else if (!strcasecmp(header, "Content-Type")) {
adamc@853 378 type = colon;
adamc@853 379 }
adamc@853 380 }
adamc@853 381
adamc@854 382 part = memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
adamc@853 383 if (!part) {
adamc@856 384 log_error(logger_data, "Missing boundary after multipart payload\n");
adamc@853 385 return FAILED;
adamc@853 386 }
adamc@853 387 part[-2] = 0;
adamc@853 388 part_len = part - after_sub_headers - 2;
adamc@853 389 part[0] = 0;
adamc@853 390 part += boundary_len;
adamc@853 391
adamc@853 392 if (filename) {
adamc@853 393 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
adamc@853 394
adamc@853 395 if (uw_set_file_input(ctx, name, f)) {
adamc@856 396 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 397 return FAILED;
adamc@853 398 }
adamc@853 399 } else if (uw_set_input(ctx, name, after_sub_headers)) {
adamc@856 400 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 401 return FAILED;
adamc@853 402 }
adamc@853 403 }
adamc@853 404 }
adam@1347 405 else if (!uw_hasPostBody(ctx)) {
adamc@854 406 inputs = is_post ? body : query_string;
adamc@853 407
adamc@853 408 if (inputs) {
adamc@853 409 char *name, *value;
adam@1370 410 int len = strlen(inputs);
adam@1370 411
adam@1370 412 if (len+1 > rc->queryString_size) {
adam@1370 413 rc->queryString_size = len+1;
adam@1370 414 rc->queryString = realloc(rc->queryString, len+1);
adam@1370 415 }
adam@1370 416 strcpy(rc->queryString, inputs);
adamc@853 417
adamc@853 418 while (*inputs) {
adamc@853 419 name = inputs;
adamc@1134 420 if ((inputs = strchr(inputs, '&')))
adamc@853 421 *inputs++ = 0;
adamc@853 422 else
adamc@853 423 inputs = strchr(name, 0);
adamc@853 424
adamc@1134 425 if ((value = strchr(name, '='))) {
adamc@853 426 *value++ = 0;
adamc@853 427 if (uw_set_input(ctx, name, value)) {
adamc@856 428 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 429 return FAILED;
adamc@853 430 }
adamc@853 431 }
adamc@853 432 else if (uw_set_input(ctx, name, "")) {
adamc@856 433 log_error(logger_data, "%s\n", uw_error_message(ctx));
adamc@853 434 return FAILED;
adamc@853 435 }
adamc@853 436 }
adamc@853 437 }
adamc@853 438 }
adamc@853 439
adamc@853 440 while (1) {
adam@1371 441 uw_setQueryString(ctx, rc->queryString);
adam@1371 442
adam@1294 443 if (!had_error) {
adam@1294 444 size_t path_len = strlen(path);
adamc@853 445
adam@1294 446 on_success(ctx);
adamc@853 447
adam@1294 448 if (path_len + 1 > rc->path_copy_size) {
adam@1294 449 rc->path_copy_size = path_len + 1;
adam@1294 450 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
adam@1294 451 }
adam@1294 452 strcpy(rc->path_copy, path);
adam@1308 453
adam@1308 454 uw_set_deadline(ctx, uw_time + uw_time_max);
adam@1294 455 fk = uw_begin(ctx, rc->path_copy);
adam@1308 456 } else {
adam@1308 457 uw_set_deadline(ctx, uw_time + uw_time_max);
adam@1294 458 fk = uw_begin_onError(ctx, errmsg);
adam@1308 459 }
adam@1294 460
adamc@1065 461 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
adamc@853 462 uw_commit(ctx);
adam@1294 463 if (uw_has_error(ctx) && !had_error) {
adamc@1131 464 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
adamc@1131 465 uw_reset_keep_error_message(ctx);
adamc@1131 466 on_failure(ctx);
adam@1294 467
adam@1294 468 if (uw_get_app(ctx)->on_error) {
adam@1294 469 had_error = 1;
adam@1294 470 strcpy(errmsg, uw_error_message(ctx));
adam@1294 471 } else {
adam@1294 472 uw_write_header(ctx, "Content-type: text/html\r\n");
adam@1294 473 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adam@1294 474 uw_write(ctx, "Fatal error: ");
adam@1294 475 uw_write(ctx, uw_error_message(ctx));
adam@1294 476 uw_write(ctx, "\n</body></html>");
adamc@1131 477
adam@1294 478 return FAILED;
adam@1294 479 }
adamc@1131 480 } else
adam@1294 481 return had_error ? FAILED : SERVED;
adamc@853 482 } else if (fk == BOUNDED_RETRY) {
adamc@853 483 if (retries_left) {
adamc@856 484 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@853 485 --retries_left;
adamc@853 486 }
adamc@853 487 else {
adamc@856 488 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@853 489
adam@1327 490 try_rollback(ctx, 0, logger_data, log_error);
adamc@853 491
adam@1294 492 if (!had_error && uw_get_app(ctx)->on_error) {
adam@1294 493 had_error = 1;
adam@1294 494 strcpy(errmsg, uw_error_message(ctx));
adam@1294 495 } else {
adam@1294 496 uw_reset_keep_error_message(ctx);
adam@1294 497 on_failure(ctx);
adam@1294 498 uw_write_header(ctx, "Content-type: text/plain\r\n");
adam@1294 499 uw_write(ctx, "Fatal error (out of retries): ");
adam@1294 500 uw_write(ctx, uw_error_message(ctx));
adam@1294 501 uw_write(ctx, "\n");
adam@1294 502
adam@1294 503 return FAILED;
adam@1294 504 }
adamc@853 505 }
adamc@853 506 } else if (fk == UNLIMITED_RETRY)
adamc@856 507 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@853 508 else if (fk == FATAL) {
adamc@856 509 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
adamc@853 510
adam@1327 511 try_rollback(ctx, 0, logger_data, log_error);
adamc@853 512
adam@1294 513 if (uw_get_app(ctx)->on_error && !had_error) {
adam@1294 514 had_error = 1;
adam@1294 515 strcpy(errmsg, uw_error_message(ctx));
adam@1294 516 } else {
adam@1294 517 uw_reset_keep_error_message(ctx);
adam@1294 518 on_failure(ctx);
adam@1294 519 uw_write_header(ctx, "Content-type: text/html\r\n");
adam@1294 520 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adam@1294 521 uw_write(ctx, "Fatal error: ");
adam@1294 522 uw_write(ctx, uw_error_message(ctx));
adam@1294 523 uw_write(ctx, "\n</body></html>");
adamc@853 524
adam@1294 525 return FAILED;
adam@1294 526 }
adamc@853 527 } else {
adamc@856 528 log_error(logger_data, "Unknown uw_handle return code!\n");
adamc@853 529
adam@1327 530 try_rollback(ctx, 0, logger_data, log_error);
adamc@853 531
adam@1294 532 if (uw_get_app(ctx)->on_error && !had_error) {
adam@1294 533 had_error = 1;
adam@1294 534 strcpy(errmsg, "Unknown uw_handle return code");
adam@1294 535 } else {
adam@1294 536 uw_reset_keep_request(ctx);
adam@1294 537 on_failure(ctx);
adam@1294 538 uw_write_header(ctx, "Content-type: text/plain\r\n");
adam@1294 539 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@853 540
adam@1294 541 return FAILED;
adam@1294 542 }
adamc@853 543 }
adamc@853 544
adam@1327 545 if (try_rollback(ctx, 1, logger_data, log_error))
adamc@853 546 return FAILED;
adamc@853 547
adamc@853 548 uw_reset_keep_request(ctx);
adamc@853 549 }
adamc@853 550 }
adamc@853 551
adamc@853 552 void *client_pruner(void *data) {
adamc@856 553 loggers *ls = (loggers *)data;
adam@1446 554 uw_context ctx = uw_request_new_context(0, ls->app, ls->logger_data, ls->log_error, ls->log_debug);
adamc@853 555
adamc@853 556 if (!ctx)
adamc@853 557 exit(1);
adamc@853 558
adamc@853 559 while (1) {
adamc@853 560 uw_prune_clients(ctx);
adamc@853 561 sleep(5);
adamc@853 562 }
adamc@853 563 }