-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathHttpServer.cpp
More file actions
321 lines (283 loc) · 9.76 KB
/
HttpServer.cpp
File metadata and controls
321 lines (283 loc) · 9.76 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
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
321
#include "HttpServer.h"
#include "hmain.h" // import master_workers_run
#include "herr.h"
#include "hlog.h"
#include "htime.h"
#include "EventLoop.h"
using namespace hv;
#include "HttpHandler.h"
static void on_accept(hio_t* io);
static void on_recv(hio_t* io, void* _buf, int readbytes);
static void on_close(hio_t* io);
struct HttpServerPrivdata {
std::vector<EventLoopPtr> loops;
std::vector<hthread_t> threads;
std::mutex mutex_;
std::shared_ptr<HttpService> service;
FileCache filecache;
};
static void on_recv(hio_t* io, void* buf, int readbytes) {
// printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
HttpHandler* handler = (HttpHandler*)hevent_userdata(io);
assert(handler != NULL);
int nfeed = handler->FeedRecvData((const char*)buf, readbytes);
if (nfeed != readbytes) {
hio_close(io);
return;
}
}
static void on_close(hio_t* io) {
HttpHandler* handler = (HttpHandler*)hevent_userdata(io);
if (handler == NULL) return;
hevent_set_userdata(io, NULL);
delete handler;
EventLoop* loop = currentThreadEventLoop;
if (loop) {
--loop->connectionNum;
}
}
static void on_accept(hio_t* io) {
http_server_t* server = (http_server_t*)hevent_userdata(io);
HttpService* service = server->service;
/*
printf("on_accept connfd=%d\n", hio_fd(io));
char localaddrstr[SOCKADDR_STRLEN] = {0};
char peeraddrstr[SOCKADDR_STRLEN] = {0};
printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
*/
EventLoop* loop = currentThreadEventLoop;
if (loop->connectionNum >= server->worker_connections) {
hlogw("over worker_connections");
hio_close(io);
return;
}
++loop->connectionNum;
hio_setcb_close(io, on_close);
hio_setcb_read(io, on_recv);
hio_read(io);
if (service->keepalive_timeout > 0) {
hio_set_keepalive_timeout(io, service->keepalive_timeout);
}
// new HttpHandler, delete on_close
HttpHandler* handler = new HttpHandler(io);
// ssl
handler->ssl = hio_is_ssl(io);
// ip:port
sockaddr_u* peeraddr = (sockaddr_u*)hio_peeraddr(io);
sockaddr_ip(peeraddr, handler->ip, sizeof(handler->ip));
handler->port = sockaddr_port(peeraddr);
// http service
handler->service = service;
// websocket service
handler->ws_service = server->ws;
// FileCache
HttpServerPrivdata* privdata = (HttpServerPrivdata*)server->privdata;
handler->files = &privdata->filecache;
hevent_set_userdata(io, handler);
}
static void loop_thread(void* userdata) {
http_server_t* server = (http_server_t*)userdata;
HttpService* service = server->service;
auto loop = std::make_shared<EventLoop>();
hloop_t* hloop = loop->loop();
// http
if (server->listenfd[0] >= 0) {
hio_t* listenio = haccept(hloop, server->listenfd[0], on_accept);
hevent_set_userdata(listenio, server);
}
// https
if (server->listenfd[1] >= 0) {
hio_t* listenio = haccept(hloop, server->listenfd[1], on_accept);
hevent_set_userdata(listenio, server);
hio_enable_ssl(listenio);
if (server->ssl_ctx) {
hio_set_ssl_ctx(listenio, server->ssl_ctx);
}
}
HttpServerPrivdata* privdata = (HttpServerPrivdata*)server->privdata;
privdata->mutex_.lock();
if (privdata->loops.size() == 0) {
// NOTE: fsync logfile when idle
hlog_disable_fsync();
hidle_add(hloop, [](hidle_t*) {
hlog_fsync();
}, INFINITE);
// NOTE: add timer to update s_date every 1s
htimer_add(hloop, [](htimer_t* timer) {
gmtime_fmt(hloop_now(hevent_loop(timer)), HttpMessage::s_date);
}, 1000);
// document_root
if (service->document_root.size() > 0 && service->GetStaticFilepath("/").empty()) {
service->Static("/", service->document_root.c_str());
}
// FileCache
FileCache* filecache = &privdata->filecache;
filecache->stat_interval = service->file_cache_stat_interval;
filecache->expired_time = service->file_cache_expired_time;
if (filecache->expired_time > 0) {
// NOTE: add timer to remove expired file cache
htimer_t* timer = htimer_add(hloop, [](htimer_t* timer) {
FileCache* filecache = (FileCache*)hevent_userdata(timer);
filecache->RemoveExpiredFileCache();
}, filecache->expired_time * 1000);
hevent_set_userdata(timer, filecache);
}
}
privdata->loops.push_back(loop);
privdata->mutex_.unlock();
hlogi("EventLoop started, pid=%ld tid=%ld", hv_getpid(), hv_gettid());
if (server->onWorkerStart) {
loop->queueInLoop([server](){
server->onWorkerStart();
});
}
loop->run();
if (server->onWorkerStop) {
server->onWorkerStop();
}
hlogi("EventLoop stopped, pid=%ld tid=%ld", hv_getpid(), hv_gettid());
}
#ifdef OS_WIN
static void WINAPI loop_thread_stdcall(void* userdata) {
return loop_thread(userdata);
}
#endif
/* @workflow:
* http_server_run -> Listen -> master_workers_run / hthread_create ->
* loop_thread -> accept -> EventLoop::run ->
* on_accept -> new HttpHandler -> hio_read ->
* on_recv -> HttpHandler::FeedRecvData ->
* on_close -> delete HttpHandler
*/
int http_server_run(http_server_t* server, int wait) {
// http_port
if (server->port > 0) {
server->listenfd[0] = Listen(server->port, server->host);
if (server->listenfd[0] < 0) return server->listenfd[0];
hlogi("http server listening on %s:%d", server->host, server->port);
}
// https_port
if (server->https_port > 0 && HV_WITH_SSL) {
server->listenfd[1] = Listen(server->https_port, server->host);
if (server->listenfd[1] < 0) return server->listenfd[1];
hlogi("https server listening on %s:%d", server->host, server->https_port);
}
// SSL_CTX
if (server->listenfd[1] >= 0) {
if (server->ssl_ctx == NULL) {
server->ssl_ctx = hssl_ctx_instance();
}
if (server->ssl_ctx == NULL) {
hloge("new SSL_CTX failed!");
return ERR_NEW_SSL_CTX;
}
#ifdef WITH_NGHTTP2
#ifdef WITH_OPENSSL
static unsigned char s_alpn_protos[] = "\x02h2\x08http/1.1\x08http/1.0\x08http/0.9";
hssl_ctx_set_alpn_protos(server->ssl_ctx, s_alpn_protos, sizeof(s_alpn_protos) - 1);
#endif
#endif
}
HttpServerPrivdata* privdata = new HttpServerPrivdata;
server->privdata = privdata;
if (server->service == NULL) {
privdata->service = std::make_shared<HttpService>();
server->service = privdata->service.get();
}
if (server->worker_processes) {
// multi-processes
return master_workers_run(loop_thread, server, server->worker_processes, server->worker_threads, wait);
}
else {
// multi-threads
if (server->worker_threads == 0) server->worker_threads = 1;
for (int i = wait ? 1 : 0; i < server->worker_threads; ++i) {
#ifdef OS_WIN
hthread_t thrd = hthread_create((hthread_routine)loop_thread_stdcall, server);
#else
hthread_t thrd = hthread_create((hthread_routine)loop_thread, server);
#endif
privdata->threads.push_back(thrd);
}
if (wait) {
loop_thread(server);
}
return 0;
}
}
/* @workflow:
* http_server_stop -> EventLoop::stop -> hthread_join
*/
int http_server_stop(http_server_t* server) {
HttpServerPrivdata* privdata = (HttpServerPrivdata*)server->privdata;
if (privdata == NULL) return 0;
#ifdef OS_UNIX
if (server->worker_processes) {
if (g_main_ctx.pid) kill(g_main_ctx.pid, SIGNAL_KILLWORKER);
return 0;
}
#endif
// wait for all threads started and all loops running
while (1) {
hv_delay(1);
std::lock_guard<std::mutex> locker(privdata->mutex_);
// wait for all loops created
if (privdata->loops.size() < server->worker_threads) {
continue;
}
// wait for all loops running
bool all_loops_running = true;
for (auto& loop : privdata->loops) {
if (loop->status() < hv::Status::kRunning) {
all_loops_running = false;
break;
}
}
if (all_loops_running) break;
}
// stop all loops
for (auto& loop : privdata->loops) {
loop->stop();
}
// join all threads
for (auto& thrd : privdata->threads) {
hthread_join(thrd);
}
if (server->alloced_ssl_ctx && server->ssl_ctx) {
hssl_ctx_free(server->ssl_ctx);
server->alloced_ssl_ctx = 0;
server->ssl_ctx = NULL;
}
delete privdata;
server->privdata = NULL;
return 0;
}
namespace hv {
std::shared_ptr<hv::EventLoop> HttpServer::loop(int idx) {
HttpServerPrivdata* privdata = (HttpServerPrivdata*)this->privdata;
if (privdata == NULL) return NULL;
std::lock_guard<std::mutex> locker(privdata->mutex_);
if (privdata->loops.empty()) return NULL;
if (idx >= 0 && idx < (int)privdata->loops.size()) {
return privdata->loops[idx];
}
EventLoop* cur = currentThreadEventLoop;
for (auto& loop : privdata->loops) {
if (loop.get() == cur) return loop;
}
return NULL;
}
size_t HttpServer::connectionNum() {
HttpServerPrivdata* privdata = (HttpServerPrivdata*)this->privdata;
if (privdata == NULL) return 0;
std::lock_guard<std::mutex> locker(privdata->mutex_);
if (privdata->loops.empty()) return 0;
size_t total = 0;
for (auto& loop : privdata->loops) {
total += loop->connectionNum;
}
return total;
}
}