From 646d1e1c50ad972e8098c2b23e531d58a043c181 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Fri, 16 Mar 2012 09:06:13 +0100 Subject: [PATCH] Added a new abstraction: hash. A a lot of things within http are key/value things based on stings i created this generic hash class and use it to store the header right now. In future it will also be used to store cookie, get and post vars --- include/hash.h | 20 +++ include/http/header.h | 2 - include/http/message.h | 4 +- .../get.c => include/interface/hashable.h | 38 +++--- src/Makefile.am | 10 +- src/hash.c | 42 +++++++ src/hash/add.c | 32 +++++ src/hash/delete.c | 25 ++++ src/hash/each.c | 25 ++++ src/hash/get.c | 25 ++++ src/http/header.c | 31 ++++- src/http/header/add.c | 69 ----------- src/http/message.c | 20 +-- src/http/message/has_keep_alive.c | 3 +- src/http/message/header_size_get.c | 9 +- src/http/message/header_to_string.c | 11 +- src/http/parser/header.c | 4 +- src/http/response/304.c | 7 +- src/http/response/404.c | 3 +- src/http/response/asset.c | 8 +- src/http/response/login_form.c | 3 +- src/http/response/me.c | 116 ------------------ src/http/response/randval.c | 3 +- src/http/response/session.c | 3 +- src/http/worker/add_common_header.c | 14 +-- src/http/worker/get_asset.c | 5 +- src/http/worker/process.c | 9 +- src/interface/hashable.c | 51 ++++++++ 28 files changed, 325 insertions(+), 267 deletions(-) create mode 100644 include/hash.h rename src/http/header/get.c => include/interface/hashable.h (54%) create mode 100644 src/hash.c create mode 100644 src/hash/add.c create mode 100644 src/hash/delete.c create mode 100644 src/hash/each.c create mode 100644 src/hash/get.c delete mode 100644 src/http/header/add.c delete mode 100644 src/http/response/me.c create mode 100644 src/interface/hashable.c diff --git a/include/hash.h b/include/hash.h new file mode 100644 index 0000000..0738df9 --- /dev/null +++ b/include/hash.h @@ -0,0 +1,20 @@ +#ifndef __HASH_H__ +#define __HASH_H__ + +#include + +#include "class.h" + + +CLASS(Hash) { + void * root; +}; + +void * hashAdd(Hash, void *); +void * hashDelete(Hash, const char *, size_t); +void * hashGet(Hash, const char *, size_t); +void hashEach(Hash, void (*)(const void*)); + +#endif // __HASH_H__ + +// vim: set ts=4 sw=4: diff --git a/include/http/header.h b/include/http/header.h index eac9dd8..fbd7ee8 100644 --- a/include/http/header.h +++ b/include/http/header.h @@ -41,8 +41,6 @@ CLASS(HttpHeader) { size_t size; //!< full size of this header }; -HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader); -HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t); size_t httpHeaderToString(HttpHeader, char *); #endif // __HTTP_HEADER_H__ diff --git a/include/http/message.h b/include/http/message.h index 8a51e16..5ca44f6 100644 --- a/include/http/message.h +++ b/include/http/message.h @@ -25,7 +25,7 @@ #define __HTTP_MESSAGE__ #include "class.h" -#include "http/header.h" +#include "hash.h" #include "stream.h" typedef enum e_HttpMessageType { @@ -37,7 +37,7 @@ typedef enum e_HttpMessageType { CLASS(HttpMessage) { char * version; - HttpHeader header; + Hash header; HttpMessageType type; Stream handle; diff --git a/src/http/header/get.c b/include/interface/hashable.h similarity index 54% rename from src/http/header/get.c rename to include/interface/hashable.h index 81c24ba..4f71b52 100644 --- a/src/http/header/get.c +++ b/include/interface/hashable.h @@ -1,7 +1,6 @@ /** * \file - * Get a header from a tree containing headers by its name. - * The key for the tree is the hased lowercase header identifier. + * The logger interface. * * \author Georg Hopp * @@ -22,30 +21,25 @@ * along with this program. If not, see . */ -#include -#include +#ifndef __INTERFACE_HASHABLE_H__ +#define __INTERFACE_HASHABLE_H__ -#include "http/header.h" -#include "utils/hash.h" +#include "interface.h" -static -inline -int -comp(const void * _a, const void * _b) -{ - const unsigned long * a = _a; - HttpHeader b = (HttpHeader)_b; - return (*a < b->hash)? -1 : (*a > b->hash)? 1 : 0; -} +typedef unsigned long (* fptr_hashableGetHash)(void *); +typedef void (* fptr_hashableHandleDouble)(void *, void *); -HttpHeader -httpHeaderGet(const HttpHeader * root, const char * name, size_t nname) -{ - unsigned long hash = sdbm((const unsigned char*)name, nname); +extern const struct interface i_Hashable; - HttpHeader * found = tfind(&hash, (void**)root, comp); +struct i_Hashable { + const struct interface * const _; + fptr_hashableGetHash getHash; + fptr_hashableHandleDouble handleDouble; +}; - return (NULL != found)? *found : NULL; -} +extern unsigned long hashableGetHash(void *); +extern void hashableHandleDouble(void *, void *); + +#endif // __INTERFACE_HASHABLE_H__ // vim: set ts=4 sw=4: diff --git a/src/Makefile.am b/src/Makefile.am index 8834d3c..e22e7c6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,12 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ interface/subject.c interface/observer.c interface.c SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c STREAM = stream.c stream/read.c stream/write.c +HASH = hash.c \ + hash/add.c \ + hash/get.c \ + hash/delete.c \ + hash/each.c \ + interface/hashable.c SERVER = server.c server/run.c server/close_conn.c server/poll.c \ server/handle_accept.c server/read.c server/write.c LOGGER = logger.c logger/stderr.c logger/syslog.c @@ -44,7 +50,7 @@ WORKER = http/worker.c \ http/worker/write.c \ http/worker/get_asset.c \ http/worker/add_common_header.c -HEADER = http/header.c http/header/get.c http/header/add.c \ +HEADER = http/header.c \ http/header/to_string.c SESSION = session.c session/add.c session/get.c session/delete.c UTILS = utils/hash.c \ @@ -61,6 +67,6 @@ bin_PROGRAMS = webgameserver webgameserver_SOURCES = webgameserver.c \ $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ - $(UTILS) $(MSGQ) $(SESSION) $(STREAM) + $(UTILS) $(MSGQ) $(SESSION) $(STREAM) $(HASH) webgameserver_CFLAGS = -Wall -I ../include/ webgameserver_LDFLAGS = -lrt -lssl diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..ce26584 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,42 @@ +#define _GNU_SOURCE + +#include +#include + +#include "hash.h" +#include "class.h" +#include "interface/class.h" + +static +int +hashCtor(void * _this, va_list * params) +{ + return 0; +} + +static +inline +void +tDelete(void * node) +{ + delete(node); +} + +static +void +hashDtor(void * _this) +{ + Hash this = _this; + + /** + * this is a GNU extension...anyway on most non + * GNUish systems i would not use tsearch anyway + * as the trees will be unbalanced. + */ + tdestroy(this->root, tDelete); +} + +INIT_IFACE(Class, hashCtor, hashDtor, NULL); +CREATE_CLASS(Hash, NULL, IFACE(Class)); + +// vim: set ts=4 sw=4: diff --git a/src/hash/add.c b/src/hash/add.c new file mode 100644 index 0000000..330d158 --- /dev/null +++ b/src/hash/add.c @@ -0,0 +1,32 @@ +#include + +#include "hash.h" +#include "interface/hashable.h" +#include "interface/class.h" + +static +inline +int +hashAddComp(const void * a, const void * b) +{ + return hashableGetHash((void*)b) - hashableGetHash((void*)a); +} + +void * +hashAdd(Hash this, void * operand) +{ + void * found = tsearch(operand, &(this->root), hashAddComp); + + if (NULL == found) { + return NULL; + } + + if (operand != *(void**)found) { + hashableHandleDouble(*(void**)found, operand); + delete(operand); + } + + return *(void**)found; +} + +// vim: set ts=4 sw=4: diff --git a/src/hash/delete.c b/src/hash/delete.c new file mode 100644 index 0000000..b48f06a --- /dev/null +++ b/src/hash/delete.c @@ -0,0 +1,25 @@ +#include +#include + +#include "hash.h" +#include "interface/hashable.h" +#include "utils/hash.h" + +static +inline +int +hashDeleteComp(const void * a, const void * b) +{ + return hashableGetHash((void*)b) - *(const unsigned long*)a; +} + +void * +hashDelete(Hash this, const char * search, size_t nsearch) +{ + unsigned long hash = sdbm((const unsigned char *)search, nsearch); + void * found = tfind(&hash, &(this->root), hashDeleteComp); + + return (NULL != found)? *(void**)found : NULL; +} + +// vim: set ts=4 sw=4: diff --git a/src/hash/each.c b/src/hash/each.c new file mode 100644 index 0000000..1fa06c7 --- /dev/null +++ b/src/hash/each.c @@ -0,0 +1,25 @@ +#include + +#include "hash.h" + +static void (*cb)(void*); + +static +inline +void +walk(const void * node, const VISIT which, const int depth) +{ + if (endorder == which || leaf == which) { + cb(*(void**)node); + } +} + +void +hashEach(Hash this, void (*callback)(const void*)) +{ + cb = callback; + + twalk(this->root, walk); +} + +// vim: set ts=4 sw=4: diff --git a/src/hash/get.c b/src/hash/get.c new file mode 100644 index 0000000..97b08de --- /dev/null +++ b/src/hash/get.c @@ -0,0 +1,25 @@ +#include +#include + +#include "hash.h" +#include "interface/hashable.h" +#include "utils/hash.h" + +static +inline +int +hashGetComp(const void * a, const void * b) +{ + return hashableGetHash((void*)b) - *(const unsigned long*)a; +} + +void * +hashGet(Hash this, const char * search, size_t nsearch) +{ + unsigned long hash = sdbm((const unsigned char *)search, nsearch); + void * found = tfind(&hash, &(this->root), hashGetComp); + + return (NULL != found)? *(void**)found : NULL; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/header.c b/src/http/header.c index 344ee87..0f66f15 100644 --- a/src/http/header.c +++ b/src/http/header.c @@ -27,6 +27,7 @@ #include "class.h" #include "interface/class.h" #include "http/header.h" +#include "interface/hashable.h" #include "utils/hash.h" #include "utils/memory.h" @@ -72,7 +73,35 @@ httpHeaderDtor(void * _this) } } +static +unsigned long +httpHeaderGetHash(void * _this) +{ + HttpHeader this = _this; + + return this->hash; +} + +static +void +httpHeaderHandleDouble(void * _this, void * _double) +{ + HttpHeader this = _this; + HttpHeader doub = _double; + + if (this->cvalue >= N_VALUES) { + //! \todo do dome error logging...or change to HEAP + return; + } + + (this->nvalue)[this->cvalue] = (doub->nvalue)[0]; + (this->value)[(this->cvalue)++] = (doub->value)[0]; + this->size += doub->size; + (doub->value)[0] = NULL; +} + INIT_IFACE(Class, httpHeaderCtor, httpHeaderDtor, NULL); -CREATE_CLASS(HttpHeader, NULL, IFACE(Class)); +INIT_IFACE(Hashable, httpHeaderGetHash, httpHeaderHandleDouble); +CREATE_CLASS(HttpHeader, NULL, IFACE(Class), IFACE(Hashable)); // vim: set ts=4 sw=4: diff --git a/src/http/header/add.c b/src/http/header/add.c deleted file mode 100644 index b8eda6a..0000000 --- a/src/http/header/add.c +++ /dev/null @@ -1,69 +0,0 @@ -/** - * \file - * - * \author Georg Hopp - * - * \copyright - * Copyright (C) 2012 Georg Hopp - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include - -#include "class.h" -#include "interface/class.h" -#include "http/header.h" -#include "utils/hash.h" - -static -inline -int -comp(const void * _a, const void * _b) -{ - HttpHeader a = (HttpHeader)_a; - HttpHeader b = (HttpHeader)_b; - return (a->hash < b->hash)? -1 : (a->hash > b->hash)? 1 : 0; -} - -HttpHeader -httpHeaderAdd(const HttpHeader * root, HttpHeader header) -{ - HttpHeader * _found = tsearch(header, (void **)root, comp); - HttpHeader found; - - if (NULL == _found) { - return NULL; - } - - found = *_found; - - if (found != header) { - if (found->cvalue >= N_VALUES) { - return NULL; - } - (found->nvalue)[found->cvalue] = (header->nvalue)[0]; - (found->value)[(found->cvalue)++] = (header->value)[0]; - found->size += header->size; - (header->value)[0] = NULL; - delete(header); - } - - return found; -} - -// vim: set ts=4 sw=4: diff --git a/src/http/message.c b/src/http/message.c index 3b3c063..747ae55 100644 --- a/src/http/message.c +++ b/src/http/message.c @@ -32,19 +32,11 @@ #include "class.h" #include "interface/class.h" - +#include "hash.h" #include "http/message.h" #include "utils/memory.h" -static -inline -void -tDelete(void * node) -{ - delete(node); -} - static int httpMessageCtor(void * _this, va_list * params) @@ -55,6 +47,8 @@ httpMessageCtor(void * _this, va_list * params) this->version = calloc(1, strlen(version)+1); strcpy(this->version, version); + this->header = new(Hash); + return 0; } @@ -64,15 +58,9 @@ httpMessageDtor(void * _this) { HttpMessage this = _this; + delete(this->header); FREE(this->version); - /** - * this is a GNU extension...anyway on most non - * GNUish systems i would not use tsearch anyway - * as the trees will be unbalanced. - */ - tdestroy(this->header, tDelete); - switch (this->type) { case HTTP_MESSAGE_BUFFERED: FREE(this->body); diff --git a/src/http/message/has_keep_alive.c b/src/http/message/has_keep_alive.c index 24623e4..d6aab1d 100644 --- a/src/http/message/has_keep_alive.c +++ b/src/http/message/has_keep_alive.c @@ -31,6 +31,7 @@ #include "utils/memory.h" #include "commons.h" +#include "hash.h" char httpMessageHasKeepAlive(HttpMessage message) @@ -39,7 +40,7 @@ httpMessageHasKeepAlive(HttpMessage message) size_t size; char * value; - header = httpHeaderGet(&(message->header), CSTRA("connection")); + header = hashGet(message->header, CSTRA("connection")); if (NULL == header) { return 0; diff --git a/src/http/message/header_size_get.c b/src/http/message/header_size_get.c index e46ab25..dc0a70d 100644 --- a/src/http/message/header_size_get.c +++ b/src/http/message/header_size_get.c @@ -28,17 +28,16 @@ #include "http/response.h" #include "http/header.h" #include "interface/http_intro.h" +#include "hash.h" static size_t size; static inline void -addHeaderSize(const void * node, const VISIT which, const int depth) +addHeaderSize(const void * node) { - if (endorder == which || leaf == which) { - size += (*(HttpHeader *)node)->size; - } + size += ((HttpHeader)node)->size; } size_t @@ -46,7 +45,7 @@ httpMessageHeaderSizeGet(HttpMessage message) { size = httpIntroSizeGet(message); - twalk(message->header, addHeaderSize); + hashEach(message->header, addHeaderSize); size += 2; return size; diff --git a/src/http/message/header_to_string.c b/src/http/message/header_to_string.c index ba223ab..a4a8707 100644 --- a/src/http/message/header_to_string.c +++ b/src/http/message/header_to_string.c @@ -27,15 +27,16 @@ #include "http/response.h" #include "http/header.h" #include "interface/http_intro.h" +#include "hash.h" static char * string; +static +inline void -addHeaderString(const void * node, const VISIT which, const int depth) +addHeaderString(const void * node) { - if (endorder == which || leaf == which) { - string += httpHeaderToString(*(HttpHeader *)node, string); - } + string += httpHeaderToString((HttpHeader)node, string); } char * @@ -45,7 +46,7 @@ httpMessageHeaderToString(HttpMessage response, char * _string) string = httpIntroToString(response, _string); - twalk(message->header, addHeaderString); + hashEach(message->header, addHeaderString); *string++ = '\r'; *string++ = '\n'; diff --git a/src/http/parser/header.c b/src/http/parser/header.c index d6b48cf..6b2f149 100644 --- a/src/http/parser/header.c +++ b/src/http/parser/header.c @@ -29,6 +29,7 @@ #include "http/header.h" #include "http/parser.h" #include "http/message.h" +#include "hash.h" #define MAX(x,y) ((x) > (y) ? (x) : (y)) @@ -66,8 +67,7 @@ httpParserHeader( current->dbody = 0; } - httpHeaderAdd( - &(current->header), + hashAdd(current->header, new(HttpHeader, name, nname, value, lend - value)); } diff --git a/src/http/response/304.c b/src/http/response/304.c index 28fa1aa..694a4da 100644 --- a/src/http/response/304.c +++ b/src/http/response/304.c @@ -30,6 +30,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" HttpResponse httpResponse304( @@ -47,11 +48,11 @@ httpResponse304( message->nbody = 0; message->body = NULL; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("ETag"), etag, netag)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); return response; diff --git a/src/http/response/404.c b/src/http/response/404.c index eb76df7..e80c0f9 100644 --- a/src/http/response/404.c +++ b/src/http/response/404.c @@ -33,6 +33,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "\n" \ "header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/asset.c b/src/http/response/asset.c index d5a0b92..d7e4b6f 100644 --- a/src/http/response/asset.c +++ b/src/http/response/asset.c @@ -35,7 +35,7 @@ #include "http/header.h" #include "utils/memory.h" - +#include "hash.h" HttpResponse httpResponseAsset( @@ -73,11 +73,11 @@ httpResponseAsset( message->handle = new(Stream, STREAM_FD, handle); message->nbody = st.st_size; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("ETag"), etag, netag)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); return response; diff --git a/src/http/response/login_form.c b/src/http/response/login_form.c index c2f790b..4086565 100644 --- a/src/http/response/login_form.c +++ b/src/http/response/login_form.c @@ -34,6 +34,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "
" \ "" \ @@ -49,7 +50,7 @@ httpResponseLoginForm() response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/me.c b/src/http/response/me.c deleted file mode 100644 index 741f5a9..0000000 --- a/src/http/response/me.c +++ /dev/null @@ -1,116 +0,0 @@ -/** - * \file - * - * \author Georg Hopp - * - * \copyright - * Copyright (C) 2012 Georg Hopp - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include - -#include "class.h" -#include "interface/class.h" - -#include "http/response.h" -#include "http/message.h" -#include "http/header.h" - -#include "utils/memory.h" - - -#define RESP_DATA "\n" \ - "\n" \ - "" \ - "" \ - "My own little Web-App" \ - "" \ - "" \ - "" \ - "" \ - "" \ - "" - "
    " \ - "
  • serverval
  • " \ - "
