src/server/daemon/httprequest.c

changeset 101
7fbcdbad0baa
parent 96
0185b13bf41f
child 102
136a76e293b5
--- a/src/server/daemon/httprequest.c	Sat Oct 17 18:07:04 2015 +0200
+++ b/src/server/daemon/httprequest.c	Sat Oct 17 21:17:34 2015 +0200
@@ -56,6 +56,33 @@
     req->headers = hd;
 }
 
+void http_request_cleanup(HTTPRequest *req) {
+    // TODO: implement
+}
+
+sstr_t http_request_get_abspath(HTTPRequest *req) {
+    sstr_t uri = req->uri;
+    
+    int i = 0;
+    if(uri.ptr[0] == '/') {
+        return uri;
+    } else if(sstrprefix(uri, S("http://"))) {
+        i = 7;
+    } else if(sstrprefix(uri, S("https://"))) {
+        i = 8;
+    } else if(!sstrcmp(uri, S("*"))) {
+        return uri;
+    }
+    
+    for(;i<uri.length;i++) {
+        if(uri.ptr[i] == '/') {
+            return sstrsubs(uri, i);
+        }
+    }
+    
+    return S("/");
+}
+
 int handle_request(HTTPRequest *request, threadpool_t *thrpool) {
     // handle nsapi request
     
@@ -137,13 +164,28 @@
             request->httpv.ptr,
             request->httpv.length,
             rq->rq.reqpb);
-    // TODO: protocol num
+    
+    if(!sstrcmp(request->httpv, S("HTTP/1.1"))) {
+        rq->rq.protv_num = PROTOCOL_VERSION_HTTP11;
+    } else if(!sstrcmp(request->httpv, S("HTTP/1.0"))) {
+        rq->rq.protv_num = PROTOCOL_VERSION_HTTP10;
+    } else {
+        // TODO: invalid protocol version - abort
+    }
 
     /*
      * get absolute path and query of the request uri
      */
     // TODO: check for '#'
-    sstr_t absPath = request->uri;
+    sstr_t absPath = http_request_get_abspath(request);
+    if(!absPath.ptr) {
+        // TODO: error msg
+        return 1;
+    } else if(absPath.ptr[0] == '*') {
+        // TODO: implement global OPTIONS
+        return 1;
+    }
+    
     sstr_t query;
     query.length = 0;
     
@@ -200,24 +242,38 @@
             i = 0;
             hlen = ha->len;
         }
+        
+        Header header = ha->headers[i];
 
-        if(ha->headers[i].name[0] < 90) {
-            ha->headers[i].name[0] += 32;
+        if(header.name.ptr[0] < 90) {
+            header.name.ptr[0] += 32;
         }
 
         // change to lower case
-        char *header_name = ha->headers[i].name;
-        for(int i=0;header_name[i]!=0;i++) {
-            if(header_name[i] > 64 && header_name[i] < 97) {
-                header_name[i] += 32;
+        for(int i=0;i<header.name.length;i++) {
+            if(header.name.ptr[i] > 64 && header.name.ptr[i] < 97) {
+                header.name.ptr[i] += 32;
             }
         }
-
-        pblock_nvinsert(
-                ha->headers[i].name,
-                ha->headers[i].value,
+        
+        pblock_nvlinsert(
+                header.name.ptr,
+                header.name.length,
+                header.value.ptr,
+                header.value.length,
                 rq->rq.headers);
     }
+    
+    // parse connection header
+    rq->rq.rq_attr.keep_alive = (rq->rq.protv_num >= PROTOCOL_VERSION_HTTP11);
+    char *conn_str = pblock_findkeyval(pb_key_connection, rq->rq.headers);
+    if(conn_str) {
+        if(!strcasecmp(conn_str, "keep-alive")) {
+            rq->rq.rq_attr.keep_alive = 1;
+        } else if(!strcasecmp(conn_str, "close")) {
+            rq->rq.rq_attr.keep_alive = 0;
+        }
+    }
        
     // check for request body and prepare input buffer
     char *ctlen_str = pblock_findkeyval(pb_key_content_length, rq->rq.headers);
@@ -284,7 +340,7 @@
 
 
 
-void header_add(HeaderArray *hd, char *name, char *value) {
+void header_add(HeaderArray *hd, sstr_t name, sstr_t value) {
     while(hd->len >= hd->alloc) {
         if(hd->next == NULL) {
             HeaderArray *block = malloc(sizeof(HeaderArray));

mercurial