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