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