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