annotate src/c/request.c @ 1431:4a6f84092399

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