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

Last change on this file since 359 was 359, checked in by Nishi, on Oct 16, 2024 at 11:34:51 PM

add dos supportr

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