From 92379ebb150deb998266c9ba14f8221e624164a1 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 13 Feb 2012 21:27:47 +0100 Subject: [PATCH] use one dynamic buffer less and save at least one write on small responses --- ChangeLog | 32 +++++++- include/http/response/writer.h | 10 ++- src/http/response/writer.c | 2 - src/http/response/writer/write.c | 122 ++++++++++++++----------------- src/server/run.c | 2 +- 5 files changed, 91 insertions(+), 77 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3547130..7fc616e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,34 @@ +2012-02-13 21:27:47 +0100 Georg Hopp + + * use one dynamic buffer less and save at least one write on small responses (HEAD, master) + +2012-02-13 18:25:36 +0100 Georg Hopp + + * removed generated docs + +2012-02-13 17:55:52 +0100 Georg Hopp + + * fixed bug in new response handling (origin/master, origin/HEAD) + +2012-02-13 17:29:35 +0100 Georg Hopp + + * better response handling but still buggy with stream piping + +2012-02-13 09:46:39 +0100 Georg Hopp + + * now load image from actual server + +2012-02-13 09:39:21 +0100 Georg Hopp + + * first working version of content delivery from file....very crude... @TODO: rewrite complete response handline. + +2012-02-12 20:39:12 +0100 Georg Hopp + + * more generalizing of response writing (implemented a response writer...now it should be possible to implement a stream writer for images + 2012-02-12 12:43:56 +0100 Georg Hopp - * make http request and response childs of a common parent http message (HEAD, master) + * make http request and response childs of a common parent http message 2012-02-12 04:13:54 +0100 Georg Hopp @@ -8,7 +36,7 @@ 2012-02-12 04:05:38 +0100 Georg Hopp - * change response to tree based header storage and make everything work. (origin/master, origin/HEAD) + * change response to tree based header storage and make everything work. 2012-02-12 00:05:13 +0100 Georg Hopp diff --git a/include/http/response/writer.h b/include/http/response/writer.h index c1813f2..053fd83 100644 --- a/include/http/response/writer.h +++ b/include/http/response/writer.h @@ -5,17 +5,19 @@ #include "http/response.h" #include "http/message/queue.h" +#define RESPONSE_WRITER_BUF_SIZE 10240 + + typedef enum e_HttpResponseState { HTTP_RESPONSE_GET=0, - HTTP_RESPONSE_HEADER, - HTTP_RESPONSE_PIPE, + HTTP_RESPONSE_WRITE, HTTP_RESPONSE_DONE } HttpResponseState; CLASS(HttpResponseWriter) { - char * buffer; - char pipe[1024]; + char pipe[RESPONSE_WRITER_BUF_SIZE]; + size_t nheader; size_t nbuffer; size_t written; size_t pstart; diff --git a/src/http/response/writer.c b/src/http/response/writer.c index 62409cf..e64a452 100644 --- a/src/http/response/writer.c +++ b/src/http/response/writer.c @@ -22,7 +22,6 @@ dtor(void * _this) { HttpResponseWriter this = _this; - if (NULL != this->buffer) free(this->buffer); delete(&(this->response_queue)); if (NULL != this->cur_response) @@ -34,7 +33,6 @@ void _clone(void * _this, void * _base) { HttpResponseWriter this = _this; - //HttpResponseWriter base = _base; this->response_queue = new(HttpMessageQueue); } diff --git a/src/http/response/writer/write.c b/src/http/response/writer/write.c index b5cee5d..1de73f0 100644 --- a/src/http/response/writer/write.c +++ b/src/http/response/writer/write.c @@ -31,97 +31,83 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) &(respq->msgs[1]), sizeof(void*) * (--respq->nmsgs + 1)); - this->nbuffer = httpMessageHeaderSizeGet(message); - this->buffer = malloc(this->nbuffer); - this->written = 0; - - httpMessageHeaderToString(message, this->buffer); - - this->state = HTTP_RESPONSE_HEADER; - } - else { - cont = 0; - } - break; - - case HTTP_RESPONSE_HEADER: - this->written += write( - fd, - &(this->buffer[this->written]), - this->nbuffer - this->written); - - if (this->written == this->nbuffer) { - free(this->buffer); - this->buffer = NULL; this->nbuffer = 0; this->written = 0; this->pstart = 0; - this->pend = 0; + this->pend = httpMessageHeaderSizeGet(message); + this->nheader = this->pend; + httpMessageHeaderToString(message, this->pipe); - this->state = HTTP_RESPONSE_PIPE; + this->state = HTTP_RESPONSE_WRITE; } else { cont = 0; } break; - case HTTP_RESPONSE_PIPE: - switch (message->type) { - case HTTP_MESSAGE_BUFFERED: - this->written += write( - fd, - &(message->body[this->written]), - message->nbody - this->written); - break; - - case HTTP_MESSAGE_PIPED: - /** - * read - */ - if (this->nbuffer < message->nbody) { - size_t rsize; - size_t temp; - - this->pend = (1024 == this->pend)? - 0 : this->pend; - - rsize = (this->pstart <= this->pend)? - 1024 - this->pend : this->pstart - 1; + case HTTP_RESPONSE_WRITE: + /** + * read + */ + if (this->nbuffer < message->nbody) { + size_t temp = 0; + size_t rsize; + + this->pend = (RESPONSE_WRITER_BUF_SIZE == this->pend)? + 0 : this->pend; + + rsize = (this->pstart <= this->pend)? + RESPONSE_WRITER_BUF_SIZE - this->pend : + this->pstart - 1; + + switch (message->type) { + case HTTP_MESSAGE_BUFFERED: + temp = message->nbody - this->nbuffer; + temp = (rsizepipe[this->pend]), + &(message->body[this->nbuffer]), + temp); + break; + + case HTTP_MESSAGE_PIPED: temp = read( message->handle, &(this->pipe[this->pend]), rsize); + break; + } - this->nbuffer += temp; - this->pend += temp; - } - - /** - * write - */ - { - size_t wsize; - size_t temp; + this->nbuffer += temp; + this->pend += temp; + } - wsize = (this->pstart <= this->pend)? - this->pend - this->pstart : 1024 - this->pstart; + /** + * write + */ + { + size_t wsize; + size_t temp; - temp = write(fd, &(this->pipe[this->pstart]), wsize); + wsize = (this->pstart <= this->pend)? + this->pend - this->pstart : + RESPONSE_WRITER_BUF_SIZE - this->pstart; - this->written += temp; - this->pstart += temp; + temp = write(fd, &(this->pipe[this->pstart]), wsize); - this->pstart = (1024 == this->pstart)? - 0 : this->pstart; - } - break; + this->written += temp; + this->pstart += temp; - default: - break; + this->pstart = (RESPONSE_WRITER_BUF_SIZE == this->pstart)? + 0 : this->pstart; } - if (this->written == message->nbody) { + if (this->written == message->nbody + this->nheader) { + if (HTTP_MESSAGE_PIPED == message->type) { + close(message->handle); + } + this->nheader = 0; this->nbuffer = 0; this->written = 0; this->pstart = 0; diff --git a/src/server/run.c b/src/server/run.c index 09dcfe8..b452f9c 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -166,7 +166,7 @@ serverRun(Server this) events--; nwrites--; - message = streamWriterWrite(writer, fd); + message = (HttpMessage)streamWriterWrite(writer, fd); if (NULL != message && writer->state == HTTP_RESPONSE_GET) { if (httpMessageHasKeepAlive(message)) {