-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_worker.cc
161 lines (116 loc) · 3.45 KB
/
http_worker.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "http_worker.h"
////////////////////////////////////////////////////////////////////////////////
HttpWorker::HttpWorker()
: http_state(HttpState::START),
firstline_crlf_mh("\r\n", 2),
header_crlfcrlf_mh("\r\n\r\n", 4) {
// TODO(Janitha): BROKEN WORK IN PROGRESS
}
HttpWorker::~HttpWorker() {
}
HttpWorker::HttpState HttpWorker::consume(char *recvbuf, size_t recvlen, size_t &remlen) {
debug_print("called\n");
// TODO(Janitha): BROKEN WORK IN PROGRESS
remlen = recvlen;
switch(http_state) {
case HttpState::START:
http_state = HttpState::FIRSTLINE;
firstline_state = FirstlineState::START;
case HttpState::FIRSTLINE:
consume_firstline(recvbuf, recvlen, remlen);
if(!remlen) break;
recvbuf += recvlen - remlen;
recvlen = remlen;
http_state = HttpState::HEADERS;
header_state = HeaderState::START;
case HttpState::HEADERS:
consume_header(recvbuf, recvlen, remlen);
if(!remlen) break;
recvbuf += recvlen - remlen;
recvlen = remlen;
http_state = HttpState::BODY;
body_state = BodyState::START;
case HttpState::BODY:
consume_body(recvbuf, recvlen, remlen);
if(!remlen) break;
recvbuf += recvlen - remlen;
recvlen = remlen;
http_state= HttpState::DONE;
case HttpState::DONE:
default:
break;
}
if(remlen) {
perror("error: unconsumed data in http");
exit(EXIT_FAILURE);
}
return http_state;
}
void HttpWorker::consume_firstline(char *recvbuf, size_t recvlen, size_t &remlen) {
debug_print("called\n");
// TODO(Janitha): BROKEN WORK IN PROGRESS
remlen = recvlen;
char *end;
switch(firstline_state) {
case FirstlineState::START:
firstline_state = FirstlineState::DATA;
case FirstlineState::DATA:
end = firstline_crlf_mh.findend(recvbuf, recvlen);
if(!end) {
remlen = 0;
break;
}
remlen = end - recvbuf;
recvbuf = end;
recvlen = remlen;
firstline_state = FirstlineState::DONE;
case FirstlineState::DONE:
default:
break;
}
}
void HttpWorker::consume_header(char *recvbuf, size_t recvlen, size_t &remlen) {
debug_print("called\n");
// TODO(Janitha): BROKEN WORK IN PROGRESS
remlen = recvlen;
char *end;
switch(header_state) {
case HeaderState::START:
header_state = HeaderState::DATA;
case HeaderState::DATA:
end = header_crlfcrlf_mh.findend(recvbuf, recvlen);
if(!end) {
remlen = 0;
break;
}
remlen = end - recvbuf;
recvbuf = end;
recvlen = remlen;
header_state = HeaderState::DONE;
case HeaderState::DONE:
default:
break;
}
}
void HttpWorker::consume_body(char *recvbuf, size_t recvlen, size_t &remlen) {
debug_print("called\n");
// TODO(Janitha): BROKEN WORK IN PROGRESS
remlen = recvlen;
switch(body_state) {
case BodyState::START:
body_state = BodyState::DATA;
case BodyState::DATA:
// TODO(Janitha): What do?
remlen = 0;
break;
case BodyState::DONE:
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
HttpServerWorker::HttpServerWorker()
: HttpWorker() {
}
HttpServerWorker::~HttpServerWorker() {
}