Added some safs

Fri, 30 Dec 2011 14:06:56 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 30 Dec 2011 14:06:56 +0100
changeset 10
e3ae779232a9
parent 9
30e51941a673
child 11
24d804a2799f

Added some safs

src/server/httprequest.c file | annotate | diff | comparison | revisions
src/server/httprequest.h file | annotate | diff | comparison | revisions
src/server/nametrans.c file | annotate | diff | comparison | revisions
src/server/nametrans.h file | annotate | diff | comparison | revisions
src/server/objecttype.c file | annotate | diff | comparison | revisions
src/server/objecttype.h file | annotate | diff | comparison | revisions
src/server/objs.mk file | annotate | diff | comparison | revisions
src/server/service.c file | annotate | diff | comparison | revisions
src/server/service.h file | annotate | diff | comparison | revisions
src/server/ws-fn.c file | annotate | diff | comparison | revisions
templates/conf/obj.conf file | annotate | diff | comparison | revisions
templates/docs/hello.txt file | annotate | diff | comparison | revisions
templates/docs/test.txt file | annotate | diff | comparison | revisions
--- a/src/server/httprequest.c	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/httprequest.c	Fri Dec 30 14:06:56 2011 +0100
@@ -268,6 +268,10 @@
             }
             case NSAPIObjectType: {
                 printf(">>> ObjectType\n");
+                r = nsapi_objecttype(sn, rq);
+                if(r != REQ_PROCEED) {
+                    break;
+                }
                 rq->phase++;
                 nsapi_context_next_stage(&rq->context);
             }
@@ -378,6 +382,73 @@
     return REQ_PROCEED;
 }
 
+int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) {
+    printf("nsapi_objecttype\n");
+    httpd_objset *objset = rq->rq.os;
+
+    if(NCX_OI(rq) == -1) {
+        /* object index is undefined -> define correct object index */
+        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, NSAPIObjectType);
+
+        // execute directives
+        for(int j=0;j<dt->ndir;j++) {
+            directive *d = dt->dirs[j];
+
+            ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
+            switch(ret) {
+                case REQ_PROCEED: {
+                    char *type = pblock_findkeyval(
+                            pb_key_content_type,
+                            rq->rq.srvhdrs);
+                    if(type == NULL) {
+                        ret = REQ_NOACTION;
+                        break;
+                    }
+                    return ret;
+                }
+                case 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;
+                }
+                case REQ_NOACTION: {
+                    break;
+                }
+                default: {
+                    return ret;
+                }
+            }
+        }
+    }
+
+    /*
+     * No function returned with REQ_PROCEED, but we need a content type.
+     * If the path ends with a '/', we set the content type to
+     * 'internal/directory' so that 'index-common' can serve the content.
+     * Otherwise we set the content type to text/plain
+     */
+    sstr_t path = sstr(pblock_findkeyval(pb_key_ppath, rq->rq.vars));
+    sstr_t ct;
+    if(path.ptr[path.length - 1] == '/') {
+        /* directory */
+        ct = sstrn("internal/directory", 18);
+    } else {
+        ct = sstrn("text/plain", 10);
+    }
+    pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->rq.srvhdrs);
+
+    return REQ_PROCEED;
+}
+
 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
     printf("nsapi_service\n");
     httpd_objset *objset = rq->rq.os;
@@ -425,10 +496,22 @@
         char *path)
 {
     /* first, add all objects with a matching path */
+    /* TODO */
 
 
     /* add object with object with matching name */
-    
+    if(name == NULL) {
+        return REQ_PROCEED;
+    }
+
+    for(int i=0;i<objs->nobj;i++) {
+        httpd_object *obj = objs->objects[i];
+
+        if(obj->name && !strcmp(name, obj->name)) {
+            printf("name is %s -> add object %s\n", name, obj->name);
+            objset_add_object(sn->sn.pool, os, obj);
+        }
+    } 
 
     return REQ_PROCEED;
 }
--- a/src/server/httprequest.h	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/httprequest.h	Fri Dec 30 14:06:56 2011 +0100
@@ -81,6 +81,7 @@
 int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq);
+int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq);
 
 
--- a/src/server/nametrans.c	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/nametrans.c	Fri Dec 30 14:06:56 2011 +0100
@@ -39,3 +39,43 @@
 
     return REQ_NOACTION;
 }
+
+/*
+ * assign_name
+ *
+ * Assigns the name specified by the name parameter if the uri has the
+ * specified prefix.
+ *
+ * pblock parameter:
+ * name     object name
+ * from     optional uri prefix
+ */
+int assign_name(pblock *pb, Session *sn, Request *rq) {
+    /* TODO: expression instead of simple prefix */
+
+    char *name = pblock_findkeyval(pb_key_name, pb);
+    char *from = pblock_findkeyval(pb_key_from, pb);
+
+    if(!name) {
+        /* TODO: add log */
+        protocol_status(sn, rq, 500, NULL);
+        return REQ_ABORTED;
+    }
+    
+    if(from) {
+        char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
+        char c;
+        int i = 0;
+        while((c = from[i]) != 0) {
+            if(c != uri[i]) {
+                return REQ_NOACTION;
+            }
+            i++;
+        }
+    }
+
+    /* add object to rq->vars */
+    pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
+
+    return REQ_NOACTION;
+}
--- a/src/server/nametrans.h	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/nametrans.h	Fri Dec 30 14:06:56 2011 +0100
@@ -37,6 +37,7 @@
 
 int test_nametrans(pblock *pb, Session *sn, Request *rq);
 
