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

Last change on this file since 312 was 312, checked in by Nishi, on Oct 14, 2024 at 3:17:37 AM

works on os2

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