-
Notifications
You must be signed in to change notification settings - Fork 0
/
shttpd.c
111 lines (93 loc) · 1.88 KB
/
shttpd.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
#include "shttpd.h"
struct conf_opts conf_para={
/*CGIRoot*/ "/usr/local/var/www/cgi-bin/",
/*DefaultFile*/ "index.html",
/*DocumentRoot*/"/usr/local/var/www/",
/*ConfigFile*/ "/etc/sHTTPd.conf",
/*ListenPort*/ 8080,
/*MaxClient*/ 100,
/*TimeOut*/ 3,//s
/*InitClient*/ 20
};
struct vec _shttpd_methods[] = {
{"GET", 3, METHOD_GET},
{"POST", 4, METHOD_POST},
{"PUT", 3, METHOD_PUT},
{"DELETE", 6, METHOD_DELETE},
{"HEAD", 4, METHOD_HEAD},
{NULL, 0}
};
/*SIGINT信号截取函数*/
static void sig_int(int num)
{
Worker_ScheduleStop();
return;
}
/*SIGPIPE信号截取函数*/
static void sig_pipe(int num)
{
return;
}
int do_listen()
{
struct sockaddr_in server;
int ss = -1;
int err = -1;
int reuse = 1;
int ret = -1;
/* 初始化服务器地址 */
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port = htons(conf_para.ListenPort);
/* 信号截取函数 */
signal(SIGINT, sig_int);
signal(SIGPIPE, sig_pipe);
/* 生成套接字文件描述符 */
ss = socket (AF_INET, SOCK_STREAM, 0);
if (ss == -1)
{
printf("socket() error\n");
ret = -1;
goto EXITshttpd_listen;
}
/* 设置套接字地址和端口复用 */
err = setsockopt (ss, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (err == -1)
{
printf("setsockopt SO_REUSEADDR failed\n");
}
/* 绑定IP和套接字描述符 */
err = bind (ss, (struct sockaddr*) &server, sizeof(server));
if (err == -1)
{
printf("bind() error\n");
ret = -2;
goto EXITshttpd_listen;
}
/* 设置服务器侦听队列长度 */
err = listen(ss, conf_para.MaxClient*2);
if (err)
{
printf ("listen() error\n");
ret = -3;
goto EXITshttpd_listen;
}
ret = ss;
EXITshttpd_listen:
return ret;
}
int l_main()
{
int ss = -1;
ss = do_listen();
return 0;
}
int main(int argc, char *argv[])
{
signal(SIGINT, sig_int);
Para_Init(argc,argv);
int s = do_listen();
Worker_ScheduleRun(s);
return 0;
}