From 1c5d6099f584fc1b510a69e70d9027c64d727c46 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 9 Feb 2012 11:32:28 +0100 Subject: [PATCH] add missing header_get to repo and build header hash only from lowercase letters now as it seems header identifier should be case insensitive --- ChangeLog | 6 ++++- src/http/request/header_get.c | 46 +++++++++++++++++++++++++++++++++++ src/http/request_parser.c | 2 +- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/http/request/header_get.c diff --git a/ChangeLog b/ChangeLog index 367e858..828bfa3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,10 @@ +2012-02-09 11:32:28 +0100 Georg Hopp + + * add missing header_get to repo and build header hash only from lowercase letters now as it seems header identifier should be case insensitive (HEAD, master) + 2012-02-09 09:34:21 +0100 Georg Hopp - * access to headers via hash, read body (actually only with content-length header should also look for content-encoding) (HEAD, master) + * access to headers via hash, read body (actually only with content-length header should also look for content-encoding) 2012-02-08 16:51:49 +0100 Georg Hopp diff --git a/src/http/request/header_get.c b/src/http/request/header_get.c new file mode 100644 index 0000000..0d73755 --- /dev/null +++ b/src/http/request/header_get.c @@ -0,0 +1,46 @@ +#include +#include + +#include "http/request.h" + +static +inline +unsigned long +sdbm(const unsigned char * str) +{ + unsigned long hash = 0; + int c; + + while ((c = tolower(*str++))) + hash = c + (hash << 6) + (hash << 16) - hash; + + return hash; +} + +static +inline +int +comp (const void * _a, const void * _b) +{ + unsigned long a = *(unsigned long *)_a; + const struct HttpRequestHeader * b = _b; + return (a < b->hash)? -1 : (a > b->hash)? 1 : 0; +} + +char * +httpRequestHeaderGet(HttpRequest this, const char * name) +{ + unsigned long hash = sdbm((unsigned char *)name); + struct HttpRequestHeader * header; + + header = bsearch( + &hash, + this->header, + this->nheader, + sizeof(struct HttpRequestHeader), + comp); + + return (NULL != header)? header->value : NULL; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/request_parser.c b/src/http/request_parser.c index 4df71ce..2976c09 100644 --- a/src/http/request_parser.c +++ b/src/http/request_parser.c @@ -150,7 +150,7 @@ sdbm(unsigned char * str) unsigned long hash = 0; int c; - while ((c = *str++)) + while ((c = tolower(*str++))) hash = c + (hash << 6) + (hash << 16) - hash; return hash;