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

Last change on this file since 275 was 275, checked in by Nishi, on Oct 8, 2024 at 8:00:48 PM

revert and add

  • Property svn:keywords set to Id
File size: 28.4 KB
RevLine 
[8]1/* $Id: server.c 275 2024-10-08 11:00:48Z 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
[215]18#if !defined(_MSC_VER) && !defined(__BORLANDC__)
[8]19#include <unistd.h>
[212]20#endif
[8]21#include <string.h>
[11]22#include <stdbool.h>
[20]23#include <stdarg.h>
[43]24#include <stdio.h>
25#include <stdlib.h>
[84]26#include <errno.h>
[212]27#include <sys/types.h>
[21]28#include <sys/stat.h>
[32]29#include <time.h>
[8]30
[18]31#include <cm_string.h>
[8]32#include <cm_log.h>
[21]33#include <cm_dir.h>
[8]34
[219]35#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[255]36#ifndef NO_GETNAMEINFO
[116]37#include <ws2tcpip.h>
38#include <wspiapi.h>
[163]39#endif
[240]40#ifdef USE_WINSOCK1
41#include <winsock.h>
42#else
[8]43#include <winsock2.h>
[240]44#endif
[11]45#include <process.h>
[62]46#include <windows.h>
[32]47
48#include "strptime.h"
[216]49typedef int socklen_t;
[8]50#else
[118]51#ifdef USE_POLL
[187]52#ifdef __PPU__
53#include <net/poll.h>
54#else
[118]55#include <poll.h>
[187]56#endif
[118]57#else
[8]58#include <sys/select.h>
[118]59#endif
[8]60#include <sys/socket.h>
61#include <arpa/inet.h>
62#include <netinet/in.h>
[187]63#ifndef __PPU__
[8]64#include <netinet/tcp.h>
[187]65#endif
[255]66#ifndef NO_GETNAMEINFO
[116]67#include <netdb.h>
[8]68#endif
[163]69#endif
[8]70
[189]71#if defined(_PSP) || defined(__ps2sdk__)
[182]72#include "strptime.h"
73#endif
74
[97]75#ifdef __HAIKU__
76#include <OS.h>
77#endif
78
[212]79#ifndef S_ISDIR
[272]80#define S_ISDIR(x) ((x) & _S_IFDIR)
[212]81#endif
82
[8]83extern struct tw_config config;
[18]84extern char tw_server[];
[8]85
86int sockcount = 0;
87
[9]88SOCKADDR addresses[MAX_PORTS];
89int sockets[MAX_PORTS];
[8]90
[219]91#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[70]92const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
93#endif
94
[23]95/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
96int tw_wildcard_match(const char* wildcard, const char* target) {
97 const char *pw = wildcard, *pt = target;
98
99 while(1) {
100 if(*pt == 0) {
101 while(*pw == '*') pw++;
102 return *pw == 0;
103 } else if(*pw == 0) {
104 return 0;
105 } else if(*pw == '*') {
106 return *(pw + 1) == 0 || tw_wildcard_match(pw, pt + 1) || tw_wildcard_match(pw + 1, pt);
107 } else if(*pw == '?' || (*pw == *pt)) {
108 pw++;
109 pt++;
110 continue;
111 } else {
112 return 0;
113 }
114 }
115}
116
[8]117void close_socket(int sock) {
[219]118#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[8]119 closesocket(sock);
120#else
121 close(sock);
122#endif
123}
124
125int tw_server_init(void) {
126 int i;
[219]127#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[8]128 WSADATA wsa;
[240]129#ifdef USE_WINSOCK1
130 WSAStartup(MAKEWORD(1, 1), &wsa);
131#else
[8]132 WSAStartup(MAKEWORD(2, 0), &wsa);
133#endif
[240]134#endif
[8]135 for(i = 0; config.ports[i] != -1; i++)
136 ;
137 sockcount = i;
138 for(i = 0; config.ports[i] != -1; i++) {
[212]139 int yes = 1;
140 int no = 0;
[8]141#ifdef NO_IPV6
142 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
143#else
144 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
145#endif
[215]146#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
[8]147 if(sock == INVALID_SOCKET)
148#else
149 if(sock < 0)
150#endif
151 {
152 cm_log("Server", "Socket creation failure");
153 return 1;
154 }
155 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
156 close_socket(sock);
157 cm_log("Server", "setsockopt failure (reuseaddr)");
158 return 1;
159 }
[275]160#if !defined(__PPU__) && !defined(__minix)
[8]161 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
[274]162#endif
[8]163 close_socket(sock);
164 cm_log("Server", "setsockopt failure (nodelay)");
165 return 1;
166 }
[187]167#endif
[8]168#ifndef NO_IPV6
169 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
170 close_socket(sock);
171 cm_log("Server", "setsockopt failure (IPv6)");
172 return 1;
173 }
174#endif
175 memset(&addresses[i], 0, sizeof(addresses[i]));
176#ifdef NO_IPV6
177 addresses[i].sin_family = AF_INET;
178 addresses[i].sin_addr.s_addr = INADDR_ANY;
[214]179 addresses[i].sin_port = htons(config.ports[i] & 0xffff);
[8]180#else
181 addresses[i].sin6_family = AF_INET6;
182 addresses[i].sin6_addr = in6addr_any;
[214]183 addresses[i].sin6_port = htons(config.ports[i] & 0xffff);
[8]184#endif
185 if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
186 close_socket(sock);
187 cm_log("Server", "Bind failure");
188 return 1;
189 }
190 if(listen(sock, 128) < 0) {
191 close_socket(sock);
192 cm_log("Server", "Listen failure");
193 return 1;
194 }
195 sockets[i] = sock;
196 }
197 return 0;
198}
[9]199
[16]200size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
[43]201#ifndef NO_SSL
[16]202 if(ssl == NULL) {
203 return recv(s, data, len, 0);
204 } else {
205 return SSL_read(ssl, data, len);
206 }
[43]207#else
208 return recv(s, data, len, 0);
209#endif
[16]210}
211
212size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
[43]213#ifndef NO_SSL
[16]214 if(ssl == NULL) {
215 return send(s, data, len, 0);
216 } else {
217 return SSL_write(ssl, data, len);
218 }
[43]219#else
220 return send(s, data, len, 0);
221#endif
[16]222}
223
[20]224#define ERROR_HTML \
[18]225 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
226 "<html>\n" \
227 " <head>\n" \
[20]228 " <title>%s</title>\n" \
[18]229 " </head>\n" \
230 " <body>\n" \
[20]231 " <h1>%s</h1>\n" \
[18]232 " <hr>\n" \
233 " ", \
234 address, \
235 "\n" \
236 " </body>\n" \
237 "</html>\n"
238
[32]239void _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]240 char construct[512];
[212]241 size_t incr;
[32]242 if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
243 status = "304 Not Modified";
244 type = NULL;
245 size = 0;
246 headers = NULL;
247 f = NULL;
248 doc = NULL;
249 }
[215]250#if defined(_MSC_VER) || defined(__BORLANDC__)
[212]251 sprintf(construct, "%lu", (unsigned long)size);
252#else
[18]253 sprintf(construct, "%llu", (unsigned long long)size);
[212]254#endif
[18]255 tw_write(ssl, sock, "HTTP/1.1 ", 9);
256 tw_write(ssl, sock, (char*)status, strlen(status));
257 tw_write(ssl, sock, "\r\n", 2);
[24]258 if(type != NULL) {
259 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
260 tw_write(ssl, sock, (char*)type, strlen(type));
261 tw_write(ssl, sock, "\r\n", 2);
262 }
[18]263 tw_write(ssl, sock, "Server: ", 6 + 2);
264 tw_write(ssl, sock, tw_server, strlen(tw_server));
265 tw_write(ssl, sock, "\r\n", 2);
[24]266 if(size != 0) {
267 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
268 tw_write(ssl, sock, construct, strlen(construct));
269 tw_write(ssl, sock, "\r\n", 2);
[32]270 if(mtime != 0) {
271 struct tm* tm = gmtime(&mtime);
272 char date[513];
273 strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
274 tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2);
275 tw_write(ssl, sock, date, strlen(date));
276 tw_write(ssl, sock, "\r\n", 2);
277 }
[24]278 }
279 if(headers != NULL) {
[212]280 int i;
[24]281 for(i = 0; headers[i] != NULL; i += 2) {
282 tw_write(ssl, sock, headers[i], strlen(headers[i]));
283 tw_write(ssl, sock, ": ", 2);
284 tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1]));
285 tw_write(ssl, sock, "\r\n", 2);
286 }
287 }
[18]288 tw_write(ssl, sock, "\r\n", 2);
[24]289 if(doc == NULL && f == NULL) return;
[212]290 incr = 0;
[18]291 while(1) {
[22]292 if(f != NULL) {
[21]293 char buffer[128];
294 fread(buffer, size < 128 ? size : 128, 1, f);
295 tw_write(ssl, sock, buffer, size < 128 ? size : 128);
[22]296 } else {
[21]297 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
298 }
[18]299 incr += 128;
[19]300 if(size <= 128) break;
[18]301 size -= 128;
302 }
303}
304
[32]305void 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]306
[18]307const char* tw_http_status(int code) {
[20]308 if(code == 200) {
309 return "200 OK";
[166]310 } else if(code == 301) {
[167]311 return "301 Moved Permanently";
[24]312 } else if(code == 308) {
313 return "308 Permanent Redirect";
[20]314 } else if(code == 400) {
[18]315 return "400 Bad Request";
[20]316 } else if(code == 401) {
317 return "401 Unauthorized";
318 } else if(code == 403) {
319 return "403 Forbidden";
320 } else if(code == 404) {
321 return "404 Not Found";
[161]322 } else if(code == 500) {
323 return "500 Internal Server Error";
[18]324 } else {
325 return "400 Bad Request";
326 }
327}
328
[123]329char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
[18]330 char address[1024];
[212]331 char* st;
332 char* st2;
333 char* buffer;
334 char* str;
335 int i;
[20]336
[123]337 if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) {
338 sprintf(address, "<address>%s Server at %s</address>", tw_server, name, port);
339 } else {
340 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
341 }
342
[212]343 st = cm_strdup(tw_http_status(code));
[20]344 for(i = 0; st[i] != 0; i++) {
345 if(st[i] == ' ') {
346 st2 = cm_strdup(st + i + 1);
347 break;
348 }
[18]349 }
[212]350 buffer = malloc(4096);
351 str = cm_strcat3(ERROR_HTML);
[20]352 sprintf(buffer, str, st, st2);
353 free(str);
354 free(st);
355 return buffer;
[18]356}
357
[123]358void tw_http_error(SSL* ssl, int sock, int error, char* name, int port, struct tw_config_entry* vhost) {
359 char* str = tw_http_default_error(error, name, port, vhost);
[32]360 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
[18]361 free(str);
362}
363
[20]364void addstring(char** str, const char* add, ...) {
365 int i;
366 char cbuf[2];
[212]367 va_list va;
[20]368 cbuf[1] = 0;
369 va_start(va, add);
370 for(i = 0; add[i] != 0; i++) {
371 cbuf[0] = add[i];
372 if(add[i] == '%') {
373 i++;
374 if(add[i] == 's') {
375 char* tmp = *str;
376 *str = cm_strcat(tmp, va_arg(va, const char*));
377 free(tmp);
378 } else if(add[i] == 'h') {
379 char* h = cm_html_escape(va_arg(va, const char*));
380 char* tmp = *str;
381 *str = cm_strcat(tmp, h);
382 free(tmp);
383 free(h);
[21]384 } else if(add[i] == 'l') {
385 char* h = cm_url_escape(va_arg(va, const char*));
386 char* tmp = *str;
387 *str = cm_strcat(tmp, h);
388 free(tmp);
389 free(h);
[20]390 } else if(add[i] == 'd') {
391 int n = va_arg(va, int);
392 char* h = malloc(512);
[212]393 char* tmp = *str;
[20]394 sprintf(h, "%d", n);
395 *str = cm_strcat(tmp, h);
396 free(tmp);
397 free(h);
398 } else if(add[i] == '%') {
399 char* tmp = *str;
400 *str = cm_strcat(tmp, "%");
401 free(tmp);
402 }
403 } else {
404 char* tmp = *str;
405 *str = cm_strcat(tmp, cbuf);
406 free(tmp);
407 }
408 }
[74]409 va_end(va);
[20]410}
411
[22]412char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
413 char* mime = "application/octet-stream";
414 bool set = false;
415 int i;
[212]416 if(ext == NULL) return mime;
[22]417 for(i = 0; i < vhost_entry->mime_count; i++) {
[23]418 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
[22]419 mime = vhost_entry->mimes[i].mime;
420 set = true;
421 }
422 }
423 if(!set) {
424 for(i = 0; i < config.root.mime_count; i++) {
[23]425 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
[22]426 mime = config.root.mimes[i].mime;
427 }
428 }
429 }
430 return mime;
431}
432
433char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
434 char* icon = "";
435 bool set = false;
436 int i;
[212]437 if(mime == NULL) return "";
[22]438 for(i = 0; i < vhost_entry->icon_count; i++) {
[23]439 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
[22]440 icon = vhost_entry->icons[i].icon;
441 set = true;
442 }
443 }
444 if(!set) {
445 for(i = 0; i < config.root.icon_count; i++) {
[23]446 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
[22]447 icon = config.root.icons[i].icon;
448 }
449 }
450 }
451 return icon;
452}
453
[11]454struct pass_entry {
455 int sock;
[12]456 int port;
[11]457 bool ssl;
[21]458 SOCKADDR addr;
[11]459};
460
[219]461#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[216]462#define NO_RETURN_THREAD
[215]463void tw_server_pass(void* ptr) {
[97]464#elif defined(__HAIKU__)
465int32_t tw_server_pass(void* ptr) {
[187]466#elif defined(_PSP) || defined(__PPU__)
[183]467int tw_server_pass(void* ptr) {
[105]468#endif
[219]469#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[212]470#define FREE_PTR
[11]471 int sock = ((struct pass_entry*)ptr)->sock;
472 bool ssl = ((struct pass_entry*)ptr)->ssl;
[14]473 int port = ((struct pass_entry*)ptr)->port;
[21]474 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
[11]475#else
[216]476#define NO_RETURN_THREAD
[105]477 void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
[11]478#endif
[212]479 SSL* s = NULL;
[43]480#ifndef NO_SSL
[12]481 SSL_CTX* ctx = NULL;
[15]482 bool sslworks = false;
[12]483 if(ssl) {
484 ctx = tw_create_ssl_ctx(port);
485 s = SSL_new(ctx);
486 SSL_set_fd(s, sock);
487 if(SSL_accept(s) <= 0) goto cleanup;
[15]488 sslworks = true;
[12]489 }
[43]490#endif
[212]491 char* name = config.hostname;
[116]492 char address[513];
[212]493 int ret;
494 struct tw_http_request req;
495 struct tw_http_response res;
496 struct tw_tool tools;
[255]497 char* addrstr;
498#ifndef NO_GETNAMEINFO
[116]499 struct sockaddr* sa = (struct sockaddr*)&addr;
500 getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
[257]501#else
502 addrstr = inet_ntoa(addr.sin_addr);
503 strcpy(address, addrstr);
504 address[strlen(addrstr)] = 0;
[163]505#endif
[212]506#ifdef FREE_PTR
507 free(ptr);
508#endif
[116]509
[20]510 res._processed = false;
511 tw_init_tools(&tools);
[212]512 ret = tw_http_parse(s, sock, &req);
[17]513 if(ret == 0) {
[116]514 char date[513];
515 time_t t = time(NULL);
516 struct tm* tm = localtime(&t);
[212]517 char* useragent = cm_strdup("");
518 int i;
519 char* tmp;
520 char* tmp2;
521 char* tmp3;
522 char* tmp4;
523 char* tmp5;
524 char* log;
525 char* vhost;
526 time_t cmtime;
527 bool rej;
528 char* host;
529 int port;
530 struct tw_config_entry* vhost_entry;
[116]531 strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
532
[117]533 for(i = 0; req.headers[i] != NULL; i += 2) {
534 if(cm_strcaseequ(req.headers[i], "User-Agent")) {
[116]535 free(useragent);
536 useragent = cm_strdup(req.headers[i + 1]);
537 }
538 }
539
[212]540 tmp = cm_strcat3(address, " - [", date);
541 tmp2 = cm_strcat3(tmp, "] \"", req.method);
542 tmp3 = cm_strcat3(tmp2, " ", req.path);
543 tmp4 = cm_strcat3(tmp3, " ", req.version);
544 tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
545 log = cm_strcat(tmp5, "\"");
[116]546 free(tmp);
547 free(tmp2);
548 free(tmp3);
549 free(tmp4);
550 free(tmp5);
551 free(useragent);
552 cm_force_log(log);
553 free(log);
554
[212]555 vhost = cm_strdup(config.hostname);
556 cmtime = 0;
[70]557 if(req.headers != NULL) {
[64]558 for(i = 0; req.headers[i] != NULL; i += 2) {
559 if(cm_strcaseequ(req.headers[i], "Host")) {
560 free(vhost);
561 vhost = cm_strdup(req.headers[i + 1]);
562 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
563 struct tm tm;
[212]564 time_t t;
565 struct tm* btm;
[64]566 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
[219]567#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[212]568 t = 0;
569 btm = localtime(&t);
[64]570 cmtime = mktime(&tm);
571 cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
[32]572#else
[140]573 cmtime = timegm(&tm);
[32]574#endif
[64]575 }
[21]576 }
577 }
[212]578 rej = false;
[21]579 cm_log("Server", "Host is %s", vhost);
[212]580 port = s == NULL ? 80 : 443;
581 host = cm_strdup(vhost);
[22]582 for(i = 0; vhost[i] != 0; i++) {
583 if(vhost[i] == ':') {
[21]584 host[i] = 0;
585 port = atoi(host + i + 1);
586 break;
587 }
588 }
[136]589 name = host;
[21]590 cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
[212]591 vhost_entry = tw_vhost_match(host, port);
[161]592#ifdef HAS_CHROOT
593 char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path;
594 if(chrootpath != NULL) {
595 if(chdir(chrootpath) == 0) {
596 if(chroot(".") == 0) {
597 cm_log("Server", "Chroot successful");
598 }
599 } else {
600 cm_log("Server", "chdir() failed, cannot chroot");
601 tw_http_error(s, sock, 500, name, port, vhost_entry);
602 rej = true;
603 }
604 }
605#endif
[20]606 for(i = 0; i < config.module_count; i++) {
607 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
608 if(mod_req != NULL) {
609 int ret = mod_req(&tools, &req, &res);
610 int co = ret & 0xff;
[141]611 if(co == _TW_MODULE_PASS) {
612 continue;
613 } else if(co == _TW_MODULE_STOP) {
614 /* Handle response here ... */
[20]615 res._processed = true;
616 break;
[141]617 } else if(co == _TW_MODULE_STOP2) {
618 res._processed = true;
619 break;
620 } else if(co == _TW_MODULE_ERROR) {
[123]621 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port, vhost_entry);
[20]622 break;
623 }
624 }
625 }
626 if(!res._processed) {
[212]627 char* path;
628 char* rpath;
629 struct stat st;
[215]630 char* slash;
[21]631 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
[212]632 path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
[21]633 cm_log("Server", "Filesystem path is %s", path);
[219]634#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[216]635 for(i = strlen(path) - 1; i >= 0; i--) {
636 if(path[i] == '/') {
[215]637 path[i] = 0;
[216]638 } else {
[215]639 break;
640 }
641 }
642#endif
[219]643#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[212]644 rpath = cm_strdup(path);
[72]645 for(i = strlen(rpath) - 1; i >= 0; i--) {
[73]646 if(rpath[i] == '/') {
647 int j;
648 for(j = i + 1; rpath[j] != 0; j++) {
649 if(rpath[j] == ':' || rpath[j] == '.') {
650 rpath[j] = 0;
651 break;
652 }
653 }
[71]654 break;
655 }
656 }
[70]657 for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
658 char* n = cm_strcat("/", reserved_names[i]);
[71]659 if(cm_nocase_endswith(rpath, n)) {
[124]660 tw_http_error(s, sock, 403, name, port, vhost_entry);
[70]661 free(n);
662 rej = true;
663 cm_log("Server", "XP Patch ; rejecting access to device");
664 break;
665 }
666 free(n);
667 }
[71]668 free(rpath);
[70]669#endif
670 if(!rej && stat(path, &st) == 0) {
[22]671 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
[123]672 tw_http_error(s, sock, 403, name, port, vhost_entry);
[22]673 } else if(S_ISDIR(st.st_mode)) {
[24]674 if(req.path[strlen(req.path) - 1] != '/') {
[215]675 char* headers[3] = {"Location", NULL, NULL};
676 headers[1] = cm_strcat(req.path, "/");
[61]677 cm_log("Server", "Accessing directory without the slash at the end");
[166]678 _tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0);
[24]679 free(headers[1]);
680 } else {
681 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
682 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
683 bool found = false;
684 for(i = 0; i < index_count; i++) {
685 char* p = cm_strcat3(path, "/", indexes[i]);
686 FILE* f = fopen(p, "rb");
687 if(f != NULL) {
688 char* ext = NULL;
689 int j;
[212]690 struct stat st;
691 char* mime;
[24]692 for(j = strlen(p) - 1; j >= 0; j--) {
693 if(p[j] == '.') {
694 ext = cm_strdup(p + j);
695 break;
696 } else if(p[j] == '/') {
697 break;
698 }
[22]699 }
[24]700 stat(p, &st);
[212]701 mime = tw_get_mime(ext, vhost_entry);
[32]702 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
[24]703 fclose(f);
[74]704 if(ext != NULL) free(ext);
[24]705 free(p);
706 found = true;
707 break;
[22]708 }
[24]709 free(p);
710 }
711 if(!found) {
712 char* str = malloc(1);
[212]713 char** items;
714 int readme;
715 char** readmes;
716 int readme_count;
717 int hp;
[24]718 str[0] = 0;
[212]719 items = cm_scandir(path);
[122]720 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
[24]721 addstring(&str, "<html>\n");
722 addstring(&str, " <head>\n");
723 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
[122]724 addstring(&str, " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
[24]725 addstring(&str, " <title>Index of %h</title>\n", req.path);
726 addstring(&str, " </head>\n");
727 addstring(&str, " <body>\n");
728 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
729 addstring(&str, " <hr>\n");
730 addstring(&str, " <table border=\"0\">\n");
731 addstring(&str, " <tr>\n");
732 addstring(&str, " <th></th>\n");
733 addstring(&str, " <th>Filename</th>\n");
[28]734 addstring(&str, " <th>MIME</th>\n");
735 addstring(&str, " <th>Size</th>\n");
[24]736 addstring(&str, " </tr>\n");
[212]737 readme = -1;
738 readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
739 readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
[24]740 if(items != NULL) {
[29]741 int phase = 0;
742 doit:
[24]743 for(i = 0; items[i] != NULL; i++) {
[33]744 int j;
[212]745 char* ext;
746 char* itm;
747 char* icon;
[28]748 char* fpth = cm_strcat3(path, "/", items[i]);
749 struct stat s;
750 char size[512];
[212]751 char* showmime;
752 char* mime;
[28]753 size[0] = 0;
754 stat(fpth, &s);
[29]755 if(phase == 0 && !S_ISDIR(s.st_mode)) {
756 free(fpth);
757 continue;
758 } else if(phase == 1 && S_ISDIR(s.st_mode)) {
759 free(fpth);
760 continue;
761 }
[33]762 if(readme == -1) {
763 for(j = 0; j < readme_count; j++) {
764 if(strcmp(items[i], readmes[j]) == 0) {
765 readme = j;
766 break;
767 }
768 }
769 if(readme != -1) {
770 free(fpth);
771 continue;
772 }
773 }
[212]774 if(s.st_size < NUM1024) {
[31]775 sprintf(size, "%d", (int)s.st_size);
[212]776 } else if(s.st_size < NUM1024 * 1024) {
[29]777 sprintf(size, "%.1fK", (double)s.st_size / 1024);
[212]778 } else if(s.st_size < NUM1024 * 1024 * 1024) {
[29]779 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
[212]780 } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) {
[29]781 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
[212]782 } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) {
[29]783 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
[28]784 }
785
786 free(fpth);
787
[212]788 ext = NULL;
[24]789 for(j = strlen(items[i]) - 1; j >= 0; j--) {
790 if(items[i][j] == '.') {
791 ext = cm_strdup(items[i] + j);
792 break;
793 } else if(items[i][j] == '/') {
794 break;
795 }
796 }
[212]797 showmime = "";
798 mime = tw_get_mime(ext, vhost_entry);
[24]799 if(strcmp(items[i], "../") == 0) {
800 mime = "misc/parent";
[29]801 size[0] = 0;
[24]802 } else if(items[i][strlen(items[i]) - 1] == '/') {
803 mime = "misc/dir";
[29]804 size[0] = 0;
805 } else {
[28]806 showmime = mime;
[24]807 }
[212]808 icon = tw_get_icon(mime, vhost_entry);
[24]809 if(ext != NULL) free(ext);
[212]810 itm = cm_strdup(items[i]);
[24]811 if(strlen(itm) >= 32) {
812 if(itm[strlen(itm) - 1] == '/') {
813 itm[31] = 0;
814 itm[30] = '/';
815 itm[29] = '.';
816 itm[28] = '.';
817 itm[27] = '.';
818 } else {
819 itm[31] = 0;
820 itm[30] = '.';
821 itm[29] = '.';
822 itm[28] = '.';
823 }
824 }
825 addstring(&str, "<tr>\n");
826 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
827 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
[60]828 addstring(&str, " <td><code> %h </code></td>\n", showmime);
829 addstring(&str, " <td><code> %s </code></td>\n", size);
[24]830 addstring(&str, "</tr>\n");
831 free(itm);
[22]832 }
[29]833 phase++;
834 if(phase != 2) goto doit;
835 for(i = 0; items[i] != NULL; i++) free(items[i]);
836 free(items);
[22]837 }
[24]838 addstring(&str, " </table>\n");
[33]839 if(readme != -1) {
[212]840 struct stat s;
841 FILE* fr;
842 char* fpth;
[33]843 addstring(&str, "<hr>\n");
[212]844 fpth = cm_strcat3(path, "/", readmes[readme]);
[33]845 stat(fpth, &s);
[212]846 fr = fopen(fpth, "r");
[33]847 if(fr != NULL) {
848 char* rmbuf = malloc(s.st_size + 1);
849 rmbuf[s.st_size] = 0;
850 fread(rmbuf, s.st_size, 1, fr);
851 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
852 fclose(fr);
[70]853 free(rmbuf);
[33]854 }
[66]855 free(fpth);
[33]856 }
[24]857 addstring(&str, " <hr>\n");
[212]858 hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
[123]859 if(hp == 0) {
860 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
861 } else {
862 addstring(&str, " <address>%s Server at %s</address>\n", tw_server, name, port);
863 }
[24]864 addstring(&str, " </body>\n");
865 addstring(&str, "</html>\n");
[32]866 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
[24]867 free(str);
[21]868 }
869 }
[22]870 } else {
[21]871 char* ext = NULL;
[212]872 char* mime;
873 FILE* f;
[22]874 for(i = strlen(req.path) - 1; i >= 0; i--) {
875 if(req.path[i] == '.') {
[21]876 ext = cm_strdup(req.path + i);
877 break;
[24]878 } else if(req.path[i] == '/') {
879 break;
[21]880 }
881 }
[212]882 mime = tw_get_mime(ext, vhost_entry);
[21]883 if(ext != NULL) free(ext);
[212]884 f = fopen(path, "rb");
[173]885 if(f == NULL) {
886 tw_http_error(s, sock, 403, name, port, vhost_entry);
887 } else {
888 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
889 fclose(f);
890 }
[21]891 }
[22]892 } else {
[161]893 if(!rej) {
894 tw_http_error(s, sock, 404, name, port, vhost_entry);
895 }
[21]896 }
897 free(path);
[20]898 }
[21]899 free(vhost);
900 free(host);
[22]901 } else if(ret == -1) {
[18]902 } else {
[123]903 tw_http_error(s, sock, 400, name, port, &config.root);
[17]904 }
[70]905 tw_free_request(&req);
[12]906cleanup:
[43]907#ifndef NO_SSL
[16]908 if(sslworks) {
[15]909 SSL_shutdown(s);
910 }
911 SSL_free(s);
[46]912#endif
[11]913 close_socket(sock);
[219]914#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[216]915 _endthread();
[100]916#elif defined(__HAIKU__)
[105]917 exit_thread(0);
[11]918#endif
[216]919#ifndef NO_RETURN_THREAD
[214]920 return 0;
[215]921#endif
[11]922}
923
[62]924#ifdef SERVICE
925extern SERVICE_STATUS status;
926extern SERVICE_STATUS_HANDLE status_handle;
927#endif
928
[219]929#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[65]930struct thread_entry {
[101]931#ifdef __HAIKU__
932 thread_id thread;
933#else
[65]934 HANDLE handle;
[101]935#endif
[65]936 bool used;
937};
938#endif
939
[183]940extern int running;
941
[11]942void tw_server_loop(void) {
[68]943 int i;
[212]944#ifndef USE_POLL
[213]945 fd_set fdset;
946 struct timeval tv;
[212]947#endif
[219]948#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[65]949 struct thread_entry threads[2048];
[70]950 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
[65]951 threads[i].used = false;
952 }
953#endif
[118]954#ifdef USE_POLL
955 struct pollfd pollfds[sockcount];
956 for(i = 0; i < sockcount; i++) {
957 pollfds[i].fd = sockets[i];
958 pollfds[i].events = POLLIN | POLLPRI;
959 }
960#endif
[183]961 while(running) {
[212]962 int ret;
[118]963#ifdef USE_POLL
[212]964 ret = poll(pollfds, sockcount, 1000);
[118]965#else
966 FD_ZERO(&fdset);
967 for(i = 0; i < sockcount; i++) {
968 FD_SET(sockets[i], &fdset);
969 }
970 tv.tv_sec = 1;
971 tv.tv_usec = 0;
[86]972#ifdef __HAIKU__
[212]973 ret = select(32, &fdset, NULL, NULL, &tv);
[86]974#else
[212]975 ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
[86]976#endif
[118]977#endif
[11]978 if(ret == -1) {
[219]979#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
[84]980 cm_log("Server", "Select failure: %s", strerror(errno));
981#endif
[9]982 break;
[70]983 } else if(ret == 0) {
[62]984#ifdef SERVICE
[70]985 if(status.dwCurrentState == SERVICE_STOP_PENDING) {
[62]986 break;
987 }
988#endif
[11]989 } else if(ret > 0) {
[9]990 /* connection */
991 int i;
[11]992 for(i = 0; i < sockcount; i++) {
[118]993 bool cond;
994#ifdef USE_POLL
995 cond = pollfds[i].revents & POLLIN;
996#else
997 cond = FD_ISSET(sockets[i], &fdset);
998#endif
999 if(cond) {
[9]1000 SOCKADDR claddr;
[182]1001 socklen_t clen = sizeof(claddr);
[9]1002 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
[219]1003#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[212]1004 int j;
1005 struct pass_entry* e = malloc(sizeof(*e));
[12]1006 cm_log("Server", "New connection accepted");
[11]1007 e->sock = sock;
[215]1008#if defined(_MSC_VER) || defined(__BORLANDC__)
[212]1009 e->ssl = config.ports[i] & (1UL << 31);
1010#else
1011 e->ssl = config.ports[i] & (1ULL << 31);
1012#endif
[12]1013 e->port = config.ports[i];
[21]1014 e->addr = claddr;
[97]1015#endif
[219]1016#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[212]1017 _beginthread(tw_server_pass, 0, e);
[187]1018#elif defined(_PSP) || defined(__PPU__)
[183]1019 tw_server_pass(e);
[97]1020#elif defined(__HAIKU__)
[183]1021 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
1022 if(threads[j].used) {
1023 thread_info info;
1024 bool kill = false;
1025 if(get_thread_info(threads[j].thread, &info) == B_OK) {
1026 } else {
1027 kill = true;
[101]1028 }
[183]1029 if(kill) {
1030 threads[j].used = false;
[101]1031 }
1032 }
[183]1033 }
1034 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
1035 if(!threads[j].used) {
1036 threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
1037 threads[j].used = true;
1038 resume_thread(threads[j].thread);
1039 break;
1040 }
1041 }
[11]1042#else
1043 pid_t pid = fork();
1044 if(pid == 0) {
[89]1045 int j;
1046 for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
[212]1047 tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr);
[11]1048 _exit(0);
1049 } else {
1050 close_socket(sock);
1051 }
1052#endif
[9]1053 }
1054 }
1055 }
1056 }
[255]1057 for(i = 0; i < sockcount; i++) {
[253]1058 close_socket(sockets[i]);
1059 }
1060 cm_force_log("Server is down");
[9]1061}
Note: See TracBrowser for help on using the repository browser.