Commit 92379ebb150deb998266c9ba14f8221e624164a1
1 parent
6b1605d2
use one dynamic buffer less and save at least one write on small responses
Showing
5 changed files
with
91 additions
and
77 deletions
1 | +2012-02-13 21:27:47 +0100 Georg Hopp | ||
2 | + | ||
3 | + * use one dynamic buffer less and save at least one write on small responses (HEAD, master) | ||
4 | + | ||
5 | +2012-02-13 18:25:36 +0100 Georg Hopp | ||
6 | + | ||
7 | + * removed generated docs | ||
8 | + | ||
9 | +2012-02-13 17:55:52 +0100 Georg Hopp | ||
10 | + | ||
11 | + * fixed bug in new response handling (origin/master, origin/HEAD) | ||
12 | + | ||
13 | +2012-02-13 17:29:35 +0100 Georg Hopp | ||
14 | + | ||
15 | + * better response handling but still buggy with stream piping | ||
16 | + | ||
17 | +2012-02-13 09:46:39 +0100 Georg Hopp | ||
18 | + | ||
19 | + * now load image from actual server | ||
20 | + | ||
21 | +2012-02-13 09:39:21 +0100 Georg Hopp | ||
22 | + | ||
23 | + * first working version of content delivery from file....very crude... @TODO: rewrite complete response handline. | ||
24 | + | ||
25 | +2012-02-12 20:39:12 +0100 Georg Hopp | ||
26 | + | ||
27 | + * more generalizing of response writing (implemented a response writer...now it should be possible to implement a stream writer for images | ||
28 | + | ||
1 | 2012-02-12 12:43:56 +0100 Georg Hopp | 29 | 2012-02-12 12:43:56 +0100 Georg Hopp |
2 | 30 | ||
3 | - * make http request and response childs of a common parent http message (HEAD, master) | 31 | + * make http request and response childs of a common parent http message |
4 | 32 | ||
5 | 2012-02-12 04:13:54 +0100 Georg Hopp | 33 | 2012-02-12 04:13:54 +0100 Georg Hopp |
6 | 34 | ||
@@ -8,7 +36,7 @@ | @@ -8,7 +36,7 @@ | ||
8 | 36 | ||
9 | 2012-02-12 04:05:38 +0100 Georg Hopp | 37 | 2012-02-12 04:05:38 +0100 Georg Hopp |
10 | 38 | ||
11 | - * change response to tree based header storage and make everything work. (origin/master, origin/HEAD) | 39 | + * change response to tree based header storage and make everything work. |
12 | 40 | ||
13 | 2012-02-12 00:05:13 +0100 Georg Hopp | 41 | 2012-02-12 00:05:13 +0100 Georg Hopp |
14 | 42 |
@@ -5,17 +5,19 @@ | @@ -5,17 +5,19 @@ | ||
5 | #include "http/response.h" | 5 | #include "http/response.h" |
6 | #include "http/message/queue.h" | 6 | #include "http/message/queue.h" |
7 | 7 | ||
8 | +#define RESPONSE_WRITER_BUF_SIZE 10240 | ||
9 | + | ||
10 | + | ||
8 | typedef enum e_HttpResponseState { | 11 | typedef enum e_HttpResponseState { |
9 | HTTP_RESPONSE_GET=0, | 12 | HTTP_RESPONSE_GET=0, |
10 | - HTTP_RESPONSE_HEADER, | ||
11 | - HTTP_RESPONSE_PIPE, | 13 | + HTTP_RESPONSE_WRITE, |
12 | HTTP_RESPONSE_DONE | 14 | HTTP_RESPONSE_DONE |
13 | } HttpResponseState; | 15 | } HttpResponseState; |
14 | 16 | ||
15 | CLASS(HttpResponseWriter) { | 17 | CLASS(HttpResponseWriter) { |
16 | - char * buffer; | ||
17 | - char pipe[1024]; | 18 | + char pipe[RESPONSE_WRITER_BUF_SIZE]; |
18 | 19 | ||
20 | + size_t nheader; | ||
19 | size_t nbuffer; | 21 | size_t nbuffer; |
20 | size_t written; | 22 | size_t written; |
21 | size_t pstart; | 23 | size_t pstart; |
@@ -22,7 +22,6 @@ dtor(void * _this) | @@ -22,7 +22,6 @@ dtor(void * _this) | ||
22 | { | 22 | { |
23 | HttpResponseWriter this = _this; | 23 | HttpResponseWriter this = _this; |
24 | 24 | ||
25 | - if (NULL != this->buffer) free(this->buffer); | ||
26 | delete(&(this->response_queue)); | 25 | delete(&(this->response_queue)); |
27 | 26 | ||
28 | if (NULL != this->cur_response) | 27 | if (NULL != this->cur_response) |
@@ -34,7 +33,6 @@ void | @@ -34,7 +33,6 @@ void | ||
34 | _clone(void * _this, void * _base) | 33 | _clone(void * _this, void * _base) |
35 | { | 34 | { |
36 | HttpResponseWriter this = _this; | 35 | HttpResponseWriter this = _this; |
37 | - //HttpResponseWriter base = _base; | ||
38 | 36 | ||
39 | this->response_queue = new(HttpMessageQueue); | 37 | this->response_queue = new(HttpMessageQueue); |
40 | } | 38 | } |
@@ -31,97 +31,83 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) | @@ -31,97 +31,83 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) | ||
31 | &(respq->msgs[1]), | 31 | &(respq->msgs[1]), |
32 | sizeof(void*) * (--respq->nmsgs + 1)); | 32 | sizeof(void*) * (--respq->nmsgs + 1)); |
33 | 33 | ||
34 | - this->nbuffer = httpMessageHeaderSizeGet(message); | ||
35 | - this->buffer = malloc(this->nbuffer); | ||
36 | - this->written = 0; | ||
37 | - | ||
38 | - httpMessageHeaderToString(message, this->buffer); | ||
39 | - | ||
40 | - this->state = HTTP_RESPONSE_HEADER; | ||
41 | - } | ||
42 | - else { | ||
43 | - cont = 0; | ||
44 | - } | ||
45 | - break; | ||
46 | - | ||
47 | - case HTTP_RESPONSE_HEADER: | ||
48 | - this->written += write( | ||
49 | - fd, | ||
50 | - &(this->buffer[this->written]), | ||
51 | - this->nbuffer - this->written); | ||
52 | - | ||
53 | - if (this->written == this->nbuffer) { | ||
54 | - free(this->buffer); | ||
55 | - this->buffer = NULL; | ||
56 | this->nbuffer = 0; | 34 | this->nbuffer = 0; |
57 | this->written = 0; | 35 | this->written = 0; |
58 | this->pstart = 0; | 36 | this->pstart = 0; |
59 | - this->pend = 0; | 37 | + this->pend = httpMessageHeaderSizeGet(message); |
38 | + this->nheader = this->pend; | ||
39 | + httpMessageHeaderToString(message, this->pipe); | ||
60 | 40 | ||
61 | - this->state = HTTP_RESPONSE_PIPE; | 41 | + this->state = HTTP_RESPONSE_WRITE; |
62 | } | 42 | } |
63 | else { | 43 | else { |
64 | cont = 0; | 44 | cont = 0; |
65 | } | 45 | } |
66 | break; | 46 | break; |
67 | 47 | ||
68 | - case HTTP_RESPONSE_PIPE: | ||
69 | - switch (message->type) { | ||
70 | - case HTTP_MESSAGE_BUFFERED: | ||
71 | - this->written += write( | ||
72 | - fd, | ||
73 | - &(message->body[this->written]), | ||
74 | - message->nbody - this->written); | ||
75 | - break; | ||
76 | - | ||
77 | - case HTTP_MESSAGE_PIPED: | ||
78 | - /** | ||
79 | - * read | ||
80 | - */ | ||
81 | - if (this->nbuffer < message->nbody) { | ||
82 | - size_t rsize; | ||
83 | - size_t temp; | ||
84 | - | ||
85 | - this->pend = (1024 == this->pend)? | ||
86 | - 0 : this->pend; | ||
87 | - | ||
88 | - rsize = (this->pstart <= this->pend)? | ||
89 | - 1024 - this->pend : this->pstart - 1; | 48 | + case HTTP_RESPONSE_WRITE: |
49 | + /** | ||
50 | + * read | ||
51 | + */ | ||
52 | + if (this->nbuffer < message->nbody) { | ||
53 | + size_t temp = 0; | ||
54 | + size_t rsize; | ||
55 | + | ||
56 | + this->pend = (RESPONSE_WRITER_BUF_SIZE == this->pend)? | ||
57 | + 0 : this->pend; | ||
58 | + | ||
59 | + rsize = (this->pstart <= this->pend)? | ||
60 | + RESPONSE_WRITER_BUF_SIZE - this->pend : | ||
61 | + this->pstart - 1; | ||
62 | + | ||
63 | + switch (message->type) { | ||
64 | + case HTTP_MESSAGE_BUFFERED: | ||
65 | + temp = message->nbody - this->nbuffer; | ||
66 | + temp = (rsize<temp)? rsize : temp; | ||
67 | + memcpy( | ||
68 | + &(this->pipe[this->pend]), | ||
69 | + &(message->body[this->nbuffer]), | ||
70 | + temp); | ||
90 | 71 | ||
72 | + break; | ||
73 | + | ||
74 | + case HTTP_MESSAGE_PIPED: | ||
91 | temp = read( | 75 | temp = read( |
92 | message->handle, | 76 | message->handle, |
93 | &(this->pipe[this->pend]), | 77 | &(this->pipe[this->pend]), |
94 | rsize); | 78 | rsize); |
79 | + break; | ||
80 | + } | ||
95 | 81 | ||
96 | - this->nbuffer += temp; | ||
97 | - this->pend += temp; | ||
98 | - } | ||
99 | - | ||
100 | - /** | ||
101 | - * write | ||
102 | - */ | ||
103 | - { | ||
104 | - size_t wsize; | ||
105 | - size_t temp; | 82 | + this->nbuffer += temp; |
83 | + this->pend += temp; | ||
84 | + } | ||
106 | 85 | ||
107 | - wsize = (this->pstart <= this->pend)? | ||
108 | - this->pend - this->pstart : 1024 - this->pstart; | 86 | + /** |
87 | + * write | ||
88 | + */ | ||
89 | + { | ||
90 | + size_t wsize; | ||
91 | + size_t temp; | ||
109 | 92 | ||
110 | - temp = write(fd, &(this->pipe[this->pstart]), wsize); | 93 | + wsize = (this->pstart <= this->pend)? |
94 | + this->pend - this->pstart : | ||
95 | + RESPONSE_WRITER_BUF_SIZE - this->pstart; | ||
111 | 96 | ||
112 | - this->written += temp; | ||
113 | - this->pstart += temp; | 97 | + temp = write(fd, &(this->pipe[this->pstart]), wsize); |
114 | 98 | ||
115 | - this->pstart = (1024 == this->pstart)? | ||
116 | - 0 : this->pstart; | ||
117 | - } | ||
118 | - break; | 99 | + this->written += temp; |
100 | + this->pstart += temp; | ||
119 | 101 | ||
120 | - default: | ||
121 | - break; | 102 | + this->pstart = (RESPONSE_WRITER_BUF_SIZE == this->pstart)? |
103 | + 0 : this->pstart; | ||
122 | } | 104 | } |
123 | 105 | ||
124 | - if (this->written == message->nbody) { | 106 | + if (this->written == message->nbody + this->nheader) { |
107 | + if (HTTP_MESSAGE_PIPED == message->type) { | ||
108 | + close(message->handle); | ||
109 | + } | ||
110 | + this->nheader = 0; | ||
125 | this->nbuffer = 0; | 111 | this->nbuffer = 0; |
126 | this->written = 0; | 112 | this->written = 0; |
127 | this->pstart = 0; | 113 | this->pstart = 0; |
@@ -166,7 +166,7 @@ serverRun(Server this) | @@ -166,7 +166,7 @@ serverRun(Server this) | ||
166 | events--; | 166 | events--; |
167 | nwrites--; | 167 | nwrites--; |
168 | 168 | ||
169 | - message = streamWriterWrite(writer, fd); | 169 | + message = (HttpMessage)streamWriterWrite(writer, fd); |
170 | 170 | ||
171 | if (NULL != message && writer->state == HTTP_RESPONSE_GET) { | 171 | if (NULL != message && writer->state == HTTP_RESPONSE_GET) { |
172 | if (httpMessageHasKeepAlive(message)) { | 172 | if (httpMessageHasKeepAlive(message)) { |
Please
register
or
login
to post a comment