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