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

Last change on this file since 67 was 67, checked in by Nishi, on Sep 19, 2024 at 6:30:19 AM

fix mingw

  • Property svn:keywords set to Id
File size: 21.2 KB
RevLine 
[8]1/* $Id: server.c 67 2024-09-18 21:30:19Z 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 }
[66]651 free(fpth);
[33]652 }
[24]653 addstring(&str, " <hr>\n");
654 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
655 addstring(&str, " </body>\n");
656 addstring(&str, "</html>\n");
[32]657 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
[24]658 free(str);
[21]659 }
660 }
[22]661 } else {
[21]662 char* ext = NULL;
[22]663 for(i = strlen(req.path) - 1; i >= 0; i--) {
664 if(req.path[i] == '.') {
[21]665 ext = cm_strdup(req.path + i);
666 break;
[24]667 } else if(req.path[i] == '/') {
668 break;
[21]669 }
670 }
[22]671 char* mime = tw_get_mime(ext, vhost_entry);
[21]672 if(ext != NULL) free(ext);
673 FILE* f = fopen(path, "rb");
[32]674 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
[21]675 fclose(f);
676 }
[22]677 } else {
[21]678 tw_http_error(s, sock, 404, name, port);
679 }
680 free(path);
[20]681 }
[21]682 free(vhost);
683 free(host);
[32]684 tw_free_request(&req);
[22]685 } else if(ret == -1) {
[18]686 } else {
687 tw_http_error(s, sock, 400, name, port);
[17]688 }
[12]689cleanup:
[43]690#ifndef NO_SSL
[16]691 if(sslworks) {
[15]692 SSL_shutdown(s);
693 }
694 SSL_free(s);
[46]695#endif
[11]696 close_socket(sock);
697#ifdef __MINGW32__
698 _endthreadex(0);
699#endif
[43]700 ;
[11]701}
702
[62]703#ifdef SERVICE
704extern SERVICE_STATUS status;
705extern SERVICE_STATUS_HANDLE status_handle;
706#endif
707
[65]708#ifdef __MINGW32__
709struct thread_entry {
710 HANDLE handle;
711 bool used;
712};
713#endif
714
[11]715void tw_server_loop(void) {
[9]716 struct timeval tv;
[65]717#ifdef __MINGW32__
718 struct thread_entry threads[2048];
719 int i;
720 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++){
721 threads[i].used = false;
722 }
723#endif
[11]724 while(1) {
[10]725 FD_ZERO(&fdset);
[11]726 for(i = 0; i < sockcount; i++) {
[10]727 FD_SET(sockets[i], &fdset);
728 }
[9]729 tv.tv_sec = 1;
730 tv.tv_usec = 0;
[10]731 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
[11]732 if(ret == -1) {
[9]733 break;
[62]734 }else if(ret == 0){
[67]735#ifdef __MINGW32__
[65]736 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++){
737 if(threads[i].used){
738 DWORD ex;
739 GetExitCodeThread(threads[i].handle, &ex);
740 if(ex != STILL_ACTIVE){
741 CloseHandle(threads[i].handle);
742 threads[i].used = false;
743 }
744 }
745 }
[67]746#endif
[62]747#ifdef SERVICE
748 if(status.dwCurrentState == SERVICE_STOP_PENDING){
749 break;
750 }
751#endif
[11]752 } else if(ret > 0) {
[9]753 /* connection */
754 int i;
[11]755 for(i = 0; i < sockcount; i++) {
756 if(FD_ISSET(sockets[i], &fdset)) {
[9]757 SOCKADDR claddr;
758 int clen = sizeof(claddr);
759 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
[12]760 cm_log("Server", "New connection accepted");
[11]761#ifdef __MINGW32__
762 struct pass_entry* e = malloc(sizeof(*e));
763 e->sock = sock;
764 e->ssl = config.ports[i] & (1ULL << 32);
[12]765 e->port = config.ports[i];
[21]766 e->addr = claddr;
[65]767 int j;
768 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++){
769 if(threads[j].used){
770 DWORD ex;
771 GetExitCodeThread(threads[j].handle, &ex);
772 if(ex != STILL_ACTIVE){
773 CloseHandle(threads[j].handle);
774 threads[j].used = false;
775 }
776 }
777 }
778 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++){
779 if(!threads[j].used){
780 threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
781 threads[j].used = true;
782 break;
783 }
784 }
[11]785#else
786 pid_t pid = fork();
787 if(pid == 0) {
[21]788 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
[11]789 _exit(0);
790 } else {
791 close_socket(sock);
792 }
793#endif
[9]794 }
795 }
796 }
797 }
798}
Note: See TracBrowser for help on using the repository browser.