src/server/daemon/httprequest.c

changeset 46
636e05eb48f6
parent 45
a24aa388f02f
child 47
ce9790523346
--- a/src/server/daemon/httprequest.c	Wed Jan 02 16:03:50 2013 +0100
+++ b/src/server/daemon/httprequest.c	Sat Jan 12 14:00:47 2013 +0100
@@ -42,8 +42,7 @@
 #include "httplistener.h"
 #include "error.h"
 
-HTTPRequest *http_request_new() {
-    HTTPRequest *req = malloc(sizeof(HTTPRequest));
+void http_request_init(HTTPRequest *req) {
     req->connection = NULL;
     req->uri.ptr = NULL;
 
@@ -54,22 +53,20 @@
     hd->alloc = 16;
 
     req->headers = hd;
-
-    return req;
 }
 
 int handle_request(HTTPRequest *request, threadpool_t *thrpool) {
     // handle nsapi request
 
     // create pool
-    request->pool = pool_create();
+    pool_handle_t *pool = pool_create();
 
     // create nsapi data structures
-    NSAPISession *sn = malloc(sizeof(NSAPISession));
+    NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession));
     if(sn == NULL) {
         /* TODO: error */
     }
-    NSAPIRequest *rq = malloc(sizeof(NSAPIRequest));
+    NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest));
     if(rq == NULL) {
         /* TODO: error */
     }
@@ -77,9 +74,10 @@
     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->connection = request->connection;
+    sn->netbuf = request->netbuf;
+    sn->sn.pool = pool;
+    sn->sn.csd = stream_new_from_fd(pool, request->connection->fd);
     sn->sn.client = pblock_create_pool(sn->sn.pool, 8);
     sn->sn.next = NULL;
     sn->sn.fill = 1;
@@ -88,7 +86,7 @@
     // the session needs the current server configuration
     sn->config = request->connection->listener->cfg; // TODO: ref
 
-    /* add ip to sn->client pblock */
+    // add ip to sn->client pblock
     char ip_str[INET_ADDRSTRLEN];
     if(inet_ntop(
             AF_INET,
@@ -100,7 +98,7 @@
     }
 
     // init NSAPI request structure
-    if(request_initialize(request->pool, request, rq) != 0) {
+    if(request_initialize(pool, request, rq) != 0) {
         printf("Cannot initialize request structure\n");
         return 1;
     }
@@ -108,9 +106,8 @@
     // set default virtual server
     rq->vs = request->connection->listener->default_vs.vs;
 
-    /* Pass request line as "clf-request" */
-    /* TODO: with or without \r\n ? */
-    // hack to remove \r\n 
+    // Pass request line as "clf-request"
+    // remove \r\n 
     sstr_t clfreq = request->request_line;
     while(clfreq.ptr[clfreq.length - 1] < 33) {
         clfreq.length--;
@@ -170,7 +167,7 @@
     
     /* Get abs_path part of request URI, and canonicalize the path */
     absPath.ptr = util_canonicalize_uri(
-            request->pool,
+            pool,
             absPath.ptr,
             absPath.length,
             (int*)&absPath.length);
@@ -202,7 +199,7 @@
             ha->headers[i].name[0] += 32;
         }
 
-        /* change to lower case */
+        // 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) {
@@ -216,7 +213,7 @@
                 rq->rq.headers);
     }
        
-    /* check for request body and prepare input buffer */
+    // check for request body and prepare input buffer
     char *ctlen_str = pblock_findkeyval(pb_key_content_length, rq->rq.headers);
     if(ctlen_str) {
         int ctlen = atoi(ctlen_str);
@@ -227,14 +224,15 @@
 
         /* create new netbuf */
         NetIOStream *net_io = (NetIOStream*)net_stream_from_fd(
+                pool,
                 request->connection->fd);
         net_io->max_read = ctlen;
         
-        sn->sn.inbuf = malloc(sizeof(netbuf));
+        sn->sn.inbuf = pool_malloc(pool, sizeof(netbuf));
         sn->sn.inbuf->sd = net_io;
         sn->sn.inbuf->pos = 0;
         
-        /* prepare buffer */
+        // prepare buffer
         int cur_input_len = nb->cursize - nb->pos;
         
         if(cur_input_len >= ctlen) {
@@ -247,10 +245,10 @@
             sn->sn.inbuf->inbuf = nb->inbuf + nb->pos;
         } else {
             sn->sn.inbuf->maxsize = (ctlen > 2048) ? (2048) : (ctlen);
-            sn->sn.inbuf->inbuf = malloc(sn->sn.inbuf->maxsize);
+            sn->sn.inbuf->inbuf = pool_malloc(pool, sn->sn.inbuf->maxsize);
 
             if(cur_input_len > 0) {
-                /* we have read a part of the request body -> copy to netbuf */
+                // we have read a part of the request body -> copy to netbuf
                 memcpy(sn->sn.inbuf->inbuf, nb->inbuf+nb->pos, cur_input_len);
             }
 
@@ -297,6 +295,16 @@
     hd->len++;
 }
 
+void header_array_free(HeaderArray *hd) {
+    HeaderArray *next;
+    while(hd) {
+        next = hd->next;
+        free(hd->headers);
+        free(hd);
+        hd = next;
+    }
+}
+
 
 /*
  * NSAPI Processing
@@ -383,8 +391,17 @@
 }
 
 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
-    // TODO: free memory
-    close(sn->sys_fd);
+    // TODO: keep alive
+    close(sn->connection->fd);
+    
+    // free all memory
+    free(sn->connection);
+    
+    free(sn->netbuf->inbuf);
+    free(sn->netbuf);
+    
+    pool_destroy(sn->sn.pool);
+    
 
     return 0;
 }

mercurial