|
|
|
@ -12,11 +12,14 @@ |
|
|
|
#include "http/response.h" |
|
|
|
#include "http/response/writer.h" |
|
|
|
|
|
|
|
#define MAX(x,y) ((x) > (y) ? (x) : (y)) |
|
|
|
#define _PSIZE(x) (MAX((x),RESPONSE_WRITER_MAX_BUF)) |
|
|
|
#define PSIZE _PSIZE(this->nheader+message->nbody) |
|
|
|
|
|
|
|
HttpResponse |
|
|
|
httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
{ |
|
|
|
HttpMessageQueue respq = this->response_queue; |
|
|
|
HttpResponse retval = this->cur_response; |
|
|
|
HttpMessage message = (HttpMessage)this->cur_response; |
|
|
|
int cont = 1; |
|
|
|
|
|
|
|
@ -25,7 +28,7 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
case HTTP_RESPONSE_GET: |
|
|
|
if (NULL == this->cur_response && 0 < respq->nmsgs) { |
|
|
|
message = respq->msgs[0]; |
|
|
|
retval = this->cur_response = (HttpResponse)message; |
|
|
|
this->cur_response = (HttpResponse)message; |
|
|
|
|
|
|
|
memmove(respq->msgs, |
|
|
|
&(respq->msgs[1]), |
|
|
|
@ -34,8 +37,9 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
this->nbuffer = 0; |
|
|
|
this->written = 0; |
|
|
|
this->pstart = 0; |
|
|
|
this->pend = httpMessageHeaderSizeGet(message); |
|
|
|
this->nheader = this->pend; |
|
|
|
this->nheader = httpMessageHeaderSizeGet(message); |
|
|
|
this->pend = this->nheader; |
|
|
|
this->pipe = malloc(PSIZE); |
|
|
|
httpMessageHeaderToString(message, this->pipe); |
|
|
|
|
|
|
|
this->state = HTTP_RESPONSE_WRITE; |
|
|
|
@ -53,11 +57,11 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
size_t temp = 0; |
|
|
|
size_t rsize; |
|
|
|
|
|
|
|
this->pend = (RESPONSE_WRITER_BUF_SIZE == this->pend)? |
|
|
|
this->pend = (PSIZE == this->pend)? |
|
|
|
0 : this->pend; |
|
|
|
|
|
|
|
rsize = (this->pstart <= this->pend)? |
|
|
|
RESPONSE_WRITER_BUF_SIZE - this->pend : |
|
|
|
PSIZE - this->pend : |
|
|
|
this->pstart - 1; |
|
|
|
|
|
|
|
switch (message->type) { |
|
|
|
@ -92,27 +96,18 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
|
|
|
|
wsize = (this->pstart <= this->pend)? |
|
|
|
this->pend - this->pstart : |
|
|
|
RESPONSE_WRITER_BUF_SIZE - this->pstart; |
|
|
|
PSIZE - this->pstart; |
|
|
|
|
|
|
|
temp = write(fd, &(this->pipe[this->pstart]), wsize); |
|
|
|
|
|
|
|
this->written += temp; |
|
|
|
this->pstart += temp; |
|
|
|
|
|
|
|
this->pstart = (RESPONSE_WRITER_BUF_SIZE == this->pstart)? |
|
|
|
this->pstart = (PSIZE == this->pstart)? |
|
|
|
0 : this->pstart; |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
this->pend = 0; |
|
|
|
|
|
|
|
this->state = HTTP_RESPONSE_DONE; |
|
|
|
} |
|
|
|
else { |
|
|
|
@ -121,15 +116,31 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) |
|
|
|
break; |
|
|
|
|
|
|
|
case HTTP_RESPONSE_DONE: |
|
|
|
this->cur_response = NULL; |
|
|
|
this->state = HTTP_RESPONSE_GET; |
|
|
|
cont = 0; |
|
|
|
if (HTTP_MESSAGE_PIPED == message->type) { |
|
|
|
close(message->handle); |
|
|
|
} |
|
|
|
|
|
|
|
free(this->pipe); |
|
|
|
|
|
|
|
this->nheader = 0; |
|
|
|
this->nbuffer = 0; |
|
|
|
this->written = 0; |
|
|
|
this->pstart = 0; |
|
|
|
this->pend = 0; |
|
|
|
|
|
|
|
if (httpMessageHasKeepAlive(message)) { |
|
|
|
delete(&this->cur_response); |
|
|
|
} |
|
|
|
else { |
|
|
|
cont = 0; |
|
|
|
} |
|
|
|
|
|
|
|
this->state = HTTP_RESPONSE_GET; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return retval; |
|
|
|
return this->cur_response; |
|
|
|
} |
|
|
|
|
|
|
|
// vim: set ts=4 sw=4: |