+int assign_name(pblock *pb, Session *sn, Request *rq);
 
 #ifdef	__cplusplus
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/objecttype.c	Fri Dec 30 14:06:56 2011 +0100
@@ -0,0 +1,69 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2011 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "objecttype.h"
+#include "pblock.h"
+
+#include "sstring.h"
+
+int object_type_by_extension(pblock *pb, Session *sn, Request *rq) {
+    sstr_t ppath = sstr(pblock_findkeyval(pb_key_ppath, rq->vars));
+    sstr_t ct;
+    if(ppath.ptr[ppath.length - 1] == '/') {
+        /* directory */
+        ct = sstrn("internal/directory", 18);
+    } else {
+        sstr_t ext;
+        ext.length = 0;
+        for(int i=ppath.length - 1;i>=0;i--) {
+            if(ppath.ptr[i] == '.') {
+                ext.ptr = ppath.ptr + i + 1;
+                ext.length = ppath.length - i - 1;
+            }
+        }
+
+        if(ext.length == 0) {
+            /* no extension, no type */
+            return REQ_NOACTION;
+        }
+
+        /* TODO: we need a map for extensions/types */
+        if(!sstrcmp(ext, sstrn("txt", 3))) {
+            ct = sstr("text/plain");
+        } else if(!sstrcmp(ext, sstrn("htm", 3))) {
+            ct = sstr("text/html");
+        } else if(!sstrcmp(ext, sstrn("html", 4))) {
+            ct = sstr("text/html");
+        } else if(!sstrcmp(ext, sstrn("xml", 3))) {
+            ct = sstr("text/xml");
+        }
+    }
+
+    pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->srvhdrs);
+    return REQ_PROCEED;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/objecttype.h	Fri Dec 30 14:06:56 2011 +0100
@@ -0,0 +1,45 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2011 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef OBJECTTYPE_H
+#define	OBJECTTYPE_H
+
+#include "nsapi.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+int object_type_by_extension(pblock *pb, Session *sn, Request *rq);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* OBJECTTYPE_H */
+
--- a/src/server/objs.mk	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/objs.mk	Fri Dec 30 14:06:56 2011 +0100
@@ -60,6 +60,7 @@
 MOBJ += protocol.o
 MOBJ += netbuf.o
 MOBJ += shexp.o
+MOBJ += objecttype.o
 
 MAINOBJS = $(MOBJ:%=$(OBJPRE)%)
 
--- a/src/server/service.c	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/service.c	Fri Dec 30 14:06:56 2011 +0100
@@ -97,7 +97,7 @@
     return fd;
 }
 
-int test_service(pblock *pb, Session *sn, Request *rq) {
+int send_file(pblock *pb, Session *sn, Request *rq) {
     printf("test_service\n");
 
     // request body test begin
@@ -136,3 +136,12 @@
 
     return REQ_PROCEED;
 }
+
+int service_hello(pblock *pb, Session *sn, Request *rq) {
+    pblock_nninsert("content-length", 13, rq->srvhdrs);
+    pblock_nvinsert("content-type", "text/plain", rq->srvhdrs);
+    protocol_status(sn, rq, 200, NULL);
+    http_start_response(sn, rq);
+    net_write(sn->csd, "Hello World!\n", 13);
+    return REQ_PROCEED;
+}
--- a/src/server/service.h	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/service.h	Fri Dec 30 14:06:56 2011 +0100
@@ -35,7 +35,9 @@
 extern "C" {
 #endif
 
-int test_service(pblock *pb, Session *sn, Request *rq);
+int send_file(pblock *pb, Session *sn, Request *rq);
+
+int service_hello(pblock *pb, Session *sn, Request *rq);
 
 
 #ifdef	__cplusplus
--- a/src/server/ws-fn.c	Thu Dec 29 18:51:23 2011 +0100
+++ b/src/server/ws-fn.c	Fri Dec 30 14:06:56 2011 +0100
@@ -29,10 +29,14 @@
 #include "nsapi.h"
 
 #include "nametrans.h"
+#include "objecttype.h"
 #include "service.h"
 
 struct FuncStruct webserver_funcs[] = {
     { "test-nametrans", test_nametrans, NULL, 0 },
-    { "test-service", test_service, NULL, 0},
+    { "assign-name", assign_name, NULL, 0},
+    { "type-by-extension", object_type_by_extension, NULL, 0},
+    { "send-file", send_file, NULL, 0},
+    { "service-hello", service_hello, NULL, 0},
     {NULL, NULL, NULL, 0}
 };
--- a/templates/conf/obj.conf	Thu Dec 29 18:51:23 2011 +0100
+++ b/templates/conf/obj.conf	Fri Dec 30 14:06:56 2011 +0100
@@ -5,11 +5,12 @@
 #
 
 <Object name="default">
-NameTrans fn="test-nametrans"
-Service fn="test-service"
+NameTrans fn="assign-name" from="/hello" name="hello"
+ObjectType fn="type-by-extension"
+Service fn="send-file"
 </Object>
 
-<Object ppath="/hello">
+<Object name="hello">
 Service fn="service-hello"
 </Object>
 
--- a/templates/docs/hello.txt	Thu Dec 29 18:51:23 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-Hello World!
-------------
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/docs/test.txt	Fri Dec 30 14:06:56 2011 +0100
@@ -0,0 +1,5 @@
+Hello World!
+------------
+
+UAP Webserver
+

mercurial