Subversion Repositories Tewi

Rev

Rev 315 | Rev 329 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 nishi 1
/* $Id: server.c 328 2024-10-14 17:54:06Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
43 nishi 5
#include "../config.h"
6
 
8 nishi 7
#include "tw_server.h"
8
 
43 nishi 9
#ifndef NO_SSL
12 nishi 10
#include "tw_ssl.h"
43 nishi 11
#endif
12
 
8 nishi 13
#include "tw_config.h"
16 nishi 14
#include "tw_http.h"
18 nishi 15
#include "tw_module.h"
16
#include "tw_version.h"
8 nishi 17
 
215 nishi 18
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
8 nishi 19
#include <unistd.h>
212 nishi 20
#endif
312 nishi 21
#include <ctype.h>
8 nishi 22
#include <string.h>
11 nishi 23
#include <stdbool.h>
20 nishi 24
#include <stdarg.h>
43 nishi 25
#include <stdio.h>
26
#include <stdlib.h>
84 nishi 27
#include <errno.h>
212 nishi 28
#include <sys/types.h>
21 nishi 29
#include <sys/stat.h>
32 nishi 30
#include <time.h>
8 nishi 31
 
18 nishi 32
#include <cm_string.h>
8 nishi 33
#include <cm_log.h>
21 nishi 34
#include <cm_dir.h>
8 nishi 35
 
312 nishi 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"
45
typedef int socklen_t;
46
 
47
#include <tcpustd.h>
48
#endif
49
 
315 nishi 50
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__))
255 nishi 51
#ifndef NO_GETNAMEINFO
116 nishi 52
#include <ws2tcpip.h>
53
#include <wspiapi.h>
163 nishi 54
#endif
240 nishi 55
#ifdef USE_WINSOCK1
56
#include <winsock.h>
57
#else
8 nishi 58
#include <winsock2.h>
240 nishi 59
#endif
11 nishi 60
#include <process.h>
62 nishi 61
#include <windows.h>
32 nishi 62
 
63
#include "strptime.h"
216 nishi 64
typedef int socklen_t;
315 nishi 65
#elif defined(__NETWARE__)
66
#include <sys/socket.h>
67
#include <nwthread.h>
68
 
69
typedef int socklen_t;
70
#define IPPROTO_TCP 0
71
#define INADDR_ANY 0
72
#define htons(x) x
73
#include "strptime.h"
8 nishi 74
#else
118 nishi 75
#ifdef USE_POLL
187 nishi 76
#ifdef __PPU__
77
#include <net/poll.h>
78
#else
118 nishi 79
#include <poll.h>
187 nishi 80
#endif
118 nishi 81
#else
328 nishi 82
#ifndef __NeXT__
8 nishi 83
#include <sys/select.h>
118 nishi 84
#endif
328 nishi 85
#endif
8 nishi 86
#include <sys/socket.h>
87
#include <arpa/inet.h>
88
#include <netinet/in.h>
187 nishi 89
#ifndef __PPU__
8 nishi 90
#include <netinet/tcp.h>
187 nishi 91
#endif
255 nishi 92
#ifndef NO_GETNAMEINFO
116 nishi 93
#include <netdb.h>
8 nishi 94
#endif
163 nishi 95
#endif
8 nishi 96
 
189 nishi 97
#if defined(_PSP) || defined(__ps2sdk__)
182 nishi 98
#include "strptime.h"
99
#endif
100
 
97 nishi 101
#ifdef __HAIKU__
102
#include <OS.h>
103
#endif
104
 
303 nishi 105
#ifdef __USLC__
106
typedef int socklen_t;
107
#endif
108
 
212 nishi 109
#ifndef S_ISDIR
272 nishi 110
#define S_ISDIR(x) ((x) & _S_IFDIR)
212 nishi 111
#endif
112
 
8 nishi 113
extern struct tw_config config;
18 nishi 114
extern char tw_server[];
8 nishi 115
 
116
int sockcount = 0;
117
 
9 nishi 118
SOCKADDR addresses[MAX_PORTS];
119
int sockets[MAX_PORTS];
8 nishi 120
 
219 nishi 121
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
70 nishi 122
const 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
 
23 nishi 125
/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
126
int 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);
312 nishi 137
		} else if(*pw == '?' || (tolower(*pw) == tolower(*pt))) {
23 nishi 138
			pw++;
139
			pt++;
140
			continue;
141
		} else {
142
			return 0;
143
		}
144
	}
145
}
146
 
8 nishi 147
void close_socket(int sock) {
312 nishi 148
#ifdef __OS2__
149
	soclose(sock);
315 nishi 150
#elif defined(__NETWARE__)
151
	shutdown(sock, 2);
312 nishi 152
#elif defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
8 nishi 153
	closesocket(sock);
154
#else
155
	close(sock);
156
#endif
157
}
158
 
159
int tw_server_init(void) {
160
	int i;
315 nishi 161
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__))
8 nishi 162
	WSADATA wsa;
240 nishi 163
#ifdef USE_WINSOCK1
164
	WSAStartup(MAKEWORD(1, 1), &wsa);
165
#else
8 nishi 166
	WSAStartup(MAKEWORD(2, 0), &wsa);
167
#endif
240 nishi 168
#endif
312 nishi 169
#ifdef __OS2__
170
	sock_init();
171
#endif
8 nishi 172
	for(i = 0; config.ports[i] != -1; i++)
173
		;
174
	sockcount = i;
175
	for(i = 0; config.ports[i] != -1; i++) {
212 nishi 176
		int yes = 1;
177
		int no = 0;
8 nishi 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
215 nishi 183
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
8 nishi 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
		}
275 nishi 197
#if !defined(__PPU__) && !defined(__minix)
8 nishi 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
		}
187 nishi 203
#endif
8 nishi 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;
214 nishi 215
		addresses[i].sin_port = htons(config.ports[i] & 0xffff);
8 nishi 216
#else
217
		addresses[i].sin6_family = AF_INET6;
218
		addresses[i].sin6_addr = in6addr_any;
214 nishi 219
		addresses[i].sin6_port = htons(config.ports[i] & 0xffff);
8 nishi 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
}
9 nishi 235
 
16 nishi 236
size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
43 nishi 237
#ifndef NO_SSL
16 nishi 238
	if(ssl == NULL) {
239
		return recv(s, data, len, 0);
240
	} else {
241
		return SSL_read(ssl, data, len);
242
	}
43 nishi 243
#else
244
	return recv(s, data, len, 0);
245
#endif
16 nishi 246
}
247
 
248
size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
43 nishi 249
#ifndef NO_SSL
16 nishi 250
	if(ssl == NULL) {
251
		return send(s, data, len, 0);
252
	} else {
253
		return SSL_write(ssl, data, len);
254
	}
43 nishi 255
#else
256
	return send(s, data, len, 0);
257
#endif
16 nishi 258
}
259
 
20 nishi 260
#define ERROR_HTML \
18 nishi 261
	"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
262
	"<html>\n" \
263
	"	<head>\n" \
20 nishi 264
	"		<title>%s</title>\n" \
18 nishi 265
	"	</head>\n" \
266
	"	<body>\n" \
20 nishi 267
	"		<h1>%s</h1>\n" \
18 nishi 268
	"		<hr>\n" \
269
	"		", \
270
	    address, \
271
	    "\n" \
272
	    "	</body>\n" \
273
	    "</html>\n"
274
 
32 nishi 275
void _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) {
18 nishi 276
	char construct[512];
212 nishi 277
	size_t incr;
32 nishi 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
	}
215 nishi 286
#if defined(_MSC_VER) || defined(__BORLANDC__)
212 nishi 287
	sprintf(construct, "%lu", (unsigned long)size);
288
#else
18 nishi 289
	sprintf(construct, "%llu", (unsigned long long)size);
212 nishi 290
#endif
18 nishi 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);
24 nishi 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
	}
18 nishi 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);
24 nishi 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);
32 nishi 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
		}
24 nishi 314
	}
315
	if(headers != NULL) {
212 nishi 316
		int i;
24 nishi 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
	}
18 nishi 324
	tw_write(ssl, sock, "\r\n", 2);
24 nishi 325
	if(doc == NULL && f == NULL) return;
212 nishi 326
	incr = 0;
18 nishi 327
	while(1) {
22 nishi 328
		if(f != NULL) {
21 nishi 329
			char buffer[128];
330
			fread(buffer, size < 128 ? size : 128, 1, f);
331
			tw_write(ssl, sock, buffer, size < 128 ? size : 128);
22 nishi 332
		} else {
21 nishi 333
			tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
334
		}
18 nishi 335
		incr += 128;
19 nishi 336
		if(size <= 128) break;
18 nishi 337
		size -= 128;
338
	}
339
}
340
 
32 nishi 341
void 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); }
24 nishi 342
 
18 nishi 343
const char* tw_http_status(int code) {
20 nishi 344
	if(code == 200) {
345
		return "200 OK";
166 nishi 346
	} else if(code == 301) {
167 nishi 347
		return "301 Moved Permanently";
24 nishi 348
	} else if(code == 308) {
349
		return "308 Permanent Redirect";
20 nishi 350
	} else if(code == 400) {
18 nishi 351
		return "400 Bad Request";
20 nishi 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";
161 nishi 358
	} else if(code == 500) {
359
		return "500 Internal Server Error";
18 nishi 360
	} else {
361
		return "400 Bad Request";
362
	}
363
}
364
 
123 nishi 365
char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
18 nishi 366
	char address[1024];
212 nishi 367
	char* st;
368
	char* st2;
369
	char* buffer;
370
	char* str;
371
	int i;
20 nishi 372
 
123 nishi 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
 
212 nishi 379
	st = cm_strdup(tw_http_status(code));
20 nishi 380
	for(i = 0; st[i] != 0; i++) {
381
		if(st[i] == ' ') {
382
			st2 = cm_strdup(st + i + 1);
383
			break;
384
		}
18 nishi 385
	}
212 nishi 386
	buffer = malloc(4096);
387
	str = cm_strcat3(ERROR_HTML);
20 nishi 388
	sprintf(buffer, str, st, st2);
389
	free(str);
390
	free(st);
391
	return buffer;
18 nishi 392
}
393
 
123 nishi 394
void 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);
32 nishi 396
	tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
18 nishi 397
	free(str);
398
}
399
 
20 nishi 400
void addstring(char** str, const char* add, ...) {
401
	int i;
402
	char cbuf[2];
212 nishi 403
	va_list va;
20 nishi 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);
21 nishi 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);
20 nishi 426
			} else if(add[i] == 'd') {
427
				int n = va_arg(va, int);
428
				char* h = malloc(512);
212 nishi 429
				char* tmp = *str;
20 nishi 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
	}
74 nishi 445
	va_end(va);
20 nishi 446
}
447
 
22 nishi 448
char* 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;
212 nishi 452
	if(ext == NULL) return mime;
22 nishi 453
	for(i = 0; i < vhost_entry->mime_count; i++) {
23 nishi 454
		if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
22 nishi 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++) {
23 nishi 461
			if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
22 nishi 462
				mime = config.root.mimes[i].mime;
463
			}
464
		}
465
	}
466
	return mime;
467
}
468
 
469
char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
470
	char* icon = "";
471
	bool set = false;
472
	int i;
212 nishi 473
	if(mime == NULL) return "";
22 nishi 474
	for(i = 0; i < vhost_entry->icon_count; i++) {
23 nishi 475
		if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
22 nishi 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++) {
23 nishi 482
			if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
22 nishi 483
				icon = config.root.icons[i].icon;
484
			}
485
		}
486
	}
487
	return icon;
488
}
489
 
11 nishi 490
struct pass_entry {
491
	int sock;
12 nishi 492
	int port;
11 nishi 493
	bool ssl;
21 nishi 494
	SOCKADDR addr;
11 nishi 495
};
496
 
219 nishi 497
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
216 nishi 498
#define NO_RETURN_THREAD
215 nishi 499
void tw_server_pass(void* ptr) {
97 nishi 500
#elif defined(__HAIKU__)
501
int32_t tw_server_pass(void* ptr) {
187 nishi 502
#elif defined(_PSP) || defined(__PPU__)
183 nishi 503
int tw_server_pass(void* ptr) {
105 nishi 504
#endif
219 nishi 505
#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 506
#define FREE_PTR
11 nishi 507
	int sock = ((struct pass_entry*)ptr)->sock;
508
	bool ssl = ((struct pass_entry*)ptr)->ssl;
14 nishi 509
	int port = ((struct pass_entry*)ptr)->port;
21 nishi 510
	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
11 nishi 511
#else
216 nishi 512
#define NO_RETURN_THREAD
105 nishi 513
	void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
11 nishi 514
#endif
212 nishi 515
	SSL* s = NULL;
43 nishi 516
#ifndef NO_SSL
12 nishi 517
	SSL_CTX* ctx = NULL;
15 nishi 518
	bool sslworks = false;
12 nishi 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;
15 nishi 524
		sslworks = true;
12 nishi 525
	}
43 nishi 526
#endif
212 nishi 527
	char* name = config.hostname;
116 nishi 528
	char address[513];
212 nishi 529
	int ret;
530
	struct tw_http_request req;
531
	struct tw_http_response res;
532
	struct tw_tool tools;
255 nishi 533
	char* addrstr;
534
#ifndef NO_GETNAMEINFO
116 nishi 535
	struct sockaddr* sa = (struct sockaddr*)&addr;
536
	getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
315 nishi 537
#elif defined(__NETWARE__)
538
		address[0] = 0;
257 nishi 539
#else
315 nishi 540
	addrstr = inet_ntoa(addr.sin_addr);
541
	strcpy(address, addrstr);
542
	address[strlen(addrstr)] = 0;
163 nishi 543
#endif
212 nishi 544
#ifdef FREE_PTR
545
	free(ptr);
546
#endif
116 nishi 547
 
20 nishi 548
	res._processed = false;
549
	tw_init_tools(&tools);
212 nishi 550
	ret = tw_http_parse(s, sock, &req);
17 nishi 551
	if(ret == 0) {
116 nishi 552
		char date[513];
553
		time_t t = time(NULL);
554
		struct tm* tm = localtime(&t);
212 nishi 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;
293 nishi 568
		char* chrootpath;
212 nishi 569
		struct tw_config_entry* vhost_entry;
116 nishi 570
		strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
571
 
117 nishi 572
		for(i = 0; req.headers[i] != NULL; i += 2) {
573
			if(cm_strcaseequ(req.headers[i], "User-Agent")) {
116 nishi 574
				free(useragent);
575
				useragent = cm_strdup(req.headers[i + 1]);
576
			}
577
		}
578
 
212 nishi 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, "\"");
116 nishi 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
 
212 nishi 594
		vhost = cm_strdup(config.hostname);
595
		cmtime = 0;
70 nishi 596
		if(req.headers != NULL) {
64 nishi 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;
212 nishi 603
					time_t t;
604
					struct tm* btm;
64 nishi 605
					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
304 nishi 606
#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__USLC__)
212 nishi 607
					t = 0;
608
					btm = localtime(&t);
64 nishi 609
					cmtime = mktime(&tm);
610
					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
32 nishi 611
#else
140 nishi 612
						cmtime = timegm(&tm);
32 nishi 613
#endif
64 nishi 614
				}
21 nishi 615
			}
616
		}
212 nishi 617
		rej = false;
21 nishi 618
		cm_log("Server", "Host is %s", vhost);
212 nishi 619
		port = s == NULL ? 80 : 443;
620
		host = cm_strdup(vhost);
22 nishi 621
		for(i = 0; vhost[i] != 0; i++) {
622
			if(vhost[i] == ':') {
21 nishi 623
				host[i] = 0;
624
				port = atoi(host + i + 1);
625
				break;
626
			}
627
		}
136 nishi 628
		name = host;
21 nishi 629
		cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
212 nishi 630
		vhost_entry = tw_vhost_match(host, port);
161 nishi 631
#ifdef HAS_CHROOT
293 nishi 632
		chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path;
161 nishi 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
20 nishi 645
		for(i = 0; i < config.module_count; i++) {
314 nishi 646
#ifdef __OS2__
647
			tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "MOD_REQUEST");
648
#else
315 nishi 649
				tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
314 nishi 650
#endif
20 nishi 651
			if(mod_req != NULL) {
652
				int ret = mod_req(&tools, &req, &res);
653
				int co = ret & 0xff;
141 nishi 654
				if(co == _TW_MODULE_PASS) {
655
					continue;
656
				} else if(co == _TW_MODULE_STOP) {
657
					/* Handle response here ... */
