From e7553ea2187eaab0ae20b2188df060f49b46f394 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 20 Mar 2012 15:58:59 +0100 Subject: [PATCH] add cookie request parsing and fix post (form vars) parsing. User new parsed cookies within worker/process --- src/http/parser/header.c | 38 +++++++++++++++++++++++++++++++++++++ src/http/parser/post_vars.c | 1 + src/http/worker/process.c | 27 +++++++++++--------------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/http/parser/header.c b/src/http/parser/header.c index 6b2f149..eb56010 100644 --- a/src/http/parser/header.c +++ b/src/http/parser/header.c @@ -29,7 +29,9 @@ #include "http/header.h" #include "http/parser.h" #include "http/message.h" +#include "http/request.h" #include "hash.h" +#include "hash_value.h" #define MAX(x,y) ((x) > (y) ? (x) : (y)) @@ -67,6 +69,42 @@ httpParserHeader( current->dbody = 0; } + if (0 == strncasecmp("cookie", name, nname-1)) { + HttpRequest request = (HttpRequest)this->current; + char * pair = value; + size_t togo = lend - value; + + while(NULL != pair && 0 < togo) { + char * key = pair; + char * eqsign; + char * val; + size_t nval; + + for (; *key == ' ' && key < lend; key++, togo--); + eqsign = memchr(key, '=', togo); + + if (NULL == eqsign) { + break; + } + + togo -= (eqsign - key); + pair = memchr(eqsign, ';', togo); + + if (NULL == pair) { + pair = (char *)lend; + } + + nval = pair-eqsign-1; + val = (0 != nval)? eqsign+1 : NULL; + + hashAdd(request->cookies, + new(HashValue, key, eqsign-key, val, nval)); + + togo -= (pair - eqsign); + pair++; + } + } + hashAdd(current->header, new(HttpHeader, name, nname, value, lend - value)); } diff --git a/src/http/parser/post_vars.c b/src/http/parser/post_vars.c index df8f42c..9fe6d81 100644 --- a/src/http/parser/post_vars.c +++ b/src/http/parser/post_vars.c @@ -43,6 +43,7 @@ httpParserPostVars(HttpParser this) new(HashValue, key, eqsign-key, value, nvalue)); togo -= (pair - eqsign); + pair++; } } diff --git a/src/http/worker/process.c b/src/http/worker/process.c index 4693c14..0b55939 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -64,25 +64,20 @@ httpWorkerProcess(HttpWorker this, Stream st) HttpMessage rmessage = reqq->msgs[i]; HttpRequest request = (HttpRequest)(reqq->msgs[i]); HttpMessage response = NULL; - HttpHeader cookie = hashGet( - rmessage->header, - CSTRA("cookie")); - if (NULL == this->session && NULL != cookie) { - int i; + /** + * \todo store the cookie count in the request to make a simple + * check possible to prevent this lookup if no cookies exists + * at all + */ + if (NULL == this->session) { + HashValue sidstr = hashGet(request->cookies, CSTRA("sid")); - for (i=0; icvalue; i++) { - char * sidstr = strstr(cookie->value[i], "sid"); + if (NULL != sidstr) { + unsigned long sid; - if (NULL != sidstr) { - unsigned long sid; - - sidstr = strchr(sidstr, '=')+1; - sid = strtoul(sidstr, NULL, 10); - - this->session = sessionGet(this->sroot, sid); - break; - } + sid = strtoul((char*)(sidstr->value), NULL, 10); + this->session = sessionGet(this->sroot, sid); } }