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