-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
427 lines (360 loc) · 13 KB
/
server.c
File metadata and controls
427 lines (360 loc) · 13 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <curl/curl.h>
#include <regex.h>
#define PORT 8080
#define BUF_SIZE 8192
#define WEB_ROOT "./web_root"
#define UPLOAD_DIR "./web_root/upload_dir"
#define CRAWL_DIR "./web_root/crawl_result"
#define HTML_FILE "./web_root/index.html"
#define MAX_FILENAME 256
void error(const char *msg) {
perror(msg);
exit(1);
}
void send_404(int client_sock) {
const char *not_found = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
send(client_sock, not_found, strlen(not_found), 0);
}
void send_response(int client_sock, const char *content_type, const char *body) {
char header[1024];
snprintf(header, sizeof(header),
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n",
content_type, strlen(body));
send(client_sock, header, strlen(header), 0);
send(client_sock, body, strlen(body), 0);
}
void send_file(int client_sock, const char *filepath) {
int file_fd = open(filepath, O_RDONLY);
if (file_fd < 0) {
send_404(client_sock);
return;
}
char buffer[BUF_SIZE];
struct stat st;
fstat(file_fd, &st);
snprintf(buffer, sizeof(buffer),
"HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: %ld\r\nContent-Disposition: attachment; filename=\"%s\"\r\n\r\n",
st.st_size, strrchr(filepath, '/') ? strrchr(filepath, '/') + 1 : filepath);
send(client_sock, buffer, strlen(buffer), 0);
ssize_t n;
while ((n = read(file_fd, buffer, BUF_SIZE)) > 0) {
send(client_sock, buffer, n, 0);
}
close(file_fd);
}
void handle_index(int client_sock) {
int fd = open(HTML_FILE, O_RDONLY);
if (fd < 0) {
perror("open index.html");
send_404(client_sock);
return;
}
struct stat st;
fstat(fd, &st);
char header[256];
snprintf(header, sizeof(header),
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %ld\r\n\r\n",
st.st_size);
send(client_sock, header, strlen(header), 0);
char buf[BUF_SIZE];
ssize_t n;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
send(client_sock, buf, n, 0);
}
close(fd);
}
void parse_request(char *request, char *method, char *path) {
sscanf(request, "%s %s", method, path);
}
void save_uploaded_files(const char *body) {
const char *p = body;
while ((p = strstr(p, "filename=\"")) != NULL) {
char filename[MAX_FILENAME] = {0};
sscanf(p + 10, "%[^\"]", filename);
if (strlen(filename) == 0) break;
char *file_start = strstr(p, "\r\n\r\n");
if (!file_start) break;
file_start += 4;
char *file_end = strstr(file_start, "\r\n------");
if (!file_end) file_end = file_start + strlen(file_start);
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", UPLOAD_DIR, filename);
FILE *fp = fopen(fullpath, "wb");
if (fp) {
fwrite(file_start, 1, file_end - file_start, fp);
fclose(fp);
}
p = file_end;
}
}
void handle_upload(int client_sock, char *body) {
save_uploaded_files(body);
send_response(client_sock, "text/plain", "Upload complete.\n");
}
void handle_mkdir(int client_sock, char *body) {
char dirname[MAX_FILENAME] = {0};
sscanf(body, "name=%s", dirname);
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", UPLOAD_DIR, dirname);
if (mkdir(fullpath, 0755) == 0) {
send_response(client_sock, "text/plain", "Directory created.\n");
} else {
send_response(client_sock, "text/plain", "Failed to create directory.\n");
}
}
void delete_recursive(const char *path) {
struct stat st;
if (stat(path, &st) != 0) return;
if (S_ISDIR(st.st_mode)) {
DIR *d = opendir(path);
struct dirent *entry;
while ((entry = readdir(d))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name);
delete_recursive(fullpath);
}
closedir(d);
rmdir(path);
} else {
remove(path);
}
}
void handle_delete(int client_sock, char *body) {
char target[MAX_FILENAME] = {0};
sscanf(body, "name=%s", target);
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", UPLOAD_DIR, target);
delete_recursive(fullpath);
send_response(client_sock, "text/plain", "Target deleted.\n");
}
struct MemoryStruct {
char *memory;
size_t size;
};
size_t write_mem_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
void download_image(const char *url, const char *base_dir) {
CURL *curl = curl_easy_init();
if (!curl) return;
const char *filename = strrchr(url, '/');
if (!filename) filename = "img.jpg"; else filename++;
char filepath[512];
snprintf(filepath, sizeof(filepath), "%s/%s", base_dir, filename);
FILE *fp = fopen(filepath, "wb");
if (!fp) {
curl_easy_cleanup(curl);
return;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_perform(curl);
fclose(fp);
curl_easy_cleanup(curl);
}
void extract_and_download_images(const char *html, const char *base_dir, const char *page_url) {
regex_t regex;
regcomp(®ex, "<img[^>]*src=[\"']([^\"']+)[\"']", REG_EXTENDED);
regmatch_t matches[2];
const char *p = html;
while (regexec(®ex, p, 2, matches, 0) == 0) {
char img_url[512] = {0};
int len = matches[1].rm_eo - matches[1].rm_so;
strncpy(img_url, p + matches[1].rm_so, len);
img_url[len] = '\0';
// Normalize URL
char full_url[1024] = {0};
if (strstr(img_url, "http://") == img_url || strstr(img_url, "https://") == img_url) {
snprintf(full_url, sizeof(full_url), "%s", img_url);
} else if (img_url[0] == '/') {
// Get domain from page_url
char domain[512] = {0};
sscanf(page_url, "%[^/]//%[^/]", domain, domain);
snprintf(full_url, sizeof(full_url), "%s%s", domain, img_url);
} else {
// Relative path to page_url
const char *last_slash = strrchr(page_url, '/');
if (last_slash) {
size_t base_len = last_slash - page_url + 1;
snprintf(full_url, sizeof(full_url), "%.*s%s", (int)base_len, page_url, img_url);
} else {
snprintf(full_url, sizeof(full_url), "%s/%s", page_url, img_url);
}
}
download_image(full_url, base_dir);
p += matches[0].rm_eo;
}
regfree(®ex);
}
void extract_text(const char *html, const char *text_path) {
FILE *fp = fopen(text_path, "w");
if (!fp) return;
int in_tag = 0;
for (const char *p = html; *p; ++p) {
if (*p == '<') {
in_tag = 1;
} else if (*p == '>') {
in_tag = 0;
} else if (!in_tag) {
fputc(*p, fp);
}
}
fclose(fp);
}
void handle_crawl(int client_sock, char *body) {
char url[512];
sscanf(body, "url=%s", url);
CURL *curl = curl_easy_init();
if (!curl) {
send_response(client_sock, "text/plain", "CURL init failed\n");
return;
}
struct MemoryStruct html = {malloc(1), 0};
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_mem_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&html);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (html.size > 0) {
char html_path[512], text_path[512];
snprintf(html_path, sizeof(html_path), "%s/crawl.html", CRAWL_DIR);
snprintf(text_path, sizeof(text_path), "%s/crawl.txt", CRAWL_DIR);
FILE *fp = fopen(html_path, "wb");
if (fp) {
fwrite(html.memory, 1, html.size, fp);
fclose(fp);
}
extract_text(html.memory, text_path);
extract_and_download_images(html.memory, CRAWL_DIR, url);
free(html.memory);
send_response(client_sock, "text/plain", "Crawling complete. HTML, text and images saved.\n");
} else {
send_response(client_sock, "text/plain", "Failed to fetch HTML content.\n");
}
}
void list_files(const char *base, char *result, int depth) {
DIR *d = opendir(base);
struct dirent *entry;
if (!d) return;
while ((entry = readdir(d))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", base, entry->d_name);
struct stat st;
stat(fullpath, &st);
for (int i = 0; i < depth; ++i) strcat(result, " ");
strcat(result, entry->d_name);
strcat(result, S_ISDIR(st.st_mode) ? "/\n" : "\n");
if (S_ISDIR(st.st_mode)) {
list_files(fullpath, result, depth + 1);
}
}
closedir(d);
}
void handle_list(int client_sock) {
char result[BUF_SIZE * 10] = "";
list_files(UPLOAD_DIR, result, 0);
send_response(client_sock, "text/plain", result);
}
void handle_client(int client_sock) {
char buffer[BUF_SIZE] = {0};
int bytes = recv(client_sock, buffer, sizeof(buffer) - 1, 0);
if (bytes <= 0) {
close(client_sock);
return;
}
char method[8], path[256];
parse_request(buffer, method, path);
char *body = strstr(buffer, "\r\n\r\n");
if (body) body += 4; else body = "";
// 拆分 query string
char *query = strchr(path, '?');
if (query) *query++ = '\0'; // 切掉 path,query 指向 f=xxx
if (strcmp(path, "/") == 0 || strcmp(path, "/index.html") == 0) {
handle_index(client_sock);
} else if (strcmp(path, "/download") == 0 && query && strncmp(query, "f=", 2) == 0) {
char *encoded = query + 2;
// URL 解码
char filename[MAX_FILENAME] = {0};
int j = 0;
for (int i = 0; encoded[i] && j < MAX_FILENAME - 1; ++i) {
if (encoded[i] == '%' && isxdigit(encoded[i+1]) && isxdigit(encoded[i+2])) {
char hex[3] = {encoded[i+1], encoded[i+2], 0};
filename[j++] = (char) strtol(hex, NULL, 16);
i += 2;
} else if (encoded[i] == '+') {
filename[j++] = ' '; // '+' 转为空格
} else {
filename[j++] = encoded[i];
}
}
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s", UPLOAD_DIR, filename);
printf("[DEBUG] Looking for file: %s\n", fullpath);
send_file(client_sock, fullpath);
} else if (strcmp(path, "/upload") == 0 && strcmp(method, "POST") == 0) {
handle_upload(client_sock, body);
} else if (strcmp(path, "/mkdir") == 0 && strcmp(method, "POST") == 0) {
handle_mkdir(client_sock, body);
} else if (strcmp(path, "/delete") == 0 && strcmp(method, "POST") == 0) {
handle_delete(client_sock, body);
} else if (strcmp(path, "/crawl") == 0 && strcmp(method, "POST") == 0) {
handle_crawl(client_sock, body);
} else if (strcmp(path, "/files") == 0 && strcmp(method, "GET") == 0) {
handle_list(client_sock);
} else {
send_404(client_sock);
}
close(client_sock);
}
int main() {
mkdir(WEB_ROOT, 0755);
mkdir(UPLOAD_DIR, 0755);
mkdir(CRAWL_DIR, 0755);
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) error("socket");
int opt = 1;
setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in server_addr = {0};
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
error("bind");
if (listen(server_sock, 10) < 0)
error("listen");
printf("\u2705 Server running at: http://localhost:%d\n", PORT);
while (1) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_len);
if (client_sock >= 0) {
handle_client(client_sock);
}
}
close(server_sock);
return 0;
}