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

Last change on this file since 98 was 98, checked in by Nishi, on Sep 19, 2024 at 11:33:36 PM

fix select

  • Property svn:keywords set to Id
File size: 22.7 KB
Line 
1/* $Id: server.c 98 2024-09-19 14:33:36Z 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 int sock = ((struct pass_entry*)ptr)->sock;
410 bool ssl = ((struct pass_entry*)ptr)->ssl;
411 int port = ((struct pass_entry*)ptr)->port;
412 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
413 free(ptr);
414#else
415void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
416#endif
417 char* name = config.hostname;
418
419#ifndef NO_SSL
420 SSL_CTX* ctx = NULL;
421 SSL* s = NULL;
422 bool sslworks = false;
423 if(ssl) {
424 ctx = tw_create_ssl_ctx(port);
425 s = SSL_new(ctx);
426 SSL_set_fd(s, sock);
427 if(SSL_accept(s) <= 0) goto cleanup;
428 sslworks = true;
429 }
430#else
431 void* s = NULL;
432#endif
433 struct tw_http_request req;
434 struct tw_http_response res;
435 struct tw_tool tools;
436 res._processed = false;
437 tw_init_tools(&tools);
438 int ret = tw_http_parse(s, sock, &req);
439 if(ret == 0) {
440 char* vhost = cm_strdup(config.hostname);
441 int i;
442 time_t cmtime = 0;
443 if(req.headers != NULL) {
444 for(i = 0; req.headers[i] != NULL; i += 2) {
445 if(cm_strcaseequ(req.headers[i], "Host")) {
446 free(vhost);
447 vhost = cm_strdup(req.headers[i + 1]);
448 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
449 struct tm tm;
450 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
451#ifdef __MINGW32__
452 time_t t = 0;
453 struct tm* btm = localtime(&t);
454 cmtime = mktime(&tm);
455 cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
456#else
457 cmtime = timegm(&tm);
458#endif
459 }
460 }
461 }
462 cm_log("Server", "Host is %s", vhost);
463 int port = s == NULL ? 80 : 443;
464 char* host = cm_strdup(vhost);
465 for(i = 0; vhost[i] != 0; i++) {
466 if(vhost[i] == ':') {
467 host[i] = 0;
468 port = atoi(host + i + 1);
469 break;
470 }
471 }
472 cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
473 struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
474 for(i = 0; i < config.module_count; i++) {
475 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
476 if(mod_req != NULL) {
477 int ret = mod_req(&tools, &req, &res);
478 int co = ret & 0xff;
479 if(co == _TW_MODULE_PASS) continue;
480 if(co == _TW_MODULE_STOP) {
481 res._processed = true;
482 break;
483 }
484 if(co == _TW_MODULE_ERROR) {
485 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
486 break;
487 }
488 }
489 }
490 if(!res._processed) {
491 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
492 char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
493 cm_log("Server", "Filesystem path is %s", path);
494 bool rej = false;
495#ifdef __MINGW32__
496 char* rpath = cm_strdup(path);
497 for(i = strlen(rpath) - 1; i >= 0; i--) {
498 if(rpath[i] == '/') {
499 int j;
500 for(j = i + 1; rpath[j] != 0; j++) {
501 if(rpath[j] == ':' || rpath[j] == '.') {
502 rpath[j] = 0;
503 break;
504 }
505 }
506 break;
507 }
508 }
509 for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
510 char* n = cm_strcat("/", reserved_names[i]);
511 if(cm_nocase_endswith(rpath, n)) {
512 tw_http_error(s, sock, 403, name, port);
513 free(n);
514 rej = true;
515 cm_log("Server", "XP Patch ; rejecting access to device");
516 break;
517 }
518 free(n);
519 }
520 free(rpath);
521#endif
522 struct stat st;
523 if(!rej && stat(path, &st) == 0) {
524 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
525 tw_http_error(s, sock, 403, name, port);
526 } else if(S_ISDIR(st.st_mode)) {
527 if(req.path[strlen(req.path) - 1] != '/') {
528 cm_log("Server", "Accessing directory without the slash at the end");
529 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
530 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
531 free(headers[1]);
532 } else {
533 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
534 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
535 bool found = false;
536 for(i = 0; i < index_count; i++) {
537 char* p = cm_strcat3(path, "/", indexes[i]);
538 FILE* f = fopen(p, "rb");
539 if(f != NULL) {
540 char* ext = NULL;
541 int j;
542 for(j = strlen(p) - 1; j >= 0; j--) {
543 if(p[j] == '.') {
544 ext = cm_strdup(p + j);
545 break;
546 } else if(p[j] == '/') {
547 break;
548 }
549 }
550 struct stat st;
551 stat(p, &st);
552 char* mime = tw_get_mime(ext, vhost_entry);
553 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
554 fclose(f);
555 if(ext != NULL) free(ext);
556 free(p);
557 found = true;
558 break;
559 }
560 free(p);
561 }
562 if(!found) {
563 char* str = malloc(1);
564 str[0] = 0;
565 char** items = cm_scandir(path);
566 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
567 addstring(&str, "<html>\n");
568 addstring(&str, " <head>\n");
569 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
570 addstring(&str, " <title>Index of %h</title>\n", req.path);
571 addstring(&str, " </head>\n");
572 addstring(&str, " <body>\n");
573 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
574 addstring(&str, " <hr>\n");
575 addstring(&str, " <table border=\"0\">\n");
576 addstring(&str, " <tr>\n");
577 addstring(&str, " <th></th>\n");
578 addstring(&str, " <th>Filename</th>\n");
579 addstring(&str, " <th>MIME</th>\n");
580 addstring(&str, " <th>Size</th>\n");
581 addstring(&str, " </tr>\n");
582 int readme = -1;
583 char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
584 int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
585 if(items != NULL) {
586 int phase = 0;
587 doit:
588 for(i = 0; items[i] != NULL; i++) {
589 int j;
590 char* fpth = cm_strcat3(path, "/", items[i]);
591 struct stat s;
592 char size[512];
593 size[0] = 0;
594 stat(fpth, &s);
595 if(phase == 0 && !S_ISDIR(s.st_mode)) {
596 free(fpth);
597 continue;
598 } else if(phase == 1 && S_ISDIR(s.st_mode)) {
599 free(fpth);
600 continue;
601 }
602 if(readme == -1) {
603 for(j = 0; j < readme_count; j++) {
604 if(strcmp(items[i], readmes[j]) == 0) {
605 readme = j;
606 break;
607 }
608 }
609 if(readme != -1) {
610 free(fpth);
611 continue;
612 }
613 }
614 if(s.st_size < 1024ULL) {
615 sprintf(size, "%d", (int)s.st_size);
616 } else if(s.st_size < 1024ULL * 1024) {
617 sprintf(size, "%.1fK", (double)s.st_size / 1024);
618 } else if(s.st_size < 1024ULL * 1024 * 1024) {
619 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
620 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
621 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
622 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
623 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
624 }
625
626 free(fpth);
627
628 char* ext = NULL;
629 for(j = strlen(items[i]) - 1; j >= 0; j--) {
630 if(items[i][j] == '.') {
631 ext = cm_strdup(items[i] + j);
632 break;
633 } else if(items[i][j] == '/') {
634 break;
635 }
636 }
637 char* showmime = "";
638 char* mime = tw_get_mime(ext, vhost_entry);
639 if(strcmp(items[i], "../") == 0) {
640 mime = "misc/parent";
641 size[0] = 0;
642 } else if(items[i][strlen(items[i]) - 1] == '/') {
643 mime = "misc/dir";
644 size[0] = 0;
645 } else {
646 showmime = mime;
647 }
648 char* icon = tw_get_icon(mime, vhost_entry);
649 if(ext != NULL) free(ext);
650 char* itm = cm_strdup(items[i]);
651 if(strlen(itm) >= 32) {
652 if(itm[strlen(itm) - 1] == '/') {
653 itm[31] = 0;
654 itm[30] = '/';
655 itm[29] = '.';
656 itm[28] = '.';
657 itm[27] = '.';
658 } else {
659 itm[31] = 0;
660 itm[30] = '.';
661 itm[29] = '.';
662 itm[28] = '.';
663 }
664 }
665 addstring(&str, "<tr>\n");
666 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
667 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
668 addstring(&str, " <td><code> %h </code></td>\n", showmime);
669 addstring(&str, " <td><code> %s </code></td>\n", size);
670 addstring(&str, "</tr>\n");
671 free(itm);
672 }
673 phase++;
674 if(phase != 2) goto doit;
675 for(i = 0; items[i] != NULL; i++) free(items[i]);
676 free(items);
677 }
678 addstring(&str, " </table>\n");
679 if(readme != -1) {
680 addstring(&str, "<hr>\n");
681 char* fpth = cm_strcat3(path, "/", readmes[readme]);
682 struct stat s;
683 stat(fpth, &s);
684 FILE* fr = fopen(fpth, "r");
685 if(fr != NULL) {
686 char* rmbuf = malloc(s.st_size + 1);
687 rmbuf[s.st_size] = 0;
688 fread(rmbuf, s.st_size, 1, fr);
689 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
690 fclose(fr);
691 free(rmbuf);
692 }
693 free(fpth);
694 }
695 addstring(&str, " <hr>\n");
696 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
697 addstring(&str, " </body>\n");
698 addstring(&str, "</html>\n");
699 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
700 free(str);
701 }
702 }
703 } else {
704 char* ext = NULL;
705 for(i = strlen(req.path) - 1; i >= 0; i--) {
706 if(req.path[i] == '.') {
707 ext = cm_strdup(req.path + i);
708 break;
709 } else if(req.path[i] == '/') {
710 break;
711 }
712 }
713 char* mime = tw_get_mime(ext, vhost_entry);
714 if(ext != NULL) free(ext);
715 FILE* f = fopen(path, "rb");
716 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
717 fclose(f);
718 }
719 } else {
720 tw_http_error(s, sock, 404, name, port);
721 }
722 free(path);
723 }
724 free(vhost);
725 free(host);
726 } else if(ret == -1) {
727 } else {
728 tw_http_error(s, sock, 400, name, port);
729 }
730 tw_free_request(&req);
731cleanup:
732#ifndef NO_SSL
733 if(sslworks) {
734 SSL_shutdown(s);
735 }
736 SSL_free(s);
737#endif
738 close_socket(sock);
739#ifdef __MINGW32__
740 _endthreadex(0);
741#endif
742 ;
743}
744
745#ifdef SERVICE
746extern SERVICE_STATUS status;
747extern SERVICE_STATUS_HANDLE status_handle;
748#endif
749
750#ifdef __MINGW32__
751struct thread_entry {
752 HANDLE handle;
753 bool used;
754};
755#endif
756
757void tw_server_loop(void) {
758 struct timeval tv;
759 int i;
760#ifdef __MINGW32__
761 struct thread_entry threads[2048];
762 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
763 threads[i].used = false;
764 }
765#endif
766 while(1) {
767 FD_ZERO(&fdset);
768 for(i = 0; i < sockcount; i++) {
769 FD_SET(sockets[i], &fdset);
770 }
771 tv.tv_sec = 1;
772 tv.tv_usec = 0;
773#ifdef __HAIKU__
774 int ret = select(32, &fdset, NULL, NULL, &tv);
775#else
776 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
777#endif
778 if(ret == -1) {
779#ifndef __MINGW32__
780 cm_log("Server", "Select failure: %s", strerror(errno));
781#endif
782 break;
783 } else if(ret == 0) {
784#ifdef __MINGW32__
785 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
786 if(threads[i].used) {
787 DWORD ex;
788 GetExitCodeThread(threads[i].handle, &ex);
789 if(ex != STILL_ACTIVE) {
790 CloseHandle(threads[i].handle);
791 threads[i].used = false;
792 }
793 }
794 }
795#endif
796#ifdef SERVICE
797 if(status.dwCurrentState == SERVICE_STOP_PENDING) {
798 break;
799 }
800#endif
801 } else if(ret > 0) {
802 /* connection */
803 int i;
804 for(i = 0; i < sockcount; i++) {
805 if(FD_ISSET(sockets[i], &fdset)) {
806 SOCKADDR claddr;
807 int clen = sizeof(claddr);
808 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
809 cm_log("Server", "New connection accepted");
810#if defined(__MINGW32__) || defined(__HAIKU__)
811 struct pass_entry* e = malloc(sizeof(*e));
812 e->sock = sock;
813 e->ssl = config.ports[i] & (1ULL << 32);
814 e->port = config.ports[i];
815 e->addr = claddr;
816#endif
817#ifdef __MINGW32__
818 int j;
819 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
820 if(threads[j].used) {
821 DWORD ex;
822 GetExitCodeThread(threads[j].handle, &ex);
823 if(ex != STILL_ACTIVE) {
824 CloseHandle(threads[j].handle);
825 threads[j].used = false;
826 }
827 }
828 }
829 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
830 if(!threads[j].used) {
831 threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
832 threads[j].used = true;
833 break;
834 }
835 }
836#elif defined(__HAIKU__)
837 thread_id thr = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
838#else
839 pid_t pid = fork();
840 if(pid == 0) {
841 int j;
842 for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
843 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
844 _exit(0);
845 } else {
846 close_socket(sock);
847 }
848#endif
849 }
850 }
851 }
852 }
853}
Note: See TracBrowser for help on using the repository browser.