write.c
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "class.h"
#include "interface/class.h"
#include "http/response.h"
#include "http/response/writer.h"
size_t
httpResponseWriterWrite(HttpResponseWriter this, int fd)
{
HttpMessageQueue respq = this->response_queue;
int cont = 1;
size_t written = 0;
while (cont) {
switch (this->state) {
case HTTP_RESPONSE_NO:
if (NULL == this->cur_response && 0 < respq->nmsgs) {
this->cur_response = (HttpResponse)respq->msgs[0];
memmove(respq->msgs, &(respq->msgs[1]), --respq->nmsgs);
this->state = HTTP_RESPONSE_START;
}
else {
cont = 0;
}
break;
case HTTP_RESPONSE_START:
if (NULL == this->buffer) {
this->nbuffer = httpResponseSizeGet(this->cur_response);
this->buffer = calloc(1, this->nbuffer);
httpResponseToString(this->cur_response, this->buffer);
}
{
written = write(fd, this->buffer, this->nbuffer);
if (-1 == written) {
free (this->buffer);
this->buffer = NULL;
return written;
}
if (written == this->nbuffer) {
if (HTTP_MESSAGE_BUFFERED ==
((HttpMessage)(this->cur_response))->type) {
this->state = HTTP_RESPONSE_DONE;
}
else {
this->state = HTTP_RESPONSE_PIPE;
}
}
else {
this->nbuffer -= written;
memmove(this->buffer, this->buffer + written, this->nbuffer);
cont = 0;
}
}
break;
case HTTP_RESPONSE_PIPE:
break;
case HTTP_RESPONSE_DONE:
free (this->buffer);
this->buffer = NULL;
delete(&(this->cur_response));
this->state = HTTP_RESPONSE_NO;
break;
}
}
return written;
}
// vim: set ts=4 sw=4: