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