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

Last change on this file since 82 was 82, checked in by Nishi, on Sep 19, 2024 at 9:47:40 PM

trying to add haiku support

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