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

Last change on this file since 331 was 331, checked in by Nishi, on Oct 15, 2024 at 3:00:31 AM

trying to add nextstep support

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