-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhttp_server.c
357 lines (297 loc) · 10.5 KB
/
http_server.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <getopt.h>
#include <easy_io.h>
#include <easy_http_handler.h>
#include <fcntl.h>
// 命令行参数结构
typedef struct cmdline_param {
int port;
int io_thread_cnt;
int file_thread_cnt;
int print_stat;
char root_dir[256];
easy_thread_pool_t *threads;
} cmdline_param;
/*************************************************************************************************
* 函数定义部分
*************************************************************************************************/
cmdline_param cp;
static void print_usage(char *prog_name);
static int parse_cmd_line(int argc, char *const argv[], cmdline_param *cp);
static int easy_http_server_on_process(easy_request_t *r);
static void easy_buf_string_tobuf(easy_pool_t *pool, easy_list_t *bc, char *name, easy_buf_string_t *s);
static void easy_http_request_tobuf(easy_pool_t *pool, easy_list_t *bc, easy_http_request_t *p);
static int easy_http_server_request_process(easy_request_t *r, void *args);
__thread easy_client_wait_t client_wait = {0};
/**
* 程序入口
*/
int main(int argc, char **argv)
{
easy_listen_t *listen;
easy_io_handler_pt io_handler;
int ret;
// default
memset(&cp, 0, sizeof(cmdline_param));
cp.io_thread_cnt = 1;
cp.file_thread_cnt = 1;
// parse cmd line
if (parse_cmd_line(argc, argv, &cp) == EASY_ERROR)
return EASY_ERROR;
// 检查必需参数
if (cp.port == 0) {
print_usage(argv[0]);
return EASY_ERROR;
}
// 对easy_io初始化, 设置io的线程数, file的线程数
if (!easy_io_create(cp.io_thread_cnt)) {
easy_error_log("easy_io_init error.\n");
return EASY_ERROR;
}
// 为监听端口设置处理函数,并增加一个监听端口
memset(&io_handler, 0, sizeof(io_handler));
io_handler.decode = easy_http_server_on_decode;
io_handler.encode = easy_http_server_on_encode;
io_handler.process = easy_http_server_on_process;
cp.threads = easy_request_thread_create(cp.file_thread_cnt,
easy_http_server_request_process, NULL);
if ((listen = easy_io_add_listen("", cp.port, &io_handler)) == NULL) {
easy_error_log("easy_io_add_listen error, port: %d, %s\n",
cp.port, strerror(errno));
return EASY_ERROR;
} else {
easy_error_log("listen start, port = %d\n", cp.port);
}
// 起处理速度统计定时器
if (cp.print_stat) {
ev_timer stat_watcher;
easy_io_stat_t iostat;
easy_io_stat_watcher_start(&stat_watcher, 5.0, &iostat, NULL);
}
// 起线程并开始
if (easy_io_start()) {
easy_error_log("easy_io_start error.\n");
return EASY_ERROR;
}
// 等待线程退出
ret = easy_io_wait();
easy_io_destroy();
return ret;
}
/**
* 命令行帮助
*/
static void print_usage(char *prog_name)
{
fprintf(stderr, "%s -p port [-R root_dir] [-t thread_cnt]\n"
" -p, --port server port\n"
" -R, --root_dir root directory\n"
" -t, --io_thread_cnt thread count for listen, default: 1\n"
" -D, --file_thread_cnt thread count for disk io, default: 1\n"
" -s, --print_stat print statistics\n"
" -h, --help display this help and exit\n"
" -V, --version version and build time\n\n"
"eg: %s -p 5000\n\n", prog_name, prog_name);
}
/**
* 解析命令行
*/
static int parse_cmd_line(int argc, char *const argv[], cmdline_param *cp)
{
int opt;
const char *opt_string = "hVp:t:R:D:s";
struct option long_opts[] = {
{"port", 1, NULL, 'p'},
{"root_dir", 1, NULL, 'R'},
{"io_thread_cnt", 1, NULL, 't'},
{"file_thread_cnt", 1, NULL, 'D'},
{"print_stat", 0, NULL, 's'},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'V'},
{0, 0, 0, 0}
};
opterr = 0;
while ((opt = getopt_long(argc, argv, opt_string, long_opts, NULL)) != -1) {
switch (opt) {
case 'p':
cp->port = atoi(optarg);
break;
case 'R':
if (realpath(optarg, cp->root_dir) == NULL) {
cp->root_dir[0] = '\0';
fprintf(stderr, "directory: %s not found.\n", optarg);
return EASY_ERROR;
}
break;
case 't':
cp->io_thread_cnt = atoi(optarg);
break;
case 'D':
cp->file_thread_cnt = atoi(optarg);
break;
case 's':
cp->print_stat = 1;
break;
case 'V':
fprintf(stderr, "BUILD_TIME: %s %s\n", __DATE__, __TIME__);
return EASY_ERROR;
case 'h':
print_usage(argv[0]);
return EASY_ERROR;
default:
break;
}
}
return EASY_OK;
}
static int easy_http_server_request_process_one(easy_request_t *r)
{
easy_file_task_t *ft = (easy_file_task_t *) r->args;
easy_http_request_t *p = (easy_http_request_t *) r->ipacket;
int rc;
while(ft->offset < ft->count) {
easy_file_task_reset(ft, 0);
rc = pread(ft->fd, ft->buffer, ft->bufsize, ft->offset);
fprintf(stderr, "rc:%d\n", rc);
if (rc <= 0)
return EASY_ERROR;
ft->b->last = ft->buffer + rc;
easy_buf_chain_offer(&p->output, ft->b);
r->opacket = p;
if (ft->offset == 0) {
p->content_length = ft->count;
p->is_raw_header = 0;
} else {
p->is_raw_header = 1;
}
ft->offset += rc;
easy_request_wakeup(r);
// wait
pthread_mutex_lock(&client_wait.mutex);
pthread_cond_wait(&client_wait.cond, &client_wait.mutex);
pthread_mutex_unlock(&client_wait.mutex);
}
close(ft->fd);
return EASY_OK;
}
static int easy_http_server_request_process(easy_request_t *r, void *args)
{
easy_file_task_t *ft = (easy_file_task_t *) r->args;
easy_http_request_t *p = (easy_http_request_t *) r->ipacket;
int rc = 0;
easy_file_buf_t *fb = NULL;
if (p->str_query_string.len == 0) {
easy_file_task_reset(ft, EASY_BUF_FILE);
#ifdef POSIX_FADV_WILLNEED
posix_fadvise(ft->fd, ft->offset, ft->bufsize, POSIX_FADV_WILLNEED);
#endif
// set sendfile to output
fb = (easy_file_buf_t *)ft->b;
fb->fd = ft->fd;
fb->offset = ft->offset;
fb->count = ft->bufsize;
rc = ft->bufsize;
} else if (memcmp("one=y", p->str_query_string.data, 5) == 0) {
easy_client_wait_init(&client_wait);
r->retcode = EASY_AGAIN;
r->client_wait = &client_wait;
rc = easy_http_server_request_process_one(r);
r->client_wait = NULL;
easy_client_wait_cleanup(&client_wait);
return rc;
} else {
easy_file_task_reset(ft, 0);
rc = pread(ft->fd, ft->buffer, ft->bufsize, ft->offset);
ft->b->last = ft->buffer + rc;
}
if (rc > 0) {
easy_buf_chain_offer(&p->output, ft->b);
r->opacket = p;
}
// first
if (ft->offset == 0) {
p->content_length = ft->count;
p->is_raw_header = 0;
} else {
p->is_raw_header = 1;
}
// next
ft->offset += rc;
if (ft->offset >= ft->count || rc <= 0) {
if (fb)
easy_file_buf_set_close(fb);
else
close(ft->fd);
return EASY_OK;
} else if (ft->offset + ft->bufsize > ft->count) {
ft->bufsize = ft->count - ft->offset;
}
return EASY_AGAIN;
}
/**
* 处理函数
*/
static int easy_http_server_on_process(easy_request_t *r)
{
if (r->retcode == EASY_AGAIN) {
if (r->client_wait) {
easy_client_wait_wakeup_request(r);
} else {
easy_thread_pool_push(cp.threads, r, easy_hash_key((uint64_t)(long)r));
}
return EASY_AGAIN;
}
easy_http_request_t *p;
int fd;
char filename[128], url[128];
p = (easy_http_request_t *)r->ipacket;
if (!cp.root_dir[0]) {
easy_http_request_tobuf(r->ms->pool, &p->output, p);
} else {
// 处理文件名
url[0] = '\0';
if (p->str_path.len) {
lnprintf(url, 128, "%s", easy_buf_string_ptr(&p->str_path));
if (strchr(url, '?')) fprintf(stderr, "p->str_path.len: %d\n", p->str_path.len);
}
if (easy_http_merge_path(filename, 128, cp.root_dir, url)) {
easy_buf_string_set(&p->status_line, EASY_HTTP_STATUS_400);
return EASY_OK;
}
// 打开文件
fd = open(filename, O_RDONLY);
if (fd >= 0) {
r->args = (void *)easy_file_task_create(r, fd, 0);
easy_thread_pool_push(cp.threads, r, easy_hash_key((uint64_t)(long)r));
return EASY_AGAIN;
} else {
easy_warn_log("open file failure: %s\n", filename);
easy_buf_string_set(&p->status_line, EASY_HTTP_STATUS_404);
}
}
r->opacket = p;
return EASY_OK;
}
static void easy_buf_string_tobuf(easy_pool_t *pool, easy_list_t *bc, char *name, easy_buf_string_t *s)
{
if (s->len) {
easy_buf_t *b = easy_buf_check_write_space(pool, bc, s->len + strlen(name) + 16);
b->last += sprintf(b->last, "%s%.*s<br>\n", name, s->len, easy_buf_string_ptr(s));
}
}
static void easy_http_request_tobuf(easy_pool_t *pool, easy_list_t *bc, easy_http_request_t *p)
{
easy_string_pair_t *t;
easy_buf_string_tobuf(pool, bc, "path: ", &p->str_path);
easy_buf_string_tobuf(pool, bc, "query_sring: ", &p->str_query_string);
easy_buf_string_tobuf(pool, bc, "str_fragment: ", &p->str_fragment);
easy_buf_string_tobuf(pool, bc, "str_body: ", &p->str_body);
// headers
easy_list_for_each_entry(t, &p->headers_in->list, list) {
if (t->name.len && t->value.len) {
easy_buf_t *b = easy_buf_check_write_space(pool, bc, t->name.len + t->value.len + 16);
b->last += sprintf(b->last, "%.*s: %.*s<br>\n",
t->name.len, easy_buf_string_ptr(&t->name),
t->value.len, easy_buf_string_ptr(&t->value));
}
}
}