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