From 092f2b181848d4575f4317267866dade7312c542 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 3 Dec 2020 19:20:59 +0000 Subject: Be more specific when a HTTP parser error happens. --- src/modules/m_httpd.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index 330e98c6a..b4e221e62 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -264,14 +264,14 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru Close(); } - void SendHTTPError(unsigned int response) + void SendHTTPError(unsigned int response, const char* errstr = NULL) { static HTTPHeaders empty; std::string data = InspIRCd::Format( "" "

Error %u

%s


" "Powered by InspIRCd", - response, http_status_str((http_status)response)); + response, errstr ? errstr : http_status_str((http_status)response)); Page(data, response, &empty); } @@ -303,8 +303,10 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru if (parser.upgrade || HTTP_PARSER_ERRNO(&parser)) return; http_parser_execute(&parser, &parser_settings, recvq.data(), recvq.size()); - if (parser.upgrade || HTTP_PARSER_ERRNO(&parser)) + if (parser.upgrade) SendHTTPError(status_code ? status_code : 400); + else if (HTTP_PARSER_ERRNO(&parser)) + SendHTTPError(status_code ? status_code : 400, http_errno_description((http_errno)parser.http_errno)); } void ServeData() -- cgit v1.3.1-10-gc9f91 From 08572a9376e6f41109e233cb45d7e491ad1ebf07 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 4 Dec 2020 14:16:19 +0000 Subject: Improve HTTP logging. --- src/modules/m_httpd.cpp | 6 +++++- src/modules/m_httpd_config.cpp | 2 +- src/modules/m_httpd_stats.cpp | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index b4e221e62..efed34799 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -266,12 +266,16 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru void SendHTTPError(unsigned int response, const char* errstr = NULL) { + if (!errstr) + errstr = http_status_str((http_status)response); + + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Sending HTTP error %u: %s", response, errstr); static HTTPHeaders empty; std::string data = InspIRCd::Format( "" "

Error %u

%s


" "Powered by InspIRCd", - response, errstr ? errstr : http_status_str((http_status)response)); + response, errstr); Page(data, response, &empty); } diff --git a/src/modules/m_httpd_config.cpp b/src/modules/m_httpd_config.cpp index fe1427772..bc669dae3 100644 --- a/src/modules/m_httpd_config.cpp +++ b/src/modules/m_httpd_config.cpp @@ -42,7 +42,7 @@ class ModuleHttpConfig : public Module, public HTTPRequestEventListener if ((request.GetPath() != "/config") && (request.GetPath() != "/config/")) return MOD_RES_PASSTHRU; - ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling request for the HTTP /config route"); + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling HTTP request for %s", request.GetPath().c_str()); std::stringstream buffer; ConfigDataHash& config = ServerInstance->Config->config_data; diff --git a/src/modules/m_httpd_stats.cpp b/src/modules/m_httpd_stats.cpp index b40c44d12..0b674cf64 100644 --- a/src/modules/m_httpd_stats.cpp +++ b/src/modules/m_httpd_stats.cpp @@ -431,7 +431,7 @@ class ModuleHttpStats : public Module, public HTTPRequestEventListener if (path[path.size() - 1] == '/') path.erase(path.size() - 1, 1); - ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event"); + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling HTTP request for %s", http->GetPath().c_str()); bool found = true; std::stringstream data; -- cgit v1.3.1-10-gc9f91 From 151a902ced88b0c1f1791d975332bbe293259acb Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 4 Dec 2020 14:16:26 +0000 Subject: Normalise paths in the httpd module. --- src/modules/m_httpd.cpp | 27 ++++++++++++++++++++++++++- src/modules/m_httpd_config.cpp | 2 +- src/modules/m_httpd_stats.cpp | 13 ++++--------- 3 files changed, 31 insertions(+), 11 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index efed34799..a155dd278 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -351,7 +351,32 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru return false; if (url.field_set & (1 << UF_PATH)) - out.path = uri.substr(url.field_data[UF_PATH].off, url.field_data[UF_PATH].len); + { + // Normalise the path. + std::vector pathsegments; + irc::sepstream pathstream(uri.substr(url.field_data[UF_PATH].off, url.field_data[UF_PATH].len), '/'); + for (std::string pathsegment; pathstream.GetToken(pathsegment); ) + { + if (pathsegment == ".") + { + // Stay at the current level. + continue; + } + + if (pathsegment == "..") + { + // Traverse up to the previous level. + if (!pathsegments.empty()) + pathsegment.pop_back(); + continue; + } + + pathsegments.push_back(pathsegment); + } + + out.path.reserve(url.field_data[UF_PATH].len); + out.path.append("/").append(stdalgo::string::join(pathsegments, '/')); + } if (url.field_set & (1 << UF_FRAGMENT)) out.fragment = uri.substr(url.field_data[UF_FRAGMENT].off, url.field_data[UF_FRAGMENT].len); diff --git a/src/modules/m_httpd_config.cpp b/src/modules/m_httpd_config.cpp index bc669dae3..b8b1fd7a7 100644 --- a/src/modules/m_httpd_config.cpp +++ b/src/modules/m_httpd_config.cpp @@ -39,7 +39,7 @@ class ModuleHttpConfig : public Module, public HTTPRequestEventListener ModResult OnHTTPRequest(HTTPRequest& request) CXX11_OVERRIDE { - if ((request.GetPath() != "/config") && (request.GetPath() != "/config/")) + if (request.GetPath() != "/config") return MOD_RES_PASSTHRU; ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling HTTP request for %s", request.GetPath().c_str()); diff --git a/src/modules/m_httpd_stats.cpp b/src/modules/m_httpd_stats.cpp index 0b674cf64..f24fd9aec 100644 --- a/src/modules/m_httpd_stats.cpp +++ b/src/modules/m_httpd_stats.cpp @@ -423,32 +423,27 @@ class ModuleHttpStats : public Module, public HTTPRequestEventListener ModResult HandleRequest(HTTPRequest* http) { - std::string path = http->GetPath(); - - if (path != "/stats" && path.substr(0, 7) != "/stats/") + if (http->GetPath() != "/stats") return MOD_RES_PASSTHRU; - if (path[path.size() - 1] == '/') - path.erase(path.size() - 1, 1); - ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling HTTP request for %s", http->GetPath().c_str()); bool found = true; std::stringstream data; data << ""; - if (path == "/stats") + if (http->GetPath() == "/stats") { data << Stats::ServerInfo << Stats::General << Stats::XLines << Stats::Modules << Stats::Channels << Stats::Users << Stats::Servers << Stats::Commands; } - else if (path == "/stats/general") + else if (http->GetPath() == "/stats/general") { data << Stats::General; } - else if (path == "/stats/users") + else if (http->GetPath() == "/stats/users") { if (enableparams) Stats::ListUsers(data, http->GetParsedURI().query_params); -- cgit v1.3.1-10-gc9f91 From 18e46ce87dae88f48269f03b01b93cfe598b247b Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 4 Dec 2020 15:56:14 +0000 Subject: Fix a copy/paste error in the http path normalising code. --- src/modules/m_httpd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index a155dd278..eefb3ed93 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -367,7 +367,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru { // Traverse up to the previous level. if (!pathsegments.empty()) - pathsegment.pop_back(); + pathsegments.pop_back(); continue; } -- cgit v1.3.1-10-gc9f91