diff --git a/ChangeLog b/ChangeLog index 9a06e75..6fc3273 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,18 @@ +2012-02-23 13:02:23 +0100 Georg Hopp + + * changed all string operation within header handling with fixed length mem operations, preventing multiple iterations over these strings. In theory this should improve performance in reality it seems that it is worse...CHECK WHY (HEAD, master) + +2012-02-23 00:05:25 +0100 Georg Hopp + + * fix initialization of search value + +2012-02-22 21:50:21 +0100 Georg Hopp + + * ed + 2012-02-22 21:49:52 +0100 Georg Hopp - * structural changes for worker/process. @TODO actually i have no idea why this happens. (HEAD, master) + * structural changes for worker/process. @TODO actually i have no idea why this happens. 2012-02-22 12:19:40 +0100 Georg Hopp diff --git a/include/cbuf.h b/include/cbuf.h index c79e6e3..13ffbfa 100644 --- a/include/cbuf.h +++ b/include/cbuf.h @@ -62,7 +62,7 @@ CLASS(Cbuf) { ssize_t cbufRead(Cbuf, int fd); ssize_t cbufWrite(Cbuf, int fd); -char * cbufGetLine(Cbuf); +char * cbufGetLine(Cbuf, char **); char * cbufGetData(Cbuf, size_t); char * cbufSetData(Cbuf, const void *, size_t); void cbufEmpty(Cbuf); diff --git a/include/http/header.h b/include/http/header.h index bcfc480..7959800 100644 --- a/include/http/header.h +++ b/include/http/header.h @@ -25,21 +25,25 @@ #ifndef __HTTP_HEADER_H__ #define __HTTP_HEADER_H__ +#include + #include "class.h" -#define HTTP_HEADER_VALUE_CHUNK_SIZE 128 +#define N_VALUES 128 CLASS(HttpHeader) { unsigned long hash; char * name; - char * value[HTTP_HEADER_VALUE_CHUNK_SIZE]; - size_t nvalue; + char * value[N_VALUES]; + size_t nname; //!< len of name without \0 + size_t nvalue[N_VALUES]; //!< len of value without \0 + size_t cvalue; //!< count of values up to N_VALUE }; HttpHeader httpHeaderParse(char * line); // @INFO: destructive HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader); -HttpHeader httpHeaderGet(const HttpHeader *, const char *); +HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t); size_t httpHeaderSizeGet(HttpHeader); size_t httpHeaderToString(HttpHeader, char *); diff --git a/include/http/request/parser.h b/include/http/request/parser.h index 165bf9f..2adb383 100644 --- a/include/http/request/parser.h +++ b/include/http/request/parser.h @@ -63,9 +63,8 @@ CLASS(HttpRequestParser) { ssize_t httpRequestParserParse(HttpRequestParser, int); void httpRequestParserGetBody(HttpRequestParser); -ssize_t httpRequestParserGetRequestLine(HttpRequestParser, char *); -ssize_t httpRequestParserGetHeader(HttpRequestParser, char *); -void httpRequestParserGetBody(HttpRequestParser); +void httpRequestParserGetRequestLine(HttpRequest, char *); +void httpRequestParserGetHeader(HttpRequest, char *, char *); #endif // __HTTP_REQUEST_PARSER_H__ diff --git a/include/http/response.h b/include/http/response.h index 09f5277..1178897 100644 --- a/include/http/response.h +++ b/include/http/response.h @@ -38,10 +38,16 @@ CLASS(HttpResponse) { char * reason; }; -HttpResponse httpResponse304(const char *, const char *, const char *); +HttpResponse httpResponse304( + const char *, size_t, + const char *, size_t, + const char *, size_t); HttpResponse httpResponse404(); HttpResponse httpResponseMe(int); -HttpResponse httpResponseAsset(const char *, const char *, const char *); +HttpResponse httpResponseAsset( + const char *, + const char *, size_t, + const char *, size_t); #endif // __HTTP_RESPONSE_H__ diff --git a/include/utils/hash.h b/include/utils/hash.h index cd2345d..a247590 100644 --- a/include/utils/hash.h +++ b/include/utils/hash.h @@ -24,7 +24,9 @@ #ifndef __UTILS_HASH_H__ #define __UTILS_HASH_H__ -unsigned long sdbm(const unsigned char *); +#include + +unsigned long sdbm(const unsigned char *, size_t); #endif // __UTILS_HASH_H__ diff --git a/src/cbuf/get_line.c b/src/cbuf/get_line.c index 030fb2d..526c22a 100644 --- a/src/cbuf/get_line.c +++ b/src/cbuf/get_line.c @@ -27,7 +27,7 @@ #include "cbuf.h" char * -cbufGetLine(Cbuf this) +cbufGetLine(Cbuf this, char ** line_end) { char * nl = cbufMemchr(this, '\n'); char * ret = NULL; @@ -35,8 +35,9 @@ cbufGetLine(Cbuf this) if (NULL != nl) { size_t len = cbufAddrIndex(this, nl) + 1; - *nl = 0; - *(nl-1) = ('\r' == *(nl-1))? 0 : *(nl-1); + *line_end = nl - 1; + *nl = 0; + *(nl-1) = ('\r' == *(nl-1))? 0 : *(nl-1); ret = cbufGetRead(this); cbufIncRead(this, len); diff --git a/src/http/header.c b/src/http/header.c index 7ba5ca5..d732613 100644 --- a/src/http/header.c +++ b/src/http/header.c @@ -38,16 +38,19 @@ httpHeaderCtor(void * _this, va_list * params) { char * name; char * value; - name = va_arg(* params, char *); - value = va_arg(* params, char *); + name = va_arg(* params, char *); + this->nname = va_arg(* params, size_t); + value = va_arg(* params, char *); + this->nvalue[0] = va_arg(* params, size_t); - this->name = malloc(strlen(name) + 1); - strcpy(this->name, name); + this->name = malloc(this->nname); + memcpy(this->name, name, this->nname); - this->hash = sdbm((unsigned char *)name); + this->hash = sdbm((unsigned char *)name, this->nname); - (this->value)[this->nvalue] = malloc(strlen(value) + 1); - strcpy((this->value)[this->nvalue++], value); + (this->value)[0] = malloc((this->nvalue)[0]); + memcpy((this->value)[0], value, (this->nvalue)[0]); + this->cvalue = 1; return 0; } @@ -61,7 +64,7 @@ httpHeaderDtor(void * _this) FREE(this->name); - for (i=0; invalue; i++) { + for (i=0; icvalue; i++) { FREE(this->value[i]); } } diff --git a/src/http/header/add.c b/src/http/header/add.c index 0adb69c..d69a38a 100644 --- a/src/http/header/add.c +++ b/src/http/header/add.c @@ -43,18 +43,26 @@ comp(const void * _a, const void * _b) HttpHeader httpHeaderAdd(const HttpHeader * root, HttpHeader header) { - HttpHeader * found = tsearch(header, (void **)root, comp); + HttpHeader * _found = tsearch(header, (void **)root, comp); + HttpHeader found; + + if (NULL == _found) { + return NULL; + } + + found = *_found; - if (*found != header) { - if ((*found)->nvalue >= HTTP_HEADER_VALUE_CHUNK_SIZE) { + if (found != header) { + if (found->cvalue >= N_VALUES) { return NULL; } - (*found)->value[(*found)->nvalue++] = (header->value)[0]; + (found->nvalue)[found->cvalue] = (header->nvalue)[0]; + (found->value)[(found->cvalue)++] = (header->value)[0]; (header->value)[0] = NULL; delete(header); } - return *found; + return found; } // vim: set ts=4 sw=4: diff --git a/src/http/header/get.c b/src/http/header/get.c index 3a56737..4534263 100644 --- a/src/http/header/get.c +++ b/src/http/header/get.c @@ -39,10 +39,15 @@ comp(const void * _a, const void * _b) } HttpHeader -httpHeaderGet(const HttpHeader * root, const char * name) +httpHeaderGet(const HttpHeader * root, const char * name, size_t nname) { - struct c_HttpHeader search = {sdbm((const unsigned char*)name), NULL, {}, 0}; - HttpHeader * found = tfind(&search, (void**)root, comp); + struct c_HttpHeader search = { + sdbm((const unsigned char*)name, nname), + NULL, + {}, + 0}; + + HttpHeader * found = tfind(&search, (void**)root, comp); return (NULL != found)? *found : NULL; } diff --git a/src/http/header/size_get.c b/src/http/header/size_get.c index 0f5656a..801a07f 100644 --- a/src/http/header/size_get.c +++ b/src/http/header/size_get.c @@ -28,12 +28,12 @@ size_t httpHeaderSizeGet(HttpHeader header) { - size_t nsize = strlen(header->name) + 2; - size_t size = header->nvalue * nsize; + size_t nsize = header->nname + 2; + size_t size = header->cvalue * nsize; int i; - for (i=0; invalue; i++) { - size += strlen(header->value[i]) + 2; + for (i=0; icvalue; i++) { + size += (header->nvalue)[i] + 2; } return size; diff --git a/src/http/header/to_string.c b/src/http/header/to_string.c index 8b52596..1c6675a 100644 --- a/src/http/header/to_string.c +++ b/src/http/header/to_string.c @@ -31,15 +31,15 @@ httpHeaderToString(HttpHeader header, char * string) size_t size = httpHeaderSizeGet(header); int i; - for (i=0; invalue; i++) { - strcpy(string, header->name); - string += strlen(string); + for (i=0; icvalue; i++) { + memcpy(string, header->name, header->nname); + string += header->nname; *string++ = ':'; *string++ = ' '; - strcpy(string, header->value[i]); - string += strlen(string); + memcpy(string, header->value[i], header->nvalue[i]); + string += header->nvalue[i]; *string++ = '\r'; *string++ = '\n'; diff --git a/src/http/message/has_keep_alive.c b/src/http/message/has_keep_alive.c index aa30d13..652cf5d 100644 --- a/src/http/message/has_keep_alive.c +++ b/src/http/message/has_keep_alive.c @@ -21,6 +21,7 @@ */ #include +#include #include #include "http/message.h" @@ -32,20 +33,30 @@ char httpMessageHasKeepAlive(HttpMessage message) { HttpHeader header; - char * con; + size_t size; + char * value; + char * keep_alive = "keep-alive"; - header = httpHeaderGet(&(message->header), "connection"); + header = httpHeaderGet( + &(message->header), + "connection", + sizeof("connection")-1); if (NULL == header) { return 0; } - con = (header->value)[0]; - for (; 0 != *con; con++) { - *con = tolower(*con); + if ((sizeof("keep-alive")-1) != (header->nvalue)[0]) { + return 0; } - if (0 == strcmp((header->value)[0], "keep-alive")) { + size = (header->nvalue)[0]; + value = (header->value)[0]; + + for (; 0 < size && tolower(*value) == *keep_alive; + size--, value++, keep_alive++); + + if (0 == size) { return 1; } diff --git a/src/http/request/parser/get_body.c b/src/http/request/parser/get_body.c index f9004c9..a6477b0 100644 --- a/src/http/request/parser/get_body.c +++ b/src/http/request/parser/get_body.c @@ -45,7 +45,8 @@ httpRequestParserGetBody(HttpRequestParser this) if (0 == message->nbody) { HttpHeader clen = httpHeaderGet( &(message->header), - "Content-Length"); + "Content-Length", + sizeof("Content-Length")-1); if (NULL == clen) { this->state = HTTP_REQUEST_DONE; diff --git a/src/http/request/parser/get_header.c b/src/http/request/parser/get_header.c index 9bf2696..84835bd 100644 --- a/src/http/request/parser/get_header.c +++ b/src/http/request/parser/get_header.c @@ -21,6 +21,7 @@ */ #include +#include #include "class.h" #include "interface/class.h" @@ -30,28 +31,20 @@ #include "ringbuffer.h" void -httpRequestParserGetHeader(HttpMessage message, char * line) +httpRequestParserGetHeader( + HttpMessage message, + const char * line, + const char * line_end) { - HttpMessage message = (HttpMessage)this->cur_request; - char * value; - char * name = this->buffer->buffer + this->buffer->bstart; - size_t len = cr - name; + char * name = line; + char * value = strchr(line, ':'); + size_t nname = (value++) - name; - value = memchr( - this->buffer->buffer + this->buffer->bstart, - ':', len); + for (; *value == ' ' && *value != 0; value++); - if (NULL == value) { - return -1; - } - - *cr = 0; - *(value++) = 0; - while(' ' == *value) value++; - - httpHeaderAdd(&(message->header), new(HttpHeader, name, value)); - - httpHeaderAdd(&(message->header), new(HttpHeader, name, value)); + httpHeaderAdd( + &(message->header), + new(HttpHeader, name, nname, value, line_end-value)); } // vim: set ts=4 sw=4: diff --git a/src/http/request/parser/get_request_line.c b/src/http/request/parser/get_request_line.c index 538b2df..51a26e0 100644 --- a/src/http/request/parser/get_request_line.c +++ b/src/http/request/parser/get_request_line.c @@ -60,8 +60,9 @@ char * method[N_METHODS] = { ssize_t httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) { - char * method, * uri, * version; + char * method, * uri, * version; HttpMessage message = (HttpMessage)request; + int mlen, ulen, vlen; method = line; uri = strchr(line, ' '); @@ -69,23 +70,25 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) if (NULL == uri) return; - *uri++ = 0; + mlen = uri - method; for (; *uri == ' ' && *uri != 0; uri++); - version = strchr(uri, ' '); + version = strchr(uri, ' '); if (NULL == version) return; - *version++ = 0; + ulen = version - uri; for (; *version == ' ' && *version != 0; version++); - request->method = malloc(strlen(method) + 1); - strcpy(request->method, method); - request->uri = malloc(strlen(uri) + 1); - strcpy(request->uri, uri); - message->version = malloc(strlen(version) + 1); - strcpy(message->version, version); + vlen = strlen(version); + + request->method = calloc(1, mlen + 1); + memcpy(request->method, method, mlen); + request->uri = calloc(1, ulen + 1); + memcpy(request->uri, uri, ulen); + message->version = calloc(1, vlen + 1); + memcpy(message->version, version, vlen); } // vim: set ts=4 sw=4: diff --git a/src/http/request/parser/parse.c b/src/http/request/parser/parse.c index 709a06e..c7788b1 100644 --- a/src/http/request/parser/parse.c +++ b/src/http/request/parser/parse.c @@ -34,6 +34,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) int cont = 1; ssize_t read; char * line; + char * line_end; if (cbufIsLocked(this->buffer)) { if (FALSE == this->ourLock) @@ -71,7 +72,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) break; case HTTP_REQUEST_START: - if (NULL == (line = cbufGetLine(this->buffer))) { + if (NULL == (line = cbufGetLine(this->buffer, &line_end))) { if (! cbufIsEmpty(this->buffer)) { this->isize = this->buffer->bused; this->incomplete = malloc(this->isize); @@ -101,7 +102,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) break; case HTTP_REQUEST_REQUEST_LINE_DONE: - if (NULL == (line = cbufGetLine(this->buffer))) { + if (NULL == (line = cbufGetLine(this->buffer, &line_end))) { if (! cbufIsEmpty(this->buffer)) { this->isize = this->buffer->bused; this->incomplete = malloc(this->isize); @@ -126,15 +127,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) break; } - httpRequestParserGetHeader(this, line_end); - - len = line_end - this->buffer->buffer - this->buffer->bstart + 2; - this->buffer->bstart += len; - if (this->buffer->bstart >= this->buffer->bsize) { - this->buffer->bstart -= this->buffer->bsize; - } - this->buffer->bused -= len; - + httpRequestParserGetHeader(this->cur_request, line, line_end); break; case HTTP_REQUEST_HEADERS_DONE: diff --git a/src/http/response/304.c b/src/http/response/304.c index 25d469c..b1a591b 100644 --- a/src/http/response/304.c +++ b/src/http/response/304.c @@ -20,6 +20,8 @@ * along with this program. If not, see . */ +#include + #include "class.h" #include "interface/class.h" @@ -29,7 +31,10 @@ HttpResponse -httpResponse304(const char * mime, const char * etag, const char * mtime) +httpResponse304( + const char * mime, size_t nmime, + const char * etag, size_t netag, + const char * mtime, size_t nmtime) { HttpResponse response; HttpMessage message; @@ -42,11 +47,23 @@ httpResponse304(const char * mime, const char * etag, const char * mtime) message->body = NULL; httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Type", mime)); + new(HttpHeader, + "Content-Type", + sizeof("Content-Type")-1, + mime, + nmime)); httpHeaderAdd(&(message->header), - new(HttpHeader, "ETag", etag)); + new(HttpHeader, + "ETag", + sizeof("ETag")-1, + etag, + netag)); httpHeaderAdd(&(message->header), - new(HttpHeader, "Last-Modified", mtime)); + new(HttpHeader, + "Last-Modified", + sizeof("Last-Modified")-1, + mtime, + nmtime)); return response; } diff --git a/src/http/response/404.c b/src/http/response/404.c index 9b3c6ab..4a85e4d 100644 --- a/src/http/response/404.c +++ b/src/http/response/404.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "class.h" #include "interface/class.h" @@ -47,21 +48,31 @@ httpResponse404() char buffer[200]; HttpResponse response; HttpMessage message; + size_t nbuf; response = new(HttpResponse, "HTTP/1.1", 404, "Not Found"); message = (HttpMessage)response; httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Type", "text/html")); + new(HttpHeader, + "Content-Type", + sizeof("Content-Type")-1, + "text/html", + sizeof("text/html")-1)); message->type = HTTP_MESSAGE_BUFFERED; message->nbody = sizeof(RESP_DATA) - 1; message->body = calloc(1, sizeof(RESP_DATA)); - strcpy(message->body, RESP_DATA); + memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)); + + nbuf = sprintf(buffer, "%d", message->nbody); - sprintf(buffer, "%d", message->nbody); httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Length", buffer)); + new(HttpHeader, + "Content-Length", + sizeof("Content-Length")-1, + buffer, + nbuf)); return response; } diff --git a/src/http/response/asset.c b/src/http/response/asset.c index b94674d..ed60a4f 100644 --- a/src/http/response/asset.c +++ b/src/http/response/asset.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "class.h" #include "interface/class.h" @@ -34,12 +35,20 @@ HttpResponse -httpResponseAsset(const char * fname, const char * mime, const char * match) +httpResponseAsset( + const char * fname, + const char * mime, + size_t nmime, + const char * match, + size_t nmatch) { struct tm * tmp; char etag[200]; + size_t netag; char mtime[200]; + size_t nmtime; char clen[200]; + size_t nclen; struct stat st; HttpResponse response; HttpMessage message; @@ -48,12 +57,12 @@ httpResponseAsset(const char * fname, const char * mime, const char * match) handle = open(fname, O_RDONLY); fstat(handle, &st); - tmp = localtime(&(st.st_mtime)); - strftime(etag, sizeof(etag), "%s", tmp); - strftime(mtime, sizeof(mtime), "%a, %d %b %Y %T %Z", tmp); + tmp = localtime(&(st.st_mtime)); + netag = strftime(etag, sizeof(etag), "%s", tmp); + nmtime = strftime(mtime, sizeof(mtime), "%a, %d %b %Y %T %Z", tmp); - if (0 == strcmp(etag, match)) { - return httpResponse304(mime, etag, mtime); + if (netag == nmatch && 0 == memcmp(etag, match, netag)) { + return httpResponse304(mime, nmime, etag, netag, mtime, nmime); } response = new(HttpResponse, "HTTP/1.1", 200, "OK"); @@ -63,16 +72,32 @@ httpResponseAsset(const char * fname, const char * mime, const char * match) message->handle = handle; message->nbody = st.st_size; - sprintf(clen, "%d", message->nbody); + nclen = sprintf(clen, "%d", message->nbody); httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Type", mime)); + new(HttpHeader, + "Content-Type", + sizeof("Content-Type")-1, + mime, + nmime)); httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Length", clen)); + new(HttpHeader, + "Content-Length", + sizeof("Content-Length")-1, + clen, + nclen)); httpHeaderAdd(&(message->header), - new(HttpHeader, "ETag", etag)); + new(HttpHeader, + "ETag", + sizeof("ETag")-1, + etag, + netag)); httpHeaderAdd(&(message->header), - new(HttpHeader, "Last-Modified", mtime)); + new(HttpHeader, + "Last-Modified", + sizeof("Last-Modified")-1, + mtime, + nmtime)); return response; } diff --git a/src/http/response/me.c b/src/http/response/me.c index 293b017..6520cbf 100644 --- a/src/http/response/me.c +++ b/src/http/response/me.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include "class.h" #include "interface/class.h" @@ -62,25 +64,35 @@ httpResponseMe(int value) char buffer[200]; HttpResponse response; HttpMessage message; + size_t nbuf; response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Type", "text/html")); - httpHeaderAdd(&(message->header), + new(HttpHeader, + "Content-Type", + sizeof("Content-Type")-1, + "text/html", + sizeof("text/html")-1)); +/* httpHeaderAdd(&(message->header), new(HttpHeader, "Set-Cookie", "name=\"Georg Hopp\"")); httpHeaderAdd(&(message->header), - new(HttpHeader, "Set-Cookie", "profession=\"coder\"")); + new(HttpHeader, "Set-Cookie", "profession=\"coder\""));*/ message->type = HTTP_MESSAGE_BUFFERED; message->nbody = sizeof(RESP_DATA)-1-2; message->body = calloc(1, sizeof(RESP_DATA)-2); sprintf(message->body, RESP_DATA, value); - sprintf(buffer, "%d", message->nbody); + nbuf = sprintf(buffer, "%d", message->nbody); + httpHeaderAdd(&(message->header), - new(HttpHeader, "Content-Length", buffer)); + new(HttpHeader, + "Content-Length", + sizeof("Content-Length")-1, + buffer, + nbuf)); return response; } diff --git a/src/http/worker/add_common_header.c b/src/http/worker/add_common_header.c index ca9166f..9b0a97c 100644 --- a/src/http/worker/add_common_header.c +++ b/src/http/worker/add_common_header.c @@ -1,4 +1,5 @@ #include +#include #include "class.h" #include "interface/class.h" @@ -12,26 +13,43 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) time_t t; struct tm * tmp; char buffer[200]; + size_t ndate; if (httpMessageHasKeepAlive(request)) { httpHeaderAdd( &(response->header), - new(HttpHeader, "Connection", "Keep-Alive")); + new(HttpHeader, + "Connection", + sizeof("Connection")-1, + "Keep-Alive", + sizeof("Keep-Alive")-1)); } else { httpHeaderAdd( &(response->header), - new(HttpHeader, "Connection", "Close")); + new(HttpHeader, + "Connection", + sizeof("Connection")-1, + "Close", + sizeof("Close")-1)); } httpHeaderAdd(&(response->header), - new(HttpHeader, "Server", "testserver")); + new(HttpHeader, + "Server", + sizeof("Server")-1, + "testserver", + sizeof("testserver")-1)); - t = time(NULL); - tmp = localtime(&t); - strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); + t = time(NULL); + tmp = localtime(&t); + ndate = strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); httpHeaderAdd(&(response->header), - new(HttpHeader, "Date", buffer)); + new(HttpHeader, + "Date", + sizeof("Date")-1, + buffer, + ndate)); } // vim: set ts=4 sw=4: diff --git a/src/http/worker/get_asset.c b/src/http/worker/get_asset.c index 36811df..3a03281 100644 --- a/src/http/worker/get_asset.c +++ b/src/http/worker/get_asset.c @@ -1,3 +1,5 @@ +#include + #include "http/header.h" #include "http/message.h" #include "http/request.h" @@ -7,23 +9,29 @@ HttpMessage httpWorkerGetAsset( HttpRequest request, const char * fname, - const char * mime) + const char * mime, + size_t nmime) { char * match; + size_t nmatch; HttpHeader header; header = httpHeaderGet( &(((HttpMessage)request)->header), - "If-None-Match"); + "If-None-Match", + sizeof("If-None-Match")-1); if (NULL == header) { - match = ""; + match = ""; + nmatch = 0; } else { - match = (header->value)[0]; + match = (header->value)[0]; + nmatch = (header->nvalue)[0]; } - return (HttpMessage)httpResponseAsset(fname, mime, match); + return (HttpMessage)httpResponseAsset( + fname, mime, nmime, match, nmatch); } // vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index 6fd41d7..ee5353f 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -34,7 +34,7 @@ #include "http/request.h" #include "http/message.h" -HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *); +HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); ssize_t @@ -64,14 +64,16 @@ httpWorkerProcess(HttpWorker this, int fd) response = httpWorkerGetAsset( request, "./assets/waldschrat.jpg", - "image/jpeg"); + "image/jpeg", + sizeof("image/jpeg")-1); } if (0 == strcmp("/jquery/", request->uri)) { response = httpWorkerGetAsset( request, "./assets/jquery-1.7.1.min.js", - "text/javascript"); + "text/javascript", + sizeof("text/javascript")-1); } } diff --git a/src/utils/hash.c b/src/utils/hash.c index 0dfeb78..dc6dcea 100644 --- a/src/utils/hash.c +++ b/src/utils/hash.c @@ -1,4 +1,5 @@ #include +#include #include "utils/hash.h" @@ -16,13 +17,12 @@ * is one of the algorithms used in berkeley db (see sleepycat) and elsewhere. */ unsigned long -sdbm(const unsigned char * str) +sdbm(const unsigned char * str, size_t len) { unsigned long hash = 0; - int c; - while ((c = tolower(*str++))) - hash = c + (hash << 6) + (hash << 16) - hash; + for(; 0 < len; str++, len--) + hash = tolower(*str) + (hash << 6) + (hash << 16) - hash; return hash; }