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

Last change on this file since 65 was 65, checked in by Nishi, on Sep 19, 2024 at 6:28:33 AM

close threads

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