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