20 nishi 658
					res._processed = true;
659
					break;
141 nishi 660
				} else if(co == _TW_MODULE_STOP2) {
661
					res._processed = true;
662
					break;
663
				} else if(co == _TW_MODULE_ERROR) {
123 nishi 664
					tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port, vhost_entry);
20 nishi 665
					break;
666
				}
667
			}
668
		}
669
		if(!res._processed) {
212 nishi 670
			char* path;
671
			char* rpath;
672
			struct stat st;
215 nishi 673
			char* slash;
21 nishi 674
			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
212 nishi 675
			path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
21 nishi 676
			cm_log("Server", "Filesystem path is %s", path);
219 nishi 677
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
216 nishi 678
			for(i = strlen(path) - 1; i >= 0; i--) {
679
				if(path[i] == '/') {
215 nishi 680
					path[i] = 0;
216 nishi 681
				} else {
215 nishi 682
					break;
683
				}
684
			}
685
#endif
219 nishi 686
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 687
			rpath = cm_strdup(path);
72 nishi 688
			for(i = strlen(rpath) - 1; i >= 0; i--) {
73 nishi 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
					}
71 nishi 697
					break;
698
				}
699
			}
70 nishi 700
			for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
701
				char* n = cm_strcat("/", reserved_names[i]);
