src/server/httprequest.c

changeset 6
ce8fecc9847d
parent 5
dbc01588686e
child 7
3c2ed7a7a5fd
--- a/src/server/httprequest.c	Mon Dec 26 15:48:32 2011 +0100
+++ b/src/server/httprequest.c	Tue Dec 27 20:12:21 2011 +0100
@@ -67,6 +67,7 @@
     rq->phase = NSAPIAuthTrans;
 
     // fill session structure
+    sn->sys_fd = request->connection->fd;
     sn->sn.pool = pool_create();
     sn->sn.csd = stream_new_from_fd(request->connection->fd);
     sn->sn.client = NULL;
@@ -176,6 +177,7 @@
         switch(rq->phase) {
             case NSAPIAuthTrans: {
                 rq->phase++;
+                nsapi_context_next_stage(&rq->context);
             }
             case NSAPINameTrans: {
                 printf(">>> NameTrans\n");
@@ -184,13 +186,17 @@
                     break;
                 }
                 rq->phase++;
+                nsapi_context_next_stage(&rq->context);
             }
             case NSAPIPathCheck: {
                 printf(">>> PathCheck\n");
                 rq->phase++;
+                nsapi_context_next_stage(&rq->context);
             }
             case NSAPIObjectType: {
+                printf(">>> ObjectType\n");
                 rq->phase++;
+                nsapi_context_next_stage(&rq->context);
             }
             case NSAPIService: {
                 printf(">>> Service\n");
@@ -199,6 +205,12 @@
                     break;
                 }
                 rq->phase++;
+                nsapi_context_next_stage(&rq->context);
+            }
+            case NSAPIAddLog: {
+                printf(">>> AddLog\n");
+                rq->phase++;
+                nsapi_context_next_stage(&rq->context);
             }
             case REQ_FINISH: {
                 printf(">>> Finish\n");
@@ -212,49 +224,97 @@
 }
 
 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
+    // TODO: free memory
+    close(sn->sys_fd);
+
     return 0;
 }
 
 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) {
-    httpd_objset *objset = rq->vs->objset;
+    HTTPObjectConfig *objconf = rq->vs->objects;
     printf("nsapi_nametrans\n");
+    httpd_objset *objset = objset_create(sn->sn.pool);
+    rq->rq.os = objset;
+    /* first object in objconf is the default object  TODO: make sure it is */
+    objset_add_object(sn->sn.pool, objset, objconf->objects[0]);
 
-    int ret = -1;
-    for(int i=0;i<objset->pos;i++) {
-        httpd_object *obj = objset->obj[i];
-        dtable *dt = object_get_dtable(obj, NSAPINameTrans);
-
-        printf("object[%s] dt: %d\n", obj->name, dt);
+    httpd_object *obj = objset->obj[0]; /* nametrans only in default object */
+    dtable *dt = object_get_dtable(obj, NSAPINameTrans);
 
-        // execute directives
-        for(int j=0;j<dt->ndir;j++) {
-            directive *d = dt->dirs[j];
+    /* execute directives */
+    int ret = rq->context.last_req_code;
+    char *name = NULL;
+    char *ppath = NULL;
+    for(int i=NCX_DI(rq);i<dt->ndir;i++) {
+        directive *d = dt->dirs[i];
 
-            printf("execute [%s]\n", d->func->name);
-            ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
-            if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
-                break;
-            }
+        printf("execute [%s]\n", d->func->name);
+        ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
+
+        /* check for name or ppath */
+        name = pblock_findkeyval(pb_key_name, rq->rq.vars);
+        ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars);
+
+        /* add additional objects to the objset */
+        if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) {
+            fprintf(stderr, "add_objects failed\n");
+            return REQ_ABORTED;
         }
 
-        // TODO: stultus
-        if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
-            break;
+        if(ret != REQ_NOACTION) {
+            /*
+             * if a saf is still processing, we need to save the context, to
+             * process this object at a later time
+             */
+            if(ret == REQ_PROCESSING) {
+                /* save nsapi context */
+                /* add +1 to start next round with next function */
+                rq->context.dtable_index = i + 1;
+            }
+
+            return ret;
         }
     }
 
-    // todo: check object, path, ...
-    char *ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars);
-    printf("ppath: [%s]\n", ppath);
+    /* if no function has set the ppath var, translate it to docroot */
+    if(ret == REQ_NOACTION && ppath == NULL) {
+        sstr_t docroot = rq->vs->document_root;
+        if(docroot.length < 1) {
+            printf("docroot too short\n");
+            return REQ_ABORTED; /* docroot too short */
+        }
+        /* if there is a trailing '/', remove it */
+        if(docroot.ptr[docroot.length - 1] == '/') {
+            docroot.length--;
+        }
+
+        sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb));
 
-    return ret;
+        sstr_t translated;
+        translated.length = docroot.length + uri.length;
+        translated.ptr = alloca(translated.length + 1);
+        translated = sstrncat(2, translated, docroot, uri);
+
+        pblock_kvinsert(
+            pb_key_ppath,
+            translated.ptr,
+            translated.length,
+            rq->rq.vars);
+    }
+
+    return REQ_PROCEED;
 }
 
 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
-    httpd_objset *objset = rq->vs->objset;
+    printf("nsapi_service\n");
+    httpd_objset *objset = rq->rq.os;
 
-    int ret = -1;
-    for(int i=0;i<objset->pos;i++) {
+    if(NCX_OI(rq) == -1) {
+        NCX_OI(rq) = objset->pos - 1;
+    }
+
+    int ret = rq->context.last_req_code;
+    for(int i=NCX_OI(rq);i>=0;i--) {
         httpd_object *obj = objset->obj[i];
         dtable *dt = object_get_dtable(obj, NSAPIService);
 
@@ -263,17 +323,39 @@
             directive *d = dt->dirs[j];
 
             ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
-            if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
-                break;
+            if(ret != REQ_NOACTION) {
+                if(ret == REQ_PROCESSING) {
+                    /* save nsapi context */
+                    rq->context.objset_index = i;
+
+                    /* add +1 to start next round with next function */
+                    rq->context.dtable_index = j + 1;
+                }
+
+                return ret;
             }
         }
-
-        // TODO: stultus
-        if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
-            break;
-        }
     }
 
     return ret;
 }
 
+/*
+ * adds objects with specific name or path to the httpd_objset
+ */
+int add_objects(
+        HTTPObjectConfig *objs,
+        httpd_objset *os,
+        NSAPISession *sn,
+        NSAPIRequest *rq,
+        char *name,
+        char *path)
+{
+    /* first, add all objects with a matching path */
+
+
+    /* add object with object with matching name */
+    
+
+    return REQ_PROCEED;
+}

mercurial