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