71 nishi 702
				if(cm_nocase_endswith(rpath, n)) {
124 nishi 703
					tw_http_error(s, sock, 403, name, port, vhost_entry);
70 nishi 704
					free(n);
705
					rej = true;
706
					cm_log("Server", "XP Patch ; rejecting access to device");
707
					break;
708
				}
709
				free(n);
710
			}
71 nishi 711
			free(rpath);
70 nishi 712
#endif
713
			if(!rej && stat(path, &st) == 0) {
22 nishi 714
				if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
123 nishi 715
					tw_http_error(s, sock, 403, name, port, vhost_entry);
22 nishi 716
				} else if(S_ISDIR(st.st_mode)) {
24 nishi 717
					if(req.path[strlen(req.path) - 1] != '/') {
215 nishi 718
						char* headers[3] = {"Location", NULL, NULL};
719
						headers[1] = cm_strcat(req.path, "/");
61 nishi 720
						cm_log("Server", "Accessing directory without the slash at the end");
166 nishi 721
						_tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0);
24 nishi 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;
212 nishi 733
								struct stat st;
734
								char* mime;
24 nishi 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
									}
22 nishi 742
								}
24 nishi 743
								stat(p, &st);
212 nishi 744
								mime = tw_get_mime(ext, vhost_entry);
