comparison src/c/request.c @ 1294:b4480a56cab7

Server-side 'onError'
author Adam Chlipala <adam@chlipala.net>
date Tue, 07 Sep 2010 08:28:07 -0400
parents 236dc296c32d
children 714e8b84221b
comparison
equal deleted inserted replaced
1293:acabf3935060 1294:b4480a56cab7
129 char *boundary = NULL; 129 char *boundary = NULL;
130 size_t boundary_len = 0; 130 size_t boundary_len = 0;
131 char *inputs; 131 char *inputs;
132 const char *prefix = uw_get_url_prefix(ctx); 132 const char *prefix = uw_get_url_prefix(ctx);
133 char *s; 133 char *s;
134 int had_error = 0;
135 char errmsg[ERROR_BUF_LEN];
134 136
135 for (s = path; *s; ++s) { 137 for (s = path; *s; ++s) {
136 if (s[0] == '%' && s[1] == '2' && s[2] == '7') { 138 if (s[0] == '%' && s[1] == '2' && s[2] == '7') {
137 s[0] = '\''; 139 s[0] = '\'';
138 memmove(s+1, s+3, strlen(s+3)+1); 140 memmove(s+1, s+3, strlen(s+3)+1);
334 } 336 }
335 337
336 log_debug(logger_data, "Serving URI %s....\n", path); 338 log_debug(logger_data, "Serving URI %s....\n", path);
337 339
338 while (1) { 340 while (1) {
339 size_t path_len = strlen(path); 341 if (!had_error) {
340 342 size_t path_len = strlen(path);
341 on_success(ctx); 343
342 344 on_success(ctx);
343 if (path_len + 1 > rc->path_copy_size) { 345
344 rc->path_copy_size = path_len + 1; 346 if (path_len + 1 > rc->path_copy_size) {
345 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size); 347 rc->path_copy_size = path_len + 1;
346 } 348 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
347 strcpy(rc->path_copy, path); 349 }
348 fk = uw_begin(ctx, rc->path_copy); 350 strcpy(rc->path_copy, path);
351 fk = uw_begin(ctx, rc->path_copy);
352 } else
353 fk = uw_begin_onError(ctx, errmsg);
354
349 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) { 355 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
350 uw_commit(ctx); 356 uw_commit(ctx);
351 if (uw_has_error(ctx)) { 357 if (uw_has_error(ctx) && !had_error) {
352 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx)); 358 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
353 359
360 uw_reset_keep_error_message(ctx);
361 on_failure(ctx);
362
363 if (uw_get_app(ctx)->on_error) {
364 had_error = 1;
365 strcpy(errmsg, uw_error_message(ctx));
366 } else {
367 uw_write_header(ctx, "Content-type: text/html\r\n");
368 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
369 uw_write(ctx, "Fatal error: ");
370 uw_write(ctx, uw_error_message(ctx));
371 uw_write(ctx, "\n</body></html>");
372
373 return FAILED;
374 }
375 } else
376 return had_error ? FAILED : SERVED;
377 } else if (fk == BOUNDED_RETRY) {
378 if (retries_left) {
379 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx));
380 --retries_left;
381 }
382 else {
383 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
384
385 try_rollback(ctx, logger_data, log_error);
386
387 if (!had_error && uw_get_app(ctx)->on_error) {
388 had_error = 1;
389 strcpy(errmsg, uw_error_message(ctx));
390 } else {
391 uw_reset_keep_error_message(ctx);
392 on_failure(ctx);
393 uw_write_header(ctx, "Content-type: text/plain\r\n");
394 uw_write(ctx, "Fatal error (out of retries): ");
395 uw_write(ctx, uw_error_message(ctx));
396 uw_write(ctx, "\n");
397
398 return FAILED;
399 }
400 }
401 } else if (fk == UNLIMITED_RETRY)
402 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
403 else if (fk == FATAL) {
404 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
405
406 try_rollback(ctx, logger_data, log_error);
407
408 if (uw_get_app(ctx)->on_error && !had_error) {
409 had_error = 1;
410 strcpy(errmsg, uw_error_message(ctx));
411 } else {
354 uw_reset_keep_error_message(ctx); 412 uw_reset_keep_error_message(ctx);
355 on_failure(ctx); 413 on_failure(ctx);
356 uw_write_header(ctx, "Content-type: text/html\r\n"); 414 uw_write_header(ctx, "Content-type: text/html\r\n");
357 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>"); 415 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
358 uw_write(ctx, "Fatal error: "); 416 uw_write(ctx, "Fatal error: ");
359 uw_write(ctx, uw_error_message(ctx)); 417 uw_write(ctx, uw_error_message(ctx));
360 uw_write(ctx, "\n</body></html>"); 418 uw_write(ctx, "\n</body></html>");
361 419
362 return FAILED; 420 return FAILED;
363 } else 421 }
364 return SERVED; 422 } else {
365 } else if (fk == BOUNDED_RETRY) { 423 log_error(logger_data, "Unknown uw_handle return code!\n");
366 if (retries_left) { 424
367 log_debug(logger_data, "Error triggers bounded retry: %s\n", uw_error_message(ctx)); 425 try_rollback(ctx, logger_data, log_error);
368 --retries_left; 426
369 } 427 if (uw_get_app(ctx)->on_error && !had_error) {
370 else { 428 had_error = 1;
371 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx)); 429 strcpy(errmsg, "Unknown uw_handle return code");
372 430 } else {
373 try_rollback(ctx, logger_data, log_error); 431 uw_reset_keep_request(ctx);
374
375 uw_reset_keep_error_message(ctx);
376 on_failure(ctx); 432 on_failure(ctx);
377 uw_write_header(ctx, "Content-type: text/plain\r\n"); 433 uw_write_header(ctx, "Content-type: text/plain\r\n");
378 uw_write(ctx, "Fatal error (out of retries): "); 434 uw_write(ctx, "Unknown uw_handle return code!\n");
379 uw_write(ctx, uw_error_message(ctx)); 435
380 uw_write(ctx, "\n"); 436 return FAILED;
381 437 }
382 return FAILED;
383 }
384 } else if (fk == UNLIMITED_RETRY)
385 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
386 else if (fk == FATAL) {
387 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
388
389 try_rollback(ctx, logger_data, log_error);
390
391 uw_reset_keep_error_message(ctx);
392 on_failure(ctx);
393 uw_write_header(ctx, "Content-type: text/html\r\n");
394 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
395 uw_write(ctx, "Fatal error: ");
396 uw_write(ctx, uw_error_message(ctx));
397 uw_write(ctx, "\n</body></html>");
398
399 return FAILED;
400 } else {
401 log_error(logger_data, "Unknown uw_handle return code!\n");
402
403 try_rollback(ctx, logger_data, log_error);
404
405 uw_reset_keep_request(ctx);
406 on_failure(ctx);
407 uw_write_header(ctx, "Content-type: text/plain\r\n");
408 uw_write(ctx, "Unknown uw_handle return code!\n");
409
410 return FAILED;
411 } 438 }
412 439
413 if (try_rollback(ctx, logger_data, log_error)) 440 if (try_rollback(ctx, logger_data, log_error))
414 return FAILED; 441 return FAILED;
415 442