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

Last change on this file since 44 was 43, checked in by Nishi, on Sep 18, 2024 at 6:19:03 PM

add NO_SSL

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