Subversion Repositories Tewi

Rev

Rev 216 | Rev 240 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 216 Rev 219
Line 1... Line 1...
1
/* $Id: server.c 216 2024-10-02 19:31:23Z nishi $ */
1
/* $Id: server.c 219 2024-10-02 20:40:37Z nishi $ */
2
 
2
 
3
#define SOURCE
3
#define SOURCE
4
 
4
 
5
#include "../config.h"
5
#include "../config.h"
6
 
6
 
Line 30... Line 30...
30
 
30
 
31
#include <cm_string.h>
31
#include <cm_string.h>
32
#include <cm_log.h>
32
#include <cm_log.h>
33
#include <cm_dir.h>
33
#include <cm_dir.h>
34
 
34
 
35
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
35
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
36
#ifndef NO_GETADDRINFO
36
#ifndef NO_GETADDRINFO
37
#include <ws2tcpip.h>
37
#include <ws2tcpip.h>
38
#include <wspiapi.h>
38
#include <wspiapi.h>
39
#endif
39
#endif
40
#include <winsock2.h>
40
#include <winsock2.h>
Line 82... Line 82...
82
int sockcount = 0;
82
int sockcount = 0;
83
 
83
 
84
SOCKADDR addresses[MAX_PORTS];
84
SOCKADDR addresses[MAX_PORTS];
85
int sockets[MAX_PORTS];
85
int sockets[MAX_PORTS];
86
 
86
 
87
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
87
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
88
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"};
88
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"};
89
#endif
89
#endif
90
 
90
 
91
/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
91
/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
92
int tw_wildcard_match(const char* wildcard, const char* target) {
92
int tw_wildcard_match(const char* wildcard, const char* target) {
Line 109... Line 109...
109
		}
109
		}
110
	}
110
	}
111
}
111
}
112
 
112
 
113
void close_socket(int sock) {
113
void close_socket(int sock) {
114
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
114
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
115
	closesocket(sock);
115
	closesocket(sock);
116
#else
116
#else
117
	close(sock);
117
	close(sock);
118
#endif
118
#endif
119
}
119
}
120
 
120
 
121
int tw_server_init(void) {
121
int tw_server_init(void) {
122
	int i;
122
	int i;
123
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
123
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
124
	WSADATA wsa;
124
	WSADATA wsa;
125
	WSAStartup(MAKEWORD(2, 0), &wsa);
125
	WSAStartup(MAKEWORD(2, 0), &wsa);
126
#endif
126
#endif
127
	for(i = 0; config.ports[i] != -1; i++)
127
	for(i = 0; config.ports[i] != -1; i++)
128
		;
128
		;
Line 447... Line 447...
447
	int port;
447
	int port;
448
	bool ssl;
448
	bool ssl;
449
	SOCKADDR addr;
449
	SOCKADDR addr;
450
};
450
};
451
 
451
 