32 nishi 745
								tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
24 nishi 746
								fclose(f);
74 nishi 747
								if(ext != NULL) free(ext);
24 nishi 748
								free(p);
749
								found = true;
750
								break;
22 nishi 751
							}
24 nishi 752
							free(p);
753
						}
754
						if(!found) {
755
							char* str = malloc(1);
212 nishi 756
							char** items;
757
							int readme;
758
							char** readmes;
759
							int readme_count;
760
							int hp;
24 nishi 761
							str[0] = 0;
212 nishi 762
							items = cm_scandir(path);
122 nishi 763
							addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
24 nishi 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");
122 nishi 767
							addstring(&str, "		<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
24 nishi 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");
28 nishi 777
							addstring(&str, "				<th>MIME</th>\n");
778
							addstring(&str, "				<th>Size</th>\n");
24 nishi 779
							addstring(&str, "			</tr>\n");
212 nishi 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;
24 nishi 783
							if(items != NULL) {
29 nishi 784
								int phase = 0;
785
							doit:
24 nishi 786
								for(i = 0; items[i] != NULL; i++) {
33 nishi 787
									int j;
212 nishi 788
									char* ext;
789
									char* itm;
790
									char* icon;
28 nishi 791
									char* fpth = cm_strcat3(path, "/", items[i]);
792
									struct stat s;
793
									char size[512];
212 nishi 794
									char* showmime;
795
									char* mime;
28 nishi 796
									size[0] = 0;
797
									stat(fpth, &s);
29 nishi 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
									}
33 nishi 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
									}
212 nishi 817
									if(s.st_size < NUM1024) {
31 nishi 818
										sprintf(size, "%d", (int)s.st_size);
212 nishi 819
									} else if(s.st_size < NUM1024 * 1024) {
29 nishi 820
										sprintf(size, "%.1fK", (double)s.st_size / 1024);
212 nishi 821
									} else if(s.st_size < NUM1024 * 1024 * 1024) {
29 nishi 822
										sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
212 nishi 823
									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) {
29 nishi 824
										sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
212 nishi 825
									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) {
29 nishi 826
										sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
28 nishi 827
									}
828
 
829
									free(fpth);
830
 
212 nishi 831
									ext = NULL;
24 nishi 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
									}
212 nishi 840
									showmime = "";
841
									mime = tw_get_mime(ext, vhost_entry);
24 nishi 842
									if(strcmp(items[i], "../") == 0) {
843
										mime = "misc/parent";
29 nishi 844
										size[0] = 0;
24 nishi 845
									} else if(items[i][strlen(items[i]) - 1] == '/') {
846
										mime = "misc/dir";
29 nishi 847
										size[0] = 0;
848
									} else {
28 nishi 849
										showmime = mime;
24 nishi 850
									}
212 nishi 851
									icon = tw_get_icon(mime, vhost_entry);
24 nishi 852
									if(ext != NULL) free(ext);
212 nishi 853
									itm = cm_strdup(items[i]);
24 nishi 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);
60 nishi 871
									addstring(&str, "	<td><code>  %h  </code></td>\n", showmime);
