Changeset 212 in Main for trunk/Server
- Timestamp:
- Oct 3, 2024, 2:44:55 AM (6 weeks ago)
- Location:
- trunk/Server
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Server/Makefile
r197 r212 1 1 # $Id$ 2 2 3 OBJ=o 4 STATIC=a 3 5 include $(PWD)/Platform/$(PLATFORM).mk 4 6 5 7 .PHONY: all clean 6 .SUFFIXES: .c . o8 .SUFFIXES: .c .$(OBJ) 7 9 8 OBJS = main. o version.o config.o server.o http.o module.o strptime.o font.o$(EXTOBJS) $(PREOBJS)10 OBJS = main.$(OBJ) version.$(OBJ) config.$(OBJ) server.$(OBJ) http.$(OBJ) module.$(OBJ) strptime.$(OBJ) font.$(OBJ) $(EXTOBJS) $(PREOBJS) 9 11 10 12 all: tewi$(EXEC) $(TARGET) … … 12 14 tewi_strip$(EXEC): tewi$(EXEC) 13 15 14 tewi$(EXEC): $(OBJS) ../Common/common. a15 $(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common. a$(LIBS)16 tewi$(EXEC): $(OBJS) ../Common/common.$(STATIC) 17 $(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common.$(STATIC) $(LIBS) 16 18 $(SERVADD) 17 19 … … 41 43 package_finalize $@ 42 44 43 .c. o:45 .c.$(OBJ): 44 46 $(CC) $(CFLAGS) $(EXTCFLAGS) -c -o $@ $< 45 47 … … 48 50 49 51 clean: 50 rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg 52 rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg *.obj -
trunk/Server/config.c
r189 r212 10 10 #include <stdlib.h> 11 11 #include <string.h> 12 13 #ifndef _MSC_VER 12 14 #include <unistd.h> 13 14 #ifdef __MINGW32__ 15 #endif 16 17 #if defined(__MINGW32__) || defined(_MSC_VER) 15 18 #include <winsock2.h> 16 19 #endif … … 38 41 again: 39 42 for(i = 0; i < vhost->dir_count; i++) { 43 char* noslash; 40 44 struct tw_dir_entry* e = &vhost->dirs[i]; 41 45 pathstart = false; 42 46 if(strlen(path) >= strlen(e->dir)) { 47 int j; 43 48 pathstart = true; 44 int j;45 49 for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) { 46 50 if(path[j] != e->dir[j]) { … … 50 54 } 51 55 } 52 char*noslash = cm_strdup(e->dir);56 noslash = cm_strdup(e->dir); 53 57 noslash[strlen(noslash) - 1] = 0; 54 58 if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) { … … 120 124 121 125 int tw_config_read(const char* path) { 122 cm_log("Config", "Reading %s", path);123 126 char cbuf[2]; 124 cbuf[1] = 0;125 127 int ln = 0; 126 128 int ifbr = 0; 127 129 int ignore = -1; 128 FILE* f = fopen(path, "r"); 130 FILE* f; 131 cm_log("Config", "Reading %s", path); 132 f = fopen(path, "r"); 133 cbuf[1] = 0; 129 134 if(f != NULL) { 130 135 char* line = malloc(1); 131 line[0] = 0;132 136 int stop = 0; 133 137 struct tw_config_entry* current = &config.root; 134 138 char* vhost = NULL; 135 139 char* dir = NULL; 140 line[0] = 0; 136 141 while(stop == 0) { 137 142 int c = fread(cbuf, 1, 1, f); 138 143 if(cbuf[0] == '\n' || c <= 0) { 144 char* l = cm_trim(line); 139 145 ln++; 140 char* l = cm_trim(line);141 146 if(strlen(l) > 0 && l[0] != '#') { 142 147 char** r = cm_split(l, " \t"); … … 227 232 stop = 1; 228 233 } else { 234 int i; 229 235 vhost = cm_strdup(r[1]); 230 236 current = &config.vhosts[config.vhost_count++]; … … 235 241 current->readme_count = 0; 236 242 current->hideport = -1; 237 int i;238 243 current->name = cm_strdup(vhost); 239 244 current->port = -1; … … 262 267 ) { 263 268 for(i = 1; r[i] != NULL; i++) { 269 #ifdef _MSC_VER 270 uint32_t port = atoi(r[i]); 271 #else 264 272 uint64_t port = atoi(r[i]); 273 #endif 274 int j; 265 275 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : ""); 266 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32); 267 int j; 276 #ifdef _MSC_VER 277 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1UL << 31); 278 #else 279 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 31); 280 #endif 268 281 for(j = 0; config.ports[j] != -1; j++) 269 282 ; … … 319 332 cm_log("Config", "Missing condition type at line %d", ln); 320 333 } else { 334 bool ign = false; 321 335 ifbr++; 322 bool ign = false;323 336 if(cm_strcaseequ(r[1], "False")) { 324 337 ign = true; … … 429 442 if(r[0] != NULL) { 430 443 int argc; 444 int i; 445 bool called = false; 446 struct tw_tool tools; 431 447 for(argc = 0; r[argc] != NULL; argc++) 432 448 ; 433 449 stop = 0; 434 int i;435 bool called = false;436 struct tw_tool tools;437 450 tw_init_tools(&tools); 438 451 for(i = 0; i < config.module_count; i++) { -
trunk/Server/http.c
r187 r212 16 16 #include <string.h> 17 17 18 #if def __MINGW32__18 #if defined(__MINGW32__) || defined(_MSC_VER) 19 19 #include <winsock2.h> 20 20 #else … … 54 54 char cbuf[2]; 55 55 int phase = 0; 56 char* header; 57 int nl; 56 58 57 59 #ifdef USE_POLL … … 74 76 req->version = NULL; 75 77 76 char*header = malloc(1);78 header = malloc(1); 77 79 header[0] = 0; 78 intnl = 0;80 nl = 0; 79 81 80 82 while(1) { 83 int i; 84 int len; 85 int n; 81 86 #ifndef USE_POLL 87 struct timeval tv; 82 88 FD_ZERO(&fds); 83 89 FD_SET(sock, &fds); 84 struct timeval tv;85 90 tv.tv_sec = 5; 86 91 tv.tv_usec = 0; … … 90 95 #endif 91 96 #ifdef USE_POLL 92 intn = poll(pollfds, 1, 5000);97 n = poll(pollfds, 1, 5000); 93 98 #else 94 99 #ifdef __HAIKU__ 95 intn = select(32, &fds, NULL, NULL, &tv);96 #else 97 intn = select(FD_SETSIZE, &fds, NULL, NULL, &tv);100 n = select(32, &fds, NULL, NULL, &tv); 101 #else 102 n = select(FD_SETSIZE, &fds, NULL, NULL, &tv); 98 103 #endif 99 104 #endif … … 107 112 } 108 113 #endif 109 intlen = tw_read(ssl, sock, buffer, 512);114 len = tw_read(ssl, sock, buffer, 512); 110 115 if(len <= 0) { 111 116 bad = true; 112 117 break; 113 118 } 114 int i;115 119 for(i = 0; i < len; i++) { 116 120 char c = buffer[i]; … … 125 129 } 126 130 } else { 131 char* tmp; 127 132 if(req->method == NULL) { 128 133 req->method = malloc(1); … … 130 135 } 131 136 cbuf[0] = c; 132 char*tmp = req->method;137 tmp = req->method; 133 138 req->method = cm_strcat(tmp, cbuf); 134 139 free(tmp); … … 144 149 } 145 150 } else { 151 char* tmp; 146 152 if(req->path == NULL) { 147 153 req->path = malloc(1); … … 149 155 } 150 156 cbuf[0] = c; 151 char*tmp = req->path;157 tmp = req->path; 152 158 req->path = cm_strcat(tmp, cbuf); 153 159 free(tmp); … … 161 167 } else { 162 168 /* We have Method, Path, Version now */ 163 169 int j; 170 char* p; 171 int incr; 164 172 if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) { 165 173 cm_log("HTTP", "Bad HTTP Version"); … … 168 176 } 169 177 170 int j; 171 char* p = malloc(1); 178 p = malloc(1); 172 179 p[0] = 0; 173 180 for(j = 0; req->path[j] != 0; j++) { 181 char* tmp; 174 182 if(req->path[j] == '/') { 175 183 cbuf[0] = '/'; … … 180 188 cbuf[0] = req->path[j]; 181 189 } 182 char*tmp = p;190 tmp = p; 183 191 p = cm_strcat(tmp, cbuf); 184 192 free(tmp); … … 187 195 req->path = p; 188 196 189 in t incr = 0;197 incr = 0; 190 198 p = malloc(1); 191 199 p[0] = 0; … … 193 201 if(req->path[j] == '/' || req->path[j] == 0) { 194 202 char oldc = req->path[j]; 203 char* pth; 195 204 cbuf[0] = oldc; 196 205 req->path[j] = 0; 197 206 198 char*pth = req->path + incr;207 pth = req->path + incr; 199 208 200 209 if(strcmp(pth, "..") == 0) { … … 230 239 } 231 240 } else if(c != '\r') { 241 char* tmp; 232 242 if(req->version == NULL) { 233 243 req->version = malloc(1); … … 235 245 } 236 246 cbuf[0] = c; 237 char*tmp = req->version;247 tmp = req->version; 238 248 req->version = cm_strcat(tmp, cbuf); 239 249 free(tmp); … … 246 256 goto getout; 247 257 } else { 258 int j; 248 259 if(req->headers == NULL) { 249 260 req->headers = malloc(sizeof(*req->headers)); 250 261 req->headers[0] = NULL; 251 262 } 252 int j;253 263 for(j = 0; header[j] != 0; j++) { 254 264 if(header[j] == ':') { 265 char* kv; 266 char* vv; 267 char** old; 268 int k; 255 269 header[j] = 0; 256 270 j++; 257 271 for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++) 258 272 ; 259 char* kv = header; 260 char* vv = header + j; 261 262 char** old = req->headers; 263 int k; 273 kv = header; 274 vv = header + j; 275 276 old = req->headers; 264 277 for(k = 0; old[k] != NULL; k++) 265 278 ; … … 281 294 } 282 295 } else if(c != '\r') { 296 char* tmp; 283 297 nl = 0; 284 298 cbuf[0] = c; 285 char*tmp = header;299 tmp = header; 286 300 header = cm_strcat(tmp, cbuf); 287 301 free(tmp); … … 296 310 return 1; 297 311 } 298 char* result = malloc(1); 299 result[0] = 0; 300 int i; 301 for(i = 0; req->path[i] != 0; i++) { 302 if(req->path[i] == '?') { 303 req->path[i] = 0; 304 req->query = cm_strdup(req->path + i + 1); 305 break; 306 } 307 } 308 for(i = 0; req->path[i] != 0; i++) { 309 if(req->path[i] == '%') { 310 if(req->path[i + 1] == 0) continue; 311 cbuf[0] = cm_hex(req->path + i + 1, 2); 312 if(cbuf[0] != '\\') { 313 char* tmp = result; 312 { 313 char* result = malloc(1); 314 int i; 315 int j; 316 int incr; 317 char* p; 318 result[0] = 0; 319 for(i = 0; req->path[i] != 0; i++) { 320 if(req->path[i] == '?') { 321 req->path[i] = 0; 322 req->query = cm_strdup(req->path + i + 1); 323 break; 324 } 325 } 326 for(i = 0; req->path[i] != 0; i++) { 327 if(req->path[i] == '%') { 328 if(req->path[i + 1] == 0) continue; 329 cbuf[0] = cm_hex(req->path + i + 1, 2); 330 if(cbuf[0] != '\\') { 331 char* tmp = result; 332 result = cm_strcat(tmp, cbuf); 333 free(tmp); 334 } 335 i += 2; 336 } else if(req->path[i] != '\\') { 337 char* tmp; 338 cbuf[0] = req->path[i]; 339 tmp = result; 314 340 result = cm_strcat(tmp, cbuf); 315 341 free(tmp); 316 342 } 317 i += 2; 318 } else if(req->path[i] != '\\') { 319 cbuf[0] = req->path[i]; 320 char* tmp = result; 321 result = cm_strcat(tmp, cbuf); 322 free(tmp); 323 } 343 } 344 free(req->path); 345 req->path = result; 346 347 incr = 0; 348 p = malloc(1); 349 p[0] = 0; 350 for(j = 0;; j++) { 351 if(req->path[j] == '/' || req->path[j] == 0) { 352 char oldc = req->path[j]; 353 char* pth; 354 cbuf[0] = oldc; 355 req->path[j] = 0; 356 357 pth = req->path + incr; 358 359 if(strcmp(pth, "..") == 0) { 360 int k; 361 if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0; 362 for(k = strlen(p) - 1; k >= 0; k--) { 363 if(p[k] == '/') { 364 p[k + 1] = 0; 365 break; 366 } 367 } 368 if(strlen(p) == 0) { 369 free(p); 370 p = cm_strdup("/"); 371 } 372 } else if(strcmp(pth, ".") == 0) { 373 } else { 374 char* tmp = p; 375 p = cm_strcat3(tmp, pth, cbuf); 376 free(tmp); 377 } 378 379 incr = j + 1; 380 if(oldc == 0) break; 381 } 382 } 383 free(req->path); 384 req->path = p; 385 return 0; 324 386 } 325 free(req->path);326 req->path = result;327 328 int incr = 0;329 char* p = malloc(1);330 p[0] = 0;331 int j;332 for(j = 0;; j++) {333 if(req->path[j] == '/' || req->path[j] == 0) {334 char oldc = req->path[j];335 cbuf[0] = oldc;336 req->path[j] = 0;337 338 char* pth = req->path + incr;339 340 if(strcmp(pth, "..") == 0) {341 int k;342 if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;343 for(k = strlen(p) - 1; k >= 0; k--) {344 if(p[k] == '/') {345 p[k + 1] = 0;346 break;347 }348 }349 if(strlen(p) == 0) {350 free(p);351 p = cm_strdup("/");352 }353 } else if(strcmp(pth, ".") == 0) {354 } else {355 char* tmp = p;356 p = cm_strcat3(tmp, pth, cbuf);357 free(tmp);358 }359 360 incr = j + 1;361 if(oldc == 0) break;362 }363 }364 free(req->path);365 req->path = p;366 return 0;367 387 } -
trunk/Server/main.c
r209 r212 5 5 #include "../config.h" 6 6 7 #ifndef _MSC_VER 7 8 #include <unistd.h> 9 #endif 8 10 #include <stdio.h> 9 11 #include <stdbool.h> … … 23 25 #include "tw_version.h" 24 26 25 #if def __MINGW32__27 #if defined(__MINGW32__) || defined(_MSC_VER) 26 28 #include <windows.h> 27 29 #endif … … 60 62 #define printf(...) tt_printf(__VA_ARGS__) 61 63 #define STDERR_LOG(...) tt_printf(__VA_ARGS__) 64 #elif defined(_MSC_VER) 65 void STDERR_LOG(const char* format, ...){ 66 va_list args; 67 va_start(args, format); 68 vfprintf(stderr, format, args); 69 va_end(args); 70 } 62 71 #else 63 72 #define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__) … … 72 81 int startup(int argc, char** argv); 73 82 74 #if def __MINGW32__83 #if defined(__MINGW32__) || defined(_MSC_VER) 75 84 char* get_registry(const char* main, const char* sub) { 76 85 DWORD bufsize = 512; … … 511 520 512 521 int main(int argc, char** argv) { 522 int st; 513 523 logfile = stderr; 514 524 #ifdef SERVICE … … 667 677 SleepThread(); 668 678 #endif 669 intst = startup(argc, argv);679 st = startup(argc, argv); 670 680 if(st != -1) { 671 681 #ifdef _PSP … … 692 702 int startup(int argc, char** argv) { 693 703 int i; 694 #ifdef __MINGW32__ 704 char* r; 705 #if defined(__MINGW32__) || defined(_MSC_VER) 695 706 char* confpath = cm_strdup(PREFIX "/etc/tewi.conf"); 696 707 char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir"); … … 770 781 } 771 782 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension); 772 char*r = cm_strcat(tw_server, " running...");783 r = cm_strcat(tw_server, " running..."); 773 784 cm_force_log(r); 774 785 free(r); 775 #if ndef __MINGW32__786 #if !defined(__MINGW32__) && !defined(_MSC_VER) 776 787 signal(SIGCHLD, SIG_IGN); 777 788 signal(SIGPIPE, SIG_IGN); -
trunk/Server/module.c
r189 r212 11 11 12 12 #include <string.h> 13 #include <stdlib.h> 14 #ifndef _MSC_VER 13 15 #include <unistd.h> 14 # include <stdlib.h>16 #endif 15 17 16 18 extern struct tw_config config; … … 25 27 #else 26 28 27 #if def __MINGW32__29 #if defined(__MINGW32__) || defined(_MSC_VER) 28 30 #include <windows.h> 29 31 #else … … 33 35 void* tw_module_load(const char* path) { 34 36 char* p = getcwd(NULL, 0); 37 void* lib; 35 38 chdir(config.server_root); 36 void* lib; 37 #ifdef __MINGW32__ 39 #if defined(__MINGW32__) || defined(_MSC_VER) 38 40 lib = LoadLibraryA(path); 39 41 #else … … 49 51 50 52 void* tw_module_symbol(void* mod, const char* sym) { 51 #if def __MINGW32__53 #if defined(__MINGW32__) || defined(_MSC_VER) 52 54 return GetProcAddress(mod, sym); 53 55 #else -
trunk/Server/server.c
r189 r212 16 16 #include "tw_version.h" 17 17 18 #ifndef _MSC_VER 18 19 #include <unistd.h> 20 #endif 19 21 #include <string.h> 20 22 #include <stdbool.h> … … 23 25 #include <stdlib.h> 24 26 #include <errno.h> 27 #include <sys/types.h> 25 28 #include <sys/stat.h> 26 #include <sys/types.h>27 29 #include <time.h> 28 30 … … 31 33 #include <cm_dir.h> 32 34 33 #if def __MINGW32__35 #if defined(__MINGW32__) || defined(_MSC_VER) 34 36 #ifndef NO_GETADDRINFO 35 37 #include <ws2tcpip.h> … … 70 72 #endif 71 73 74 #ifndef S_ISDIR 75 #define S_ISDIR(x) ((x) & _S_IFDIR) 76 #endif 77 72 78 extern struct tw_config config; 73 79 extern char tw_server[]; … … 78 84 int sockets[MAX_PORTS]; 79 85 80 #if def __MINGW32__86 #if defined(__MINGW32__) || defined(_MSC_VER) 81 87 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"}; 82 88 #endif … … 105 111 106 112 void close_socket(int sock) { 107 #if defined(__MINGW32__) 113 #if defined(__MINGW32__) || defined(_MSC_VER) 108 114 closesocket(sock); 109 115 #else … … 114 120 int tw_server_init(void) { 115 121 int i; 116 #if def __MINGW32__122 #if defined(__MINGW32__) || defined(_MSC_VER) 117 123 WSADATA wsa; 118 124 WSAStartup(MAKEWORD(2, 0), &wsa); … … 122 128 sockcount = i; 123 129 for(i = 0; config.ports[i] != -1; i++) { 130 int yes = 1; 131 int no = 0; 124 132 #ifdef NO_IPV6 125 133 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); … … 127 135 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 128 136 #endif 129 #if def __MINGW32__137 #if defined(__MINGW32__) || defined(_MSC_VER) 130 138 if(sock == INVALID_SOCKET) 131 139 #else … … 136 144 return 1; 137 145 } 138 int yes = 1;139 146 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) { 140 147 close_socket(sock); … … 150 157 #endif 151 158 #ifndef NO_IPV6 152 int no = 0;153 159 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) { 154 160 close_socket(sock); … … 223 229 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) { 224 230 char construct[512]; 231 size_t incr; 225 232 if(mtime != 0 && cmtime != 0 && mtime <= cmtime) { 226 233 status = "304 Not Modified"; … … 231 238 doc = NULL; 232 239 } 240 #ifdef _MSC_VER 241 sprintf(construct, "%lu", (unsigned long)size); 242 #else 233 243 sprintf(construct, "%llu", (unsigned long long)size); 244 #endif 234 245 tw_write(ssl, sock, "HTTP/1.1 ", 9); 235 246 tw_write(ssl, sock, (char*)status, strlen(status)); … … 256 267 } 257 268 } 258 int i;259 269 if(headers != NULL) { 270 int i; 260 271 for(i = 0; headers[i] != NULL; i += 2) { 261 272 tw_write(ssl, sock, headers[i], strlen(headers[i])); … … 267 278 tw_write(ssl, sock, "\r\n", 2); 268 279 if(doc == NULL && f == NULL) return; 269 size_tincr = 0;280 incr = 0; 270 281 while(1) { 271 282 if(f != NULL) { … … 308 319 char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) { 309 320 char address[1024]; 321 char* st; 322 char* st2; 323 char* buffer; 324 char* str; 325 int i; 310 326 311 327 if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) { … … 315 331 } 316 332 317 char* st = cm_strdup(tw_http_status(code)); 318 char* st2; 319 int i; 333 st = cm_strdup(tw_http_status(code)); 320 334 for(i = 0; st[i] != 0; i++) { 321 335 if(st[i] == ' ') { … … 324 338 } 325 339 } 326 char*buffer = malloc(4096);327 char*str = cm_strcat3(ERROR_HTML);340 buffer = malloc(4096); 341 str = cm_strcat3(ERROR_HTML); 328 342 sprintf(buffer, str, st, st2); 329 343 free(str); … … 341 355 int i; 342 356 char cbuf[2]; 357 va_list va; 343 358 cbuf[1] = 0; 344 va_list va;345 359 va_start(va, add); 346 360 for(i = 0; add[i] != 0; i++) { … … 367 381 int n = va_arg(va, int); 368 382 char* h = malloc(512); 383 char* tmp = *str; 369 384 sprintf(h, "%d", n); 370 char* tmp = *str;371 385 *str = cm_strcat(tmp, h); 372 386 free(tmp); … … 388 402 char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) { 389 403 char* mime = "application/octet-stream"; 390 if(ext == NULL) return mime;391 404 bool set = false; 392 405 int i; 406 if(ext == NULL) return mime; 393 407 for(i = 0; i < vhost_entry->mime_count; i++) { 394 408 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) { … … 409 423 char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) { 410 424 char* icon = ""; 411 if(mime == NULL) return "";412 425 bool set = false; 413 426 int i; 427 if(mime == NULL) return ""; 414 428 for(i = 0; i < vhost_entry->icon_count; i++) { 415 429 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) { … … 435 449 }; 436 450 437 #if def __MINGW32__451 #if defined(__MINGW32__) || defined(_MSC_VER) 438 452 unsigned int WINAPI tw_server_pass(void* ptr) { 439 453 #elif defined(__HAIKU__) … … 442 456 int tw_server_pass(void* ptr) { 443 457 #endif 444 #if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) 458 #if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) 459 #define FREE_PTR 445 460 int sock = ((struct pass_entry*)ptr)->sock; 446 461 bool ssl = ((struct pass_entry*)ptr)->ssl; 447 462 int port = ((struct pass_entry*)ptr)->port; 448 463 SOCKADDR addr = ((struct pass_entry*)ptr)->addr; 449 free(ptr);450 464 #else 451 465 void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) { 452 466 #endif 453 char* name = config.hostname; 454 467 SSL* s = NULL; 455 468 #ifndef NO_SSL 456 469 SSL_CTX* ctx = NULL; 457 SSL* s = NULL;458 470 bool sslworks = false; 459 471 if(ssl) { … … 464 476 sslworks = true; 465 477 } 466 #else 467 void* s = NULL; 468 #endif 469 478 #endif 479 char* name = config.hostname; 470 480 char address[513]; 471 address[0] = 0; 481 int ret; 482 struct tw_http_request req; 483 struct tw_http_response res; 484 struct tw_tool tools; 472 485 #ifndef NO_GETADDRINFO 473 486 struct sockaddr* sa = (struct sockaddr*)&addr; 474 487 getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST); 475 488 #endif 476 477 struct tw_http_request req; 478 struct tw_http_response res; 479 struct tw_tool tools; 489 address[0] = 0; 490 #ifdef FREE_PTR 491 free(ptr); 492 #endif 493 480 494 res._processed = false; 481 495 tw_init_tools(&tools); 482 intret = tw_http_parse(s, sock, &req);496 ret = tw_http_parse(s, sock, &req); 483 497 if(ret == 0) { 484 498 char date[513]; 485 499 time_t t = time(NULL); 486 500 struct tm* tm = localtime(&t); 501 char* useragent = cm_strdup(""); 502 int i; 503 char* tmp; 504 char* tmp2; 505 char* tmp3; 506 char* tmp4; 507 char* tmp5; 508 char* log; 509 char* vhost; 510 time_t cmtime; 511 bool rej; 512 char* host; 513 int port; 514 struct tw_config_entry* vhost_entry; 487 515 strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm); 488 516 489 char* useragent = cm_strdup("");490 491 int i;492 517 for(i = 0; req.headers[i] != NULL; i += 2) { 493 518 if(cm_strcaseequ(req.headers[i], "User-Agent")) { … … 497 522 } 498 523 499 char*tmp = cm_strcat3(address, " - [", date);500 char*tmp2 = cm_strcat3(tmp, "] \"", req.method);501 char*tmp3 = cm_strcat3(tmp2, " ", req.path);502 char*tmp4 = cm_strcat3(tmp3, " ", req.version);503 char*tmp5 = cm_strcat3(tmp4, "\" \"", useragent);504 char*log = cm_strcat(tmp5, "\"");524 tmp = cm_strcat3(address, " - [", date); 525 tmp2 = cm_strcat3(tmp, "] \"", req.method); 526 tmp3 = cm_strcat3(tmp2, " ", req.path); 527 tmp4 = cm_strcat3(tmp3, " ", req.version); 528 tmp5 = cm_strcat3(tmp4, "\" \"", useragent); 529 log = cm_strcat(tmp5, "\""); 505 530 free(tmp); 506 531 free(tmp2); … … 512 537 free(log); 513 538 514 char*vhost = cm_strdup(config.hostname);515 time_tcmtime = 0;539 vhost = cm_strdup(config.hostname); 540 cmtime = 0; 516 541 if(req.headers != NULL) { 517 542 for(i = 0; req.headers[i] != NULL; i += 2) { … … 521 546 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) { 522 547 struct tm tm; 548 time_t t; 549 struct tm* btm; 523 550 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm); 524 #if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) 525 t ime_t t= 0;526 struct tm*btm = localtime(&t);551 #if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) 552 t = 0; 553 btm = localtime(&t); 527 554 cmtime = mktime(&tm); 528 555 cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60; … … 533 560 } 534 561 } 535 boolrej = false;562 rej = false; 536 563 cm_log("Server", "Host is %s", vhost); 537 intport = s == NULL ? 80 : 443;538 char*host = cm_strdup(vhost);564 port = s == NULL ? 80 : 443; 565 host = cm_strdup(vhost); 539 566 for(i = 0; vhost[i] != 0; i++) { 540 567 if(vhost[i] == ':') { … … 546 573 name = host; 547 574 cm_log("Server", "Hostname is `%s', port is `%d'", host, port); 548 struct tw_config_entry*vhost_entry = tw_vhost_match(host, port);575 vhost_entry = tw_vhost_match(host, port); 549 576 #ifdef HAS_CHROOT 550 577 char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path; … … 582 609 } 583 610 if(!res._processed) { 611 char* path; 612 char* rpath; 613 struct stat st; 584 614 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root); 585 char*path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);615 path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path); 586 616 cm_log("Server", "Filesystem path is %s", path); 587 #if def __MINGW32__588 char*rpath = cm_strdup(path);617 #if defined(__MINGW32__) || defined(_MSC_VER) 618 rpath = cm_strdup(path); 589 619 for(i = strlen(rpath) - 1; i >= 0; i--) { 590 620 if(rpath[i] == '/') { … … 612 642 free(rpath); 613 643 #endif 614 struct stat st;615 644 if(!rej && stat(path, &st) == 0) { 616 645 if(!tw_permission_allowed(path, addr, req, vhost_entry)) { … … 618 647 } else if(S_ISDIR(st.st_mode)) { 619 648 if(req.path[strlen(req.path) - 1] != '/') { 649 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL}; 620 650 cm_log("Server", "Accessing directory without the slash at the end"); 621 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};622 651 _tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0); 623 652 free(headers[1]); … … 632 661 char* ext = NULL; 633 662 int j; 663 struct stat st; 664 char* mime; 634 665 for(j = strlen(p) - 1; j >= 0; j--) { 635 666 if(p[j] == '.') { … … 640 671 } 641 672 } 642 struct stat st;643 673 stat(p, &st); 644 char*mime = tw_get_mime(ext, vhost_entry);674 mime = tw_get_mime(ext, vhost_entry); 645 675 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0); 646 676 fclose(f); … … 654 684 if(!found) { 655 685 char* str = malloc(1); 686 char** items; 687 int readme; 688 char** readmes; 689 int readme_count; 690 int hp; 656 691 str[0] = 0; 657 char**items = cm_scandir(path);692 items = cm_scandir(path); 658 693 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n"); 659 694 addstring(&str, "<html>\n"); … … 673 708 addstring(&str, " <th>Size</th>\n"); 674 709 addstring(&str, " </tr>\n"); 675 intreadme = -1;676 char**readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;677 intreadme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;710 readme = -1; 711 readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes; 712 readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count; 678 713 if(items != NULL) { 679 714 int phase = 0; … … 681 716 for(i = 0; items[i] != NULL; i++) { 682 717 int j; 718 char* ext; 719 char* itm; 720 char* icon; 683 721 char* fpth = cm_strcat3(path, "/", items[i]); 684 722 struct stat s; 685 723 char size[512]; 724 char* showmime; 725 char* mime; 686 726 size[0] = 0; 687 727 stat(fpth, &s); … … 705 745 } 706 746 } 707 if(s.st_size < 1024ULL) {747 if(s.st_size < NUM1024) { 708 748 sprintf(size, "%d", (int)s.st_size); 709 } else if(s.st_size < 1024ULL* 1024) {749 } else if(s.st_size < NUM1024 * 1024) { 710 750 sprintf(size, "%.1fK", (double)s.st_size / 1024); 711 } else if(s.st_size < 1024ULL* 1024 * 1024) {751 } else if(s.st_size < NUM1024 * 1024 * 1024) { 712 752 sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024); 713 } else if(s.st_size < 1024ULL* 1024 * 1024 * 1024) {753 } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) { 714 754 sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024); 715 } else if(s.st_size < 1024ULL* 1024 * 1024 * 1024 * 1024) {755 } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) { 716 756 sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024); 717 757 } … … 719 759 free(fpth); 720 760 721 char*ext = NULL;761 ext = NULL; 722 762 for(j = strlen(items[i]) - 1; j >= 0; j--) { 723 763 if(items[i][j] == '.') { … … 728 768 } 729 769 } 730 char*showmime = "";731 char*mime = tw_get_mime(ext, vhost_entry);770 showmime = ""; 771 mime = tw_get_mime(ext, vhost_entry); 732 772 if(strcmp(items[i], "../") == 0) { 733 773 mime = "misc/parent"; … … 739 779 showmime = mime; 740 780 } 741 char*icon = tw_get_icon(mime, vhost_entry);781 icon = tw_get_icon(mime, vhost_entry); 742 782 if(ext != NULL) free(ext); 743 char*itm = cm_strdup(items[i]);783 itm = cm_strdup(items[i]); 744 784 if(strlen(itm) >= 32) { 745 785 if(itm[strlen(itm) - 1] == '/') { … … 771 811 addstring(&str, " </table>\n"); 772 812 if(readme != -1) { 813 struct stat s; 814 FILE* fr; 815 char* fpth; 773 816 addstring(&str, "<hr>\n"); 774 char* fpth = cm_strcat3(path, "/", readmes[readme]); 775 struct stat s; 817 fpth = cm_strcat3(path, "/", readmes[readme]); 776 818 stat(fpth, &s); 777 FILE*fr = fopen(fpth, "r");819 fr = fopen(fpth, "r"); 778 820 if(fr != NULL) { 779 821 char* rmbuf = malloc(s.st_size + 1); … … 787 829 } 788 830 addstring(&str, " <hr>\n"); 789 inthp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;831 hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport; 790 832 if(hp == 0) { 791 833 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port); … … 801 843 } else { 802 844 char* ext = NULL; 845 char* mime; 846 FILE* f; 803 847 for(i = strlen(req.path) - 1; i >= 0; i--) { 804 848 if(req.path[i] == '.') { … … 809 853 } 810 854 } 811 char*mime = tw_get_mime(ext, vhost_entry);855 mime = tw_get_mime(ext, vhost_entry); 812 856 if(ext != NULL) free(ext); 813 FILE*f = fopen(path, "rb");857 f = fopen(path, "rb"); 814 858 if(f == NULL) { 815 859 tw_http_error(s, sock, 403, name, port, vhost_entry); … … 841 885 #endif 842 886 close_socket(sock); 843 #if def __MINGW32__844 _endthread ex(0);887 #if defined(__MINGW32__) || defined(_MSC_VER) 888 _endthread(0); 845 889 #elif defined(__HAIKU__) 846 890 exit_thread(0); … … 854 898 #endif 855 899 856 #if defined(__MINGW32__) || defined(__HAIKU__) 900 #if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) 857 901 struct thread_entry { 858 902 #ifdef __HAIKU__ … … 869 913 void tw_server_loop(void) { 870 914 int i; 871 #if defined(__MINGW32__) || defined(__HAIKU__) 915 #ifndef USE_POLL 916 fd_set fdset; 917 struct timeval tv; 918 #endif 919 #if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) 872 920 struct thread_entry threads[2048]; 873 921 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) { … … 881 929 pollfds[i].events = POLLIN | POLLPRI; 882 930 } 883 #else884 fd_set fdset;885 struct timeval tv;886 931 #endif 887 932 while(running) { 933 int ret; 888 934 #ifdef USE_POLL 889 intret = poll(pollfds, sockcount, 1000);935 ret = poll(pollfds, sockcount, 1000); 890 936 #else 891 937 FD_ZERO(&fdset); … … 896 942 tv.tv_usec = 0; 897 943 #ifdef __HAIKU__ 898 intret = select(32, &fdset, NULL, NULL, &tv);899 #else 900 intret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);944 ret = select(32, &fdset, NULL, NULL, &tv); 945 #else 946 ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); 901 947 #endif 902 948 #endif 903 949 if(ret == -1) { 904 #if ndef __MINGW32__950 #if !defined(__MINGW32__) && !defined(_MSC_VER) 905 951 cm_log("Server", "Select failure: %s", strerror(errno)); 906 952 #endif … … 926 972 socklen_t clen = sizeof(claddr); 927 973 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen); 974 #if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) 975 int j; 976 struct pass_entry* e = malloc(sizeof(*e)); 928 977 cm_log("Server", "New connection accepted"); 929 #if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__)930 struct pass_entry* e = malloc(sizeof(*e));931 978 e->sock = sock; 932 e->ssl = config.ports[i] & (1ULL << 32); 979 #ifdef _MSC_VER 980 e->ssl = config.ports[i] & (1UL << 31); 981 #else 982 e->ssl = config.ports[i] & (1ULL << 31); 983 #endif 933 984 e->port = config.ports[i]; 934 985 e->addr = claddr; 935 986 #endif 936 #ifdef __MINGW32__ 937 int j; 938 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { 939 if(threads[j].used) { 940 DWORD ex; 941 GetExitCodeThread(threads[j].handle, &ex); 942 if(ex != STILL_ACTIVE) { 943 CloseHandle(threads[j].handle); 944 threads[j].used = false; 945 } 946 } 947 } 948 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { 949 if(!threads[j].used) { 950 threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL); 951 threads[j].used = true; 952 break; 953 } 954 } 987 #if defined(__MINGW32__) || defined(_MSC_VER) 988 _beginthread(tw_server_pass, 0, e); 955 989 #elif defined(_PSP) || defined(__PPU__) 956 990 tw_server_pass(e); 957 991 #elif defined(__HAIKU__) 958 int j;959 992 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { 960 993 if(threads[j].used) { … … 983 1016 int j; 984 1017 for(j = 0; j < sockcount; j++) close_socket(sockets[j]); 985 tw_server_pass(sock, config.ports[i] & (1ULL << 3 2), config.ports[i], claddr);1018 tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr); 986 1019 _exit(0); 987 1020 } else { -
trunk/Server/strptime.c
r182 r212 36 36 //__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $"); 37 37 38 #if defined(__MINGW32__) 38 #if defined(__MINGW32__) || defined(_MSC_VER) 39 39 40 40 #include <ctype.h> … … 292 292 case 'D': /* The date as "%y/%m/%d". */ 293 293 { 294 int year; 294 295 new_fmt = HERE_D_FMT; 295 296 LEGAL_ALT(0); 296 297 state |= S_MON | S_MDAY | S_YEAR; 297 const intyear = split_year ? tm->tm_year : 0;298 year = split_year ? tm->tm_year : 0; 298 299 299 300 bp = (const unsigned char *)strptime((const char *)bp, … … 399 400 400 401 #ifndef TIME_MAX 402 #ifdef _MSC_VER 403 #define TIME_MAX INT32_MAX 404 #else 401 405 #define TIME_MAX INT64_MAX 406 #endif 402 407 #endif 403 408 case 's': /* seconds since the epoch */ 404 409 { 405 410 time_t sse = 0; 411 #ifdef _MSC_VER 412 uint32_t rulim = TIME_MAX; 413 #else 406 414 uint64_t rulim = TIME_MAX; 415 #endif 407 416 408 417 if (*bp < '0' || *bp > '9') { … … 417 426 } while ((sse * 10 <= TIME_MAX) && 418 427 rulim && *bp >= '0' && *bp <= '9'); 419 428 #ifdef _MSC_VER 429 if (sse < 0 || (uint32_t)sse > TIME_MAX) { 430 #else 420 431 if (sse < 0 || (uint64_t)sse > TIME_MAX) { 432 #endif 421 433 bp = NULL; 422 434 continue; 423 435 } 424 436 #ifdef _WIN32 437 #ifdef _MSC_VER 438 if (1) 439 #else 425 440 if (localtime_s(tm, &sse) == 0) 441 #endif 426 442 #else 427 443 if (localtime_r(&sse, tm)) -
trunk/Server/tw_config.h
r187 r212 15 15 #include <stdbool.h> 16 16 17 #if def __MINGW32__17 #if defined(__MINGW32__) || defined(_MSC_VER) 18 18 #include <winsock2.h> 19 19 #define NO_IPV6 … … 42 42 #define MAX_INDEX 1024 43 43 #define MAX_README 8 44 45 #ifdef _MSC_VER 46 #define NUM1024 1024UL 47 #else 48 #define NUM1024 1024ULL 49 #endif 44 50 45 51 enum TW_DIR_TYPE { … … 89 95 90 96 struct tw_config { 97 #ifdef _MSC_VER 98 uint32_t ports[MAX_PORTS + 1]; 99 #else 91 100 uint64_t ports[MAX_PORTS + 1]; /* If port & (1 << 32) is non-zero, it is SSL */ 101 #endif 92 102 char hostname[1025]; 93 103 char* defined[1025]; -
trunk/Server/version.c
r187 r212 14 14 #elif defined(__linux__) 15 15 "Linux" 16 #elif defined(__MINGW32__) 16 #elif defined(__MINGW32__) || defined(_MSC_VER) 17 17 "Windows" 18 18 #elif defined(__HAIKU__)
Note:
See TracChangeset
for help on using the changeset viewer.