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

Last change on this file since 61 was 61, checked in by Nishi, on Sep 18, 2024 at 9:45:02 PM

add ServerRoot

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