872
									addstring(&str, "	<td><code>  %s  </code></td>\n", size);
24 nishi 873
									addstring(&str, "</tr>\n");
874
									free(itm);
22 nishi 875
								}
29 nishi 876
								phase++;
877
								if(phase != 2) goto doit;
878
								for(i = 0; items[i] != NULL; i++) free(items[i]);
879
								free(items);
22 nishi 880
							}
24 nishi 881
							addstring(&str, "		</table>\n");
33 nishi 882
							if(readme != -1) {
212 nishi 883
								struct stat s;
884
								FILE* fr;
885
								char* fpth;
33 nishi 886
								addstring(&str, "<hr>\n");
212 nishi 887
								fpth = cm_strcat3(path, "/", readmes[readme]);
33 nishi 888
								stat(fpth, &s);
212 nishi 889
								fr = fopen(fpth, "r");
33 nishi 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);
70 nishi 896
									free(rmbuf);
33 nishi 897
								}
66 nishi 898
								free(fpth);
33 nishi 899
							}
24 nishi 900
							addstring(&str, "		<hr>\n");
212 nishi 901
							hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
123 nishi 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
							}
24 nishi 907
							addstring(&str, "	</body>\n");
908
							addstring(&str, "</html>\n");
32 nishi 909
							tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
24 nishi 910
							free(str);
