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

Last change on this file since 348 was 348, checked in by Nishi, on Oct 16, 2024 at 4:55:34 AM

os/2 fix

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