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