-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiteHttpServer.cpp
320 lines (274 loc) · 8.23 KB
/
LiteHttpServer.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Created by 袁健 on 2017/12/11.
//
#include "LiteHttpServer.h"
#include <sys/poll.h>
#include "httprequestparser.h"
#include "HttpRequest.h"
#include "Log.h"
void LiteHttpServer::start(bool is_ipv4)
{
//phase init
_listen_fd = ::socket(AF_INET,SOCK_STREAM,0);
if (_listen_fd == -1)
{
_E("socket init error");
return;
}
int one = 1;
int ret = setsockopt(_listen_fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
if (ret != 0)
{
_E("setsockopt error " << ret);
return;
}
//phase bind
struct hostent *host = gethostbyname(_address.c_str());
if (!host)
{
const char *errMsg = hstrerror(h_errno);
_E(errMsg);
return;
}
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(_port);
//TODO: use correct host
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int bind_ret = ::bind(_listen_fd, reinterpret_cast<struct sockaddr*>(&sockaddr),
sizeof(sockaddr));
if (bind_ret != 0)
{
_E("bind error");
return;
}
//phase listen
int listen_ret = ::listen(_listen_fd, 5);
if (listen_ret != 0)
{
_E("listen error");
return;
}
// long listen_one = 1;
// int ioctl_ret = ioctl(_listen_fd, FIONBIO, &listen_one);
// if (ioctl_ret == -1)
// {
// _E("make none block error");
// return;
// }
//phase add listen to poll
struct pollfd pfd;
pfd.fd = _listen_fd;
//TODO : consider the out event
pfd.events = POLL_IN/* | POLL_OUT*/;
_pollfds.push_back(pfd);
_handlers[_listen_fd] = std::bind(&LiteHttpServer::accept_handler,
this,
std::placeholders::_1);
struct pollfd pfd_wakeup;
pfd_wakeup.fd = _pipe_fd[0];
//TODO : consider the out event
pfd_wakeup.events = POLL_IN/* | POLL_OUT*/;
_pollfds.push_back(pfd_wakeup);
_handlers[_pipe_fd[0]] = std::bind(&LiteHttpServer::wakeup_handler,
this,
std::placeholders::_1);
//phase start event loop
loop();
}
void LiteHttpServer::stop()
{
_stop = true;
wake_up();
}
void LiteHttpServer::loop()
{
_I("loop start");
while (!_stop)
{
auto pollfds_local = _pollfds;
int nfds = ::poll(pollfds_local.data(),pollfds_local.size(),10);
if (nfds == -1 && errno != EINTR)
{
_E("poll error");
return;
}
_polling = true;
for (int i = 0; i < pollfds_local.size() && nfds > 0; ++i)
{
int fd = pollfds_local[i].fd;
if (_handlers.count(fd) == 0)
continue;
auto &handler = _handlers[fd];
if (pollfds_local[i].revents & POLL_IN)
{
handler(fd);
}
if (pollfds_local[i].revents & POLL_OUT)
{
handler(fd);
}
}
_polling = false;
if (!_functors.empty()) {
std::vector<Functor> funcs;
{
std::lock_guard<std::mutex> lock(_mutex);
funcs.swap(_functors);
}
for (const auto &func : funcs) {
func();
}
}
}
_I("loop end");
}
void LiteHttpServer::dispatch(Functor func)
{
std::lock_guard<std::mutex> lock(_mutex);
_functors.push_back(func);
if (!_polling)
wake_up();
}
void LiteHttpServer::wake_up()
{
char tmp[1];
::write(_pipe_fd[1],&tmp,1);
}
void LiteHttpServer::accept_handler(int fd)
{
struct sockaddr_in sockaddr;
struct pollfd pfd;
socklen_t size = sizeof(sockaddr);
int acc_ret = ::accept(_listen_fd, reinterpret_cast<struct sockaddr*>(&sockaddr), &size);
if (acc_ret == -1)
{
_E("accept error");
return;
}
//make noblock io
long one = 1;
int ioctl_ret = ioctl(acc_ret, FIONBIO, &one);
if (ioctl_ret == -1)
{
_E("make none block error");
return;
}
_handlers[acc_ret] = std::bind(&LiteHttpServer::read_handler,
this,
std::placeholders::_1
);
_I("accept " << acc_ret);
pfd.fd = acc_ret;
//TODO : consider the out event
pfd.events = POLL_IN/* | POLL_OUT*/;
_pollfds.push_back(pfd);
}
void LiteHttpServer::read_handler(int fd)
{
if (_context.find(fd) == _context.end())
{
_context[fd] = std::make_shared<HttpRequestParser>();
}
HttpRequestParserPtr request_parser = std::static_pointer_cast<HttpRequestParser>(_context[fd]);
uint8_t buffer[65536];
ssize_t recv_ret = ::recv(fd,buffer, sizeof(buffer),0);
_I("http parse " << recv_ret << " " << fd);
if (recv_ret == 0)
{
//close
_E("recv_ret zero " << fd);
this->shutdown(fd);
}
else if (recv_ret > 0)
{
//parse
HttpRequestParser::ParseResult parse_res = request_parser->parse((const char *)buffer,(const char *)buffer + recv_ret);
if (parse_res == HttpRequestParser::ParsingIncompleted)
{
//TODO : callback progress
return;
} else if (parse_res == HttpRequestParser::ParsingCompleted) {
_I("http parse complete " << fd << " " << request_parser->request.uri << " " << request_parser->request.content.size());
if (request_parser->request.uri == "/up")
{
std::stringstream ss;
ss << "<body>\n"
<< "<form action=\"uploaddata\" method=\"post\" enctype=\"multipart/form-data\">\n"
<< "<input type=\"file\" name=\"fileUpload\" />\n"
<< "<input type=\"submit\" value=\"Upload File\" />\n"
<< "</form>\n"
<< "</body>";
std::string content = ss.str();
auto resp_content = make_http_resp(request_parser->request,content.data(),content.size());
::write(fd,resp_content.data(),resp_content.size());
}
else if (request_parser->request.uri == "/uploaddata")
{
std::stringstream ss;
ss << "upload suc\n";
std::string content = ss.str();
auto resp_content = make_http_resp(request_parser->request,content.data(),content.size());
::write(fd,resp_content.data(),resp_content.size());
_upCompCb(request_parser->request.content.data(),
request_parser->request.content.size(),
request_parser->filename.data(),
request_parser->filename.length());
}
this->shutdown(fd);
return;
} else {
_E("http parse error " << fd);
this->shutdown(fd);
return;
}
}
else
{
//error
if (errno == EWOULDBLOCK
|| errno == EAGAIN
|| errno == EINTR)
{
return;
}
else
{
_E("recv ret neg " << fd);
this->shutdown(fd);
}
}
}
void LiteHttpServer::shutdown(int fd) {
dispatch([=](){
for (int i = 0 ; i < _pollfds.size() ; ++ i) {
if (_pollfds[i].fd == fd)
{
_pollfds[i] = _pollfds.back();
_pollfds.pop_back();
_I("shutdown " << fd);
break;
}
}
_handlers.erase(fd);
_context.erase(fd);
::close(fd);
});
}
std::string LiteHttpServer::make_http_resp(HttpRequest& req, const char *content, size_t size)
{
std::stringstream ss;
ss << "HTTP/" << req.versionMajor << "." << req.versionMinor << " " << 200 << " " <<
"OK" << "\nContent-Length:" <<
size << "\n\n";
std::string res(ss.str().data(),ss.str().size());
res.append(content, size);
return res;
}
void LiteHttpServer::wakeup_handler(int fd)
{
int8_t temp[4096];
ssize_t wakeup_ret = ::read(fd,temp, sizeof(temp));
_I("wakeup_handler " << fd << " " << wakeup_ret);
}