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

Last change on this file since 333 was 333, checked in by Nishi, on Oct 15, 2024 at 3:03:30 AM

trying to add nextstep support

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