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

Last change on this file since 48 was 46, checked in by Nishi, on Sep 18, 2024 at 6:58:45 PM

fixing close_socket

  • Property svn:keywords set to Id
File size: 19.9 KB
RevLine 
[8]1/* $Id: server.c 46 2024-09-18 09:58:45Z nishi $ */
2
[16]3#define SOURCE
4
[43]5#include "../config.h"
6
[8]7#include "tw_server.h"
8
[43]9#ifndef NO_SSL
[12]10#include "tw_ssl.h"
[43]11#endif
12
[8]13#include "tw_config.h"
[16]14#include "tw_http.h"
[18]15#include "tw_module.h"
16#include "tw_version.h"
[8]17
18#include <unistd.h>
19#include <string.h>
[11]20#include <stdbool.h>
[20]21#include <stdarg.h>
[43]22#include <stdio.h>
23#include <stdlib.h>
[21]24#include <sys/stat.h>
[32]25#include <time.h>
[8]26
[18]27#include <cm_string.h>
[8]28#include <cm_log.h>
[21]29#include <cm_dir.h>
[8]30
31#ifdef __MINGW32__
32#include <winsock2.h>
[11]33#include <process.h>
[32]34
35#include "strptime.h"
[8]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;
[18]45extern char tw_server[];
[8]46
47fd_set fdset;
48int sockcount = 0;
49
[9]50SOCKADDR addresses[MAX_PORTS];
51int sockets[MAX_PORTS];
[8]52
[23]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
[8]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}
[9]150
[16]151size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
[43]152#ifndef NO_SSL
[16]153 if(ssl == NULL) {
154 return recv(s, data, len, 0);
155 } else {
156 return SSL_read(ssl, data, len);
157 }
[43]158#else
159 return recv(s, data, len, 0);
160#endif
[16]161}
162
163size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
[43]164#ifndef NO_SSL
[16]165 if(ssl == NULL) {
166 return send(s, data, len, 0);
167 } else {
168 return SSL_write(ssl, data, len);
169 }
[43]170#else
171 return send(s, data, len, 0);
172#endif
[16]173}
174
[20]175#define ERROR_HTML \
[18]176 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
177 "<html>\n" \
178 " <head>\n" \
[20]179 " <title>%s</title>\n" \
[18]180 " </head>\n" \
181 " <body>\n" \
[20]182 " <h1>%s</h1>\n" \
[18]183 " <hr>\n" \
184 " ", \
185 address, \
186 "\n" \
187 " </body>\n" \
188 "</html>\n"
189
[32]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) {
[18]191 char construct[512];
[32]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 }
[18]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);
[24]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 }
[18]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);
[24]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);
[32]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 }
[24]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 }
[18]234 tw_write(ssl, sock, "\r\n", 2);
[24]235 if(doc == NULL && f == NULL) return;
[18]236 size_t incr = 0;
237 while(1) {
[22]238 if(f != NULL) {
[21]239 char buffer[128];
240 fread(buffer, size < 128 ? size : 128, 1, f);
241 tw_write(ssl, sock, buffer, size < 128 ? size : 128);
[22]242 } else {
[21]243 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
244 }
[18]245 incr += 128;
[19]246 if(size <= 128) break;
[18]247 size -= 128;
248 }
249}
250
[32]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); }
[24]252
[18]253const char* tw_http_status(int code) {
[20]254 if(code == 200) {
255 return "200 OK";
[24]256 } else if(code == 308) {
257 return "308 Permanent Redirect";
[20]258 } else if(code == 400) {
[18]259 return "400 Bad Request";
[20]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";
[18]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);
[20]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 }
[18]283 }
[20]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;
[18]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);
[32]294 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
[18]295 free(str);
296}
297
[20]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);
[21]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);
[20]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
[22]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++) {
[23]351 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
[22]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++) {
[23]358 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
[22]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++) {
[23]372 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
[22]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++) {
[23]379 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
[22]380 icon = config.root.icons[i].icon;
381 }
382 }
383 }
384 return icon;
385}
386
[11]387#ifdef __MINGW32__
388struct pass_entry {
389 int sock;
[12]390 int port;
[11]391 bool ssl;
[21]392 SOCKADDR addr;
[11]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;
[14]398 int port = ((struct pass_entry*)ptr)->port;
[21]399 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
[12]400 free(ptr);
[11]401#else
[21]402void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
[11]403#endif
[13]404 char* name = config.hostname;
405
[43]406#ifndef NO_SSL
[12]407 SSL_CTX* ctx = NULL;
408 SSL* s = NULL;
[15]409 bool sslworks = false;
[12]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;
[15]415 sslworks = true;
[12]416 }
[43]417#else
418 void* s = NULL;
419#endif
[16]420 struct tw_http_request req;
[20]421 struct tw_http_response res;
422 struct tw_tool tools;
423 res._processed = false;
424 tw_init_tools(&tools);
[16]425 int ret = tw_http_parse(s, sock, &req);
[17]426 if(ret == 0) {
[21]427 char* vhost = cm_strdup(config.hostname);
[20]428 int i;
[32]429 time_t cmtime = 0;
[22]430 for(i = 0; req.headers[i] != NULL; i += 2) {
431 if(cm_strcaseequ(req.headers[i], "Host")) {
[21]432 free(vhost);
[32]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__
[45]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;
[32]442#else
443 cmtime = timegm(&tm);
444#endif
[21]445 }
446 }
447 cm_log("Server", "Host is %s", vhost);
448 int port = s == NULL ? 80 : 443;
449 char* host = cm_strdup(vhost);
[22]450 for(i = 0; vhost[i] != 0; i++) {
451 if(vhost[i] == ':') {
[21]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);
[20]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) {
[21]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;
[22]480 if(stat(path, &st) == 0) {
481 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
[21]482 tw_http_error(s, sock, 403, name, port);
[22]483 } else if(S_ISDIR(st.st_mode)) {
[24]484 if(req.path[strlen(req.path) - 1] != '/') {
485 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
[32]486 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0);
[24]487 free(headers[1]);
488 } else {
489 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
490 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
491 bool found = false;
492 for(i = 0; i < index_count; i++) {
493 char* p = cm_strcat3(path, "/", indexes[i]);
494 FILE* f = fopen(p, "rb");
495 if(f != NULL) {
496 char* ext = NULL;
497 int j;
498 for(j = strlen(p) - 1; j >= 0; j--) {
499 if(p[j] == '.') {
500 ext = cm_strdup(p + j);
501 break;
502 } else if(p[j] == '/') {
503 break;
504 }
[22]505 }
[24]506 struct stat st;
507 stat(p, &st);
508 char* mime = tw_get_mime(ext, vhost_entry);
[32]509 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
[24]510 fclose(f);
511 free(p);
512 found = true;
513 break;
[22]514 }
[24]515 free(p);
516 }
517 if(!found) {
518 char* str = malloc(1);
519 str[0] = 0;
520 char** items = cm_scandir(path);
521 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
522 addstring(&str, "<html>\n");
523 addstring(&str, " <head>\n");
524 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
525 addstring(&str, " <title>Index of %h</title>\n", req.path);
526 addstring(&str, " </head>\n");
527 addstring(&str, " <body>\n");
528 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
529 addstring(&str, " <hr>\n");
530 addstring(&str, " <table border=\"0\">\n");
531 addstring(&str, " <tr>\n");
532 addstring(&str, " <th></th>\n");
533 addstring(&str, " <th>Filename</th>\n");
[28]534 addstring(&str, " <th>MIME</th>\n");
535 addstring(&str, " <th>Size</th>\n");
[24]536 addstring(&str, " </tr>\n");
[33]537 int readme = -1;
538 char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
539 int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
[24]540 if(items != NULL) {
[29]541 int phase = 0;
542 doit:
[24]543 for(i = 0; items[i] != NULL; i++) {
[33]544 int j;
[28]545 char* fpth = cm_strcat3(path, "/", items[i]);
546 struct stat s;
547 char size[512];
548 size[0] = 0;
549 stat(fpth, &s);
[29]550 if(phase == 0 && !S_ISDIR(s.st_mode)) {
551 free(fpth);
552 continue;
553 } else if(phase == 1 && S_ISDIR(s.st_mode)) {
554 free(fpth);
555 continue;
556 }
[33]557 if(readme == -1) {
558 for(j = 0; j < readme_count; j++) {
559 if(strcmp(items[i], readmes[j]) == 0) {
560 readme = j;
561 break;
562 }
563 }
564 if(readme != -1) {
565 free(fpth);
566 continue;
567 }
568 }
[29]569 if(s.st_size < 1024ULL) {
[31]570 sprintf(size, "%d", (int)s.st_size);
[29]571 } else if(s.st_size < 1024ULL * 1024) {
572 sprintf(size, "%.1fK", (double)s.st_size / 1024);
573 } else if(s.st_size < 1024ULL * 1024 * 1024) {
574 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
575 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
576 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
577 } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
578 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
[28]579 }
580
581 free(fpth);
582
[24]583 char* ext = NULL;
584 for(j = strlen(items[i]) - 1; j >= 0; j--) {
585 if(items[i][j] == '.') {
586 ext = cm_strdup(items[i] + j);
587 break;
588 } else if(items[i][j] == '/') {
589 break;
590 }
591 }
[28]592 char* showmime = "";
[24]593 char* mime = tw_get_mime(ext, vhost_entry);
594 if(strcmp(items[i], "../") == 0) {
595 mime = "misc/parent";
[29]596 size[0] = 0;
[24]597 } else if(items[i][strlen(items[i]) - 1] == '/') {
598 mime = "misc/dir";
[29]599 size[0] = 0;
600 } else {
[28]601 showmime = mime;
[24]602 }
603 char* icon = tw_get_icon(mime, vhost_entry);
604 if(ext != NULL) free(ext);
605 char* itm = cm_strdup(items[i]);
606 if(strlen(itm) >= 32) {
607 if(itm[strlen(itm) - 1] == '/') {
608 itm[31] = 0;
609 itm[30] = '/';
610 itm[29] = '.';
611 itm[28] = '.';
612 itm[27] = '.';
613 } else {
614 itm[31] = 0;
615 itm[30] = '.';
616 itm[29] = '.';
617 itm[28] = '.';
618 }
619 }
620 addstring(&str, "<tr>\n");
621 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
622 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
[29]623 addstring(&str, " <td><code>&emsp;&emsp;%h&emsp;&emsp;</code></td>\n", showmime);
624 addstring(&str, " <td><code>&emsp;&emsp;%s&emsp;&emsp;</code></td>\n", size);
[24]625 addstring(&str, "</tr>\n");
626 free(itm);
[22]627 }
[29]628 phase++;
629 if(phase != 2) goto doit;
630 for(i = 0; items[i] != NULL; i++) free(items[i]);
631 free(items);
[22]632 }
[24]633 addstring(&str, " </table>\n");
[33]634 if(readme != -1) {
635 addstring(&str, "<hr>\n");
636 char* fpth = cm_strcat3(path, "/", readmes[readme]);
637 struct stat s;
638 stat(fpth, &s);
639 FILE* fr = fopen(fpth, "r");
640 if(fr != NULL) {
641 char* rmbuf = malloc(s.st_size + 1);
642 rmbuf[s.st_size] = 0;
643 fread(rmbuf, s.st_size, 1, fr);
644 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
645 fclose(fr);
646 }
647 }
[24]648 addstring(&str, " <hr>\n");
649 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
650 addstring(&str, " </body>\n");
651 addstring(&str, "</html>\n");
[32]652 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
[24]653 free(str);
[21]654 }
655 }
[22]656 } else {
[21]657 char* ext = NULL;
[22]658 for(i = strlen(req.path) - 1; i >= 0; i--) {
659 if(req.path[i] == '.') {
[21]660 ext = cm_strdup(req.path + i);
661 break;
[24]662 } else if(req.path[i] == '/') {
663 break;
[21]664 }
665 }
[22]666 char* mime = tw_get_mime(ext, vhost_entry);
[21]667 if(ext != NULL) free(ext);
668 FILE* f = fopen(path, "rb");
[32]669 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
[21]670 fclose(f);
671 }
[22]672 } else {
[21]673 tw_http_error(s, sock, 404, name, port);
674 }
675 free(path);
[20]676 }
[21]677 free(vhost);
678 free(host);
[32]679 tw_free_request(&req);
[22]680 } else if(ret == -1) {
[18]681 } else {
682 tw_http_error(s, sock, 400, name, port);
[17]683 }
[12]684cleanup:
[43]685#ifndef NO_SSL
[16]686 if(sslworks) {
[15]687 SSL_shutdown(s);
688 }
689 SSL_free(s);
[46]690#endif
[11]691 close_socket(sock);
692#ifdef __MINGW32__
693 _endthreadex(0);
694#endif
[43]695 ;
[11]696}
697
698void tw_server_loop(void) {
[9]699 struct timeval tv;
[11]700 while(1) {
[10]701 FD_ZERO(&fdset);
702 int i;
[11]703 for(i = 0; i < sockcount; i++) {
[10]704 FD_SET(sockets[i], &fdset);
705 }
[9]706 tv.tv_sec = 1;
707 tv.tv_usec = 0;
[10]708 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
[11]709 if(ret == -1) {
[9]710 break;
[11]711 } else if(ret > 0) {
[9]712 /* connection */
713 int i;
[11]714 for(i = 0; i < sockcount; i++) {
715 if(FD_ISSET(sockets[i], &fdset)) {
[9]716 SOCKADDR claddr;
717 int clen = sizeof(claddr);
718 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
[12]719 cm_log("Server", "New connection accepted");
[11]720#ifdef __MINGW32__
721 HANDLE thread;
722 struct pass_entry* e = malloc(sizeof(*e));
723 e->sock = sock;
724 e->ssl = config.ports[i] & (1ULL << 32);
[12]725 e->port = config.ports[i];
[21]726 e->addr = claddr;
[11]727 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
728#else
729 pid_t pid = fork();
730 if(pid == 0) {
[21]731 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
[11]732 _exit(0);
733 } else {
734 close_socket(sock);
735 }
736#endif
[9]737 }
738 }
739 }
740 }
741}
Note: See TracBrowser for help on using the repository browser.