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

Last change on this file since 62 was 62, checked in by Nishi, on Sep 19, 2024 at 5:02:26 AM

add installer.sh

  • Property svn:keywords set to Id
File size: 20.2 KB
Line 
1/* $Id: server.c 62 2024-09-18 20:02:26Z 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 for(i = 0; req.headers[i] != NULL; i += 2) {
432 if(cm_strcaseequ(req.headers[i], "Host")) {
433 free(vhost);
434 vhost = cm_strdup(req.headers[i + 1]);
435 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
436 struct tm tm;
437 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
438#ifdef __MINGW32__
439 time_t t = 0;
440 struct tm* btm = localtime(&t);
441 cmtime = mktime(&tm);
442 cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
443#else
444 cmtime = timegm(&tm);
445#endif
446 }
447 }
448 cm_log("Server", "Host is %s", vhost);
449 int port = s == NULL ? 80 : 443;
450 char* host = cm_strdup(vhost);
451 for(i = 0; vhost[i] != 0; i++) {
452 if(vhost[i] == ':') {
453 host[i] = 0;
454 port = atoi(host + i + 1);
455 break;
456 }
457 }
458 cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
459 struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
460 for(i = 0; i < config.module_count; i++) {
461 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
462 if(mod_req != NULL) {
463 int ret = mod_req(&tools, &req, &res);
464 int co = ret & 0xff;
465 if(co == _TW_MODULE_PASS) continue;
466 if(co == _TW_MODULE_STOP) {
467 res._processed = true;
468 break;
469 }
470 if(co == _TW_MODULE_ERROR) {
471 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
472 break;
473 }
474 }
475 }
476 if(!res._processed) {
477 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
478 char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
479 cm_log("Server", "Filesystem path is %s", path);
480 struct stat st;
481 if(stat(path, &st) == 0) {
482 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
483 tw_http_error(s, sock, 403, name, port);
484 } else if(S_ISDIR(st.st_mode)) {
485 if(req.path[strlen(req.path) - 1] != '/') {
486 cm_log("Server", "Accessing directory without the slash at the end");
487 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
488 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
489 free(headers[1]);
490 } else {
491 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
492 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
493 bool found = false;
494 for(i = 0; i < index_count; i++) {
495 char* p = cm_strcat3(path, "/", indexes[i]);
496 FILE* f = fopen(p, "rb");
497 if(f != NULL) {
498 char* ext = NULL;
499 int j;
500 for(j = strlen(p) - 1; j >= 0; j--) {
501 if(p[j] == '.') {
502 ext = cm_strdup(p + j);
503 break;
504 } else if(p[j] == '/') {
505 break;
506 }
507 }
508 struct stat st;
509 stat(p, &st);
510 char* mime = tw_get_mime(ext, vhost_entry);
511 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
512 fclose(f);
513 free(p);
514 found = true;
515 break;
516 }
517 free(p);
518 }
519 if(!found) {
520 char* str = malloc(1);
521 str[0] = 0;
522 char** items = cm_scandir(path);
523 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
524 addstring(&str, "<html>\n");
525 addstring(&str, " <head>\n");
526 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
527 addstring(&str, " <title>Index of %h</title>\n", req.path);
528 addstring(&str, " </head>\n");
529 addstring(&str, " <body>\n");
530 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
531 addstring(&str, " <hr>\n");
532 addstring(&str, " <table border=\"0\">\n");
533 addstring(&str, " <tr>\n");
534 addstring(&str, " <th></th>\n");
535 addstring(&str, " <th>Filename</th>\n");
536 addstring(&str, " <th>MIME</th>\n");
537 addstring(&str, " <th>Size</th>\n");
538 addstring(&str, " </tr>\n");
539 int readme = -1;
540 char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
541 int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
542 if(items != NULL) {
543 int phase = 0;
544 doit:
545 for(i = 0; items[i] != NULL; i++) {
546 int j;
547 char* fpth = cm_strcat3(path, "/", items[i]);
548 struct stat s;
549 char size[512];
550 size[0] = 0;
551 stat(fpth, &s);
552 if(phase == 0 && !S_ISDIR(s.st_mode)) {
553 free(fpth);
554 continue;
555 } else if(phase == 1 && S_ISDIR(s.st_mode)) {
556 free(fpth);
557 continue;
558 }
559 if(readme == -1) {
560 for(j = 0; j < readme_count; j++) {
561 if(strcmp(items[i], readmes[j]) == 0) {
562 readme = j;
563 break;
564 }
565 }
566 if(readme != -1) {
567 free(fpth);
568 continue;
569 }
570 }
571 if(s.st_size < 1024ULL) {
572 sprintf(size, "%d", (int)s.st_size);
573 } else if(s.st_size < 1024ULL * 1024) {
574 sprintf(size, "%.1fK", (double)s.st_size / 1024);
575 } else if(s.st_size < 1024ULL * 1024 * 1024) {
576 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
577 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
578 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
579 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
580 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
581 }
582
583 free(fpth);
584
585 char* ext = NULL;
586 for(j = strlen(items[i]) - 1; j >= 0; j--) {
587 if(items[i][j] == '.') {
588 ext = cm_strdup(items[i] + j);
589 break;
590 } else if(items[i][j] == '/') {
591 break;
592 }
593 }
594 char* showmime = "";
595 char* mime = tw_get_mime(ext, vhost_entry);
596 if(strcmp(items[i], "../") == 0) {
597 mime = "misc/parent";
598 size[0] = 0;
599 } else if(items[i][strlen(items[i]) - 1] == '/') {
600 mime = "misc/dir";
601 size[0] = 0;
602 } else {
603 showmime = mime;
604 }
605 char* icon = tw_get_icon(mime, vhost_entry);
606 if(ext != NULL) free(ext);
607 char* itm = cm_strdup(items[i]);
608 if(strlen(itm) >= 32) {
609 if(itm[strlen(itm) - 1] == '/') {
610 itm[31] = 0;
611 itm[30] = '/';
612 itm[29] = '.';
613 itm[28] = '.';
614 itm[27] = '.';
615 } else {
616 itm[31] = 0;
617 itm[30] = '.';
618 itm[29] = '.';
619 itm[28] = '.';
620 }
621 }
622 addstring(&str, "<tr>\n");
623 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
624 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
625 addstring(&str, " <td><code> %h </code></td>\n", showmime);
626 addstring(&str, " <td><code> %s </code></td>\n", size);
627 addstring(&str, "</tr>\n");
628 free(itm);
629 }
630 phase++;
631 if(phase != 2) goto doit;
632 for(i = 0; items[i] != NULL; i++) free(items[i]);
633 free(items);
634 }
635 addstring(&str, " </table>\n");
636 if(readme != -1) {
637 addstring(&str, "<hr>\n");
638 char* fpth = cm_strcat3(path, "/", readmes[readme]);
639 struct stat s;
640 stat(fpth, &s);
641 FILE* fr = fopen(fpth, "r");
642 if(fr != NULL) {
643 char* rmbuf = malloc(s.st_size + 1);
644 rmbuf[s.st_size] = 0;
645 fread(rmbuf, s.st_size, 1, fr);
646 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
647 fclose(fr);
648 }
649 }
650 addstring(&str, " <hr>\n");
651 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
652 addstring(&str, " </body>\n");
653 addstring(&str, "</html>\n");
654 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
655 free(str);
656 }
657 }
658 } else {
659 char* ext = NULL;
660 for(i = strlen(req.path) - 1; i >= 0; i--) {
661 if(req.path[i] == '.') {
662 ext = cm_strdup(req.path + i);
663 break;
664 } else if(req.path[i] == '/') {
665 break;
666 }
667 }
668 char* mime = tw_get_mime(ext, vhost_entry);
669 if(ext != NULL) free(ext);
670 FILE* f = fopen(path, "rb");
671 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
672 fclose(f);
673 }
674 } else {
675 tw_http_error(s, sock, 404, name, port);
676 }
677 free(path);
678 }
679 free(vhost);
680 free(host);
681 tw_free_request(&req);
682 } else if(ret == -1) {
683 } else {
684 tw_http_error(s, sock, 400, name, port);
685 }
686cleanup:
687#ifndef NO_SSL
688 if(sslworks) {
689 SSL_shutdown(s);
690 }
691 SSL_free(s);
692#endif
693 close_socket(sock);
694#ifdef __MINGW32__
695 _endthreadex(0);
696#endif
697 ;
698}
699
700#ifdef SERVICE
701extern SERVICE_STATUS status;
702extern SERVICE_STATUS_HANDLE status_handle;
703#endif
704
705void tw_server_loop(void) {
706 struct timeval tv;
707 while(1) {
708 FD_ZERO(&fdset);
709 int i;
710 for(i = 0; i < sockcount; i++) {
711 FD_SET(sockets[i], &fdset);
712 }
713 tv.tv_sec = 1;
714 tv.tv_usec = 0;
715 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
716 if(ret == -1) {
717 break;
718 }else if(ret == 0){
719#ifdef SERVICE
720 if(status.dwCurrentState == SERVICE_STOP_PENDING){
721 break;
722 }
723#endif
724 } else if(ret > 0) {
725 /* connection */
726 int i;
727 for(i = 0; i < sockcount; i++) {
728 if(FD_ISSET(sockets[i], &fdset)) {
729 SOCKADDR claddr;
730 int clen = sizeof(claddr);
731 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
732 cm_log("Server", "New connection accepted");
733#ifdef __MINGW32__
734 HANDLE thread;
735 struct pass_entry* e = malloc(sizeof(*e));
736 e->sock = sock;
737 e->ssl = config.ports[i] & (1ULL << 32);
738 e->port = config.ports[i];
739 e->addr = claddr;
740 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
741#else
742 pid_t pid = fork();
743 if(pid == 0) {
744 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
745 _exit(0);
746 } else {
747 close_socket(sock);
748 }
749#endif
750 }
751 }
752 }
753 }
754}
Note: See TracBrowser for help on using the repository browser.