-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.c
370 lines (342 loc) · 10 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "httpd.h"
void usage(const char *proc)
{
printf("Usage : %s [PORT]\n", proc);
}
static void not_found(int client)
{
}
void print_debug(const char *msg)
{
printf("%s\n", msg);
}
static void bad_request(int client)
{
print_debug("enter our fault...\n");
char buf[1024];
sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");
send(client, buf,strlen(buf),0);
sprintf(buf, "Content-type: text/html\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "\r\n");
send(client,buf,strlen(buf), 0);
sprintf(buf, "<html></br><p>your enter message is a bad request</p></br><html>\r\n");
send(client, buf, strlen(buf), 0);
}
void print_log(const char *fun, int line, int err_no, const char *err_str)
{
printf("[%s: %d] [%d] [%s]\n", fun, line, err_no, err_str);
}
void clear_header(int client)
{
char buf[1024];
memset(buf, '\0', sizeof(buf));
int ret = 0;
do{
ret = get_line(client, buf, sizeof(buf));
}while(ret > 0 && strcmp(buf, "\n") != 0);
}
int get_line(int sock, char *buf, int max_len)
{
if( !buf || max_len < 0){
return -1;
}
int i = 0;
int n = 0;
char c = '\0';
while( i < max_len-1 && c != '\n' ){
n = recv(sock, &c, 1, 0);
if(n > 0){//success
if( c== '\r' ){
n = recv(sock, &c, 1,MSG_PEEK);
if( n > 0 && c== '\n' ){//windows
recv(sock, &c, 1, 0);//delete
}else{
c = '\n';
}
}
buf[i++] = c;
}else{//failed
c= '\n';
}
}
buf[i] = '\0';
return i;
}
void echo_error_to_client(int client, int error_code)
{
switch(error_code){
case 400://request error
bad_request(client);
break;
case 404://not found
not_found(client);
break;
case 500://server error
//server_error(client);
break;
case 503://server unavailable
//server_unavailable(client);
break;
default:
//default_error(client);
break;
}
}
void echo_html(int client, const char *path, unsigned int file_size)
{
if( !path){
return;
}
int in_fd = open(path, O_RDONLY);
if(in_fd < 0){
print_debug("open index.html error");
// echo_error_to_client();
return;
}
print_debug("open index.html success");
char echo_line[1024];
memset(echo_line, '\0', sizeof(echo_line));
strncpy(echo_line, HTTP_VERSION, strlen(HTTP_VERSION)+1);
strcat(echo_line, "200 ok");
strcat(echo_line,"\r\n\r\n");
send(client, echo_line, strlen(echo_line), 0);
print_debug("send echo head success");
if(sendfile(client, in_fd, NULL, file_size) < 0 ){
print_debug("send_file error");
// echo_error_to_client();
close(in_fd);
return;
}
print_debug("sendfile success");
close(in_fd);
}
void exe_cgi(int sock_client, const char *path, const char *method, const char *query_string)
{
print_debug("enter cgi\n");
char buf[_COMM_SIZE_];
int numchars = 0;
int content_length = -1;
//pipe
int cgi_input[2] = {0, 0};
int cgi_output[2] = {0, 0};
//child proc
pid_t id;
print_debug(method);
if(strcasecmp(method, "GET") == 0){//GET
clear_header(sock_client);
}else{//POST
do{
memset(buf, '\0', sizeof(buf));
numchars = get_line(sock_client, buf, sizeof(buf));
if(strncasecmp(buf, "Content-Length",strlen("Content-Length"))==0){
content_length = atoi(&buf[16]);
}
}while(numchars > 0 && strcmp(buf, "\n") != 0);
if( content_length == -1 ){
// echo_error_to_client();
return;
}
}
memset(buf, '\0', sizeof(buf));
strcpy(buf, HTTP_VERSION);
strcat(buf, " 200 OK \r\n\r\n");
send(sock_client, buf, strlen(buf), 0);
if( pipe(cgi_input) == -1 ){//pipe error
// echo_error_to_client();
return;
}
if( pipe(cgi_output) == -1){
close(cgi_input[0]);
close(cgi_input[1]);
//echo_erro_to_client();
return;
}
if( (id = fork())<0){//fork error
close(cgi_input[0]);
close(cgi_input[1]);
close(cgi_output[0]);
close(cgi_output[1]);
// echo_error_to_client();
return;
}else if( id == 0 ){//child
char query_env[_COMM_SIZE_/10];
char method_env[_COMM_SIZE_];
char content_len_env[_COMM_SIZE_];
memset(method_env, '\0', sizeof(method_env));
memset(query_env, '\0', sizeof(query_env));
memset(content_len_env, '\0',sizeof(content_len_env));
close(cgi_input[1]);
close(cgi_output[0]);
//redir
dup2(cgi_input[0], 0);
dup2(cgi_output[1], 1);
sprintf(method_env, "REQUEST_METHOD=%s", method);
putenv(method_env);
if(strcasecmp("GET", method) == 0){//POST
sprintf(query_env, "QUERY_STRING=%s",query_string);
putenv(query_env);
}else{//POST
sprintf(content_len_env, "CONTENT_LENGTH=%d", content_length);
putenv(content_len_env);
}
execl(path, path, NULL);
exit(1);
}else{//father
close(cgi_input[0]);
close(cgi_output[1]);
int i = 0;
char c = '\0';
if(strcasecmp("POST", method) == 0){
for(; i < content_length; i++){
recv(sock_client, &c, 1, 0);
write(cgi_input[1], &c, 1);
}
}
while( read(cgi_output[0], &c, 1) > 0){
send(sock_client, &c, 1, 0);
}
close(cgi_input[1]);
close(cgi_output[0]);
waitpid(id, NULL ,0);
}
}
//GET &&POST
void *accept_request(void *arg)
{
print_debug("get a new connect...\n");
pthread_detach(pthread_self());
int sock_client = (int)arg;
int cgi = 0;
char *query_string = NULL;
char method[_COMM_SIZE_/10];
char url[_COMM_SIZE_];
char buffer[_COMM_SIZE_];
char path[_COMM_SIZE_];
memset(method, '\0', sizeof(method));
memset(url, '\0', sizeof(url));
memset(buffer, '\0', sizeof(buffer));
memset(path,'\0', sizeof(path));
if(get_line(sock_client, buffer, sizeof(buffer)) < 0){
//echo_error_to_client();
return NULL;
}
int i = 0;
int j = 0;//buffer line index
while( !isspace(buffer[j]) &&\
i < sizeof(method)-1 &&\
j < sizeof(buffer)){
method[i] = buffer[j];
i++,j++;
}
if( strcasecmp(method, "GET") && strcasecmp(method, "POST")){
//echo_error_to_client();
return NULL;
}
//clear space point useful url start
while( isspace(buffer[j]) &&\
j < sizeof(buffer)){
j++;
}
//get url
i = 0;
while(!isspace(buffer[j]) &&\
i < sizeof(url)-1 &&\
j < sizeof(buffer)){
url[i] = buffer[j];
i++;
j++;
}
print_debug(method);
print_debug(url);
if(strcasecmp(method, "POST") == 0){
cgi = 1;
}
if(strcasecmp(method, "GET") == 0){
query_string = url;
while( *query_string != '?' && *query_string != '\0'){
query_string++;
}
if( *query_string == '?'){//url = /xxx/xxx +arg
*query_string = '\0';
query_string++;
cgi = 1;
}
}
sprintf(path, "htdocs%s", url);
if(path[strlen(path)-1] == '/'){
strcat(path, MAIN_PAGE);
}
print_debug(path);
struct stat st;
if( stat(path, &st) < 0){
print_debug("miss cgi");
clear_header(sock_client);
// echo_error_to_client();
}else{//file exist!
if(S_ISDIR(st.st_mode)){
strcat(path, "/");
strcat(path, MAIN_PAGE);
}else if(st.st_mode & S_IXUSR ||\
st.st_mode & S_IXGRP ||\
st.st_mode & S_IXOTH){
cgi = 1;
}else{
}
if(cgi){
exe_cgi(sock_client, path, method, query_string);
}else{
clear_header(sock_client);
print_debug("begin enter our echo_html");
echo_html(sock_client, path, st.st_size);
}
}
close(sock_client);
return NULL;
}
int start(short port)
{
int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if(listen_sock == -1){
print_log(__FUNCTION__, __LINE__, errno, strerror(errno));
exit(1);
}
int flag = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_port =htons(port);
local.sin_addr.s_addr =htonl(INADDR_ANY);
socklen_t len = sizeof(local);
if(bind(listen_sock, (struct sockaddr*)&local, len) == -1){
print_log(__FUNCTION__, __LINE__, errno, strerror(errno));
exit(2);
}
if(listen(listen_sock, _BACK_LOG_) == -1){
print_log(__FUNCTION__,__LINE__, errno,strerror(errno));
exit(3);
}
return listen_sock;
}
//./httpd ip[option] port
int main(int argc,char *argv[])
{
if(argc != 2){
usage(argv[0]);
exit(1);
}
int port = atoi(argv[1]);
int sock = start(port);
struct sockaddr_in client;
socklen_t len = 0;
while(1){
int new_sock = accept(sock, (struct sockaddr*)&client, &len);
if(new_sock < 0){//accept error
print_log(__FUNCTION__,__LINE__, errno, strerror(errno));
continue;
}
pthread_t new_thread;
pthread_create(&new_thread, NULL, accept_request, (void*)new_sock);
}
return 0;
}