21 nishi 911
						}
912
					}
22 nishi 913
				} else {
21 nishi 914
					char* ext = NULL;
212 nishi 915
					char* mime;
916
					FILE* f;
22 nishi 917
					for(i = strlen(req.path) - 1; i >= 0; i--) {
918
						if(req.path[i] == '.') {
21 nishi 919
							ext = cm_strdup(req.path + i);
920
							break;
24 nishi 921
						} else if(req.path[i] == '/') {
922
							break;
21 nishi 923
						}
924
					}
212 nishi 925
					mime = tw_get_mime(ext, vhost_entry);
21 nishi 926
					if(ext != NULL) free(ext);
212 nishi 927
					f = fopen(path, "rb");
173 nishi 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
					}
21 nishi 934
				}
22 nishi 935
			} else {
161 nishi 936
				if(!rej) {
937
					tw_http_error(s, sock, 404, name, port, vhost_entry);
938
				}
21 nishi 939
			}
940
			free(path);
20 nishi 941
		}
21 nishi 942
		free(vhost);
943
		free(host);
22 nishi 944
	} else if(ret == -1) {
18 nishi 945
	} else {
123 nishi 946
		tw_http_error(s, sock, 400, name, port, &config.root);
17 nishi 947
	}
70 nishi 948
	tw_free_request(&req);
