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

Last change on this file since 82 was 82, checked in by Nishi, on Sep 19, 2024 at 9:47:40 PM

trying to add haiku support

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