Subversion Repositories Tewi

Rev

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