forked from tinylcy/vino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinyhttpd.c
559 lines (471 loc) · 13.3 KB
/
tinyhttpd.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (C) Chenyang Li
* Copyright (C) tinyhttpd
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include "tinyhttpd.h"
#include "socketlib.h"
#include "rio.h"
#include "util.h"
#include "error.h"
#include "debug.h"
#include "threadpool.h"
#include "http_epoll.h"
#define error(msg) { perror(msg); }
#define CONFIG_FILE_NAME "tinyhttpd.conf"
time_t server_started;
int server_bytes_sent;
int server_requests;
int main(int ac, char *av[]) {
int listenfd = -1;
int fd;
// int *fdptr;
struct httpd_conf conf;
if(ac != 1 && ac != 2) {
fprintf(stderr, "usage: ./tinyhttpd & || ./tinyhttpd port &\n");
return 0;
}
if(ac == 1) {
init_conf(&conf);
listenfd = make_server_socket(conf.port);
} else if(ac == 2) {
listenfd = make_server_socket(atoi(av[1]));
}
if(listenfd == -1) {
log_err("fail to make server socket.");
exit(EXIT_FAILURE);
}
if(make_socket_non_blocking(listenfd) != 0) {
log_err("fail to make socket non_blocking.");
exit(EXIT_FAILURE);
}
if(conf.thread_num == 0 || conf.job_max_num == 0) {
log_err("the thread_num is 0 or the job_max_num is 0.");
exit(EXIT_FAILURE);
}
// threadpool_t *pool = threadpool_init(conf.thread_num, conf.job_max_num);
// if(pool == NULL) {
// log_err("fail to initialize the threadpool.");
// exit(EXIT_FAILURE);
// }
/*
* create epoll and add listenfd to interest list
*/
int epfd = http_epoll_create(0);
printf("epfd: %d\n", epfd);
http_request_t *request = NULL;
request = (http_request_t *)malloc(sizeof(http_request_t));
if(request == NULL) {
log_err("fail to malloc http_request_t.");
return -1;
}
http_request_init(request, listenfd, epfd);
struct epoll_event event;
event.data.ptr = (void *)request;
// event.data.fd = listenfd;
event.events = EPOLLIN | EPOLLET;
http_epoll_add(epfd, listenfd, &event);
log_info("success to add fd: %d into interest list.", listenfd);
int i, n;
printf("\ntinyhttpd started successfully!\n");
while(1) {
n = http_epoll_wait(epfd, evlist, MAXEVENTS, -1);
if(n == -1) {
if(errno == EINTR) {
continue;
}
log_err("epoll wait error.");
return -1;
}
for(i = 0; i < n; i++) {
http_request_t *req = (http_request_t *)evlist[i].data.ptr;
fd = req->fd;
log_info("ready fd: %d.", fd);
if(fd == listenfd) {
/*
* we have a notification on the listening socket,
* which means one or more incoming connections.
*/
int connfd;
while(1) {
connfd = accept(listenfd, NULL, NULL);
if(connfd < 0) {
if((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
break;
} else {
log_err("fail to accept a connfd.");
break;
}
}
log_info("success to accept a fd: %d.", connfd);
if(make_socket_non_blocking(connfd) != 0) {
log_err("fail to make socket non-blocking: connfd = %d.", connfd);
}
http_request_t *r = (http_request_t *)malloc(sizeof(http_request_t));
http_request_init(r, connfd, epfd);
// event.data.fd = connfd;
event.data.ptr = (void *)r;
event.events = EPOLLIN | EPOLLET;
http_epoll_add(epfd, connfd, &event);
log_info("success to add fd: %d into interest list.", connfd);
}
} else {
if(evlist[i].events & EPOLLIN) {
/*
* in this case, we have to malloc an
* extra space to store fd, CASPP page 661
* explains the reason.
*/
// fdptr = (int *)malloc(sizeof(int));
// *fdptr = fd;
// threadpool_add_job(pool, handle, fdptr);
// log_info("success to add a job into threadpool: fd = %d.", fd);
// fdptr = NULL;
//threadpool_add_job(pool, do_request, evlist[i].data.ptr);
process_request(evlist[i].data.ptr);
} else if(evlist[i].events & (EPOLLHUP | EPOLLERR)) {
close(fd);
log_info("success to close fd: %d", fd);
}
}
}
}
printf("exit tinyhttpd.");
return 0;
}
/*-----------------------------------------------------------*
initialize the configuration
-----------------------------------------------------------*/
void init_conf(struct httpd_conf *conf) {
FILE *fp = NULL;
char line[BUFSIZ];
char param_name[BUFSIZ];
char param_value[BUFSIZ];
if((fp = fopen(CONFIG_FILE_NAME, "r")) == NULL) {
perror("fail to open the configuration file.");
exit(EXIT_FAILURE);
}
while(fgets(line, BUFSIZ, fp) != NULL) {
sscanf(line, "%s %s", param_name, param_value);
if(strcmp(param_name, PORT) == 0) {
conf->port = atoi(param_value);
}
if(strcmp(param_name, THREAD_NUM) == 0) {
conf->thread_num = atoi(param_value);
}
if(strcmp(param_name, JOB_MAX_NUM) == 0) {
conf->job_max_num = atoi(param_value);
}
}
fclose(fp);
}
/*-----------------------------------------------------------*
set the type of thread
-----------------------------------------------------------*/
void setup(pthread_attr_t *attrp) {
pthread_attr_init(attrp);
pthread_attr_setdetachstate(attrp, PTHREAD_CREATE_DETACHED);
time(&server_started);
server_bytes_sent = 0;
server_requests = 0;
}
void process_request(void *req_ptr) {
http_request_t *request = (http_request_t *)req_ptr;
int fd = request->fd;
char *cursor = NULL;
size_t remain_size = 0;
int n, ret;
while(1) {
cursor = &request->buf[request->last % REQ_MAX_BUF];
remain_size = 200;
n = read(fd, cursor, remain_size);
if(n == 0) { // EOF
break;
}
if(n < 0) {
if(errno == EAGAIN) {
break;
}
break;
}
request->last += n;
ret = http_request_parse(request);
if(ret == HTTP_PARSE_AGAIN) {
continue;
}else if(ret == HTTP_PARSE_OK) {
http_response(request);
break;
}
}
// close(fd);
}
void http_response(http_request_t *request) {
char method[BUFSIZ];
char uri[BUFSIZ];
memset(method, 0, BUFSIZ);
strncpy(method, request->method_start, request->method_end - request->method_start + 1);
memset(uri, 0, BUFSIZ);
strcpy(uri, ".");
strncpy(uri + 1, request->uri_start, request->uri_end - request->uri_start + 1);
if(strcmp(method, "GET") && strcmp(method, "POST")) {
not_implement(request->fd);
} else if(!file_exist(uri)) {
do_404(uri, request->fd);
} else if(is_directory(uri)) {
do_ls(uri, request->fd);
} else if(is_dynamic(uri)) {
// TODO
} else {
serve_static(uri, request->fd);
}
}
/*---------------------------------------------------------*
return the information HTTP headers about the request.
Parameters: the connfd to print the headers on
the status code and the short message
the content type
---------------------------------------------------------*/
void headers(int fd, int status_code, char *short_msg, char *content_type) {
char buf[BUFSIZ];
sprintf(buf, "HTTP/1.1 %d %s\r\n", status_code, short_msg);
rio_writen(fd, buf, strlen(buf));
if(content_type) {
sprintf(buf, "Content-type: %s\r\n", content_type);
rio_writen(fd, buf, strlen(buf));
}
sprintf(buf, "\r\n");
rio_writen(fd, buf, strlen(buf));
}
/*---------------------------------------------------------*
unimplement HTTP command.
---------------------------------------------------------*/
void not_implement(int fd) {
headers(fd, 501, "Not Implemented", "text/html");
char buf[] = "tinyhttpd does not implement this method\r\n";
rio_writen(fd, buf, strlen(buf));
close(fd);
log_info("success to close fd: %d.", fd);
}
/*--------------------------------------------------------*
no such object.
--------------------------------------------------------*/
void do_404(char *uri, int fd) {
headers(fd, 404, "Not Found", "text/html");
char buf[BUFSIZ];
sprintf(buf, "<h1>The item you request:<I> %s%s </I> is not found</h1>\r\n", uri, "\0");
rio_writen(fd, buf, strlen(buf));
close(fd);
log_info("success to close fd: %d.", fd);
}
/*-------------------------------------------------------*
when the request resource is a directory, list the
directory to client.
-------------------------------------------------------*/
void do_ls(char *dir, int fd) {
DIR *dirp;
struct dirent *dp;
char entry[BUFSIZ];
dirp = opendir(dir);
if(dirp == NULL) {
perror("opendir");
exit(1);
}
headers(fd, 200, "OK", "text/plain");
/* for each entry in this directory, print filename to client */
for(;;) {
errno = 0; /* to distinguish error from end-of-directory */
dp = readdir(dirp);
if(dp == NULL) {
break;
}
if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
continue; /* skip . and .. */
}
memset(entry, 0, BUFSIZ);
strncpy(entry, dp->d_name, strlen(dp->d_name));
strcat(entry, "\r\n");
rio_writen(fd, entry, strlen(entry));
}
if(errno != 0) {
log_err("fail to read DIR: %s.", dir);
exit(EXIT_FAILURE);
}
if(closedir(dirp) == -1) {
log_err("fail to close DIR: %s.", dir);
exit(EXIT_FAILURE);
}
close(fd);
log_info("success to close fd: %d.", fd);
}
/*--------------------------------------------------------*
sanitize the command to prevent the execution
of dangerous commands. such as 'ls ./html/;rm -rf /'
--------------------------------------------------------*/
void sanitize(char *str) {
char *src, *dest;
src= str;
dest = str;
for( ; *src; src++) {
if(*src == '/' || *src == '.' || *src == '_'
|| *src == '-' || isalnum(*src)) {
*dest++ = *src;
} else {
break;
}
}
*dest = '\0';
}
/*--------------------------------------------------------*
the cgi stuff, function to check entension and
one to run the program.
--------------------------------------------------------*/
char* file_type(char *uri) {
char *cp;
if((cp = strrchr(uri, '.')) != NULL) {
return cp + 1;
}
return "";
}
/*-------------------------------------------------------*
determine whether the type of request resource is
dynamic.
-------------------------------------------------------*/
int is_dynamic(const char *uri) {
return (strstr(uri, DYNAMIC_PATH) != NULL);
}
/*-------------------------------------------------------*
serve the dynamic content. check if a cgi program is
executable at first, then serve the dynamic content
according to the HTTP method.
-------------------------------------------------------*/
void serve_dynamic(http_request_headers_t *request, int fd) {
char prog[BUFSIZ];
strcpy(prog, ".");
strcat(prog, request->uri);
/* the file is not executable */
if(!is_executable(prog)) {
headers(fd, 403, "Forbidden", "text/html");
char buf[BUFSIZ];
sprintf(buf, "<h1>tinyhttpd couldn't run the CGI program: <I>%s</I>.</h1>\r\n", prog);
rio_writen(fd, buf, strlen(buf));
close(fd);
return;
}
if(strcmp(request->method, "GET") == 0) {
serve_get_dynamic(request, fd);
} else if(strcmp(request->method, "POST") == 0) {
serve_post_dynamic(request, fd);
}
}
/*-------------------------------------------------------*
fork a new process to serve the HTTP GET dynamic
content.
-------------------------------------------------------*/
void serve_get_dynamic(http_request_headers_t *request, int fd) {
char prog[BUFSIZ];
strcpy(prog, ".");
strcat(prog, request->uri);
pid_t pid;
pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
}
if(pid == 0) {
headers(fd, 200, "OK", "text/html");
/* set environment variables */
if(request->query_args != NULL) {
setenv("QUERY_STRING", request->query_args, 1);
} else {
setenv("QUERY_STRING", "", 1);
}
dup2(fd, 1);
dup2(fd, 2);
close(fd);
execl(prog, prog, NULL);
exit(0);
} else {
close(fd);
}
}
/*-------------------------------------------------------*
fork a new process to serve the HTTP POST dynamic
content.
-------------------------------------------------------*/
void serve_post_dynamic(http_request_headers_t *request, int fd) {
char prog[BUFSIZ];
strcpy(prog, ".");
strcat(prog, request->uri);
pid_t pid;
pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
}
if(pid == 0) {
headers(fd, 200, "OK", "text/html");
/* set environment variables */
if(request->post_data != NULL) {
setenv("POST_DATA", request->post_data, 1);
} else {
setenv("POST_DATA", "", 1);
}
dup2(fd, 1);
dup2(fd, 2);
close(fd);
execl(prog, prog, NULL);
exit(0);
} else {
close(fd);
}
}
/*--------------------------------------------------------*
serve static content.
check if a file is readable at first.
--------------------------------------------------------*/
void serve_static(char *uri, int fd) {
/* the file is not readable */
if(!is_readable(uri)) {
headers(fd, 403, "Forbidden", "text/html");
char buf[BUFSIZ];
sprintf(buf, "<h1>tinyhttpd couldn't read the file: <I>%s</I>.</h1>\r\n", uri);
rio_writen(fd, buf, strlen(buf));
close(fd);
return;
}
/* the file is readable */
char *extension = file_type(uri);
char *content_type = "text/plain";
FILE *fpfile;
int c;
if(strcmp(extension, "html") == 0) {
content_type = "text/html";
} else if(strcmp(extension, "gif") == 0) {
content_type = "image/gif";
} else if(strcmp(extension, "jpg") == 0) {
content_type = "image/jpeg";
} else if(strcmp(extension, "jpeg") == 0) {
content_type = "image/jpeg";
}
fpfile = fopen(uri, "r");
if(fpfile != NULL) {
headers(fd, 200, "OK", content_type);
while((c = getc(fpfile)) != EOF) {
rio_writen(fd, &c, 1);
}
fclose(fpfile);
close(fd);
log_info("success to close fd: %d.", fd);
}
}