diff --git a/src/server.cpp b/src/server.cpp index 699301ade..0ad3593af 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -26,6 +26,7 @@ #include #include "server/internalServer.h" +#include "tools/otherTools.h" namespace kiwix { @@ -51,13 +52,7 @@ void Server::stop() { Server::Configuration& Server::Configuration::setRoot(const std::string& root) { - m_root = root; - if (m_root[0] != '/') { - m_root = "/" + m_root; - } - if (m_root.back() == '/') { - m_root.erase(m_root.size() - 1); - } + m_root = normalizeRootUrl(root); return *this; } diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 58dbc1d50..8abadf69c 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -85,16 +85,6 @@ namespace kiwix { namespace { -inline std::string normalizeRootUrl(std::string rootUrl) -{ - while ( !rootUrl.empty() && rootUrl.back() == '/' ) - rootUrl.pop_back(); - - while ( !rootUrl.empty() && rootUrl.front() == '/' ) - rootUrl = rootUrl.substr(1); - return rootUrl.empty() ? rootUrl : "/" + rootUrl; -} - Filter get_search_filter(const RequestContext& request, const std::string& prefix="") { auto filter = kiwix::Filter().valid(true).local(true); @@ -368,7 +358,6 @@ public: InternalServer::InternalServer(const Server::Configuration& configuration) : m_configuration(configuration), - m_root(normalizeRootUrl(configuration.m_root)), m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString), mp_nameMapper(configuration.mp_nameMapper ? configuration.mp_nameMapper : defaultNameMapper), mp_daemon(nullptr), @@ -465,7 +454,7 @@ MHD_Result InternalServer::handlerCallback(struct MHD_Connection* connection, printf("Requesting : \n"); printf("full_url : %s\n", url); } - RequestContext request(connection, m_root, url, method, version); + RequestContext request(connection, m_configuration.m_root, url, method, version); if (m_configuration.m_verbose) { request.print_debug_info(); @@ -559,7 +548,7 @@ std::unique_ptr InternalServer::handle_request(const RequestContext& r if (isEndpointUrl(url, "catch")) return handle_catch(request); - std::string contentUrl = m_root + "/content" + url; + std::string contentUrl = m_configuration.m_root + "/content" + url; const std::string query = request.get_query(); if ( ! query.empty() ) contentUrl += "?" + query; @@ -578,7 +567,7 @@ std::unique_ptr InternalServer::handle_request(const RequestContext& r MustacheData InternalServer::get_default_data() const { MustacheData data; - data.set("root", m_root); + data.set("root", m_configuration.m_root); return data; } @@ -785,7 +774,7 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re } catch(std::runtime_error& e) { // Searcher->search will throw a runtime error if there is no valid xapian database to do the search. // (in case of zim file not containing a index) - const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css); + const auto cssUrl = renderUrl(m_configuration.m_root, RESOURCE::templates::url_of_search_results_css); HTTPErrorResponse response(*this, request, MHD_HTTP_NOT_FOUND, "fulltext-search-unavailable", "404-page-heading", @@ -825,8 +814,8 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re search->getEstimatedMatches()); renderer.setSearchPattern(searchInfo.pattern); renderer.setSearchBookQuery(searchInfo.bookFilterQuery); - renderer.setProtocolPrefix(m_root + "/content/"); - renderer.setSearchProtocolPrefix(m_root + "/search"); + renderer.setProtocolPrefix(m_configuration.m_root + "/content/"); + renderer.setSearchProtocolPrefix(m_configuration.m_root + "/search"); renderer.setPageLength(pageLength); if (request.get_requested_format() == "xml") { return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8"); @@ -1001,7 +990,7 @@ std::unique_ptr InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const { const auto path = kiwix::urlEncode(item.getPath()); - const auto redirectUrl = m_root + "/content/" + bookName + "/" + path; + const auto redirectUrl = m_configuration.m_root + "/content/" + bookName + "/" + path; return Response::build_redirect(*this, redirectUrl); } @@ -1025,7 +1014,7 @@ std::unique_ptr InternalServer::handle_content(const RequestContext& r } catch (const std::out_of_range& e) {} if (archive == nullptr) { - const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true); + const std::string searchURL = m_configuration.m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true); return HTTP404Response(*this, request) + urlNotFoundMsg + suggestSearchMsg(searchURL, kiwix::urlDecode(pattern)); @@ -1060,7 +1049,7 @@ std::unique_ptr InternalServer::handle_content(const RequestContext& r if (m_configuration.m_verbose) printf("Failed to find %s\n", urlStr.c_str()); - std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true); + std::string searchURL = m_configuration.m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true); return HTTP404Response(*this, request) + urlNotFoundMsg + suggestSearchMsg(searchURL, kiwix::urlDecode(pattern)); diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 0b26fd9bb..64fc2f7aa 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -151,7 +151,6 @@ class InternalServer { private: // data Server::Configuration m_configuration; std::string m_addr; - std::string m_root; std::string m_indexTemplateString; std::shared_ptr mp_nameMapper; struct MHD_Daemon* mp_daemon; diff --git a/src/tools/otherTools.cpp b/src/tools/otherTools.cpp index ab5bf5874..6d6ff43b3 100644 --- a/src/tools/otherTools.cpp +++ b/src/tools/otherTools.cpp @@ -370,6 +370,16 @@ std::string kiwix::gen_uuid(const std::string& s) return kiwix::to_string(zim::Uuid::generate(s)); } +std::string kiwix::normalizeRootUrl(std::string rootUrl) +{ + while ( !rootUrl.empty() && rootUrl.back() == '/' ) + rootUrl.pop_back(); + + while ( !rootUrl.empty() && rootUrl.front() == '/' ) + rootUrl = rootUrl.substr(1); + return rootUrl.empty() ? rootUrl : "/" + rootUrl; +} + kainjow::mustache::data kiwix::onlyAsNonEmptyMustacheValue(const std::string& s) { return s.empty() diff --git a/src/tools/otherTools.h b/src/tools/otherTools.h index c0920d7bf..54d00c8eb 100644 --- a/src/tools/otherTools.h +++ b/src/tools/otherTools.h @@ -51,6 +51,8 @@ namespace kiwix std::string gen_date_str(); std::string gen_uuid(const std::string& s); + std::string normalizeRootUrl(std::string rootUrl); + // if s is empty then returns kainjow::mustache::data(false) // otherwise kainjow::mustache::data(value) kainjow::mustache::data onlyAsNonEmptyMustacheValue(const std::string& s);