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

Last change on this file since 328 was 328, checked in by Nishi, on Oct 15, 2024 at 2:54:06 AM

trying to add nextstep support

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