Subversion Repositories Tewi

Rev

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