452
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
452
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
453
#define NO_RETURN_THREAD
453
#define NO_RETURN_THREAD
454
void tw_server_pass(void* ptr) {
454
void tw_server_pass(void* ptr) {
455
#elif defined(__HAIKU__)
455
#elif defined(__HAIKU__)
456
int32_t tw_server_pass(void* ptr) {
456
int32_t tw_server_pass(void* ptr) {
457
#elif defined(_PSP) || defined(__PPU__)
457
#elif defined(_PSP) || defined(__PPU__)
458
int tw_server_pass(void* ptr) {
458
int tw_server_pass(void* ptr) {
459
#endif
459
#endif
460
#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__)
460
#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
461
#define FREE_PTR
461
#define FREE_PTR
462
	int sock = ((struct pass_entry*)ptr)->sock;
462
	int sock = ((struct pass_entry*)ptr)->sock;
463
	bool ssl = ((struct pass_entry*)ptr)->ssl;
463
	bool ssl = ((struct pass_entry*)ptr)->ssl;
464
	int port = ((struct pass_entry*)ptr)->port;
464
	int port = ((struct pass_entry*)ptr)->port;
465
	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
465
	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
Line 549... Line 549...
549
				} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
549
				} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
550
					struct tm tm;
550
					struct tm tm;
551
					time_t t;
551
					time_t t;
552
					struct tm* btm;
552
					struct tm* btm;
553
					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
553
					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
554
#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) || defined(__BORLANDC__)
554
#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
555
					t = 0;
555
					t = 0;
556
					btm = localtime(&t);
556
					btm = localtime(&t);
557
					cmtime = mktime(&tm);
557
					cmtime = mktime(&tm);
558
					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
558
					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
559
#else
559
#else
Line 616... Line 616...
616
			struct stat st;
616
			struct stat st;
617
			char* slash;
617
			char* slash;
618
			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
618
			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
619
			path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
619
			path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
620
			cm_log("Server", "Filesystem path is %s", path);
620
			cm_log("Server", "Filesystem path is %s", path);
621
#if defined(_MSC_VER) || defined(__BORLANDC__)
621
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
622
			for(i = strlen(path) - 1; i >= 0; i--) {
622
			for(i = strlen(path) - 1; i >= 0; i--) {
623
				if(path[i] == '/') {
623
				if(path[i] == '/') {
624
					path[i] = 0;
624
					path[i] = 0;
625
				} else {
625
				} else {
626
					break;
626
					break;
627
				}
627
				}
628
			}
628
			}
629
#endif
629
#endif
630
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
630
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
631
			rpath = cm_strdup(path);
631
			rpath = cm_strdup(path);
632
			for(i = strlen(rpath) - 1; i >= 0; i--) {
632
			for(i = strlen(rpath) - 1; i >= 0; i--) {
633
				if(rpath[i] == '/') {
633
				if(rpath[i] == '/') {
634
					int j;
634
					int j;
635
					for(j = i + 1; rpath[j] != 0; j++) {
635
					for(j = i + 1; rpath[j] != 0; j++) {
Line 896... Line 896...
896
		SSL_shutdown(s);
896
		SSL_shutdown(s);
897
	}
897
	}
898
	SSL_free(s);
898
	SSL_free(s);
899
#endif
899
#endif
900
	close_socket(sock);
900
	close_socket(sock);
901
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
901
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
902
	_endthread();
902
	_endthread();
903
#elif defined(__HAIKU__)
903
#elif defined(__HAIKU__)
904
		exit_thread(0);
904
		exit_thread(0);
905
#endif
905
#endif
906
#ifndef NO_RETURN_THREAD
906
#ifndef NO_RETURN_THREAD
Line 911... Line 911...
911
#ifdef SERVICE
911
#ifdef SERVICE
912
extern SERVICE_STATUS status;
912
extern SERVICE_STATUS status;
913
extern SERVICE_STATUS_HANDLE status_handle;
913
extern SERVICE_STATUS_HANDLE status_handle;
914
#endif
914
#endif
915
 
915
 
916
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__)
916
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
917
struct thread_entry {
917
struct thread_entry {
918
#ifdef __HAIKU__
918
#ifdef __HAIKU__
919
	thread_id thread;
919
	thread_id thread;
920
#else
920
#else
921
	HANDLE handle;
921
	HANDLE handle;
Line 930... Line 930...
930
	int i;
930
	int i;
931
#ifndef USE_POLL
931
#ifndef USE_POLL
932
	fd_set fdset;
932
	fd_set fdset;
933
	struct timeval tv;
933
	struct timeval tv;
934
#endif
934
#endif
935
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__)
935
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
936
	struct thread_entry threads[2048];
936
	struct thread_entry threads[2048];
937
	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
937
	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
938
		threads[i].used = false;
938
		threads[i].used = false;
939
	}
939
	}
940
#endif
940
#endif
Line 961... Line 961...
961
#else
961
#else
962
			ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
962
			ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
963
#endif
963
#endif
964
#endif
964
#endif
965
		if(ret == -1) {
965
		if(ret == -1) {
966
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__)
966
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
967
			cm_log("Server", "Select failure: %s", strerror(errno));
967
			cm_log("Server", "Select failure: %s", strerror(errno));
968
#endif
968
#endif
969
			break;
969
			break;
970
		} else if(ret == 0) {
970
		} else if(ret == 0) {
971
#ifdef SERVICE
971
#ifdef SERVICE
Line 985... Line 985...
985
#endif
985
#endif
986
				if(cond) {
986
				if(cond) {
987
					SOCKADDR claddr;
987
					SOCKADDR claddr;
988
					socklen_t clen = sizeof(claddr);
988
					socklen_t clen = sizeof(claddr);
989
					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
989
					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
990
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__)
990
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
991
					int j;
991
					int j;
992
					struct pass_entry* e = malloc(sizeof(*e));
992
					struct pass_entry* e = malloc(sizeof(*e));
993
					cm_log("Server", "New connection accepted");
993
					cm_log("Server", "New connection accepted");
994
					e->sock = sock;
994
					e->sock = sock;
995
#if defined(_MSC_VER) || defined(__BORLANDC__)
995
#if defined(_MSC_VER) || defined(__BORLANDC__)
Line 998... Line 998...
998
					e->ssl = config.ports[i] & (1ULL << 31);
998
					e->ssl = config.ports[i] & (1ULL << 31);
999
#endif
999
#endif
1000
					e->port = config.ports[i];
1000
					e->port = config.ports[i];
1001
					e->addr = claddr;
1001
					e->addr = claddr;
1002
#endif
1002
#endif
1003
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
1003
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
1004
					_beginthread(tw_server_pass, 0, e);
1004
					_beginthread(tw_server_pass, 0, e);
1005
#elif defined(_PSP) || defined(__PPU__)
1005
#elif defined(_PSP) || defined(__PPU__)
1006
						tw_server_pass(e);
1006
						tw_server_pass(e);
1007
#elif defined(__HAIKU__)
1007
#elif defined(__HAIKU__)
1008
					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
1008
					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {