Subversion Repositories Tewi

Rev

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