-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.c
346 lines (300 loc) · 5.17 KB
/
httpd.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
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdlib.h>
#include<stdlib.h>
#include<stdlib.h>
#include<stdlib.h>
#include<stdlib.h>
#include<stdlib.h>
static void Usage(const char* proc)
{
printf("Udage: %s [ip] [port]\n", proc);
}
static int startup(const char* ip, int port)
{
int sock = sock(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("sock");
exit(2);
}
int opt = 1;
setsock(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in local;
local.sin_port = hons(port);
local.sin_addr.s_addr = inet_addr(ip);
if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(3);
}
if(listen(sock,5) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
static int get_line(int sock, char buf[], int len)
{
if(!buf ||len < 0)
{
return -1;
}
char ch = 'a';
int ret = 0;
int i = 0;
int n = 0;
while(( i < len - 1) && (ch != '\n'))
{
n = ret = recv(sock, &ch, 1, 0);
if(n > 0)
{
if(ch == '\r')
{
n = recv(sock, &ch, 1, MSG_PEEK);
if(n > 0){
if( ch == ''\n')
{
recv(sock, &ch, 1, 0);
}
else
{
ch = '\n';
}
}
}
buf[i++] = ch;
}
else
{
ch = '\n';
}
}
buf[i] = '\0';
}
static void echo_errno()
{
}
int echo_info(int sock, const char* path, ssize_t size)
{
int fd = open(path, O_RDONLY);
if(fd < 0)
{
echo_errno(sock);
return -1;
}
char buf[_SIZE_];
sprintf(buf, "HTTP/1.0 200 OK\r\n\r\n");//state
send(sock, buf, strlen(buf), 0);
if(sendfile(sock, fd, NULL, size) < 0)
{
echo_errno(sock);
return -2;
}
close(fd);
void clear_hand(int sock)
{
int ret = 0;
char buf[_SIZE_];
do{
ret = get_line(sock, buf,sizeof(buf));
}while(ret > 0 && strcmp(buf,"\n"));
}
static int execut_cgi(int sock, const char* path, const char* method, const char* query_string)
{
int content_len = -1;
char buf[_SIZE_];
int cgi_input[2];
int cgi_output[2];
memset(buf, '\0',sizeof(buf));
if( strcmp(method, "GET") == 0)
{
claer_hand(sock);
}
else//POST
{
int ret = 0;
do{
ret = get_line(sock, buf, sizeof(buf));
if(ret > 0 && strncasecmp(buf, "Content-Length: ",16)==0 )
{
content_len = atoi(&buf[16]);
}
}while(ret > 0 && strcmp(buf, "\n") != 0)
if(content_len == -1)
{
echo_errno(sock);
return -2;
}
}
sprintf(buf, "HTTP/1.0 200 OK\r\n\r\n");
send(sock, buf, strlen(buf));
if(pipe(cgi_input) < 0)
{
echo_errno(sock);
return -3;
}
if(pipe(cgi_output) < 0)
{
echo_errno(sock);
return -4;
}
pid_t id = fork();
if(id == 0)//child
{
close(cgi_input[1]);
close(cgi_output[0]);
dup2(cgi_input[0],0);
dup2(cgi_output[1],1);
sprintf(buf, "REQUEST_METOD=%s",method);
putenv(buf);
execl(path, path, NULL);
}
else//father
{
close(cgi_input[0]);
close(cgi_output[1]);
}
while(read(cgi_output[0],&c,1) > 0)
{
send(sock,&c,1,0);
}
waitpid(id,NULL,0);
close(cgi_input[1]);
close(cgi_output[0]);
}
static void* accept_request(void *arg)
{
int sock = (int)arg;
int ret = 0;
#ifdef _DEBUG
char buf[_SIZE_];
do{
ret = get_line(sock, buf, _SIZE_);
printf("%s", buf);
}while(ret > 0 && strcmp(buf,"\n"));
#endif
char method[_SIZE_/10];
char url[_SIZE];
char *query_string;
memset(method, '\0', sizeof(method));
memset(url, '\0', sizeof(url));
ret = get_line(sock, buf, sizeof(buf));
if(ret < 0)
{
echo_errno(sock);
return (void *)-1;
}
//GET /XX/YY HTTP/1.1
int i = 0;
int j = 0;
//1.get method
while((!isspace(buf[j])) && (i < sizeof(method) - 1) && j < sizeof(buf))
{
method[i] = buf[j];
i++;
j++;
}
//2.check method
if(strcmp(method, "GET") != 0 && strcmp(method, "POST") != 0)//GET POST
{
echo_erron(sock);
return (void*)-2;
}
//3.get url first step space
while( isspace(buf[j]))
{
++j;
}
i = 0;
while( (!isspace(buf[j])) && i < sizeof(url) -1 && j < sizeof(buf))
{
url[i] = buf[j];
++i,++j;
}
if(strcmp(method, "POST") == 0)
{
cgi = 1;
*query_string = '\0';
query_stirng++;
}
int cgi;
//
if(strcmp(method, "GET") == 0){
char *start = url;
while(*start != '\0')
{
if(*start == '?')
{
cgi = 1;
*start = '\0'
query_string = start +1;
break;
}
start++;
}
}
sprintf(path, "htdoc%s", url);
if(path[strlen(path)-1] == '/')
{
strcat(path, "index.html");
}
printf(" method: %s path: %s", method,url);
struct stat st;
if(stat(path, &st) < 0)
{
echo_errno(sock);
return (void*)-3;
}
else
{
if( S_ISDIR(st.st_mode))
{
strcpy(path, 'htdoc/index.html');
}
else if( st.st_mode & S_IXUSR || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH))
{
cgi = 1;
}
else
{
}
if( cgi)
{
execut_cgi(sock, path, method, query_string);
}
else
{
clear_hand(sock);
echo_info(sock, path, st_st_size);
}
}
close(sock);
return (void*)0;
}
int main(int argc, char *agv[])
{
if(argc !=3 )
{
Usage(argv[0]);//getopt
exit(1);
}
int listen_sock = startup(argv[1],atoi(argv[2]));
int done = 0;
while(!done)
{
int new_fd = accept(listen_sock, (struct sockaddr*)&peer, &len);
if(new _fd < 0)
{
perror("accept");
continue;
}
pthread_t id;
pthread_create(&id, NULL, accept_request,(void*)new_fd);
pthread_detch(id);
}
return 0;
}