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

Last change on this file since 358 was 349, checked in by Nishi, on Oct 16, 2024 at 5:08:08 AM

fix some stuff

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