Subversion Repositories Tewi

Rev

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