12 nishi 949
cleanup:
43 nishi 950
#ifndef NO_SSL
16 nishi 951
	if(sslworks) {
15 nishi 952
		SSL_shutdown(s);
953
	}
954
	SSL_free(s);
46 nishi 955
#endif
11 nishi 956
	close_socket(sock);
219 nishi 957
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
315 nishi 958
#ifdef __NETWARE__
959
	ExitThread(EXIT_THREAD, 0);
960
#else
216 nishi 961
	_endthread();
315 nishi 962
#endif
100 nishi 963
#elif defined(__HAIKU__)
105 nishi 964
		exit_thread(0);
11 nishi 965
#endif
216 nishi 966
#ifndef NO_RETURN_THREAD
214 nishi 967
	return 0;
215 nishi 968
#endif
11 nishi 969
}
970
 
62 nishi 971
#ifdef SERVICE
972
extern SERVICE_STATUS status;
973
extern SERVICE_STATUS_HANDLE status_handle;
974
#endif
975
 
219 nishi 976
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
65 nishi 977
struct thread_entry {
101 nishi 978
#ifdef __HAIKU__
979
	thread_id thread;
315 nishi 980
#elif defined(__NETWARE__)
981
	int thread;
101 nishi 982
#else
65 nishi 983
	HANDLE handle;
101 nishi 984
#endif
65 nishi 985
	bool used;
986
};
987
#endif
988
 
183 nishi 989
extern int running;
990
 
11 nishi 991
void tw_server_loop(void) {
68 nishi 992
	int i;
212 nishi 993
#ifndef USE_POLL
213 nishi 994
	fd_set fdset;
995
	struct timeval tv;
212 nishi 996
#endif
219 nishi 997
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
65 nishi 998
	struct thread_entry threads[2048];
70 nishi 999
	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
65 nishi 1000
		threads[i].used = false;
1001
	}
