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

Last change on this file since 64 was 64, checked in by Nishi, on Sep 19, 2024 at 5:58:17 AM

fix stuff

  • Property svn:keywords set to Id
File size: 20.2 KB
Line 
1/* $Id: server.c 64 2024-09-18 20:58:17Z 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#include <windows.h>
35
36#include "strptime.h"
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;
46extern char tw_server[];
47
48fd_set fdset;
49int sockcount = 0;
50
51SOCKADDR addresses[MAX_PORTS];
52int sockets[MAX_PORTS];
53
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
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}
151
152size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
153#ifndef NO_SSL
154 if(ssl == NULL) {
155 return recv(s, data, len, 0);
156 } else {
157 return SSL_read(ssl, data, len);
158 }
159#else
160 return recv(s, data, len, 0);
161#endif
162}
163
164size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
165#ifndef NO_SSL
166 if(ssl == NULL) {
167 return send(s, data, len, 0);
168 } else {
169 return SSL_write(ssl, data, len);
170 }
171#else
172 return send(s, data, len, 0);
173#endif
174}
175
176#define ERROR_HTML \
177 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
178 "<html>\n" \
179 " <head>\n" \
180 " <title>%s</title>\n" \
181 " </head>\n" \
182 " <body>\n" \
183 " <h1>%s</h1>\n" \
184 " <hr>\n" \
185 " ", \
186 address, \
187 "\n" \
188 " </body>\n" \
189 "</html>\n"
190
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) {
192 char construct[512];
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 }
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);
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 }
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);
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);
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 }
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 }
235 tw_write(ssl, sock, "\r\n", 2);
236 if(doc == NULL && f == NULL) return;
237 size_t incr = 0;
238 while(1) {
239 if(f != NULL) {
240 char buffer[128];
241 fread(buffer, size < 128 ? size : 128, 1, f);
242 tw_write(ssl, sock, buffer, size < 128 ? size : 128);
243 } else {
244 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
245 }
246 incr += 128;
247 if(size <= 128) break;
248 size -= 128;
249 }
250}
251
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); }
253
254const char* tw_http_status(int code) {
255 if(code == 200) {
256 return "200 OK";
257 } else if(code == 308) {
258 return "308 Permanent Redirect";
259 } else if(code == 400) {
260 return "400 Bad Request";
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";
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);
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 }
284 }
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;
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);
295 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
296 free(str);
297}
298
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);
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);
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
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++) {
352 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
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++) {
359 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
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++) {
373 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
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++) {
380 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
381 icon = config.root.icons[i].icon;
382 }
383 }
384 }
385 return icon;
386}
387
388#ifdef __MINGW32__
389struct pass_entry {
390 int sock;
391 int port;
392 bool ssl;
393 SOCKADDR addr;
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;
399 int port = ((struct pass_entry*)ptr)->port;
400 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
401 free(ptr);
402#else
403void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
404#endif
405 char* name = config.hostname;
406
407#ifndef NO_SSL
408 SSL_CTX* ctx = NULL;
409 SSL* s = NULL;
410 bool sslworks = false;
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;
416 sslworks = true;
417 }
418#else
419 void* s = NULL;
420#endif
421 struct tw_http_request req;
422 struct tw_http_response res;
423 struct tw_tool tools;
424 res._processed = false;
425 tw_init_tools(&tools);
426 int ret = tw_http_parse(s, sock, &req);
427 if(ret == 0) {
428 char* vhost = cm_strdup(config.hostname);
429 int i;
430 time_t cmtime = 0;
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);
439#ifdef __MINGW32__
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;
444#else
445 cmtime = timegm(&tm);
446#endif
447 }
448 }
449 }
450 cm_log("Server", "Host is %s", vhost);
451 int port = s == NULL ? 80 : 443;
452 char* host = cm_strdup(vhost);
453 for(i = 0; vhost[i] != 0; i++) {
454 if(vhost[i] == ':') {
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);
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) {
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;
483 if(stat(path, &st) == 0) {
484 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
485 tw_http_error(s, sock, 403, name, port);
486 } else if(S_ISDIR(st.st_mode)) {
487 if(req.path[strlen(req.path) - 1] != '/') {
488 cm_log("Server", "Accessing directory without the slash at the end");
489 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
490 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
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 }
509 }
510 struct stat st;
511 stat(p, &st);
512 char* mime = tw_get_mime(ext, vhost_entry);
513 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
514 fclose(f);
515 free(p);
516 found = true;
517 break;
518 }
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");
538 addstring(&str, " <th>MIME</th>\n");
539 addstring(&str, " <th>Size</th>\n");
540 addstring(&str, " </tr>\n");
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;
544 if(items != NULL) {
545 int phase = 0;
546 doit:
547 for(i = 0; items[i] != NULL; i++) {
548 int j;
549 char* fpth = cm_strcat3(path, "/", items[i]);
550 struct stat s;
551 char size[512];
552 size[0] = 0;
553 stat(fpth, &s);
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 }
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 }
573 if(s.st_size < 1024ULL) {
574 sprintf(size, "%d", (int)s.st_size);
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);
583 }
584
585 free(fpth);
586
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 }
596 char* showmime = "";
597 char* mime = tw_get_mime(ext, vhost_entry);
598 if(strcmp(items[i], "../") == 0) {
599 mime = "misc/parent";
600 size[0] = 0;
601 } else if(items[i][strlen(items[i]) - 1] == '/') {
602 mime = "misc/dir";
603 size[0] = 0;
604 } else {
605 showmime = mime;
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);
627 addstring(&str, " <td><code> %h </code></td>\n", showmime);
628 addstring(&str, " <td><code> %s </code></td>\n", size);
629 addstring(&str, "</tr>\n");
630 free(itm);
631 }
632 phase++;
633 if(phase != 2) goto doit;
634 for(i = 0; items[i] != NULL; i++) free(items[i]);
635 free(items);
636 }
637 addstring(&str, " </table>\n");
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 }
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");
656 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
657 free(str);
658 }
659 }
660 } else {
661 char* ext = NULL;
662 for(i = strlen(req.path) - 1; i >= 0; i--) {
663 if(req.path[i] == '.') {
664 ext = cm_strdup(req.path + i);
665 break;
666 } else if(req.path[i] == '/') {
667 break;
668 }
669 }
670 char* mime = tw_get_mime(ext, vhost_entry);
671 if(ext != NULL) free(ext);
672 FILE* f = fopen(path, "rb");
673 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
674 fclose(f);
675 }
676 } else {
677 tw_http_error(s, sock, 404, name, port);
678 }
679 free(path);
680 }
681 free(vhost);
682 free(host);
683 tw_free_request(&req);
684 } else if(ret == -1) {
685 } else {
686 tw_http_error(s, sock, 400, name, port);
687 }
688cleanup:
689#ifndef NO_SSL
690 if(sslworks) {
691 SSL_shutdown(s);
692 }
693 SSL_free(s);
694#endif
695 close_socket(sock);
696#ifdef __MINGW32__
697 _endthreadex(0);
698#endif
699 ;
700}
701
702#ifdef SERVICE
703extern SERVICE_STATUS status;
704extern SERVICE_STATUS_HANDLE status_handle;
705#endif
706
707void tw_server_loop(void) {
708 struct timeval tv;
709 while(1) {
710 FD_ZERO(&fdset);
711 int i;
712 for(i = 0; i < sockcount; i++) {
713 FD_SET(sockets[i], &fdset);
714 }
715 tv.tv_sec = 1;
716 tv.tv_usec = 0;
717 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
718 if(ret == -1) {
719 break;
720 }else if(ret == 0){
721#ifdef SERVICE
722 if(status.dwCurrentState == SERVICE_STOP_PENDING){
723 break;
724 }
725#endif
726 } else if(ret > 0) {
727 /* connection */
728 int i;
729 for(i = 0; i < sockcount; i++) {
730 if(FD_ISSET(sockets[i], &fdset)) {
731 SOCKADDR claddr;
732 int clen = sizeof(claddr);
733 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
734 cm_log("Server", "New connection accepted");
735#ifdef __MINGW32__
736 HANDLE thread;
737 struct pass_entry* e = malloc(sizeof(*e));
738 e->sock = sock;
739 e->ssl = config.ports[i] & (1ULL << 32);
740 e->port = config.ports[i];
741 e->addr = claddr;
742 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
743#else
744 pid_t pid = fork();
745 if(pid == 0) {
746 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
747 _exit(0);
748 } else {
749 close_socket(sock);
750 }
751#endif
752 }
753 }
754 }
755 }
756}
Note: See TracBrowser for help on using the repository browser.