Changeset 70 in Main
- Timestamp:
- Sep 19, 2024, 6:23:45 PM (8 weeks ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/cm_string.h
r21 r70 7 7 8 8 int cm_hex(const char* str, int len); 9 bool cm_nocase_endswith(const char* str, const char* end); 10 bool cm_endswith(const char* str, const char* end); 9 11 char* cm_html_escape(const char* str); 10 12 char* cm_url_escape(const char* str); -
trunk/Common/string.c
r21 r70 8 8 9 9 char* cm_strcat(const char* a, const char* b) { 10 if(a == NULL) a = ""; 11 if(b == NULL) b = ""; 10 12 char* str = malloc(strlen(a) + strlen(b) + 1); 11 13 memcpy(str, a, strlen(a)); … … 23 25 24 26 char* cm_strdup(const char* str) { return cm_strcat(str, ""); } 27 28 bool cm_endswith(const char* str, const char* end) { 29 if(strlen(str) < strlen(end)) return false; 30 int i; 31 for(i = strlen(str) - strlen(end); i < strlen(str); i++) { 32 if(str[i] != end[i - strlen(str) + strlen(end)]) return false; 33 } 34 return true; 35 } 36 37 bool cm_nocase_endswith(const char* str, const char* end) { 38 if(strlen(str) < strlen(end)) return false; 39 int i; 40 for(i = strlen(str) - strlen(end); i < strlen(str); i++) { 41 if(tolower(str[i]) != tolower(end[i - strlen(str) + strlen(end)])) return false; 42 } 43 return true; 44 } 25 45 26 46 char* cm_trimstart(const char* str) { -
trunk/Server/http.c
r69 r70 83 83 #endif 84 84 int len = tw_read(ssl, sock, buffer, 512); 85 if(len <= 0) {85 if(len <= 0) { 86 86 bad = true; 87 87 break; … … 285 285 if(req->path[i + 1] == 0) continue; 286 286 cbuf[0] = cm_hex(req->path + i + 1, 2); 287 char* tmp = result; 288 result = cm_strcat(tmp, cbuf); 289 free(tmp); 287 if(cbuf[0] != '\\') { 288 char* tmp = result; 289 result = cm_strcat(tmp, cbuf); 290 free(tmp); 291 } 290 292 i += 2; 291 } else {293 } else if(req->path[i] != '\\') { 292 294 cbuf[0] = req->path[i]; 293 295 char* tmp = result; … … 325 327 } 326 328 } else if(strcmp(pth, ".") == 0) { 327 } else if(oldc != '\\'){329 } else { 328 330 char* tmp = p; 329 331 p = cm_strcat3(tmp, pth, cbuf); -
trunk/Server/main.c
r62 r70 37 37 SERVICE_STATUS_HANDLE status_handle; 38 38 39 void WINAPI servhandler(DWORD control) {40 switch(control) {41 42 43 44 39 void WINAPI servhandler(DWORD control) { 40 switch(control) { 41 case SERVICE_CONTROL_STOP: 42 case SERVICE_CONTROL_SHUTDOWN: 43 status.dwCurrentState = SERVICE_STOP_PENDING; 44 break; 45 45 } 46 46 SetServiceStatus(status_handle, &status); 47 47 } 48 48 49 void WINAPI servmain(DWORD argc, LPSTR* argv) {49 void WINAPI servmain(DWORD argc, LPSTR* argv) { 50 50 logfile = fopen(PREFIX "/logs/tewi.log", "a"); 51 51 if(logfile == NULL) logfile = stderr; … … 61 61 if(SetServiceStatus(status_handle, &status) == 0) return; 62 62 int st = startup(argc, argv); 63 if(st != -1) {63 if(st != -1) { 64 64 status.dwWin32ExitCode = NO_ERROR; 65 65 status.dwServiceSpecificExitCode = st; … … 88 88 } 89 89 90 int startup(int argc, char** argv) {90 int startup(int argc, char** argv) { 91 91 int i; 92 92 const char* confpath = PREFIX "/etc/tewi.conf"; 93 if(argv != NULL) {93 if(argv != NULL) { 94 94 for(i = 1; i < argc; i++) { 95 95 if(argv[i][0] == '-') { … … 97 97 if(!cm_do_log) { 98 98 cm_do_log = true; 99 99 #ifndef NO_SSL 100 100 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT); 101 101 #else 102 102 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version()); 103 103 #endif 104 104 } else { 105 105 cm_do_log = true; -
trunk/Server/server.c
r68 r70 51 51 SOCKADDR addresses[MAX_PORTS]; 52 52 int sockets[MAX_PORTS]; 53 54 #ifdef __MINGW32__ 55 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"}; 56 #endif 53 57 54 58 /* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */ … … 429 433 int i; 430 434 time_t cmtime = 0; 431 if(req.headers != NULL) {435 if(req.headers != NULL) { 432 436 for(i = 0; req.headers[i] != NULL; i += 2) { 433 437 if(cm_strcaseequ(req.headers[i], "Host")) { … … 480 484 char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path); 481 485 cm_log("Server", "Filesystem path is %s", path); 486 bool rej = false; 487 #ifdef __MINGW32__ 488 for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) { 489 char* n = cm_strcat("/", reserved_names[i]); 490 if(cm_nocase_endswith(path, n)) { 491 tw_http_error(s, sock, 403, name, port); 492 free(n); 493 rej = true; 494 cm_log("Server", "XP Patch ; rejecting access to device"); 495 break; 496 } 497 free(n); 498 char* y = cm_strcat3("/", reserved_names[i], ":"); 499 if(cm_nocase_endswith(path, y)) { 500 tw_http_error(s, sock, 403, name, port); 501 free(y); 502 rej = true; 503 cm_log("Server", "XP Patch ; rejecting access to device"); 504 break; 505 } 506 free(y); 507 } 508 #endif 482 509 struct stat st; 483 if( stat(path, &st) == 0) {510 if(!rej && stat(path, &st) == 0) { 484 511 if(!tw_permission_allowed(path, addr, req, vhost_entry)) { 485 512 tw_http_error(s, sock, 403, name, port); … … 648 675 addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf); 649 676 fclose(fr); 677 free(rmbuf); 650 678 } 651 679 free(fpth); … … 682 710 free(vhost); 683 711 free(host); 684 tw_free_request(&req);685 712 } else if(ret == -1) { 686 713 } else { 687 714 tw_http_error(s, sock, 400, name, port); 688 715 } 716 tw_free_request(&req); 689 717 cleanup: 690 718 #ifndef NO_SSL … … 718 746 #ifdef __MINGW32__ 719 747 struct thread_entry threads[2048]; 720 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {748 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) { 721 749 threads[i].used = false; 722 750 } … … 732 760 if(ret == -1) { 733 761 break; 734 } else if(ret == 0){735 #ifdef __MINGW32__ 736 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {737 if(threads[i].used) {762 } else if(ret == 0) { 763 #ifdef __MINGW32__ 764 for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) { 765 if(threads[i].used) { 738 766 DWORD ex; 739 767 GetExitCodeThread(threads[i].handle, &ex); 740 if(ex != STILL_ACTIVE) {768 if(ex != STILL_ACTIVE) { 741 769 CloseHandle(threads[i].handle); 742 770 threads[i].used = false; … … 746 774 #endif 747 775 #ifdef SERVICE 748 if(status.dwCurrentState == SERVICE_STOP_PENDING) {776 if(status.dwCurrentState == SERVICE_STOP_PENDING) { 749 777 break; 750 778 } … … 766 794 e->addr = claddr; 767 795 int j; 768 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {769 if(threads[j].used) {796 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { 797 if(threads[j].used) { 770 798 DWORD ex; 771 799 GetExitCodeThread(threads[j].handle, &ex); 772 if(ex != STILL_ACTIVE) {800 if(ex != STILL_ACTIVE) { 773 801 CloseHandle(threads[j].handle); 774 802 threads[j].used = false; … … 776 804 } 777 805 } 778 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {779 if(!threads[j].used) {806 for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { 807 if(!threads[j].used) { 780 808 threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL); 781 809 threads[j].used = true; -
trunk/Server/tw_version.h
r49 r70 4 4 #define __TW_VERSION_H__ 5 5 6 #define TW_VERSION "1.0 0\0"6 #define TW_VERSION "1.01\0" 7 7 8 8 const char* tw_get_version(void);
Note:
See TracChangeset
for help on using the changeset viewer.