forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.cpp
292 lines (265 loc) · 7.65 KB
/
httpd.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
#include "hv.h"
#include "hssl.h"
#include "hmain.h"
#include "iniparser.h"
#include "HttpServer.h"
#include "hasync.h" // import hv::async
#include "router.h"
hv::HttpServer g_http_server;
hv::HttpService g_http_service;
static void print_version();
static void print_help();
static int parse_confile(const char* confile);
// short options
static const char options[] = "hvc:ts:dp:";
// long options
static const option_t long_options[] = {
{'h', "help", NO_ARGUMENT},
{'v', "version", NO_ARGUMENT},
{'c', "confile", REQUIRED_ARGUMENT},
{'t', "test", NO_ARGUMENT},
{'s', "signal", REQUIRED_ARGUMENT},
{'d', "daemon", NO_ARGUMENT},
{'p', "port", REQUIRED_ARGUMENT}
};
static const char detail_options[] = R"(
-h|--help Print this information
-v|--version Print version
-c|--confile <confile> Set configure file, default etc/{program}.conf
-t|--test Test configure file and exit
-s|--signal <signal> Send <signal> to process,
<signal>=[start,stop,restart,status,reload]
-d|--daemon Daemonize
-p|--port <port> Set listen port
)";
void print_version() {
printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
}
void print_help() {
printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
printf("Options:\n%s\n", detail_options);
}
int parse_confile(const char* confile) {
IniParser ini;
int ret = ini.LoadFromFile(confile);
if (ret != 0) {
printf("Load confile [%s] failed: %d\n", confile, ret);
exit(-40);
}
// logfile
std::string str = ini.GetValue("logfile");
if (!str.empty()) {
strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
}
hlog_set_file(g_main_ctx.logfile);
// loglevel
str = ini.GetValue("loglevel");
if (!str.empty()) {
hlog_set_level_by_str(str.c_str());
}
// log_filesize
str = ini.GetValue("log_filesize");
if (!str.empty()) {
hlog_set_max_filesize_by_str(str.c_str());
}
// log_remain_days
str = ini.GetValue("log_remain_days");
if (!str.empty()) {
hlog_set_remain_days(atoi(str.c_str()));
}
// log_fsync
str = ini.GetValue("log_fsync");
if (!str.empty()) {
logger_enable_fsync(hlog, hv_getboolean(str.c_str()));
}
hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
hlog_fsync();
// worker_processes
int worker_processes = 0;
#ifdef DEBUG
// Disable multi-processes mode for debugging
worker_processes = 0;
#else
str = ini.GetValue("worker_processes");
if (str.size() != 0) {
if (strcmp(str.c_str(), "auto") == 0) {
worker_processes = get_ncpu();
hlogd("worker_processes=ncpu=%d", worker_processes);
}
else {
worker_processes = atoi(str.c_str());
}
}
#endif
g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
// worker_threads
int worker_threads = 0;
str = ini.GetValue("worker_threads");
if (str.size() != 0) {
if (strcmp(str.c_str(), "auto") == 0) {
worker_threads = get_ncpu();
hlogd("worker_threads=ncpu=%d", worker_threads);
}
else {
worker_threads = atoi(str.c_str());
}
}
g_http_server.worker_threads = LIMIT(0, worker_threads, 64);
// worker_connections
str = ini.GetValue("worker_connections");
if (str.size() != 0) {
g_http_server.worker_connections = atoi(str.c_str());
}
// http_port
int port = 0;
const char* szPort = get_arg("p");
if (szPort) {
port = atoi(szPort);
}
if (port == 0) {
port = ini.Get<int>("port");
}
if (port == 0) {
port = ini.Get<int>("http_port");
}
g_http_server.port = port;
// https_port
if (HV_WITH_SSL) {
g_http_server.https_port = ini.Get<int>("https_port");
}
if (g_http_server.port == 0 && g_http_server.https_port == 0) {
printf("Please config listen port!\n");
exit(-10);
}
// base_url
str = ini.GetValue("base_url");
if (str.size() != 0) {
g_http_service.base_url = str;
}
// document_root
str = ini.GetValue("document_root");
if (str.size() != 0) {
g_http_service.document_root = str;
}
// home_page
str = ini.GetValue("home_page");
if (str.size() != 0) {
g_http_service.home_page = str;
}
// error_page
str = ini.GetValue("error_page");
if (str.size() != 0) {
g_http_service.error_page = str;
}
// index_of
str = ini.GetValue("index_of");
if (str.size() != 0) {
g_http_service.index_of = str;
}
// limit_rate
str = ini.GetValue("limit_rate");
if (str.size() != 0) {
g_http_service.limit_rate = atoi(str.c_str());
}
// cors
if (ini.Get<bool>("cors")) {
g_http_service.AllowCORS();
}
if (ini.Get<bool>("forward_proxy")) {
g_http_service.EnableForwardProxy();
}
// ssl
if (g_http_server.https_port > 0) {
std::string crt_file = ini.GetValue("ssl_certificate");
std::string key_file = ini.GetValue("ssl_privatekey");
std::string ca_file = ini.GetValue("ssl_ca_certificate");
hlogi("SSL backend is %s", hssl_backend());
hssl_ctx_init_param_t param;
memset(¶m, 0, sizeof(param));
param.crt_file = crt_file.c_str();
param.key_file = key_file.c_str();
param.ca_file = ca_file.c_str();
param.endpoint = HSSL_SERVER;
if (hssl_ctx_init(¶m) == NULL) {
hloge("SSL certificate verify failed!");
exit(0);
}
else {
hlogi("SSL certificate verify ok!");
}
}
hlogi("parse_confile('%s') OK", confile);
return 0;
}
static void on_reload(void* userdata) {
hlogi("reload confile [%s]", g_main_ctx.confile);
parse_confile(g_main_ctx.confile);
}
int main(int argc, char** argv) {
// g_main_ctx
main_ctx_init(argc, argv);
//int ret = parse_opt(argc, argv, options);
int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
if (ret != 0) {
print_help();
exit(ret);
}
// help
if (get_arg("h")) {
print_help();
exit(0);
}
// version
if (get_arg("v")) {
print_version();
exit(0);
}
// parse_confile
const char* confile = get_arg("c");
if (confile) {
strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
}
parse_confile(g_main_ctx.confile);
// test
if (get_arg("t")) {
printf("Test confile [%s] OK!\n", g_main_ctx.confile);
exit(0);
}
// signal
signal_init(on_reload);
const char* signal = get_arg("s");
if (signal) {
signal_handle(signal);
}
#ifdef OS_UNIX
// daemon
if (get_arg("d")) {
// nochdir, noclose
int ret = daemon(1, 1);
if (ret != 0) {
printf("daemon error: %d\n", ret);
exit(-10);
}
}
#endif
// pidfile
create_pidfile();
// http_server
Router::Register(g_http_service);
g_http_server.registerHttpService(&g_http_service);
#if 0
std::atomic_flag init_flag = ATOMIC_FLAG_INIT;
g_http_server.onWorkerStart = [&init_flag](){
if (!init_flag.test_and_set()) {
hv::async::startup();
}
};
g_http_server.onWorkerStop = [&init_flag](){
if (init_flag.test_and_set()) {
hv::async::cleanup();
}
};
#endif
g_http_server.run();
return ret;
}