From 29215f99bc663bb7b39805e83d07b220598943f1 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Wed, 15 Aug 2018 11:06:26 +0100 Subject: Rewrite m_httpd_config. - Send the configuration as plain text instead of HTML. - Show the location where each tag was located in a comment. - Indent configuration keys consistently so they are easy to read. --- src/modules/m_httpd_config.cpp | 88 ++++++++++++------------------------------ 1 file changed, 25 insertions(+), 63 deletions(-) diff --git a/src/modules/m_httpd_config.cpp b/src/modules/m_httpd_config.cpp index c47ddb5b2..f729b2774 100644 --- a/src/modules/m_httpd_config.cpp +++ b/src/modules/m_httpd_config.cpp @@ -32,78 +32,40 @@ class ModuleHttpConfig : public Module, public HTTPRequestEventListener { } - std::string Sanitize(const std::string &str) + ModResult OnHTTPRequest(HTTPRequest& request) CXX11_OVERRIDE { - std::string ret; + if ((request.GetURI() != "/config") && (request.GetURI() != "/config/")) + return MOD_RES_PASSTHRU; - for (std::string::const_iterator x = str.begin(); x != str.end(); ++x) - { - switch (*x) - { - case '<': - ret += "<"; - break; - case '>': - ret += ">"; - break; - case '&': - ret += "&"; - break; - case '"': - ret += """; - break; - default: - if (*x < 32 || *x > 126) - { - int n = *x; - ret += ("&#" + ConvToStr(n) + ";"); - } - else - ret += *x; - break; - } - } - return ret; - } - - ModResult HandleRequest(HTTPRequest* http) - { - std::stringstream data(""); + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling request for the HTTP /config route"); + std::stringstream buffer; + ConfigDataHash& config = ServerInstance->Config->config_data; + for (ConfigDataHash::const_iterator citer = config.begin(); citer != config.end(); ++citer) { - ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event"); + // Show the location of the tag in a comment. + ConfigTag* tag = citer->second; + buffer << "# " << tag->getTagLocation() << std::endl + << '<' << tag->tag << ' '; - if ((http->GetURI() == "/config") || (http->GetURI() == "/config/")) + // Print out the tag with all keys aligned vertically. + const std::string indent(tag->tag.length() + 2, ' '); + const ConfigItems& items = tag->getItems(); + for (ConfigItems::const_iterator kiter = items.begin(); kiter != items.end(); ) { - data << "InspIRCd Configuration"; - data << "

InspIRCd Configuration

"; - - for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x) - { - data << "<" << x->first << " "; - const ConfigItems& items = x->second->getItems(); - for (ConfigItems::const_iterator j = items.begin(); j != items.end(); j++) - { - data << Sanitize(j->first) << "="" << Sanitize(j->second) << "" "; - } - data << ">
"; - } - - data << ""; - /* Send the document back to m_httpd */ - HTTPDocumentResponse response(this, *http, &data, 200); - response.headers.SetHeader("X-Powered-By", MODNAME); - response.headers.SetHeader("Content-Type", "text/html"); - API->SendResponse(response); - return MOD_RES_DENY; // Handled + ConfigItems::const_iterator curr = kiter++; + buffer << curr->first << "=\"" << ServerConfig::Escape(curr->second) << '"'; + if (kiter != items.end()) + buffer << std::endl << indent; } + buffer << '>' << std::endl << std::endl; } - return MOD_RES_PASSTHRU; - } - ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE - { - return HandleRequest(&req); + HTTPDocumentResponse response(this, request, &buffer, 200); + response.headers.SetHeader("X-Powered-By", MODNAME); + response.headers.SetHeader("Content-Type", "text/plain"); + API->SendResponse(response); + return MOD_RES_DENY; } Version GetVersion() CXX11_OVERRIDE -- cgit v1.2.3