src/server/httprequest.c

changeset 10
e3ae779232a9
parent 9
30e51941a673
child 12
34aa8001ea53
equal deleted inserted replaced
9:30e51941a673 10:e3ae779232a9
266 rq->phase++; 266 rq->phase++;
267 nsapi_context_next_stage(&rq->context); 267 nsapi_context_next_stage(&rq->context);
268 } 268 }
269 case NSAPIObjectType: { 269 case NSAPIObjectType: {
270 printf(">>> ObjectType\n"); 270 printf(">>> ObjectType\n");
271 r = nsapi_objecttype(sn, rq);
272 if(r != REQ_PROCEED) {
273 break;
274 }
271 rq->phase++; 275 rq->phase++;
272 nsapi_context_next_stage(&rq->context); 276 nsapi_context_next_stage(&rq->context);
273 } 277 }
274 case NSAPIService: { 278 case NSAPIService: {
275 printf(">>> Service\n"); 279 printf(">>> Service\n");
372 pb_key_ppath, 376 pb_key_ppath,
373 translated.ptr, 377 translated.ptr,
374 translated.length, 378 translated.length,
375 rq->rq.vars); 379 rq->rq.vars);
376 } 380 }
381
382 return REQ_PROCEED;
383 }
384
385 int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) {
386 printf("nsapi_objecttype\n");
387 httpd_objset *objset = rq->rq.os;
388
389 if(NCX_OI(rq) == -1) {
390 /* object index is undefined -> define correct object index */
391 NCX_OI(rq) = objset->pos - 1;
392 }
393
394 int ret = rq->context.last_req_code;
395 for(int i=NCX_OI(rq);i>=0;i--) {
396 httpd_object *obj = objset->obj[i];
397 dtable *dt = object_get_dtable(obj, NSAPIObjectType);
398
399 // execute directives
400 for(int j=0;j<dt->ndir;j++) {
401 directive *d = dt->dirs[j];
402
403 ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
404 switch(ret) {
405 case REQ_PROCEED: {
406 char *type = pblock_findkeyval(
407 pb_key_content_type,
408 rq->rq.srvhdrs);
409 if(type == NULL) {
410 ret = REQ_NOACTION;
411 break;
412 }
413 return ret;
414 }
415 case REQ_PROCESSING: {
416 /* save nsapi context */
417 rq->context.objset_index = i;
418
419 /* add +1 to start next round with next function */
420 rq->context.dtable_index = j + 1;
421 return ret;
422 }
423 case REQ_NOACTION: {
424 break;
425 }
426 default: {
427 return ret;
428 }
429 }
430 }
431 }
432
433 /*
434 * No function returned with REQ_PROCEED, but we need a content type.
435 * If the path ends with a '/', we set the content type to
436 * 'internal/directory' so that 'index-common' can serve the content.
437 * Otherwise we set the content type to text/plain
438 */
439 sstr_t path = sstr(pblock_findkeyval(pb_key_ppath, rq->rq.vars));
440 sstr_t ct;
441 if(path.ptr[path.length - 1] == '/') {
442 /* directory */
443 ct = sstrn("internal/directory", 18);
444 } else {
445 ct = sstrn("text/plain", 10);
446 }
447 pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->rq.srvhdrs);
377 448
378 return REQ_PROCEED; 449 return REQ_PROCEED;
379 } 450 }
380 451
381 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) { 452 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
423 NSAPIRequest *rq, 494 NSAPIRequest *rq,
424 char *name, 495 char *name,
425 char *path) 496 char *path)
426 { 497 {
427 /* first, add all objects with a matching path */ 498 /* first, add all objects with a matching path */
499 /* TODO */
428 500
429 501
430 /* add object with object with matching name */ 502 /* add object with object with matching name */
431 503 if(name == NULL) {
504 return REQ_PROCEED;
505 }
506
507 for(int i=0;i<objs->nobj;i++) {
508 httpd_object *obj = objs->objects[i];
509
510 if(obj->name && !strcmp(name, obj->name)) {
511 printf("name is %s -> add object %s\n", name, obj->name);
512 objset_add_object(sn->sn.pool, os, obj);
513 }
514 }
432 515
433 return REQ_PROCEED; 516 return REQ_PROCEED;
434 } 517 }

mercurial