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

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

os/2 fix

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