This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.c
276 lines (255 loc) · 7.23 KB
/
visitor.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
#include "visitor.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
// #define _DEBUG
const char raw_buf_404[] = ""
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\"><title>404 Not Found"
"</title><h1>Not Found</h1><p>The requested URL was not found on the server. I"
"f you entered the URL manually please check your spelling and try again.</p>";
const char raw_type_html[] = "text/html";
// path of webroot, no last slash '/'
const char webroot_path[] = "./webroot";
const char default_document_name[] = "index.html";
int del_raw(RawContent *content)
{
if (content == NULL)
{
return -1;
}
if (content->raw_buf != NULL)
{
free(content->raw_buf);
content->raw_buf = NULL;
}
if (content->raw_type != NULL)
{
free(content->raw_type);
content->raw_type = NULL;
}
free(content);
return 1;
}
int is_folder(const char *path, int len)
{
return path[len - 1] == '/';
}
int is_cgi(const char *path, int len)
{
return len >= 4 && strcmp(path + len - 4, ".php") == 0;
}
int assign_content_type(RawContent *content, const char *type)
{
if (content->raw_type != NULL)
{
free(content->raw_type);
content->raw_type = NULL;
}
content->raw_type = (char *)malloc((strlen(type) + 1) * sizeof(char));
strcpy(content->raw_type, type);
return 1;
}
int get_content_type(const char *path, RawContent *content)
{
char *pos = strrchr(path, '/');
if (pos != NULL)
{
char *file_sufx = strrchr(pos, '.');
#ifdef _DEBUG
printf("[DEBUG] FileSuffix: %s\n", file_sufx);
#endif
if (file_sufx != NULL)
{
if (strcmp(file_sufx, ".html") == 0)
{
assign_content_type(content, "text/html");
}
else if (strcmp(file_sufx, ".txt") == 0)
{
assign_content_type(content, "text/plain");
}
else if (strcmp(file_sufx, ".css") == 0)
{
assign_content_type(content, "text/css");
}
else if (strcmp(file_sufx, ".js") == 0)
{
assign_content_type(content, "application/x-javascript");
}
else if (strcmp(file_sufx, ".png") == 0)
{
assign_content_type(content, "image/png");
}
else if (strcmp(file_sufx, ".jpg") == 0)
{
assign_content_type(content, "image/jpeg");
}
else if (strcmp(file_sufx, ".jpeg") == 0)
{
assign_content_type(content, "image/jpeg");
}
else if (strcmp(file_sufx, ".gif") == 0)
{
assign_content_type(content, "image/gif");
}
else
{
assign_content_type(content, "application/octet-stream");
}
}
else
{
#ifdef _DEBUG
printf("[DEBUG] NoSuffix\n");
#endif
assign_content_type(content, "application/octet-stream");
}
return 1;
}
else
{
return -1;
}
}
int get_raw_by_fp(FILE *const fp, const char *path, RawContent *content)
{
// get file size
struct stat stat_buf;
stat(path, &stat_buf);
int len_f = stat_buf.st_size;
// malloc space
content->raw_buf = (char *)malloc((len_f + 1) * sizeof(char));
content->raw_buf[len_f] = '\0';
content->raw_len = len_f;
// get content
int n_read = fread(content->raw_buf, sizeof(char), len_f, fp);
#ifdef _DEBUG
printf("[DEBUG] n_read len_f: %d %d\n", n_read, len_f);
printf("[DEBUG] get_raw_by_fp():\n%s\n", content->raw_buf);
assert(n_read == len_f);
#endif
if (n_read != len_f)
return -1;
return 1;
}
/*
get_raw_by_path()
args:
path: file path requested
retn:
content: type RawContent*, a struct made up as follows:
char* raw_buf : buffer of raw content
int raw_len : length of raw_buf
char* raw_type : content-type of resource
int raw_exist : 1 for found, 0 for not found
*/
RawContent *get_raw_by_path(const char *path)
{
int len_path = strlen(path);
int webroot_path_len = strlen(webroot_path);
int default_document_name_length = strlen(default_document_name);
#ifdef _DEBUG
printf("[DEBUG] path: %s\n", path);
// ensure valid path length
assert(len_path > 0);
assert(path[0] == '/');
#endif
// translate into relative path
// "${webroot_path}/${path}[/${default_document_name}]"
char *new_path =
(char *)malloc((webroot_path_len + len_path + default_document_name_length + 1 + 1 + 1) * sizeof(char));
strcpy(new_path, webroot_path);
strcat(new_path, "/");
strcat(new_path, path);
len_path = strlen(new_path);
#ifdef _DEBUG
printf("[DEBUG] new_path: %s\n", new_path);
#endif
// declare
RawContent *content = (RawContent *)malloc(sizeof(RawContent));
memset(content, 0, sizeof(RawContent));
// parse path and get content-type
if (is_folder(new_path, len_path))
{
#ifdef _DEBUG
printf("[DEBUG] PathType: folder\n");
#endif
strcat(new_path, "/");
strcat(new_path, default_document_name);
assign_content_type(content, raw_type_html);
}
else if (is_cgi(new_path, len_path))
{
#ifdef _DEBUG
printf("[DEBUG] PathType: cgi\n");
#endif
// get_cgi_result(new_path);
}
else
{
#ifdef _DEBUG
printf("[DEBUG] PathType: file\n");
#endif
get_content_type(new_path, content);
#ifdef _DEBUG
printf("[DEBUG] ContentType: %s\n", content->raw_type);
#endif
}
#ifdef _DEBUG
printf("[DEBUG] ReqrPath: %s\n", new_path);
#endif
content->raw_exist = 1;
// try to open file
FILE *fp = fopen(new_path, "rb");
if (fp == NULL)
{
// file not found
content->raw_exist = 0;
assign_content_type(content, raw_type_html);
char *default_404_path = (char *)malloc((strlen(webroot_path) + 10) * sizeof(char));
strcpy(default_404_path, webroot_path);
strcat(default_404_path, "/404.html");
#ifdef _DEBUG
printf("[DEBUG] 404path: %s\n", default_404_path);
#endif
fp = fopen(default_404_path, "rb");
if (fp == NULL)
{
// 404 file not exist
#ifdef _DEBUG
printf("[DEBUG] FileRet: 404 default\n");
#endif
// malloc space
int len_buf_404 = strlen(raw_buf_404);
content->raw_buf = (char *)malloc((len_buf_404 + 1) * sizeof(char));
content->raw_len = len_buf_404;
strcpy(content->raw_buf, raw_buf_404);
}
else
{
#ifdef _DEBUG
printf("[DEBUG] FileRet: 404 manual\n");
#endif
get_raw_by_fp(fp, default_404_path, content);
fclose(fp);
}
free(default_404_path);
default_404_path = NULL;
}
else
{
#ifdef _DEBUG
printf("[DEBUG] FileRet: file\n");
#endif
get_raw_by_fp(fp, new_path, content);
#ifdef _DEBUG
printf("[DEBUG] get_raw_by_path():\n%s\n", content->raw_buf);
#endif
fclose(fp);
}
free(new_path);
new_path = NULL;
return content;
}