Subversion Repositories Tewi

Rev

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