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

Last change on this file since 89 was 89, checked in by Nishi, on Sep 19, 2024 at 10:07:22 PM

fix select

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