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

Last change on this file since 346 was 342, checked in by Nishi, on Oct 15, 2024 at 3:25:41 AM

trying to add nextstep support

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