source: Main/trunk/Server/server.c@ 117

Last change on this file since 117 was 117, checked in by Nishi, on Sep 21, 2024 at 5:47:51 PM

logfile works

  • Property svn:keywords set to Id
File size: 24.1 KB
Line 
1/* $Id: server.c 117 2024-09-21 08:47:51Z 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 <errno.h>
25#include <sys/stat.h>
26#include <time.h>
27
28#include <cm_string.h>
29#include <cm_log.h>
30#include <cm_dir.h>
31
32#ifdef __MINGW32__
33#include <ws2tcpip.h>
34#include <wspiapi.h>
35#include <winsock2.h>
36#include <process.h>
37#include <windows.h>
38
39#include "strptime.h"
40#else
41#include <sys/select.h>
42#include <sys/socket.h>
43#include <arpa/inet.h>
44#include <netinet/in.h>
45#include <netinet/tcp.h>
46#include <netdb.h>
47#endif
48
49#ifdef __HAIKU__
50#include <OS.h>
51#endif
52
53extern struct tw_config config;
54extern char tw_server[];
55
56fd_set fdset;
57int sockcount = 0;
58
59SOCKADDR addresses[MAX_PORTS];
60int sockets[MAX_PORTS];
61
62#ifdef __MINGW32__
63const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
64#endif
65
66/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
67int tw_wildcard_match(const char* wildcard, const char* target) {
68 const char *pw = wildcard, *pt = target;
69
70 while(1) {
71 if(*pt == 0) {
72 while(*pw == '*') pw++;
73 return *pw == 0;
74 } else if(*pw == 0) {
75 return 0;
76 } else if(*pw == '*') {
77 return *(pw + 1) == 0 || tw_wildcard_match(pw, pt + 1) || tw_wildcard_match(pw + 1, pt);
78 } else if(*pw == '?' || (*pw == *pt)) {
79 pw++;
80 pt++;
81 continue;
82 } else {
83 return 0;
84 }
85 }
86}
87
88void close_socket(int sock) {
89#if defined(__MINGW32__)
90 closesocket(sock);
91#else
92 close(sock);
93#endif
94}
95
96int tw_server_init(void) {
97 int i;
98#ifdef __MINGW32__
99 WSADATA wsa;
100 WSAStartup(MAKEWORD(2, 0), &wsa);
101#endif
102 for(i = 0; config.ports[i] != -1; i++)
103 ;
104 sockcount = i;
105 for(i = 0; config.ports[i] != -1; i++) {
106#ifdef NO_IPV6
107 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
108#else
109 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
110#endif
111#ifdef __MINGW32__
112 if(sock == INVALID_SOCKET)
113#else
114 if(sock < 0)
115#endif
116 {
117 cm_log("Server", "Socket creation failure");
118 return 1;
119 }
120 int yes = 1;
121 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
122 close_socket(sock);
123 cm_log("Server", "setsockopt failure (reuseaddr)");
124 return 1;
125 }
126 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
127 close_socket(sock);
128 cm_log("Server", "setsockopt failure (nodelay)");
129 return 1;
130 }
131#ifndef NO_IPV6
132 int no = 0;
133 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
134 close_socket(sock);
135 cm_log("Server", "setsockopt failure (IPv6)");
136 return 1;
137 }
138#endif
139 memset(&addresses[i], 0, sizeof(addresses[i]));
140#ifdef NO_IPV6
141 addresses[i].sin_family = AF_INET;
142 addresses[i].sin_addr.s_addr = INADDR_ANY;
143 addresses[i].sin_port = htons(config.ports[i]);
144#else
145 addresses[i].sin6_family = AF_INET6;
146 addresses[i].sin6_addr = in6addr_any;
147 addresses[i].sin6_port = htons(config.ports[i]);
148#endif
149 if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
150 close_socket(sock);
151 cm_log("Server", "Bind failure");
152 return 1;
153 }
154 if(listen(sock, 128) < 0) {
155 close_socket(sock);
156 cm_log("Server", "Listen failure");
157 return 1;
158 }
159 sockets[i] = sock;
160 }
161 return 0;
162}
163
164size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
165#ifndef NO_SSL
166 if(ssl == NULL) {
167 return recv(s, data, len, 0);
168 } else {
169 return SSL_read(ssl, data, len);
170 }
171#else
172 return recv(s, data, len, 0);
173#endif
174}
175
176size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
177#ifndef NO_SSL
178 if(ssl == NULL) {
179 return send(s, data, len, 0);
180 } else {
181 return SSL_write(ssl, data, len);
182 }
183#else
184 return send(s, data, len, 0);
185#endif
186}
187
188#define ERROR_HTML \
189 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
190 "<html>\n" \
191 " <head>\n" \
192 " <title>%s</title>\n" \
193 " </head>\n" \
194 " <body>\n" \
195 " <h1>%s</h1>\n" \
196 " <hr>\n" \
197 " ", \
198 address, \
199 "\n" \
200 " </body>\n" \
201 "</html>\n"
202
203void _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) {
204 char construct[512];
205 if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
206 status = "304 Not Modified";
207 type = NULL;
208 size = 0;
209 headers = NULL;
210 f = NULL;
211 doc = NULL;
212 }
213 sprintf(construct, "%llu", (unsigned long long)size);
214 tw_write(ssl, sock, "HTTP/1.1 ", 9);
215 tw_write(ssl, sock, (char*)status, strlen(status));
216 tw_write(ssl, sock, "\r\n", 2);
217 if(type != NULL) {
218 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
219 tw_write(ssl, sock, (char*)type, strlen(type));
220 tw_write(ssl, sock, "\r\n", 2);
221 }
222 tw_write(ssl, sock, "Server: ", 6 + 2);
223 tw_write(ssl, sock, tw_server, strlen(tw_server));
224 tw_write(ssl, sock, "\r\n", 2);
225 if(size != 0) {
226 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
227 tw_write(ssl, sock, construct, strlen(construct));
228 tw_write(ssl, sock, "\r\n", 2);
229 if(mtime != 0) {
230 struct tm* tm = gmtime(&mtime);
231 char date[513];
232 strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
233 tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2);
234 tw_write(ssl, sock, date, strlen(date));
235 tw_write(ssl, sock, "\r\n", 2);
236 }
237 }
238 int i;
239 if(headers != NULL) {
240 for(i = 0; headers[i] != NULL; i += 2) {
241 tw_write(ssl, sock, headers[i], strlen(headers[i]));
242 tw_write(ssl, sock, ": ", 2);
243 tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1]));
244 tw_write(ssl, sock, "\r\n", 2);
245 }
246 }
247 tw_write(ssl, sock, "\r\n", 2);
248 if(doc == NULL && f == NULL) return;
249 size_t incr = 0;
250 while(1) {
251 if(f != NULL) {
252 char buffer[128];
253 fread(buffer, size < 128 ? size : 128, 1, f);
254 tw_write(ssl, sock, buffer, size < 128 ? size : 128);
255 } else {
256 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
257 }
258 incr += 128;
259 if(size <= 128) break;
260 size -= 128;
261 }
262}
263
264void 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); }
265
266const char* tw_http_status(int code) {
267 if(code == 200) {
268 return "200 OK";
269 } else if(code == 308) {
270 return "308 Permanent Redirect";
271 } else if(code == 400) {
272 return "400 Bad Request";
273 } else if(code == 401) {
274 return "401 Unauthorized";
275 } else if(code == 403) {
276 return "403 Forbidden";
277 } else if(code == 404) {
278 return "404 Not Found";
279 } else {
280 return "400 Bad Request";
281 }
282}
283
284char* tw_http_default_error(int code, char* name, int port) {
285 char address[1024];
286 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
287
288 char* st = cm_strdup(tw_http_status(code));
289 char* st2;
290 int i;
291 for(i = 0; st[i] != 0; i++) {
292 if(st[i] == ' ') {
293 st2 = cm_strdup(st + i + 1);
294 break;
295 }
296 }
297 char* buffer = malloc(4096);
298 char* str = cm_strcat3(ERROR_HTML);
299 sprintf(buffer, str, st, st2);
300 free(str);
301 free(st);
302 return buffer;
303}
304
305void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) {
306 char* str = tw_http_default_error(error, name, port);
307 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
308 free(str);
309}
310
311void addstring(char** str, const char* add, ...) {
312 int i;
313 char cbuf[2];
314 cbuf[1] = 0;
315 va_list va;
316 va_start(va, add);
317 for(i = 0; add[i] != 0; i++) {
318 cbuf[0] = add[i];
319 if(add[i] == '%') {
320 i++;
321 if(add[i] == 's') {
322 char* tmp = *str;
323 *str = cm_strcat(tmp, va_arg(va, const char*));
324 free(tmp);
325 } else if(add[i] == 'h') {
326 char* h = cm_html_escape(va_arg(va, const char*));
327 char* tmp = *str;
328 *str = cm_strcat(tmp, h);
329 free(tmp);
330 free(h);
331 } else if(add[i] == 'l') {
332 char* h = cm_url_escape(va_arg(va, const char*));
333 char* tmp = *str;
334 *str = cm_strcat(tmp, h);
335 free(tmp);
336 free(h);
337 } else if(add[i] == 'd') {
338 int n = va_arg(va, int);
339 char* h = malloc(512);
340 sprintf(h, "%d", n);
341 char* tmp = *str;
342 *str = cm_strcat(tmp, h);
343 free(tmp);
344 free(h);
345 } else if(add[i] == '%') {
346 char* tmp = *str;
347 *str = cm_strcat(tmp, "%");
348 free(tmp);
349 }
350 } else {
351 char* tmp = *str;
352 *str = cm_strcat(tmp, cbuf);
353 free(tmp);
354 }
355 }
356 va_end(va);
357}
358
359char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
360 char* mime = "application/octet-stream";
361 if(ext == NULL) return mime;
362 bool set = false;
363 int i;
364 for(i = 0; i < vhost_entry->mime_count; i++) {
365 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
366 mime = vhost_entry->mimes[i].mime;
367 set = true;
368 }
369 }
370 if(!set) {
371 for(i = 0; i < config.root.mime_count; i++) {
372 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
373 mime = config.root.mimes[i].mime;
374 }
375 }
376 }
377 return mime;
378}
379
380char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
381 char* icon = "";
382 if(mime == NULL) return "";
383 bool set = false;
384 int i;
385 for(i = 0; i < vhost_entry->icon_count; i++) {
386 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
387 icon = vhost_entry->icons[i].icon;
388 set = true;
389 }
390 }
391 if(!set) {
392 for(i = 0; i < config.root.icon_count; i++) {
393 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
394 icon = config.root.icons[i].icon;
395 }
396 }
397 }
398 return icon;
399}
400
401struct pass_entry {
402 int sock;
403 int port;
404 bool ssl;
405 SOCKADDR addr;
406};
407
408#ifdef __MINGW32__
409unsigned int WINAPI tw_server_pass(void* ptr) {
410#elif defined(__HAIKU__)
411int32_t tw_server_pass(void* ptr) {
412#endif
413#if defined(__HAIKU__) || defined(__MINGW32__)
414 int sock = ((struct pass_entry*)ptr)->sock;
415 bool ssl = ((struct pass_entry*)ptr)->ssl;
416 int port = ((struct pass_entry*)ptr)->port;
417 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
418 free(ptr);
419#else
420 void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
421#endif
422 char* name = config.hostname;
423
424#ifndef NO_SSL
425 SSL_CTX* ctx = NULL;
426 SSL* s = NULL;
427 bool sslworks = false;
428 if(ssl) {
429 ctx = tw_create_ssl_ctx(port);
430 s = SSL_new(ctx);
431 SSL_set_fd(s, sock);
432 if(SSL_accept(s) <= 0) goto cleanup;
433 sslworks = true;
434 }
435#else
436 void* s = NULL;
437#endif
438
439 char address[513];
440 address[0] = 0;
441 struct sockaddr* sa = (struct sockaddr*)&addr;
442 getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
443
444 struct tw_http_request req;
445 struct tw_http_response res;
446 struct tw_tool tools;
447 res._processed = false;
448 tw_init_tools(&tools);
449 int ret = tw_http_parse(s, sock, &req);
450 if(ret == 0) {
451 char date[513];
452 time_t t = time(NULL);
453 struct tm* tm = localtime(&t);
454 strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
455
456 char* useragent = cm_strdup("");
457
458 int i;
459 for(i = 0; req.headers[i] != NULL; i += 2) {
460 if(cm_strcaseequ(req.headers[i], "User-Agent")) {
461 free(useragent);
462 useragent = cm_strdup(req.headers[i + 1]);
463 }
464 }
465
466 char* tmp = cm_strcat3(address, " - [", date);
467 char* tmp2 = cm_strcat3(tmp, "] \"", req.method);
468 char* tmp3 = cm_strcat3(tmp2, " ", req.path);
469 char* tmp4 = cm_strcat3(tmp3, " ", req.version);
470 char* tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
471 char* log = cm_strcat(tmp5, "\"");
472 free(tmp);
473 free(tmp2);
474 free(tmp3);
475 free(tmp4);
476 free(tmp5);
477 free(useragent);
478 cm_force_log(log);
479 free(log);
480
481 char* vhost = cm_strdup(config.hostname);
482 time_t cmtime = 0;
483 if(req.headers != NULL) {
484 for(i = 0; req.headers[i] != NULL; i += 2) {
485 if(cm_strcaseequ(req.headers[i], "Host")) {
486 free(vhost);
487 vhost = cm_strdup(req.headers[i + 1]);
488 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
489 struct tm tm;
490 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
491#ifdef __MINGW32__
492 time_t t = 0;
493 struct tm* btm = localtime(&t);
494 cmtime = mktime(&tm);
495 cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
496#else
497 cmtime = timegm(&tm);
498#endif
499 }
500 }
501 }
502 cm_log("Server", "Host is %s", vhost);
503 int port = s == NULL ? 80 : 443;
504 char* host = cm_strdup(vhost);
505 for(i = 0; vhost[i] != 0; i++) {
506 if(vhost[i] == ':') {
507 host[i] = 0;
508 port = atoi(host + i + 1);
509 break;
510 }
511 }
512 cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
513 struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
514 for(i = 0; i < config.module_count; i++) {
515 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
516 if(mod_req != NULL) {
517 int ret = mod_req(&tools, &req, &res);
518 int co = ret & 0xff;
519 if(co == _TW_MODULE_PASS) continue;
520 if(co == _TW_MODULE_STOP) {
521 res._processed = true;
522 break;
523 }
524 if(co == _TW_MODULE_ERROR) {
525 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
526 break;
527 }
528 }
529 }
530 if(!res._processed) {
531 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
532 char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
533 cm_log("Server", "Filesystem path is %s", path);
534 bool rej = false;
535#ifdef __MINGW32__
536 char* rpath = cm_strdup(path);
537 for(i = strlen(rpath) - 1; i >= 0; i--) {
538 if(rpath[i] == '/') {
539 int j;
540 for(j = i + 1; rpath[j] != 0; j++) {
541 if(rpath[j] == ':' || rpath[j] == '.') {
542 rpath[j] = 0;
543 break;
544 }
545 }
546 break;
547 }
548 }
549 for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
550 char* n = cm_strcat("/", reserved_names[i]);
551 if(cm_nocase_endswith(rpath, n)) {
552 tw_http_error(s, sock, 403, name, port);
553 free(n);
554 rej = true;
555 cm_log("Server", "XP Patch ; rejecting access to device");
556 break;
557 }
558 free(n);
559 }
560 free(rpath);
561#endif
562 struct stat st;
563 if(!rej && stat(path, &st) == 0) {
564 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
565 tw_http_error(s, sock, 403, name, port);
566 } else if(S_ISDIR(st.st_mode)) {
567 if(req.path[strlen(req.path) - 1] != '/') {
568 cm_log("Server", "Accessing directory without the slash at the end");
569 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
570 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
571 free(headers[1]);
572 } else {
573 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
574 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
575 bool found = false;
576 for(i = 0; i < index_count; i++) {
577 char* p = cm_strcat3(path, "/", indexes[i]);
578 FILE* f = fopen(p, "rb");
579 if(f != NULL) {
580 char* ext = NULL;
581 int j;
582 for(j = strlen(p) - 1; j >= 0; j--) {
583 if(p[j] == '.') {
584 ext = cm_strdup(p + j);
585 break;
586 } else if(p[j] == '/') {
587 break;
588 }
589 }
590 struct stat st;
591 stat(p, &st);
592 char* mime = tw_get_mime(ext, vhost_entry);
593 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
594 fclose(f);
595 if(ext != NULL) free(ext);
596 free(p);
597 found = true;
598 break;
599 }
600 free(p);
601 }
602 if(!found) {
603 char* str = malloc(1);
604 str[0] = 0;
605 char** items = cm_scandir(path);
606 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
607 addstring(&str, "<html>\n");
608 addstring(&str, " <head>\n");
609 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
610 addstring(&str, " <title>Index of %h</title>\n", req.path);
611 addstring(&str, " </head>\n");
612 addstring(&str, " <body>\n");
613 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
614 addstring(&str, " <hr>\n");
615 addstring(&str, " <table border=\"0\">\n");
616 addstring(&str, " <tr>\n");
617 addstring(&str, " <th></th>\n");
618 addstring(&str, " <th>Filename</th>\n");
619 addstring(&str, " <th>MIME</th>\n");
620 addstring(&str, " <th>Size</th>\n");
621 addstring(&str, " </tr>\n");
622 int readme = -1;
623 char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
624 int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
625 if(items != NULL) {
626 int phase = 0;
627 doit:
628 for(i = 0; items[i] != NULL; i++) {
629 int j;
630 char* fpth = cm_strcat3(path, "/", items[i]);
631 struct stat s;
632 char size[512];
633 size[0] = 0;
634 stat(fpth, &s);
635 if(phase == 0 && !S_ISDIR(s.st_mode)) {
636 free(fpth);
637 continue;
638 } else if(phase == 1 && S_ISDIR(s.st_mode)) {
639 free(fpth);
640 continue;
641 }
642 if(readme == -1) {
643 for(j = 0; j < readme_count; j++) {
644 if(strcmp(items[i], readmes[j]) == 0) {
645 readme = j;
646 break;
647 }
648 }
649 if(readme != -1) {
650 free(fpth);
651 continue;
652 }
653 }
654 if(s.st_size < 1024ULL) {
655 sprintf(size, "%d", (int)s.st_size);
656 } else if(s.st_size < 1024ULL * 1024) {
657 sprintf(size, "%.1fK", (double)s.st_size / 1024);
658 } else if(s.st_size < 1024ULL * 1024 * 1024) {
659 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
660 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
661 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
662 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
663 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
664 }
665
666 free(fpth);
667
668 char* ext = NULL;
669 for(j = strlen(items[i]) - 1; j >= 0; j--) {
670 if(items[i][j] == '.') {
671 ext = cm_strdup(items[i] + j);
672 break;
673 } else if(items[i][j] == '/') {
674 break;
675 }
676 }
677 char* showmime = "";
678 char* mime = tw_get_mime(ext, vhost_entry);
679 if(strcmp(items[i], "../") == 0) {
680 mime = "misc/parent";
681 size[0] = 0;
682 } else if(items[i][strlen(items[i]) - 1] == '/') {
683 mime = "misc/dir";
684 size[0] = 0;
685 } else {
686 showmime = mime;
687 }
688 char* icon = tw_get_icon(mime, vhost_entry);
689 if(ext != NULL) free(ext);
690 char* itm = cm_strdup(items[i]);
691 if(strlen(itm) >= 32) {
692 if(itm[strlen(itm) - 1] == '/') {
693 itm[31] = 0;
694 itm[30] = '/';
695 itm[29] = '.';
696 itm[28] = '.';
697 itm[27] = '.';
698 } else {
699 itm[31] = 0;
700 itm[30] = '.';
701 itm[29] = '.';
702 itm[28] = '.';
703 }
704 }
705 addstring(&str, "<tr>\n");
706 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
707 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
708 addstring(&str, " <td><code> %h </code></td>\n", showmime);
709 addstring(&str, " <td><code> %s </code></td>\n", size);
710 addstring(&str, "</tr>\n");
711 free(itm);
712 }
713 phase++;
714 if(phase != 2) goto doit;
715 for(i = 0; items[i] != NULL; i++) free(items[i]);
716 free(items);
717 }
718 addstring(&str, " </table>\n");
719 if(readme != -1) {
720 addstring(&str, "<hr>\n");
721 char* fpth = cm_strcat3(path, "/", readmes[readme]);
722 struct stat s;
723 stat(fpth, &s);
724 FILE* fr = fopen(fpth, "r");
725 if(fr != NULL) {
726 char* rmbuf = malloc(s.st_size + 1);
727 rmbuf[s.st_size] = 0;
728 fread(rmbuf, s.st_size, 1, fr);
729 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
730 fclose(fr);
731 free(rmbuf);
732 }
733 free(fpth);
734 }
735 addstring(&str, " <hr>\n");
736 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
737 addstring(&str, " </body>\n");
738 addstring(&str, "</html>\n");
739 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
740 free(str);
741 }
742 }
743 } else {
744 char* ext = NULL;
745 for(i = strlen(req.path) - 1; i >= 0; i--) {
746 if(req.path[i] == '.') {
747 ext = cm_strdup(req.path + i);
748 break;
749 } else if(req.path[i] == '/') {
750 break;
751 }
752 }
753 char* mime = tw_get_mime(ext, vhost_entry);
754 if(ext != NULL) free(ext);
755 FILE* f = fopen(path, "rb");
756 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
757 fclose(f);
758 }
759 } else {
760 tw_http_error(s, sock, 404, name, port);
761 }
762 free(path);
763 }
764 free(vhost);
765 free(host);
766 } else if(ret == -1) {
767 } else {
768 tw_http_error(s, sock, 400, name, port);
769 }
770 tw_free_request(&req);
771cleanup:
772#ifndef NO_SSL
773 if(sslworks) {
774 SSL_shutdown(s);
775 }
776 SSL_free(s);
777#endif
778 close_socket(sock);
779#ifdef __MINGW32__
780 _endthreadex(0);
781#elif defined(__HAIKU__)
782 exit_thread(0);
783#endif
784 ;
785}
786
787#ifdef SERVICE
788extern SERVICE_STATUS status;
789extern SERVICE_STATUS_HANDLE status_handle;
790#endif
791
792#if defined(__MINGW32__) || defined(__HAIKU__)
793struct thread_entry {
794#ifdef __HAIKU__
795 thread_id thread;
796#else
797 HANDLE handle;
798#endif
799 bool used;
800};
801#endif
802
803void tw_server_loop(void) {
804 struct timeval tv;
805 int i;
806#if defined(__MINGW32__) || defined(__HAIKU__)
807 struct thread_entry threads[2048];
808 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
809 threads[i].used = false;
810 }
811#endif
812 while(1) {
813 FD_ZERO(&fdset);
814 for(i = 0; i < sockcount; i++) {
815 FD_SET(sockets[i], &fdset);
816 }
817 tv.tv_sec = 1;
818 tv.tv_usec = 0;
819#ifdef __HAIKU__
820 int ret = select(32, &fdset, NULL, NULL, &tv);
821#else
822 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
823#endif
824 if(ret == -1) {
825#ifndef __MINGW32__
826 cm_log("Server", "Select failure: %s", strerror(errno));
827#endif
828 break;
829 } else if(ret == 0) {
830#ifdef SERVICE
831 if(status.dwCurrentState == SERVICE_STOP_PENDING) {
832 break;
833 }
834#endif
835 } else if(ret > 0) {
836 /* connection */
837 int i;
838 for(i = 0; i < sockcount; i++) {
839 if(FD_ISSET(sockets[i], &fdset)) {
840 SOCKADDR claddr;
841 int clen = sizeof(claddr);
842 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
843 cm_log("Server", "New connection accepted");
844#if defined(__MINGW32__) || defined(__HAIKU__)
845 struct pass_entry* e = malloc(sizeof(*e));
846 e->sock = sock;
847 e->ssl = config.ports[i] & (1ULL << 32);
848 e->port = config.ports[i];
849 e->addr = claddr;
850#endif
851#ifdef __MINGW32__
852 int j;
853 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
854 if(threads[j].used) {
855 DWORD ex;
856 GetExitCodeThread(threads[j].handle, &ex);
857 if(ex != STILL_ACTIVE) {
858 CloseHandle(threads[j].handle);
859 threads[j].used = false;
860 }
861 }
862 }
863 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
864 if(!threads[j].used) {
865 threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
866 threads[j].used = true;
867 break;
868 }
869 }
870#elif defined(__HAIKU__)
871 int j;
872 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
873 if(threads[j].used) {
874 thread_info info;
875 bool kill = false;
876 if(get_thread_info(threads[j].thread, &info) == B_OK) {
877 } else {
878 kill = true;
879 }
880 if(kill) {
881 threads[j].used = false;
882 }
883 }
884 }
885 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
886 if(!threads[j].used) {
887 threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
888 threads[j].used = true;
889 resume_thread(threads[j].thread);
890 break;
891 }
892 }
893#else
894 pid_t pid = fork();
895 if(pid == 0) {
896 int j;
897 for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
898 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
899 _exit(0);
900 } else {
901 close_socket(sock);
902 }
903#endif
904 }
905 }
906 }
907 }
908}
Note: See TracBrowser for help on using the repository browser.