-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttplibs.c
375 lines (322 loc) · 11 KB
/
httplibs.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
#include "httplibs.h"
int handle_request(const struct mime *mime_tbl, const struct cgi *cgi_tbl, const char *path_prefix, const char *default_page, const int sockfd) {
char buf[BUFFER_SIZE], local_path[BUFFER_SIZE];
char basic_request[3][BUFFER_SIZE], *content_type=0, *query=0;
struct request req;
static time_t t;
t = time(0);
memset(buf, 0, sizeof buf);
memset(local_path, 0, sizeof local_path);
memset(&req, 0, sizeof(struct request));
read_line(buf, sockfd);
sscanf(buf, "%s %s %s", basic_request[METHOD], basic_request[PATH], basic_request[PROC]);
const int type = parse_request_type(basic_request[METHOD]);
query = has_query_string(basic_request[PATH]);
if(query) {
*query = 0;
++query;
}
/* Add default page */
if(strlen(basic_request[PATH])==1&&basic_request[PATH][0]=='/') {
strcat(basic_request[PATH], default_page);
}
strncat(local_path, path_prefix, BUFFER_SIZE-1);
strncat(local_path, basic_request[PATH], BUFFER_SIZE-1);
req.type = type;
req.uri = basic_request[PATH];
req.local_path = local_path;
req.query_string = query;
fprintf(stderr, "[%s] %s %s\n", str_strip(ctime(&t)), basic_request[METHOD], basic_request[PATH]);
write_socket(STR_PROC, strlen(STR_PROC), sockfd);
if(type==GET) {
FILE *fp = fopen(local_path, "r");
if(fp==0) {
/* File doesn't exist */
write_socket(RES_404, strlen(RES_404), sockfd);
write_socket("\r\n", 2, sockfd);
} else {
write_socket(RES_200, strlen(RES_200), sockfd);
if(handle_cgi(&req, cgi_tbl, sockfd)==0) {
content_type = find_content_type(mime_tbl, determine_ext(req.local_path));
write_socket(FLD_CONTENT_TYPE, strlen(FLD_CONTENT_TYPE), sockfd);
write_socket(content_type, strlen(content_type), sockfd);
write_socket("\r\n", 2, sockfd);
write_socket("\r\n", 2, sockfd);
send_file(local_path, sockfd);
}
}
fclose(fp);
} else if(type==POST) {
if(handle_cgi(&req, cgi_tbl, sockfd)==0) {
return -1;
}
} else {
write_socket(RES_400, strlen(RES_400), sockfd);
write_socket("\r\n", 2, sockfd);
write_socket("\r\n", 2, sockfd);
}
return 0;
}
int handle_cgi(const struct request *req, const struct cgi *cgi, const int sockfd) {
char *cmd;
char buf[BUFFER_SIZE];
int cp[2], fork_stat;
if(cgi==0) return 0;
cmd = find_cgi_command(cgi, determine_ext(req->local_path));
if(cmd==0) return 0;
build_cgi_env(req);
if(req->type==GET) {
if(req->query_string) {
setenv("CONTENT_LENGTH", "", 1);
setenv("QUERY_STRING", req->query_string, 1);
}
if(pipe(cp)<0) {
fprintf(stderr, "Cannot pipe\n");
return -1;
}
pid_t pid;
if((pid = vfork()) == -1) {
fprintf(stderr, "Failed to fork new process\n");
return -1;
}
if(pid==0) {
close(sockfd);
close(cp[0]);
dup2(cp[1], STDOUT_FILENO);
execlp(cmd, cmd, req->local_path, (char*) 0);
exit(0);
} else {
close(cp[1]);
int len;
while((len=read(cp[0], buf, BUFFER_SIZE))>0) {
buf[len] = '\0';
str_strip(buf);
len = strlen(buf);
write_socket(buf, len, sockfd);
}
close(cp[0]);
waitpid((pid_t)pid, &fork_stat, 0);
}
} else if(req->type==POST) {
int post_pipe[2];
char content_len[BUFFER_SIZE];
write_socket(RES_200, strlen(RES_200), sockfd);
memset(buf, 0, sizeof buf);
while(read_line(buf, sockfd)) {
if(buf[0]=='\r') break;
char *ptr = buf;
while(*ptr!=':') ++ptr;
*ptr = 0;
if(strcmp("Content-Type", buf)==0) {
setenv("CONTENT_TYPE", str_strip(ptr+=2), 1);
}
memset(buf, 0, sizeof buf);
}
memset(buf, 0, sizeof buf);
read_socket(buf, BUFFER_SIZE, sockfd);
sprintf(content_len, "%d", (int) strlen(buf));
setenv("CONTENT_LENGTH", content_len, 1);
setenv("QUERY_STRING", buf, 1);
if(pipe(cp)<0||pipe(post_pipe)<0) {
fprintf(stderr, "Cannot pipe\n");
return -1;
}
pid_t pid;
if((pid = vfork()) == -1) {
fprintf(stderr, "Failed to fork new process\n");
return -1;
}
if(pid==0) {
close(sockfd);
close(cp[0]);
close(post_pipe[1]);
dup2(post_pipe[0], STDIN_FILENO);
close(post_pipe[0]);
dup2(cp[1], STDOUT_FILENO);
execlp(cmd, cmd, req->local_path, (char*) 0);
exit(0);
} else {
close(post_pipe[0]);
close(cp[1]);
write(post_pipe[1], buf, strlen(buf)+1);
close(post_pipe[1]);
memset(buf, 0, sizeof buf);
int len;
while((len=read(cp[0], buf, BUFFER_SIZE))>0) {
buf[len] = '\0';
str_strip(buf);
len = strlen(buf);
write_socket(buf, len, sockfd);
}
waitpid((pid_t)pid, &fork_stat, 0);
close(cp[0]);
write_socket("\r\n", 2, sockfd);
write_socket("\r\n", 2, sockfd);
}
}
return 1;
}
int parse_request_type(const char *buf) {
if(strcmp(buf, "GET")==0) {
return GET;
} else if (strcmp(buf,"POST")==0) {
return POST;
}
return 0;
}
struct mime * init_mime_table() {
char buf[BUFFER_SIZE], ext[BUFFER_SIZE], type[BUFFER_SIZE];
struct mime *ptr = 0, *root = 0;
FILE *fp = fopen("mime.types","r");
int i;
memset(buf, 0, sizeof buf);
if(fp==0) {
fprintf(stderr, "Cannot open mime.types\n");
fclose(fp);
return 0;
}
/* Initialize two-level structure */
root = (struct mime*) malloc(sizeof(struct mime));
memset(root, 0, sizeof(struct mime));
root->next_level = (struct mime*) malloc(sizeof(struct mime) * 26);
memset(root->next_level, 0, sizeof(struct mime) * 26);
for(i=0;i<26;i++) {
root->next_level[i].next_level = (struct mime*) malloc(sizeof(struct mime) * 26);
memset(root->next_level[i].next_level, 0, sizeof(struct mime) * 26);
}
while(fgets(buf, BUFFER_SIZE, fp)!=0) {
sscanf(buf, "%s %s", ext, type);
/* Create new node in advance */
ptr = (struct mime*) malloc(sizeof(struct mime));
memset(ptr, 0, sizeof(struct mime));
ptr->ext = (char*) malloc(sizeof(char) * (strlen(ext)+1));
ptr->type = (char*) malloc(sizeof(char) * (strlen(type)+1));
strncat(ptr->ext, ext, strlen(ext));
strncat(ptr->type, type, strlen(type));
struct mime *first_level = &(root->next_level[ext[0]-'a']);
struct mime *second_level = &(first_level->next_level[ext[1]-'a']);
struct mime *start = (strlen(ext)==1) ? first_level : second_level;
if(start->ext==0) {
/* Use existing node if the node is empty */
start->ext = (char*) malloc(sizeof(char) * (strlen(ext)+1));
memset(start->ext, 0, sizeof(char) * (strlen(ext)+1));
start->type = (char*) malloc(sizeof(char) * (strlen(type)+1));
memset(start->type, 0, sizeof(char) * (strlen(type)+1));
strncat(start->ext, ext, strlen(ext));
strncat(start->type, type, strlen(type));
free(ptr);
} else {
/* Append previously created node to last */
while(start->next!=0) start = start->next;
start->next = ptr;
}
memset(buf, 0, sizeof buf);
}
fclose(fp);
return root;
}
char * find_content_type(const struct mime *tbl, const char *ext) {
struct mime *first_level = &(tbl->next_level[ext[0]-'a']);
struct mime *second_level = &(first_level->next_level[ext[1]-'a']);
struct mime *start = (strlen(ext)==1) ? first_level : second_level;
while(start) {
if(strcmp(ext, start->ext)==0) {
return start->type;
}
start = start->next;
}
return 0;
}
char * find_cgi_command(const struct cgi *tbl, const char *ext) {
while(tbl!=0) {
if(strcmp(tbl->ext,ext)==0) return tbl->cmd;
tbl = tbl->next;
}
return 0;
}
char * determine_ext(const char *path) {
const int len = strlen(path);
path += len-1;
while(*path!='.') --path;
return (char*)(path+1);
}
int build_cgi_env(const struct request *req) {
const char* local_path = req->local_path;
const char* uri = req->uri;
const int req_type = req->type;
setenv("SERVER_NAME", "localhost", 1);
setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
setenv("REQUEST_URI", uri, 1);
setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
setenv("REDIRECT_STATUS", "200", 1);
setenv("SCRIPT_FILENAME", local_path, 1);
setenv("SCRIPT_NAME", uri, 1);
if(req_type==GET) {
setenv("REQUEST_METHOD", "GET", 1);
} else if(req_type==POST) {
setenv("REQUEST_METHOD", "POST", 1);
}
return 0;
}
char * has_query_string(const char *uri) {
while(*uri!=0) {
if(*uri=='?') return (char *) uri;
++uri;
}
return 0;
}
char * str_strip(char *str) {
char *ptr = str;
while(*ptr!=0) ptr++;
--ptr;
while((isspace(*ptr))) {
*ptr = 0;
ptr--;
}
return str;
}
struct cgi * init_cgi_table() {
char buf[BUFFER_SIZE], key[BUFFER_SIZE], val[BUFFER_SIZE];
struct cgi *ptr = 0, *root = 0;
FILE *fp = fopen("cgi.conf","r");
memset(buf, 0, sizeof buf);
if(fp==0) {
fprintf(stderr, "Cannot open cgi.conf\n");
fclose(fp);
return 0;
}
while(fgets(buf, BUFFER_SIZE, fp)!=0) {
str_strip(buf);
if(strcmp("[CGI]", buf)!=0) {
return 0;
}
int i;
if(ptr==0) {
ptr = (struct cgi*) malloc(sizeof(struct cgi));
root = ptr;
} else {
ptr->next = (struct cgi*) malloc(sizeof(struct cgi));
ptr = ptr->next;
}
ptr->next = 0;
for(i=0;i<2;i++) {
if(fgets(buf, BUFFER_SIZE, fp)==0) {
free(ptr);
return 0;
}
sscanf(buf, "%s %s", key, val);
if(strcmp("EXTNAME", key)==0) {
ptr->ext = (char*) malloc(sizeof(char) * (strlen(val)+1));
memset(ptr->ext, 0, sizeof(char) * (strlen(val)+1));
strncat(ptr->ext, val, strlen(val));
} else if(strcmp("CMD", key)==0) {
ptr->cmd = (char*) malloc(sizeof(char) * (strlen(val)+1));
memset(ptr->cmd, 0, sizeof(char) * (strlen(val)+1));
strncat(ptr->cmd, val, strlen(val));
}
memset(buf, 0, sizeof buf);
}
}
fclose(fp);
return root;
}