write.c
2.86 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "class.h"
#include "interface/class.h"
#include "http/message.h"
#include "http/response.h"
#include "http/response/writer.h"
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;
while (cont) {
switch (this->state) {
case HTTP_RESPONSE_GET:
if (NULL == this->cur_response && 0 < respq->nmsgs) {
message = respq->msgs[0];
retval = this->cur_response = (HttpResponse)message;
memmove(respq->msgs,
&(respq->msgs[1]),
sizeof(void*) * (--respq->nmsgs + 1));
this->nbuffer = 0;
this->written = 0;
this->pstart = 0;
this->pend = httpMessageHeaderSizeGet(message);
this->nheader = this->pend;
httpMessageHeaderToString(message, this->pipe);
this->state = HTTP_RESPONSE_WRITE;
}
else {
cont = 0;
}
break;
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 = (rsize<temp)? rsize : temp;
memcpy(
&(this->pipe[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;
wsize = (this->pstart <= this->pend)?
this->pend - this->pstart :
RESPONSE_WRITER_BUF_SIZE - this->pstart;
temp = write(fd, &(this->pipe[this->pstart]), wsize);
this->written += temp;
this->pstart += temp;
this->pstart = (RESPONSE_WRITER_BUF_SIZE == 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 {
cont = 0;
}
break;
case HTTP_RESPONSE_DONE:
this->cur_response = NULL;
this->state = HTTP_RESPONSE_GET;
cont = 0;
break;
}
}
return retval;
}
// vim: set ts=4 sw=4: