56 req->headers = hd; |
56 req->headers = hd; |
57 } |
57 } |
58 |
58 |
59 int handle_request(HTTPRequest *request, threadpool_t *thrpool) { |
59 int handle_request(HTTPRequest *request, threadpool_t *thrpool) { |
60 // handle nsapi request |
60 // handle nsapi request |
61 |
61 |
62 // create pool |
62 // create pool |
63 pool_handle_t *pool = pool_create(); |
63 pool_handle_t *pool = pool_create(); |
64 |
64 |
65 // create nsapi data structures |
65 // create nsapi data structures |
66 NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession)); |
66 NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession)); |
438 /* |
438 /* |
439 * if a saf is still processing, we need to save the context, to |
439 * if a saf is still processing, we need to save the context, to |
440 * process this object at a later time |
440 * process this object at a later time |
441 */ |
441 */ |
442 if(ret == REQ_PROCESSING) { |
442 if(ret == REQ_PROCESSING) { |
443 /* save nsapi context */ |
443 // save nsapi context |
444 /* add +1 to start next round with next function */ |
444 // add +1 to start next round with next function |
445 rq->context.dtable_index = i + 1; |
445 rq->context.dtable_index = i + 1; |
446 } |
446 } |
447 |
447 |
448 return ret; |
448 return ret; |
449 } |
449 } |
456 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) { |
456 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) { |
457 HTTPObjectConfig *objconf = rq->vs->objects; |
457 HTTPObjectConfig *objconf = rq->vs->objects; |
458 //printf("nsapi_nametrans\n"); |
458 //printf("nsapi_nametrans\n"); |
459 httpd_objset *objset = objset_create(sn->sn.pool); |
459 httpd_objset *objset = objset_create(sn->sn.pool); |
460 rq->rq.os = objset; |
460 rq->rq.os = objset; |
461 /* first object in objconf is the default object TODO: make sure it is */ |
461 // first object in objconf is the default object TODO: make sure it is |
462 objset_add_object(sn->sn.pool, objset, objconf->objects[0]); |
462 objset_add_object(sn->sn.pool, objset, objconf->objects[0]); |
463 |
463 |
464 httpd_object *obj = objset->obj[0]; /* nametrans only in default object */ |
464 httpd_object *obj = objset->obj[0]; // nametrans only in default object |
465 dtable *dt = object_get_dtable(obj, NSAPINameTrans); |
465 dtable *dt = object_get_dtable(obj, NSAPINameTrans); |
466 |
466 |
467 /* execute directives */ |
467 // execute directives |
468 int ret = rq->context.last_req_code; |
468 int ret = rq->context.last_req_code; |
469 char *name = NULL; |
469 char *name = NULL; |
470 char *ppath = NULL; |
470 char *ppath = NULL; |
471 for(int i=NCX_DI(rq);i<dt->ndir;i++) { |
471 for(int i=NCX_DI(rq);i<dt->ndir;i++) { |
472 if(ret == REQ_NOACTION) { |
472 if(ret == REQ_NOACTION) { |
473 directive *d = dt->dirs[i]; |
473 directive *d = dt->dirs[i]; |
474 ret = nsapi_exec(d, sn, rq); |
474 ret = nsapi_exec(d, sn, rq); |
475 } |
475 } |
476 |
476 |
477 /* check for name or ppath */ |
477 // check for name or ppath |
478 name = pblock_findkeyval(pb_key_name, rq->rq.vars); |
478 name = pblock_findkeyval(pb_key_name, rq->rq.vars); |
479 ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars); |
479 ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars); |
480 |
480 |
481 /* add additional objects to the objset */ |
481 // add additional objects to the objset |
482 if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) { |
482 if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) { |
483 fprintf(stderr, "add_objects failed\n"); |
483 fprintf(stderr, "add_objects failed\n"); |
484 return REQ_ABORTED; |
484 return REQ_ABORTED; |
485 } |
485 } |
486 |
486 |
488 /* |
488 /* |
489 * if a saf is still processing, we need to save the context, to |
489 * if a saf is still processing, we need to save the context, to |
490 * process this object at a later time |
490 * process this object at a later time |
491 */ |
491 */ |
492 if(ret == REQ_PROCESSING) { |
492 if(ret == REQ_PROCESSING) { |
493 /* save nsapi context */ |
493 // save nsapi context |
494 /* add +1 to start next round with next function */ |
494 // add +1 to start next round with next function |
495 rq->context.dtable_index = i + 1; |
495 rq->context.dtable_index = i + 1; |
496 } |
496 } |
497 |
497 |
498 return ret; |
498 return ret; |
499 } |
499 } |
500 } |
500 } |
501 |
501 |
502 /* if no function has set the ppath var, translate it to docroot */ |
502 // if no function has set the ppath var, translate it to docroot |
503 if(ret == REQ_NOACTION && ppath == NULL) { |
503 if(ret == REQ_NOACTION && ppath == NULL) { |
504 sstr_t docroot = rq->vs->document_root; |
504 sstr_t docroot = rq->vs->document_root; |
505 if(docroot.length < 1) { |
505 if(docroot.length < 1) { |
506 printf("docroot too short\n"); |
506 printf("docroot too short\n"); |
507 return REQ_ABORTED; /* docroot too short */ |
507 return REQ_ABORTED; /* docroot too short */ |
508 } |
508 } |
509 /* if there is a trailing '/', remove it */ |
509 // if there is a trailing '/', remove it |
510 if(docroot.ptr[docroot.length - 1] == '/') { |
510 if(docroot.ptr[docroot.length - 1] == '/') { |
511 docroot.length--; |
511 docroot.length--; |
512 } |
512 } |
513 |
513 |
514 sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb)); |
514 sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb)); |
541 httpd_object *obj = objset->obj[i]; |
541 httpd_object *obj = objset->obj[i]; |
542 dtable *dt = object_get_dtable(obj, NSAPIPathCheck); |
542 dtable *dt = object_get_dtable(obj, NSAPIPathCheck); |
543 |
543 |
544 // execute directives |
544 // execute directives |
545 for(int j=NCX_DI(rq);j<dt->ndir;j++) { |
545 for(int j=NCX_DI(rq);j<dt->ndir;j++) { |
546 if(ret == REQ_NOACTION) { |
546 if(ret == REQ_NOACTION || REQ_PROCEED) { |
547 directive *d = dt->dirs[j]; |
547 directive *d = dt->dirs[j]; |
548 ret = nsapi_exec(d, sn, rq); |
548 ret = nsapi_exec(d, sn, rq); |
549 } |
549 } else { |
550 |
|
551 if(ret != REQ_NOACTION) { |
|
552 if(ret == REQ_PROCESSING) { |
550 if(ret == REQ_PROCESSING) { |
553 /* save nsapi context */ |
551 // save nsapi context |
554 rq->context.objset_index = i; |
552 rq->context.objset_index = i; |
555 |
553 |
556 /* add +1 to start next round with next function */ |
554 // add +1 to start next round with next function |
557 rq->context.dtable_index = j + 1; |
555 rq->context.dtable_index = j + 1; |
558 } |
556 } |
559 |
557 |
560 return ret; |
558 return ret; |
561 } |
559 } |
568 int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) { |
566 int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) { |
569 //printf("nsapi_objecttype\n"); |
567 //printf("nsapi_objecttype\n"); |
570 httpd_objset *objset = rq->rq.os; |
568 httpd_objset *objset = rq->rq.os; |
571 |
569 |
572 if(NCX_OI(rq) == -1) { |
570 if(NCX_OI(rq) == -1) { |
573 /* object index is undefined -> define correct object index */ |
571 // object index is undefined -> define correct object index |
574 NCX_OI(rq) = objset->pos - 1; |
572 NCX_OI(rq) = objset->pos - 1; |
575 } |
573 } |
576 |
574 |
577 int ret = rq->context.last_req_code; |
575 int ret = rq->context.last_req_code; |
578 for(int i=NCX_OI(rq);i>=0;i--) { |
576 for(int i=NCX_OI(rq);i>=0;i--) { |
622 * Otherwise we set the content type to text/plain |
620 * Otherwise we set the content type to text/plain |
623 */ |
621 */ |
624 sstr_t path = sstr(pblock_findkeyval(pb_key_ppath, rq->rq.vars)); |
622 sstr_t path = sstr(pblock_findkeyval(pb_key_ppath, rq->rq.vars)); |
625 sstr_t ct; |
623 sstr_t ct; |
626 if(path.ptr[path.length - 1] == '/') { |
624 if(path.ptr[path.length - 1] == '/') { |
627 /* directory */ |
625 // directory |
628 ct = sstrn("internal/directory", 18); |
626 ct = sstrn("internal/directory", 18); |
629 } else { |
627 } else { |
630 ct = sstrn("text/plain", 10); |
628 ct = sstrn("text/plain", 10); |
631 } |
629 } |
632 pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->rq.srvhdrs); |
630 pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->rq.srvhdrs); |
652 // execute directives |
650 // execute directives |
653 for(int j=NCX_DI(rq);j<dt->ndir;j++) { |
651 for(int j=NCX_DI(rq);j<dt->ndir;j++) { |
654 if(ret == REQ_NOACTION) { |
652 if(ret == REQ_NOACTION) { |
655 directive *d = dt->dirs[j]; |
653 directive *d = dt->dirs[j]; |
656 |
654 |
657 /* check type parameter */ |
655 // check type parameter |
658 char *dtp = pblock_findkeyval(pb_key_type, d->param); |
656 char *dtp = pblock_findkeyval(pb_key_type, d->param); |
659 if(dtp) { |
657 if(dtp) { |
660 /* type parameter for directive */ |
658 // type parameter for directive |
661 if(!content_type) { |
659 if(!content_type) { |
662 content_type = pblock_findkeyval( |
660 content_type = pblock_findkeyval( |
663 pb_key_content_type, |
661 pb_key_content_type, |
664 rq->rq.srvhdrs); |
662 rq->rq.srvhdrs); |
665 } |
663 } |
666 /* compare types */ |
664 // compare types |
667 if(strcmp(dtp, content_type) != 0) { |
665 if(strcmp(dtp, content_type) != 0) { |
668 continue; |
666 continue; |
669 } |
667 } |
670 } |
668 } |
671 |
669 |
672 /* check method parameter */ |
670 // check method parameter |
673 char *dmt = pblock_findkeyval(pb_key_method, d->param); |
671 char *dmt = pblock_findkeyval(pb_key_method, d->param); |
674 if(dmt) { |
672 if(dmt) { |
675 if(!method) { |
673 if(!method) { |
676 method = pblock_findkeyval(pb_key_method, rq->rq.reqpb); |
674 method = pblock_findkeyval(pb_key_method, rq->rq.reqpb); |
677 } |
675 } |
691 * flush buffer and add termination if chunked encoding |
689 * flush buffer and add termination if chunked encoding |
692 * is enabled |
690 * is enabled |
693 */ |
691 */ |
694 net_finish(sn->sn.csd); |
692 net_finish(sn->sn.csd); |
695 } else if(ret == REQ_PROCESSING) { |
693 } else if(ret == REQ_PROCESSING) { |
696 /* save nsapi context */ |
694 // save nsapi context |
697 rq->context.objset_index = i; |
695 rq->context.objset_index = i; |
698 |
696 |
699 /* add +1 to start next round with next function */ |
697 // add +1 to start next round with next function |
700 rq->context.dtable_index = j + 1; |
698 rq->context.dtable_index = j + 1; |
701 } |
699 } |
702 |
700 |
703 return ret; |
701 return ret; |
704 } |
702 } |
728 ret = nsapi_exec(d, sn, rq); |
726 ret = nsapi_exec(d, sn, rq); |
729 } |
727 } |
730 |
728 |
731 if(ret != REQ_NOACTION) { |
729 if(ret != REQ_NOACTION) { |
732 if(ret == REQ_PROCESSING) { |
730 if(ret == REQ_PROCESSING) { |
733 /* save nsapi context */ |
731 // save nsapi context |
734 rq->context.objset_index = i; |
732 rq->context.objset_index = i; |
735 |
733 |
736 /* add +1 to start next round with next function */ |
734 // add +1 to start next round with next function |
737 rq->context.dtable_index = j + 1; |
735 rq->context.dtable_index = j + 1; |
738 } |
736 } |
739 |
737 |
740 return ret; |
738 return ret; |
741 } |
739 } |