1002
#endif
118 nishi 1003
#ifdef USE_POLL
302 nishi 1004
	struct pollfd* pollfds = malloc(sizeof(*pollfds) * sockcount);
118 nishi 1005
	for(i = 0; i < sockcount; i++) {
1006
		pollfds[i].fd = sockets[i];
1007
		pollfds[i].events = POLLIN | POLLPRI;
1008
	}
1009
#endif
183 nishi 1010
	while(running) {
212 nishi 1011
		int ret;
118 nishi 1012
#ifdef USE_POLL
212 nishi 1013
		ret = poll(pollfds, sockcount, 1000);
118 nishi 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;
86 nishi 1021
#ifdef __HAIKU__
212 nishi 1022
			ret = select(32, &fdset, NULL, NULL, &tv);
86 nishi 1023
#else
212 nishi 1024
			ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
86 nishi 1025
#endif
118 nishi 1026
#endif
11 nishi 1027
		if(ret == -1) {
219 nishi 1028
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
296 nishi 1029
			if(errno == EINTR) continue;
84 nishi 1030
			cm_log("Server", "Select failure: %s", strerror(errno));
1031
#endif
9 nishi 1032
			break;
70 nishi 1033
		} else if(ret == 0) {
62 nishi 1034
#ifdef SERVICE
70 nishi 1035
			if(status.dwCurrentState == SERVICE_STOP_PENDING) {
62 nishi 1036
				break;
1037
			}
1038
#endif
11 nishi 1039
		} else if(ret > 0) {
9 nishi 1040
			/* connection */
1041
			int i;
11 nishi 1042
			for(i = 0; i < sockcount; i++) {
118 nishi 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) {
9 nishi 1050
					SOCKADDR claddr;
182 nishi 1051
					socklen_t clen = sizeof(claddr);
9 nishi 1052
					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
219 nishi 1053
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 1054
					int j;
1055
					struct pass_entry* e = malloc(sizeof(*e));
12 nishi 1056
					cm_log("Server", "New connection accepted");
11 nishi 1057
					e->sock = sock;
215 nishi 1058
#if defined(_MSC_VER) || defined(__BORLANDC__)
212 nishi 1059
					e->ssl = config.ports[i] & (1UL << 31);
1060
#else
1061
					e->ssl = config.ports[i] & (1ULL << 31);
1062
#endif
12 nishi 1063
					e->port = config.ports[i];
21 nishi 1064
					e->addr = claddr;
97 nishi 1065
#endif
219 nishi 1066
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 1067
#ifdef __OS2__
1068
					_beginthread(tw_server_pass, 0, 0, e);
315 nishi 1069
#elif defined(__NETWARE__)
1070
					BeginThread(tw_server_pass, 0, 0, e);
312 nishi 1071
#else
212 nishi 1072
					_beginthread(tw_server_pass, 0, e);
312 nishi 1073
#endif
187 nishi 1074
#elif defined(_PSP) || defined(__PPU__)
183 nishi 1075
						tw_server_pass(e);
97 nishi 1076
#elif defined(__HAIKU__)
183 nishi 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;
101 nishi 1084
							}
183 nishi 1085
							if(kill) {
1086
								threads[j].used = false;
101 nishi 1087
							}
1088
						}
183 nishi 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
					}
11 nishi 1098
#else
1099
					pid_t pid = fork();
1100
					if(pid == 0) {
89 nishi 1101
						int j;
1102
						for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
212 nishi 1103
						tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr);
11 nishi 1104
						_exit(0);
1105
					} else {
1106
						close_socket(sock);
1107
					}
1108
#endif
9 nishi 1109
				}
1110
			}
1111
		}
1112
	}
255 nishi 1113
	for(i = 0; i < sockcount; i++) {
253 nishi 1114
		close_socket(sockets[i]);
1115
	}
1116
	cm_force_log("Server is down");
9 nishi 1117
}