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

Last change on this file since 349 was 349, checked in by Nishi, on Oct 16, 2024 at 5:08:08 AM

fix some stuff

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