" \ - "
" \ - "" \ - "Value created at:

" \ - "Next value in:
" \ - "
" \ - "Value: " \ - "
" \ - "
" \ - "

Testpage

" \ - "Welcome %s
" \ - "" \ - "
" \ - "
" \ - "" \ - "" - -HttpResponse -httpResponseMe(char * uname) -{ - HttpResponse response; - HttpMessage message; - - response = new(HttpResponse, "HTTP/1.1", 200, "OK"); - message = (HttpMessage)response; - - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("name=Georg+Hopp"))); - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("profession=coder"))); - - message->type = HTTP_MESSAGE_BUFFERED; - message->nbody = sizeof(RESP_DATA)-1-2+strlen(uname); //!< the two are the %s - message->body = malloc(message->nbody+1); - sprintf(message->body, RESP_DATA, uname); - //memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)-1); - - return response; -} - -// vim: set ts=4 sw=4: diff --git a/src/http/response/randval.c b/src/http/response/randval.c index 059effa..a01b2a4 100644 --- a/src/http/response/randval.c +++ b/src/http/response/randval.c @@ -34,6 +34,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}" @@ -49,7 +50,7 @@ httpResponseRandval(time_t ctime, int value) response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/session.c b/src/http/response/session.c index c1b15f8..fd9b56e 100644 --- a/src/http/response/session.c +++ b/src/http/response/session.c @@ -35,6 +35,7 @@ #include "session.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "{\"id\":\"%lu\",\"timeout\":%d,\"timeleft\":%ld,\"username\":\"%s\"}" @@ -49,7 +50,7 @@ httpResponseSession(Session session) response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/worker/add_common_header.c b/src/http/worker/add_common_header.c index 36fbf6d..6c9d8bd 100644 --- a/src/http/worker/add_common_header.c +++ b/src/http/worker/add_common_header.c @@ -5,9 +5,11 @@ #include "interface/class.h" #include "http/message.h" +#include "http/header.h" #include "http/response.h" #include "utils/memory.h" +#include "hash.h" void httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) @@ -18,17 +20,15 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) size_t nbuf; if (httpMessageHasKeepAlive(request)) { - httpHeaderAdd( - &(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Connection"), CSTRA("Keep-Alive"))); } else { - httpHeaderAdd( - &(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Connection"), CSTRA("Close"))); } - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Server"), CSTRA("testserver"))); switch(((HttpResponse)response)->status) { @@ -37,14 +37,14 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) default: nbuf = sprintf(buffer, "%d", response->nbody); - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf)); } t = time(NULL); tmp = localtime(&t); nbuf = strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Date"), buffer, nbuf)); } diff --git a/src/http/worker/get_asset.c b/src/http/worker/get_asset.c index 180729b..7ea6a52 100644 --- a/src/http/worker/get_asset.c +++ b/src/http/worker/get_asset.c @@ -6,6 +6,7 @@ #include "http/response.h" #include "utils/memory.h" +#include "hash.h" HttpMessage httpWorkerGetAsset( @@ -18,8 +19,8 @@ httpWorkerGetAsset( size_t nmatch; HttpHeader header; - header = httpHeaderGet( - &(((HttpMessage)request)->header), + header = hashGet( + ((HttpMessage)request)->header, CSTRA("If-None-Match")); if (NULL == header) { diff --git a/src/http/worker/process.c b/src/http/worker/process.c index 0a3d3d8..f328eca 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -30,6 +30,7 @@ #include "interface/class.h" #include "http/worker.h" +#include "http/header.h" #include "http/message.h" #include "http/request.h" #include "http/response.h" @@ -39,6 +40,7 @@ #include "stream.h" #include "utils/memory.h" +#include "hash.h" HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); @@ -60,8 +62,8 @@ httpWorkerProcess(HttpWorker this, Stream st) HttpMessage rmessage = reqq->msgs[i]; HttpRequest request = (HttpRequest)(reqq->msgs[i]); HttpMessage response = NULL; - HttpHeader cookie = httpHeaderGet( - &(rmessage->header), + HttpHeader cookie = hashGet( + rmessage->header, CSTRA("cookie")); if (NULL == this->session && NULL != cookie) { @@ -111,8 +113,7 @@ httpWorkerProcess(HttpWorker this, Stream st) response = (HttpMessage)httpResponseSession(this->session); - httpHeaderAdd( - &(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Set-Cookie"), buffer, nbuf)); } } diff --git a/src/interface/hashable.c b/src/interface/hashable.c new file mode 100644 index 0000000..7e4b280 --- /dev/null +++ b/src/interface/hashable.c @@ -0,0 +1,51 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include "class.h" +#include "interface/hashable.h" + +const struct interface i_Hashable = { + "hashable", + 2 +}; + +unsigned long +hashableGetHash(void * hashable) +{ + unsigned long ret; + + RETCALL(hashable, Hashable, getHash, ret); + + return ret; +} + +void +hashableHandleDouble(void * hashable, void * new_hashable) +{ + CALL(hashable, Hashable, handleDouble, new_hashable); +} + +// vim: set ts=4 sw=4: