Subversion Repositories Tewi

Rev

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