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