From cb2b7829260babb440546d0644a7552195966bc7 Mon Sep 17 00:00:00 2001 From: Nishi Date: Sun, 22 Sep 2024 13:43:47 +0000 Subject: [PATCH] add ShowPort/HidePort git-svn-id: file:///raid/svn-personal/tewi/trunk@123 8739d7e6-ffea-ec47-b151-bdff447c6205 --- Manpage/tewi.conf.5.tmpl | 2 ++ Server/config.c | 6 ++++++ Server/server.c | 28 +++++++++++++++++++--------- Server/tw_config.h | 1 + Server/tw_version.h | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Manpage/tewi.conf.5.tmpl b/Manpage/tewi.conf.5.tmpl index 77434df..6280a17 100644 --- a/Manpage/tewi.conf.5.tmpl +++ b/Manpage/tewi.conf.5.tmpl @@ -18,6 +18,8 @@ Specifies the port(s) to be listened on. Specifies the document root. .It Pa "LoadModule" Loads the module. +.It Pa "HidePort" "ShowPort" +Shows/Hides the port in directory listing, and the default error page. .It Pa "MIMEType" Specifies the MIME type for files. \fBall\fR can be used instead of the extension if you want to make all extensions appear as same MIME. diff --git a/Server/config.c b/Server/config.c index a61ded1..9fd3fb0 100644 --- a/Server/config.c +++ b/Server/config.c @@ -84,6 +84,7 @@ void tw_config_init(void) { config.root.icon_count = 0; config.root.index_count = 0; config.root.readme_count = 0; + config.root.hideport = 0; config.vhost_count = 0; config.module_count = 0; config.extension = NULL; @@ -185,6 +186,7 @@ int tw_config_read(const char* path) { current->icon_count = 0; current->index_count = 0; current->readme_count = 0; + current->hideport = -1; int i; current->name = cm_strdup(vhost); current->port = -1; @@ -216,6 +218,10 @@ int tw_config_read(const char* path) { ; config.ports[j] = port; } + } else if(cm_strcaseequ(r[0], "HidePort")) { + current->hideport = 1; + } else if(cm_strcaseequ(r[0], "ShowPort")) { + current->hideport = 0; } else if(cm_strcaseequ(r[0], "SSLKey")) { if(r[1] == NULL) { cm_log("Config", "Missing path at line %d", ln); diff --git a/Server/server.c b/Server/server.c index 4351615..4a4857c 100644 --- a/Server/server.c +++ b/Server/server.c @@ -284,9 +284,14 @@ const char* tw_http_status(int code) { } } -char* tw_http_default_error(int code, char* name, int port) { +char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) { char address[1024]; - sprintf(address, "
%s Server at %s Port %d
", tw_server, name, port); + + if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) { + sprintf(address, "
%s Server at %s
", tw_server, name, port); + } else { + sprintf(address, "
%s Server at %s Port %d
", tw_server, name, port); + } char* st = cm_strdup(tw_http_status(code)); char* st2; @@ -305,8 +310,8 @@ char* tw_http_default_error(int code, char* name, int port) { return buffer; } -void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) { - char* str = tw_http_default_error(error, name, port); +void tw_http_error(SSL* ssl, int sock, int error, char* name, int port, struct tw_config_entry* vhost) { + char* str = tw_http_default_error(error, name, port, vhost); tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0); free(str); } @@ -525,7 +530,7 @@ int32_t tw_server_pass(void* ptr) { break; } if(co == _TW_MODULE_ERROR) { - tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port); + tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port, vhost_entry); break; } } @@ -565,7 +570,7 @@ int32_t tw_server_pass(void* ptr) { struct stat st; if(!rej && stat(path, &st) == 0) { if(!tw_permission_allowed(path, addr, req, vhost_entry)) { - tw_http_error(s, sock, 403, name, port); + tw_http_error(s, sock, 403, name, port, vhost_entry); } else if(S_ISDIR(st.st_mode)) { if(req.path[strlen(req.path) - 1] != '/') { cm_log("Server", "Accessing directory without the slash at the end"); @@ -737,7 +742,12 @@ int32_t tw_server_pass(void* ptr) { free(fpth); } addstring(&str, "
\n"); - addstring(&str, "
%s Server at %s Port %d
\n", tw_server, name, port); + int hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport; + if(hp == 0) { + addstring(&str, "
%s Server at %s Port %d
\n", tw_server, name, port); + } else { + addstring(&str, "
%s Server at %s
\n", tw_server, name, port); + } addstring(&str, " \n"); addstring(&str, "\n"); tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0); @@ -761,7 +771,7 @@ int32_t tw_server_pass(void* ptr) { fclose(f); } } else { - tw_http_error(s, sock, 404, name, port); + tw_http_error(s, sock, 404, name, port, vhost_entry); } free(path); } @@ -769,7 +779,7 @@ int32_t tw_server_pass(void* ptr) { free(host); } else if(ret == -1) { } else { - tw_http_error(s, sock, 400, name, port); + tw_http_error(s, sock, 400, name, port, &config.root); } tw_free_request(&req); cleanup: diff --git a/Server/tw_config.h b/Server/tw_config.h index 27afb4c..1ba9826 100644 --- a/Server/tw_config.h +++ b/Server/tw_config.h @@ -60,6 +60,7 @@ struct tw_config_entry { char* sslkey; char* sslcert; char* root; + int hideport; struct tw_dir_entry dirs[MAX_DIRS]; int dir_count; struct tw_mime_entry mimes[MAX_DIRS]; diff --git a/Server/tw_version.h b/Server/tw_version.h index 640f0db..fb05dca 100644 --- a/Server/tw_version.h +++ b/Server/tw_version.h @@ -3,7 +3,7 @@ #ifndef __TW_VERSION_H__ #define __TW_VERSION_H__ -#define TW_VERSION "1.02A\0" +#define TW_VERSION "1.03\0" const char* tw_get_version(void); const char* tw_get_platform(void); -- 2.45.2