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

Last change on this file since 33 was 33, checked in by Nishi, on Sep 16, 2024 at 9:52:47 PM

can show readme on index now

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