1 | /* $Id: server.c 60 2024-09-18 12:30:33Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "../config.h"
|
---|
6 |
|
---|
7 | #include "tw_server.h"
|
---|
8 |
|
---|
9 | #ifndef NO_SSL
|
---|
10 | #include "tw_ssl.h"
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "tw_config.h"
|
---|
14 | #include "tw_http.h"
|
---|
15 | #include "tw_module.h"
|
---|
16 | #include "tw_version.h"
|
---|
17 |
|
---|
18 | #include <unistd.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <stdbool.h>
|
---|
21 | #include <stdarg.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <sys/stat.h>
|
---|
25 | #include <time.h>
|
---|
26 |
|
---|
27 | #include <cm_string.h>
|
---|
28 | #include <cm_log.h>
|
---|
29 | #include <cm_dir.h>
|
---|
30 |
|
---|
31 | #ifdef __MINGW32__
|
---|
32 | #include <winsock2.h>
|
---|
33 | #include <process.h>
|
---|
34 |
|
---|
35 | #include "strptime.h"
|
---|
36 | #else
|
---|
37 | #include <sys/select.h>
|
---|
38 | #include <sys/socket.h>
|
---|
39 | #include <arpa/inet.h>
|
---|
40 | #include <netinet/in.h>
|
---|
41 | #include <netinet/tcp.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | extern struct tw_config config;
|
---|
45 | extern char tw_server[];
|
---|
46 |
|
---|
47 | fd_set fdset;
|
---|
48 | int sockcount = 0;
|
---|
49 |
|
---|
50 | SOCKADDR addresses[MAX_PORTS];
|
---|
51 | int sockets[MAX_PORTS];
|
---|
52 |
|
---|
53 | /* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
|
---|
54 | int tw_wildcard_match(const char* wildcard, const char* target) {
|
---|
55 | const char *pw = wildcard, *pt = target;
|
---|
56 |
|
---|
57 | while(1) {
|
---|
58 | if(*pt == 0) {
|
---|
59 | while(*pw == '*') pw++;
|
---|
60 | return *pw == 0;
|
---|
61 | } else if(*pw == 0) {
|
---|
62 | return 0;
|
---|
63 | } else if(*pw == '*') {
|
---|
64 | return *(pw + 1) == 0 || tw_wildcard_match(pw, pt + 1) || tw_wildcard_match(pw + 1, pt);
|
---|
65 | } else if(*pw == '?' || (*pw == *pt)) {
|
---|
66 | pw++;
|
---|
67 | pt++;
|
---|
68 | continue;
|
---|
69 | } else {
|
---|
70 | return 0;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void close_socket(int sock) {
|
---|
76 | #ifdef __MINGW32__
|
---|
77 | closesocket(sock);
|
---|
78 | #else
|
---|
79 | close(sock);
|
---|
80 | #endif
|
---|
81 | }
|
---|
82 |
|
---|
83 | int tw_server_init(void) {
|
---|
84 | int i;
|
---|
85 | #ifdef __MINGW32__
|
---|
86 | WSADATA wsa;
|
---|
87 | WSAStartup(MAKEWORD(2, 0), &wsa);
|
---|
88 | #endif
|
---|
89 | for(i = 0; config.ports[i] != -1; i++)
|
---|
90 | ;
|
---|
91 | sockcount = i;
|
---|
92 | for(i = 0; config.ports[i] != -1; i++) {
|
---|
93 | #ifdef NO_IPV6
|
---|
94 | int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
95 | #else
|
---|
96 | int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
---|
97 | #endif
|
---|
98 | #ifdef __MINGW32__
|
---|
99 | if(sock == INVALID_SOCKET)
|
---|
100 | #else
|
---|
101 | if(sock < 0)
|
---|
102 | #endif
|
---|
103 | {
|
---|
104 | cm_log("Server", "Socket creation failure");
|
---|
105 | return 1;
|
---|
106 | }
|
---|
107 | int yes = 1;
|
---|
108 | if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
|
---|
109 | close_socket(sock);
|
---|
110 | cm_log("Server", "setsockopt failure (reuseaddr)");
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 | if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
|
---|
114 | close_socket(sock);
|
---|
115 | cm_log("Server", "setsockopt failure (nodelay)");
|
---|
116 | return 1;
|
---|
117 | }
|
---|
118 | #ifndef NO_IPV6
|
---|
119 | int no = 0;
|
---|
120 | if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
|
---|
121 | close_socket(sock);
|
---|
122 | cm_log("Server", "setsockopt failure (IPv6)");
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 | memset(&addresses[i], 0, sizeof(addresses[i]));
|
---|
127 | #ifdef NO_IPV6
|
---|
128 | addresses[i].sin_family = AF_INET;
|
---|
129 | addresses[i].sin_addr.s_addr = INADDR_ANY;
|
---|
130 | addresses[i].sin_port = htons(config.ports[i]);
|
---|
131 | #else
|
---|
132 | addresses[i].sin6_family = AF_INET6;
|
---|
133 | addresses[i].sin6_addr = in6addr_any;
|
---|
134 | addresses[i].sin6_port = htons(config.ports[i]);
|
---|
135 | #endif
|
---|
136 | if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
|
---|
137 | close_socket(sock);
|
---|
138 | cm_log("Server", "Bind failure");
|
---|
139 | return 1;
|
---|
140 | }
|
---|
141 | if(listen(sock, 128) < 0) {
|
---|
142 | close_socket(sock);
|
---|
143 | cm_log("Server", "Listen failure");
|
---|
144 | return 1;
|
---|
145 | }
|
---|
146 | sockets[i] = sock;
|
---|
147 | }
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
|
---|
152 | #ifndef NO_SSL
|
---|
153 | if(ssl == NULL) {
|
---|
154 | return recv(s, data, len, 0);
|
---|
155 | } else {
|
---|
156 | return SSL_read(ssl, data, len);
|
---|
157 | }
|
---|
158 | #else
|
---|
159 | return recv(s, data, len, 0);
|
---|
160 | #endif
|
---|
161 | }
|
---|
162 |
|
---|
163 | size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
|
---|
164 | #ifndef NO_SSL
|
---|
165 | if(ssl == NULL) {
|
---|
166 | return send(s, data, len, 0);
|
---|
167 | } else {
|
---|
168 | return SSL_write(ssl, data, len);
|
---|
169 | }
|
---|
170 | #else
|
---|
171 | return send(s, data, len, 0);
|
---|
172 | #endif
|
---|
173 | }
|
---|
174 |
|
---|
175 | #define ERROR_HTML \
|
---|
176 | "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
|
---|
177 | "<html>\n" \
|
---|
178 | " <head>\n" \
|
---|
179 | " <title>%s</title>\n" \
|
---|
180 | " </head>\n" \
|
---|
181 | " <body>\n" \
|
---|
182 | " <h1>%s</h1>\n" \
|
---|
183 | " <hr>\n" \
|
---|
184 | " ", \
|
---|
185 | address, \
|
---|
186 | "\n" \
|
---|
187 | " </body>\n" \
|
---|
188 | "</html>\n"
|
---|
189 |
|
---|
190 | void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) {
|
---|
191 | char construct[512];
|
---|
192 | if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
|
---|
193 | status = "304 Not Modified";
|
---|
194 | type = NULL;
|
---|
195 | size = 0;
|
---|
196 | headers = NULL;
|
---|
197 | f = NULL;
|
---|
198 | doc = NULL;
|
---|
199 | }
|
---|
200 | sprintf(construct, "%llu", (unsigned long long)size);
|
---|
201 | tw_write(ssl, sock, "HTTP/1.1 ", 9);
|
---|
202 | tw_write(ssl, sock, (char*)status, strlen(status));
|
---|
203 | tw_write(ssl, sock, "\r\n", 2);
|
---|
204 | if(type != NULL) {
|
---|
205 | tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
|
---|
206 | tw_write(ssl, sock, (char*)type, strlen(type));
|
---|
207 | tw_write(ssl, sock, "\r\n", 2);
|
---|
208 | }
|
---|
209 | tw_write(ssl, sock, "Server: ", 6 + 2);
|
---|
210 | tw_write(ssl, sock, tw_server, strlen(tw_server));
|
---|
211 | tw_write(ssl, sock, "\r\n", 2);
|
---|
212 | if(size != 0) {
|
---|
213 | tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
|
---|
214 | tw_write(ssl, sock, construct, strlen(construct));
|
---|
215 | tw_write(ssl, sock, "\r\n", 2);
|
---|
216 | if(mtime != 0) {
|
---|
217 | struct tm* tm = gmtime(&mtime);
|
---|
218 | char date[513];
|
---|
219 | strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
|
---|
220 | tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2);
|
---|
221 | tw_write(ssl, sock, date, strlen(date));
|
---|
222 | tw_write(ssl, sock, "\r\n", 2);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | int i;
|
---|
226 | if(headers != NULL) {
|
---|
227 | for(i = 0; headers[i] != NULL; i += 2) {
|
---|
228 | tw_write(ssl, sock, headers[i], strlen(headers[i]));
|
---|
229 | tw_write(ssl, sock, ": ", 2);
|
---|
230 | tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1]));
|
---|
231 | tw_write(ssl, sock, "\r\n", 2);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | tw_write(ssl, sock, "\r\n", 2);
|
---|
235 | if(doc == NULL && f == NULL) return;
|
---|
236 | size_t incr = 0;
|
---|
237 | while(1) {
|
---|
238 | if(f != NULL) {
|
---|
239 | char buffer[128];
|
---|
240 | fread(buffer, size < 128 ? size : 128, 1, f);
|
---|
241 | tw_write(ssl, sock, buffer, size < 128 ? size : 128);
|
---|
242 | } else {
|
---|
243 | tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
|
---|
244 | }
|
---|
245 | incr += 128;
|
---|
246 | if(size <= 128) break;
|
---|
247 | size -= 128;
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, time_t mtime, time_t cmtime) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL, mtime, cmtime); }
|
---|
252 |
|
---|
253 | const char* tw_http_status(int code) {
|
---|
254 | if(code == 200) {
|
---|
255 | return "200 OK";
|
---|
256 | } else if(code == 308) {
|
---|
257 | return "308 Permanent Redirect";
|
---|
258 | } else if(code == 400) {
|
---|
259 | return "400 Bad Request";
|
---|
260 | } else if(code == 401) {
|
---|
261 | return "401 Unauthorized";
|
---|
262 | } else if(code == 403) {
|
---|
263 | return "403 Forbidden";
|
---|
264 | } else if(code == 404) {
|
---|
265 | return "404 Not Found";
|
---|
266 | } else {
|
---|
267 | return "400 Bad Request";
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | char* tw_http_default_error(int code, char* name, int port) {
|
---|
272 | char address[1024];
|
---|
273 | sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
|
---|
274 |
|
---|
275 | char* st = cm_strdup(tw_http_status(code));
|
---|
276 | char* st2;
|
---|
277 | int i;
|
---|
278 | for(i = 0; st[i] != 0; i++) {
|
---|
279 | if(st[i] == ' ') {
|
---|
280 | st2 = cm_strdup(st + i + 1);
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | char* buffer = malloc(4096);
|
---|
285 | char* str = cm_strcat3(ERROR_HTML);
|
---|
286 | sprintf(buffer, str, st, st2);
|
---|
287 | free(str);
|
---|
288 | free(st);
|
---|
289 | return buffer;
|
---|
290 | }
|
---|
291 |
|
---|
292 | void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) {
|
---|
293 | char* str = tw_http_default_error(error, name, port);
|
---|
294 | tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
|
---|
295 | free(str);
|
---|
296 | }
|
---|
297 |
|
---|
298 | void addstring(char** str, const char* add, ...) {
|
---|
299 | int i;
|
---|
300 | char cbuf[2];
|
---|
301 | cbuf[1] = 0;
|
---|
302 | va_list va;
|
---|
303 | va_start(va, add);
|
---|
304 | for(i = 0; add[i] != 0; i++) {
|
---|
305 | cbuf[0] = add[i];
|
---|
306 | if(add[i] == '%') {
|
---|
307 | i++;
|
---|
308 | if(add[i] == 's') {
|
---|
309 | char* tmp = *str;
|
---|
310 | *str = cm_strcat(tmp, va_arg(va, const char*));
|
---|
311 | free(tmp);
|
---|
312 | } else if(add[i] == 'h') {
|
---|
313 | char* h = cm_html_escape(va_arg(va, const char*));
|
---|
314 | char* tmp = *str;
|
---|
315 | *str = cm_strcat(tmp, h);
|
---|
316 | free(tmp);
|
---|
317 | free(h);
|
---|
318 | } else if(add[i] == 'l') {
|
---|
319 | char* h = cm_url_escape(va_arg(va, const char*));
|
---|
320 | char* tmp = *str;
|
---|
321 | *str = cm_strcat(tmp, h);
|
---|
322 | free(tmp);
|
---|
323 | free(h);
|
---|
324 | } else if(add[i] == 'd') {
|
---|
325 | int n = va_arg(va, int);
|
---|
326 | char* h = malloc(512);
|
---|
327 | sprintf(h, "%d", n);
|
---|
328 | char* tmp = *str;
|
---|
329 | *str = cm_strcat(tmp, h);
|
---|
330 | free(tmp);
|
---|
331 | free(h);
|
---|
332 | } else if(add[i] == '%') {
|
---|
333 | char* tmp = *str;
|
---|
334 | *str = cm_strcat(tmp, "%");
|
---|
335 | free(tmp);
|
---|
336 | }
|
---|
337 | } else {
|
---|
338 | char* tmp = *str;
|
---|
339 | *str = cm_strcat(tmp, cbuf);
|
---|
340 | free(tmp);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
|
---|
346 | char* mime = "application/octet-stream";
|
---|
347 | if(ext == NULL) return mime;
|
---|
348 | bool set = false;
|
---|
349 | int i;
|
---|
350 | for(i = 0; i < vhost_entry->mime_count; i++) {
|
---|
351 | if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
|
---|
352 | mime = vhost_entry->mimes[i].mime;
|
---|
353 | set = true;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | if(!set) {
|
---|
357 | for(i = 0; i < config.root.mime_count; i++) {
|
---|
358 | if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
|
---|
359 | mime = config.root.mimes[i].mime;
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 | return mime;
|
---|
364 | }
|
---|
365 |
|
---|
366 | char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
|
---|
367 | char* icon = "";
|
---|
368 | if(mime == NULL) return "";
|
---|
369 | bool set = false;
|
---|
370 | int i;
|
---|
371 | for(i = 0; i < vhost_entry->icon_count; i++) {
|
---|
372 | if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
|
---|
373 | icon = vhost_entry->icons[i].icon;
|
---|
374 | set = true;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | if(!set) {
|
---|
378 | for(i = 0; i < config.root.icon_count; i++) {
|
---|
379 | if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
|
---|
380 | icon = config.root.icons[i].icon;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 | return icon;
|
---|
385 | }
|
---|
386 |
|
---|
387 | #ifdef __MINGW32__
|
---|
388 | struct pass_entry {
|
---|
389 | int sock;
|
---|
390 | int port;
|
---|
391 | bool ssl;
|
---|
392 | SOCKADDR addr;
|
---|
393 | };
|
---|
394 |
|
---|
395 | unsigned int WINAPI tw_server_pass(void* ptr) {
|
---|
396 | int sock = ((struct pass_entry*)ptr)->sock;
|
---|
397 | bool ssl = ((struct pass_entry*)ptr)->ssl;
|
---|
398 | int port = ((struct pass_entry*)ptr)->port;
|
---|
399 | SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
|
---|
400 | free(ptr);
|
---|
401 | #else
|
---|
402 | void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
|
---|
403 | #endif
|
---|
404 | char* name = config.hostname;
|
---|
405 |
|
---|
406 | #ifndef NO_SSL
|
---|
407 | SSL_CTX* ctx = NULL;
|
---|
408 | SSL* s = NULL;
|
---|
409 | bool sslworks = false;
|
---|
410 | if(ssl) {
|
---|
411 | ctx = tw_create_ssl_ctx(port);
|
---|
412 | s = SSL_new(ctx);
|
---|
413 | SSL_set_fd(s, sock);
|
---|
414 | if(SSL_accept(s) <= 0) goto cleanup;
|
---|
415 | sslworks = true;
|
---|
416 | }
|
---|
417 | #else
|
---|
418 | void* s = NULL;
|
---|
419 | #endif
|
---|
420 | struct tw_http_request req;
|
---|
421 | struct tw_http_response res;
|
---|
422 | struct tw_tool tools;
|
---|
423 | res._processed = false;
|
---|
424 | tw_init_tools(&tools);
|
---|
425 | int ret = tw_http_parse(s, sock, &req);
|
---|
426 | if(ret == 0) {
|
---|
427 | char* vhost = cm_strdup(config.hostname);
|
---|
428 | int i;
|
---|
429 | time_t cmtime = 0;
|
---|
430 | for(i = 0; req.headers[i] != NULL; i += 2) {
|
---|
431 | if(cm_strcaseequ(req.headers[i], "Host")) {
|
---|
432 | free(vhost);
|
---|
433 | vhost = cm_strdup(req.headers[i + 1]);
|
---|
434 | } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
|
---|
435 | struct tm tm;
|
---|
436 | strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
---|
437 | #ifdef __MINGW32__
|
---|
438 | time_t t = 0;
|
---|
439 | struct tm* btm = localtime(&t);
|
---|
440 | cmtime = mktime(&tm);
|
---|
441 | cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
|
---|
442 | #else
|
---|
443 | cmtime = timegm(&tm);
|
---|
444 | #endif
|
---|
445 | }
|
---|
446 | }
|
---|
447 | cm_log("Server", "Host is %s", vhost);
|
---|
448 | int port = s == NULL ? 80 : 443;
|
---|
449 | char* host = cm_strdup(vhost);
|
---|
450 | for(i = 0; vhost[i] != 0; i++) {
|
---|
451 | if(vhost[i] == ':') {
|
---|
452 | host[i] = 0;
|
---|
453 | port = atoi(host + i + 1);
|
---|
454 | break;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
|
---|
458 | struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
|
---|
459 | for(i = 0; i < config.module_count; i++) {
|
---|
460 | tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
|
---|
461 | if(mod_req != NULL) {
|
---|
462 | int ret = mod_req(&tools, &req, &res);
|
---|
463 | int co = ret & 0xff;
|
---|
464 | if(co == _TW_MODULE_PASS) continue;
|
---|
465 | if(co == _TW_MODULE_STOP) {
|
---|
466 | res._processed = true;
|
---|
467 | break;
|
---|
468 | }
|
---|
469 | if(co == _TW_MODULE_ERROR) {
|
---|
470 | tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | }
|
---|
474 | }
|
---|
475 | if(!res._processed) {
|
---|
476 | cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
|
---|
477 | char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
|
---|
478 | cm_log("Server", "Filesystem path is %s", path);
|
---|
479 | struct stat st;
|
---|
480 | if(stat(path, &st) == 0) {
|
---|
481 | if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
|
---|
482 | tw_http_error(s, sock, 403, name, port);
|
---|
483 | } else if(S_ISDIR(st.st_mode)) {
|
---|
484 | if(req.path[strlen(req.path) - 1] != '/') {
|
---|
485 | char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
|
---|
486 | _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
|
---|
487 | free(headers[1]);
|
---|
488 | } else {
|
---|
489 | char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
|
---|
490 | int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
|
---|
491 | bool found = false;
|
---|
492 | for(i = 0; i < index_count; i++) {
|
---|
493 | char* p = cm_strcat3(path, "/", indexes[i]);
|
---|
494 | FILE* f = fopen(p, "rb");
|
---|
495 | if(f != NULL) {
|
---|
496 | char* ext = NULL;
|
---|
497 | int j;
|
---|
498 | for(j = strlen(p) - 1; j >= 0; j--) {
|
---|
499 | if(p[j] == '.') {
|
---|
500 | ext = cm_strdup(p + j);
|
---|
501 | break;
|
---|
502 | } else if(p[j] == '/') {
|
---|
503 | break;
|
---|
504 | }
|
---|
505 | }
|
---|
506 | struct stat st;
|
---|
507 | stat(p, &st);
|
---|
508 | char* mime = tw_get_mime(ext, vhost_entry);
|
---|
509 | tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
|
---|
510 | fclose(f);
|
---|
511 | free(p);
|
---|
512 | found = true;
|
---|
513 | break;
|
---|
514 | }
|
---|
515 | free(p);
|
---|
516 | }
|
---|
517 | if(!found) {
|
---|
518 | char* str = malloc(1);
|
---|
519 | str[0] = 0;
|
---|
520 | char** items = cm_scandir(path);
|
---|
521 | addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
|
---|
522 | addstring(&str, "<html>\n");
|
---|
523 | addstring(&str, " <head>\n");
|
---|
524 | addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
|
---|
525 | addstring(&str, " <title>Index of %h</title>\n", req.path);
|
---|
526 | addstring(&str, " </head>\n");
|
---|
527 | addstring(&str, " <body>\n");
|
---|
528 | addstring(&str, " <h1>Index of %h</h1>\n", req.path);
|
---|
529 | addstring(&str, " <hr>\n");
|
---|
530 | addstring(&str, " <table border=\"0\">\n");
|
---|
531 | addstring(&str, " <tr>\n");
|
---|
532 | addstring(&str, " <th></th>\n");
|
---|
533 | addstring(&str, " <th>Filename</th>\n");
|
---|
534 | addstring(&str, " <th>MIME</th>\n");
|
---|
535 | addstring(&str, " <th>Size</th>\n");
|
---|
536 | addstring(&str, " </tr>\n");
|
---|
537 | int readme = -1;
|
---|
538 | char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
|
---|
539 | int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
|
---|
540 | if(items != NULL) {
|
---|
541 | int phase = 0;
|
---|
542 | doit:
|
---|
543 | for(i = 0; items[i] != NULL; i++) {
|
---|
544 | int j;
|
---|
545 | char* fpth = cm_strcat3(path, "/", items[i]);
|
---|
546 | struct stat s;
|
---|
547 | char size[512];
|
---|
548 | size[0] = 0;
|
---|
549 | stat(fpth, &s);
|
---|
550 | if(phase == 0 && !S_ISDIR(s.st_mode)) {
|
---|
551 | free(fpth);
|
---|
552 | continue;
|
---|
553 | } else if(phase == 1 && S_ISDIR(s.st_mode)) {
|
---|
554 | free(fpth);
|
---|
555 | continue;
|
---|
556 | }
|
---|
557 | if(readme == -1) {
|
---|
558 | for(j = 0; j < readme_count; j++) {
|
---|
559 | if(strcmp(items[i], readmes[j]) == 0) {
|
---|
560 | readme = j;
|
---|
561 | break;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | if(readme != -1) {
|
---|
565 | free(fpth);
|
---|
566 | continue;
|
---|
567 | }
|
---|
568 | }
|
---|
569 | if(s.st_size < 1024ULL) {
|
---|
570 | sprintf(size, "%d", (int)s.st_size);
|
---|
571 | } else if(s.st_size < 1024ULL * 1024) {
|
---|
572 | sprintf(size, "%.1fK", (double)s.st_size / 1024);
|
---|
573 | } else if(s.st_size < 1024ULL * 1024 * 1024) {
|
---|
574 | sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
|
---|
575 | } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
|
---|
576 | sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
|
---|
577 | } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
|
---|
578 | sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
|
---|
579 | }
|
---|
580 |
|
---|
581 | free(fpth);
|
---|
582 |
|
---|
583 | char* ext = NULL;
|
---|
584 | for(j = strlen(items[i]) - 1; j >= 0; j--) {
|
---|
585 | if(items[i][j] == '.') {
|
---|
586 | ext = cm_strdup(items[i] + j);
|
---|
587 | break;
|
---|
588 | } else if(items[i][j] == '/') {
|
---|
589 | break;
|
---|
590 | }
|
---|
591 | }
|
---|
592 | char* showmime = "";
|
---|
593 | char* mime = tw_get_mime(ext, vhost_entry);
|
---|
594 | if(strcmp(items[i], "../") == 0) {
|
---|
595 | mime = "misc/parent";
|
---|
596 | size[0] = 0;
|
---|
597 | } else if(items[i][strlen(items[i]) - 1] == '/') {
|
---|
598 | mime = "misc/dir";
|
---|
599 | size[0] = 0;
|
---|
600 | } else {
|
---|
601 | showmime = mime;
|
---|
602 | }
|
---|
603 | char* icon = tw_get_icon(mime, vhost_entry);
|
---|
604 | if(ext != NULL) free(ext);
|
---|
605 | char* itm = cm_strdup(items[i]);
|
---|
606 | if(strlen(itm) >= 32) {
|
---|
607 | if(itm[strlen(itm) - 1] == '/') {
|
---|
608 | itm[31] = 0;
|
---|
609 | itm[30] = '/';
|
---|
610 | itm[29] = '.';
|
---|
611 | itm[28] = '.';
|
---|
612 | itm[27] = '.';
|
---|
613 | } else {
|
---|
614 | itm[31] = 0;
|
---|
615 | itm[30] = '.';
|
---|
616 | itm[29] = '.';
|
---|
617 | itm[28] = '.';
|
---|
618 | }
|
---|
619 | }
|
---|
620 | addstring(&str, "<tr>\n");
|
---|
621 | addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
|
---|
622 | addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
|
---|
623 | addstring(&str, " <td><code> %h </code></td>\n", showmime);
|
---|
624 | addstring(&str, " <td><code> %s </code></td>\n", size);
|
---|
625 | addstring(&str, "</tr>\n");
|
---|
626 | free(itm);
|
---|
627 | }
|
---|
628 | phase++;
|
---|
629 | if(phase != 2) goto doit;
|
---|
630 | for(i = 0; items[i] != NULL; i++) free(items[i]);
|
---|
631 | free(items);
|
---|
632 | }
|
---|
633 | addstring(&str, " </table>\n");
|
---|
634 | if(readme != -1) {
|
---|
635 | addstring(&str, "<hr>\n");
|
---|
636 | char* fpth = cm_strcat3(path, "/", readmes[readme]);
|
---|
637 | struct stat s;
|
---|
638 | stat(fpth, &s);
|
---|
639 | FILE* fr = fopen(fpth, "r");
|
---|
640 | if(fr != NULL) {
|
---|
641 | char* rmbuf = malloc(s.st_size + 1);
|
---|
642 | rmbuf[s.st_size] = 0;
|
---|
643 | fread(rmbuf, s.st_size, 1, fr);
|
---|
644 | addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
|
---|
645 | fclose(fr);
|
---|
646 | }
|
---|
647 | }
|
---|
648 | addstring(&str, " <hr>\n");
|
---|
649 | addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
|
---|
650 | addstring(&str, " </body>\n");
|
---|
651 | addstring(&str, "</html>\n");
|
---|
652 | tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
|
---|
653 | free(str);
|
---|
654 | }
|
---|
655 | }
|
---|
656 | } else {
|
---|
657 | char* ext = NULL;
|
---|
658 | for(i = strlen(req.path) - 1; i >= 0; i--) {
|
---|
659 | if(req.path[i] == '.') {
|
---|
660 | ext = cm_strdup(req.path + i);
|
---|
661 | break;
|
---|
662 | } else if(req.path[i] == '/') {
|
---|
663 | break;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | char* mime = tw_get_mime(ext, vhost_entry);
|
---|
667 | if(ext != NULL) free(ext);
|
---|
668 | FILE* f = fopen(path, "rb");
|
---|
669 | tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
|
---|
670 | fclose(f);
|
---|
671 | }
|
---|
672 | } else {
|
---|
673 | tw_http_error(s, sock, 404, name, port);
|
---|
674 | }
|
---|
675 | free(path);
|
---|
676 | }
|
---|
677 | free(vhost);
|
---|
678 | free(host);
|
---|
679 | tw_free_request(&req);
|
---|
680 | } else if(ret == -1) {
|
---|
681 | } else {
|
---|
682 | tw_http_error(s, sock, 400, name, port);
|
---|
683 | }
|
---|
684 | cleanup:
|
---|
685 | #ifndef NO_SSL
|
---|
686 | if(sslworks) {
|
---|
687 | SSL_shutdown(s);
|
---|
688 | }
|
---|
689 | SSL_free(s);
|
---|
690 | #endif
|
---|
691 | close_socket(sock);
|
---|
692 | #ifdef __MINGW32__
|
---|
693 | _endthreadex(0);
|
---|
694 | #endif
|
---|
695 | ;
|
---|
696 | }
|
---|
697 |
|
---|
698 | void tw_server_loop(void) {
|
---|
699 | struct timeval tv;
|
---|
700 | while(1) {
|
---|
701 | FD_ZERO(&fdset);
|
---|
702 | int i;
|
---|
703 | for(i = 0; i < sockcount; i++) {
|
---|
704 | FD_SET(sockets[i], &fdset);
|
---|
705 | }
|
---|
706 | tv.tv_sec = 1;
|
---|
707 | tv.tv_usec = 0;
|
---|
708 | int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
|
---|
709 | if(ret == -1) {
|
---|
710 | break;
|
---|
711 | } else if(ret > 0) {
|
---|
712 | /* connection */
|
---|
713 | int i;
|
---|
714 | for(i = 0; i < sockcount; i++) {
|
---|
715 | if(FD_ISSET(sockets[i], &fdset)) {
|
---|
716 | SOCKADDR claddr;
|
---|
717 | int clen = sizeof(claddr);
|
---|
718 | int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
|
---|
719 | cm_log("Server", "New connection accepted");
|
---|
720 | #ifdef __MINGW32__
|
---|
721 | HANDLE thread;
|
---|
722 | struct pass_entry* e = malloc(sizeof(*e));
|
---|
723 | e->sock = sock;
|
---|
724 | e->ssl = config.ports[i] & (1ULL << 32);
|
---|
725 | e->port = config.ports[i];
|
---|
726 | e->addr = claddr;
|
---|
727 | thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
|
---|
728 | #else
|
---|
729 | pid_t pid = fork();
|
---|
730 | if(pid == 0) {
|
---|
731 | tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
|
---|
732 | _exit(0);
|
---|
733 | } else {
|
---|
734 | close_socket(sock);
|
---|
735 | }
|
---|
736 | #endif
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 | }
|
---|
741 | }
|
---|