317 * TODO: add this to new file |
317 * TODO: add this to new file |
318 */ |
318 */ |
319 |
319 |
320 int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) { |
320 int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) { |
321 int r = REQ_NOACTION; |
321 int r = REQ_NOACTION; |
322 |
|
323 do { |
322 do { |
324 switch(rq->phase) { |
323 switch(rq->phase) { |
325 case NSAPIAuthTrans: { |
324 case NSAPIAuthTrans: { |
326 r = nsapi_authtrans(sn, rq); |
325 r = nsapi_authtrans(sn, rq); |
327 if(r != REQ_PROCEED) { |
326 if(r != REQ_PROCEED) { |
361 //printf(">>> Service\n"); |
360 //printf(">>> Service\n"); |
362 r = nsapi_service(sn, rq); |
361 r = nsapi_service(sn, rq); |
363 if(r != REQ_PROCEED) { |
362 if(r != REQ_PROCEED) { |
364 break; |
363 break; |
365 } |
364 } |
366 rq->phase++; |
365 rq->phase = NSAPIAddLog; // skip NSAPIError |
367 nsapi_context_next_stage(&rq->context); |
366 nsapi_context_next_stage(&rq->context); |
368 } |
367 } |
369 case NSAPIAddLog: { |
368 case NSAPIAddLog: { |
370 //printf(">>> AddLog\n"); |
369 //printf(">>> AddLog\n"); |
371 r = nsapi_addlog(sn, rq); |
370 r = nsapi_addlog(sn, rq); |
372 if(r != REQ_PROCESSING && r != REQ_ABORTED) { |
371 if(r == REQ_PROCESSING) { |
373 break; |
372 break; |
374 } |
373 } |
375 rq->phase++; |
374 // finish request |
376 nsapi_context_next_stage(&rq->context); |
375 r = REQ_PROCEED; |
|
376 break; |
377 } |
377 } |
378 default: // should not happen, but when it does, finish the req |
378 default: // should not happen, but when it does, finish the req |
379 case REQ_FINISH: { |
379 case REQ_FINISH: { |
380 //printf(">>> Finish\n"); |
380 //printf(">>> Finish\n"); |
381 r = nsapi_finish_request(sn, rq); |
381 //r = nsapi_finish_request(sn, rq); |
|
382 r = REQ_PROCEED; |
|
383 break; |
|
384 } |
|
385 case NSAPIError: { |
|
386 //printf(">>> Error\n"); |
|
387 r = nsapi_error(sn, rq); |
|
388 if(r == REQ_PROCEED) { |
|
389 // restart the loop to switch to AddLog directive |
|
390 r = REQ_RESTART; |
|
391 rq->phase = NSAPIAddLog; |
|
392 nsapi_context_next_stage(&rq->context); |
|
393 } else { |
|
394 /* |
|
395 * an error occured while handling an error |
|
396 * leave loop to finish the request (without AddLog) |
|
397 */ |
|
398 r = REQ_PROCEED; |
|
399 break; |
|
400 } |
382 } |
401 } |
383 } |
402 } |
384 |
403 |
385 if(r == REQ_ABORTED) { |
404 if(r == REQ_ABORTED) { |
386 // if an error occurred, we send an error page |
405 // switch to NSAPIError |
387 nsapi_error_request((Session*)sn, (Request*)rq); |
406 rq->phase = NSAPIError; |
388 break; |
407 nsapi_context_next_stage(&rq->context); |
|
408 r = REQ_RESTART; |
389 } |
409 } |
390 |
410 |
391 } while (r == REQ_RESTART); |
411 } while (r == REQ_RESTART); |
392 |
412 |
393 if(r != REQ_PROCESSING) { |
413 if(r != REQ_PROCESSING) { |
704 } |
724 } |
705 |
725 |
706 return ret; |
726 return ret; |
707 } |
727 } |
708 |
728 |
|
729 int nsapi_error(NSAPISession *sn, NSAPIRequest *rq) { |
|
730 //printf("nsapi_error\n"); |
|
731 httpd_objset *objset = rq->rq.os; |
|
732 |
|
733 if(NCX_OI(rq) == -1) { |
|
734 NCX_OI(rq) = objset->pos - 1; |
|
735 } |
|
736 |
|
737 int ret = rq->context.last_req_code; |
|
738 for(int i=NCX_OI(rq);i>=0;i--) { |
|
739 httpd_object *obj = objset->obj[i]; |
|
740 dtable *dt = object_get_dtable(obj, NSAPIError); |
|
741 |
|
742 // execute directives |
|
743 for(int j=NCX_DI(rq);j<dt->ndir;j++) { |
|
744 if(ret == REQ_NOACTION) { |
|
745 directive *d = dt->dirs[j]; |
|
746 |
|
747 // check status code parameter |
|
748 char *status = pblock_findkeyval(pb_key_type, d->param); |
|
749 if(status) { |
|
750 int statuscode = atoi(status); |
|
751 if(statuscode != rq->rq.status_num) { |
|
752 continue; |
|
753 } |
|
754 } |
|
755 |
|
756 // execute the saf |
|
757 ret = nsapi_exec(d, sn, rq); |
|
758 } |
|
759 |
|
760 if(ret == REQ_ABORTED) { |
|
761 // if an error directive fails, we use the default |
|
762 // error handler |
|
763 break; |
|
764 } |
|
765 if(ret != REQ_NOACTION) { |
|
766 if(ret == REQ_PROCEED) { |
|
767 /* |
|
768 * flush buffer and add termination if chunked encoding |
|
769 * is enabled |
|
770 */ |
|
771 net_finish(sn->sn.csd); |
|
772 } else if(ret == REQ_PROCESSING) { |
|
773 // save nsapi context |
|
774 rq->context.objset_index = i; |
|
775 |
|
776 // add +1 to start next round with next function |
|
777 rq->context.dtable_index = j + 1; |
|
778 } |
|
779 return ret; |
|
780 } |
|
781 } |
|
782 } |
|
783 |
|
784 if(ret != REQ_PROCEED) { |
|
785 // default error handler |
|
786 nsapi_error_request((Session*)sn, (Request*)rq); |
|
787 } |
|
788 |
|
789 return ret; |
|
790 } |
|
791 |
709 int nsapi_addlog(NSAPISession *sn, NSAPIRequest *rq) { |
792 int nsapi_addlog(NSAPISession *sn, NSAPIRequest *rq) { |
710 //printf("nsapi_addlog\n"); |
793 //printf("nsapi_addlog\n"); |
711 httpd_objset *objset = rq->rq.os; |
794 httpd_objset *objset = rq->rq.os; |
712 |
795 |
713 if(NCX_OI(rq) == -1) { |
796 if(NCX_OI(rq) == -1) { |