From a239fc98e8aded12a7259bc74fae2a45e617ba94 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 29 Mar 2012 11:49:53 +0200 Subject: [PATCH] changed HttpMessageQueue to be a real queue and not a fixed size array --- include/http/message/queue.h | 23 +++++++++++++++--- src/http/Makefile.am | 4 ++- src/http/message/queue.c | 10 +++++--- src/http/message/queue/get.c | 47 ++++++++++++++++++++++++++++++++++++ src/http/message/queue/put.c | 43 +++++++++++++++++++++++++++++++++ src/http/parser/parse.c | 22 ++++++++--------- src/http/worker/process.c | 22 +++++------------ src/http/writer/write.c | 19 ++++++--------- 8 files changed, 144 insertions(+), 46 deletions(-) create mode 100644 src/http/message/queue/get.c create mode 100644 src/http/message/queue/put.c diff --git a/include/http/message/queue.h b/include/http/message/queue.h index 7b024e0..0997479 100644 --- a/include/http/message/queue.h +++ b/include/http/message/queue.h @@ -26,17 +26,32 @@ #ifndef __HTTP_MESSAGE_QUEUE_H__ #define __HTTP_MESSAGE_QUEUE_H__ +#include + #include "class.h" #include "http/message.h" - -#define HTTP_MESSAGE_QUEUE_MAX 1024 +#include "commons.h" CLASS(HttpMessageQueue) { - HttpMessage msgs[HTTP_MESSAGE_QUEUE_MAX]; - size_t nmsgs; + HttpMessage msg; + HttpMessageQueue next; + + /** + * first and last are only available in the initial queue + * element (the root). This elelment does not contain any message + * and exists only for organizational purpose. + */ + HttpMessageQueue first; + HttpMessageQueue last; + size_t nmsg; }; +void httpMessageQueuePut(HttpMessageQueue, HttpMessage); +HttpMessage httpMessageQueueGet(HttpMessageQueue); + +#define httpMessageQueueEmpty(this) (0 >= (this)->nmsg) + #endif // __HTTP_MESSAGE_QUEUE_H__ // vim: set ts=4 sw=4: diff --git a/src/http/Makefile.am b/src/http/Makefile.am index e6b2ace..1512acd 100644 --- a/src/http/Makefile.am +++ b/src/http/Makefile.am @@ -6,7 +6,9 @@ MSG = message.c \ message/header_to_string.c \ message/get_version.c \ message/has_valid_version.c -MSGQ = message/queue.c +MSGQ = message/queue.c \ + message/queue/get.c \ + message/queue/put.c REQ = request.c \ request/has_valid_method.c RESP = response.c \ diff --git a/src/http/message/queue.c b/src/http/message/queue.c index 72dcf34..69134d7 100644 --- a/src/http/message/queue.c +++ b/src/http/message/queue.c @@ -38,10 +38,14 @@ void messageQueueDtor(void * _this) { HttpMessageQueue this = _this; + HttpMessageQueue node = this->first; int i; - - for (i=0; inmsgs; i++) { - delete((this->msgs)[i]); + + while (NULL != node) { + HttpMessageQueue next = node->next; + delete(node->msg); + delete(node); + node = next; } } diff --git a/src/http/message/queue/get.c b/src/http/message/queue/get.c new file mode 100644 index 0000000..00f63a6 --- /dev/null +++ b/src/http/message/queue/get.c @@ -0,0 +1,47 @@ +/** + * \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 "class.h" +#include "http/message.h" +#include "http/message/queue.h" + +HttpMessage +httpMessageQueueGet(HttpMessageQueue this) +{ + HttpMessageQueue first; + HttpMessage msg; + + if (NULL == this->first) { + return NULL; + } + + msg = this->first->msg; + first = this->first->next; + delete(this->first); + + this->first = first; + this->nmsg--; + + return msg; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/message/queue/put.c b/src/http/message/queue/put.c new file mode 100644 index 0000000..d17a7d2 --- /dev/null +++ b/src/http/message/queue/put.c @@ -0,0 +1,43 @@ +/** + * \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 "class.h" +#include "http/message.h" +#include "http/message/queue.h" + +void +httpMessageQueuePut(HttpMessageQueue this, HttpMessage msg) +{ + HttpMessageQueue node = (this->last)? this->last : this; + + node->next = new(HttpMessageQueue); + this->last = node->next; + + if (node == this) { + this->first = node->next; + } + + node->next->msg = msg; + this->nmsg++; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/parser/parse.c b/src/http/parser/parse.c index 39eb16b..5c5f12d 100644 --- a/src/http/parser/parse.c +++ b/src/http/parser/parse.c @@ -167,16 +167,16 @@ httpParserParse(void * _this, Stream st) httpParserPostVars(this); } - /** - * enqueue current request - */ - this->queue->msgs[(this->queue->nmsgs)++] = this->current; - this->current = NULL; - - /** - * prepare for next request - */ - this->state = HTTP_MESSAGE_GARBAGE; + /** + * enqueue current request + */ + httpMessageQueuePut(this->queue, this->current); + this->current = NULL; + + /** + * prepare for next request + */ + this->state = HTTP_MESSAGE_GARBAGE; } break; @@ -185,7 +185,7 @@ httpParserParse(void * _this, Stream st) } } - return this->queue->nmsgs; + return this->queue->nmsg; } // vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index 3d9b7d7..0e0c6e0 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -60,13 +60,11 @@ httpWorkerProcess(HttpWorker this, Stream st) if (0 < (size = httpParserParse(this->parser, st))) { int i; - HttpMessageQueue reqq = this->parser->queue; - HttpMessageQueue respq = this->writer->queue; - for (i=0; inmsgs; i++) { - HttpMessage rmessage = reqq->msgs[i]; - HttpRequest request = (HttpRequest)(reqq->msgs[i]); - HttpMessage response = NULL; + while (! httpMessageQueueEmpty(this->parser->queue)) { + HttpRequest request = (HttpRequest)httpMessageQueueGet( + this->parser->queue); + HttpMessage response = NULL; /** * \todo store the cookie count in the request to make a simple @@ -238,19 +236,11 @@ httpWorkerProcess(HttpWorker this, Stream st) httpWorkerAddCommonHeader((HttpMessage)request, response); - t = time(NULL); - tmp = localtime(&t); - strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); - httpHeaderAdd(&(response->header), - new(HttpHeader, "Date", buffer)); + delete(request); - respq->msgs[(respq->nmsgs)++] = response; + httpMessageQueuePut(this->writer->queue, response); response = NULL; - delete((reqq->msgs)[i]); } - - reqq->nmsgs = 0; - size = respq->nmsgs; } return size; diff --git a/src/http/writer/write.c b/src/http/writer/write.c index 5942242..4a5a655 100644 --- a/src/http/writer/write.c +++ b/src/http/writer/write.c @@ -25,6 +25,7 @@ #include "class.h" #include "http/message.h" +#include "http/message/queue.h" #include "http/writer.h" #include "cbuf.h" #include "stream.h" @@ -35,9 +36,8 @@ ssize_t httpWriterWrite(void * _this, Stream st) { - HttpWriter this = _this; - HttpMessageQueue respq = this->queue; - int cont = 1; + HttpWriter this = _this; + int cont = 1; if (cbufIsLocked(this->buffer)) { if (FALSE == this->ourLock) @@ -51,8 +51,9 @@ httpWriterWrite(void * _this, Stream st) while (cont) { switch (this->state) { case HTTP_WRITER_GET: - if (NULL == this->current && 0 < respq->nmsgs) { - this->current = respq->msgs[0]; + if (NULL == this->current && + ! httpMessageQueueEmpty(this->queue)) { + this->current = httpMessageQueueGet(this->queue); this->written = 0; this->nbody = 0; @@ -122,11 +123,7 @@ httpWriterWrite(void * _this, Stream st) break; case HTTP_WRITER_DONE: - this->state = HTTP_WRITER_GET; - - memmove(respq->msgs, - &(respq->msgs[1]), - sizeof(void*) * (--respq->nmsgs + 1)); + this->state = HTTP_WRITER_GET; cbufRelease(this->buffer); this->ourLock = FALSE; @@ -148,7 +145,7 @@ httpWriterWrite(void * _this, Stream st) } } - return respq->nmsgs; + return this->queue->nmsg; } // vim: set ts=4 sw=4: