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

Last change on this file since 110 was 105, checked in by Nishi, on Sep 20, 2024 at 10:08:44 PM

fix

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