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