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