Make internalServer use m_root from configuration instead of using its own.

This commit is contained in:
Matthieu Gautier 2022-10-07 11:48:08 +02:00
parent 3d75f24a29
commit 92c0e145d4
5 changed files with 23 additions and 28 deletions

View File

@ -26,6 +26,7 @@
#include <zim/item.h> #include <zim/item.h>
#include "server/internalServer.h" #include "server/internalServer.h"
#include "tools/otherTools.h"
namespace kiwix { namespace kiwix {
@ -51,13 +52,7 @@ void Server::stop() {
Server::Configuration& Server::Configuration::setRoot(const std::string& root) Server::Configuration& Server::Configuration::setRoot(const std::string& root)
{ {
m_root = root; m_root = normalizeRootUrl(root);
if (m_root[0] != '/') {
m_root = "/" + m_root;
}
if (m_root.back() == '/') {
m_root.erase(m_root.size() - 1);
}
return *this; return *this;
} }

View File

@ -85,16 +85,6 @@ namespace kiwix {
namespace 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="") Filter get_search_filter(const RequestContext& request, const std::string& prefix="")
{ {
auto filter = kiwix::Filter().valid(true).local(true); auto filter = kiwix::Filter().valid(true).local(true);
@ -368,7 +358,6 @@ public:
InternalServer::InternalServer(const Server::Configuration& configuration) : InternalServer::InternalServer(const Server::Configuration& configuration) :
m_configuration(configuration), m_configuration(configuration),
m_root(normalizeRootUrl(configuration.m_root)),
m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString), m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString),
mp_nameMapper(configuration.mp_nameMapper ? configuration.mp_nameMapper : defaultNameMapper), mp_nameMapper(configuration.mp_nameMapper ? configuration.mp_nameMapper : defaultNameMapper),
mp_daemon(nullptr), mp_daemon(nullptr),
@ -465,7 +454,7 @@ MHD_Result InternalServer::handlerCallback(struct MHD_Connection* connection,
printf("Requesting : \n"); printf("Requesting : \n");
printf("full_url : %s\n", url); 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) { if (m_configuration.m_verbose) {
request.print_debug_info(); request.print_debug_info();
@ -559,7 +548,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
if (isEndpointUrl(url, "catch")) if (isEndpointUrl(url, "catch"))
return handle_catch(request); 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(); const std::string query = request.get_query();
if ( ! query.empty() ) if ( ! query.empty() )
contentUrl += "?" + query; contentUrl += "?" + query;
@ -578,7 +567,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
MustacheData InternalServer::get_default_data() const MustacheData InternalServer::get_default_data() const
{ {
MustacheData data; MustacheData data;
data.set("root", m_root); data.set("root", m_configuration.m_root);
return data; return data;
} }
@ -785,7 +774,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
} catch(std::runtime_error& e) { } catch(std::runtime_error& e) {
// Searcher->search will throw a runtime error if there is no valid xapian database to do the search. // 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) // (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, HTTPErrorResponse response(*this, request, MHD_HTTP_NOT_FOUND,
"fulltext-search-unavailable", "fulltext-search-unavailable",
"404-page-heading", "404-page-heading",
@ -825,8 +814,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
search->getEstimatedMatches()); search->getEstimatedMatches());
renderer.setSearchPattern(searchInfo.pattern); renderer.setSearchPattern(searchInfo.pattern);
renderer.setSearchBookQuery(searchInfo.bookFilterQuery); renderer.setSearchBookQuery(searchInfo.bookFilterQuery);
renderer.setProtocolPrefix(m_root + "/content/"); renderer.setProtocolPrefix(m_configuration.m_root + "/content/");
renderer.setSearchProtocolPrefix(m_root + "/search"); renderer.setSearchProtocolPrefix(m_configuration.m_root + "/search");
renderer.setPageLength(pageLength); renderer.setPageLength(pageLength);
if (request.get_requested_format() == "xml") { if (request.get_requested_format() == "xml") {
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8"); return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8");
@ -1001,7 +990,7 @@ std::unique_ptr<Response>
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
{ {
const auto path = kiwix::urlEncode(item.getPath()); 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); return Response::build_redirect(*this, redirectUrl);
} }
@ -1025,7 +1014,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
} catch (const std::out_of_range& e) {} } catch (const std::out_of_range& e) {}
if (archive == nullptr) { 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) return HTTP404Response(*this, request)
+ urlNotFoundMsg + urlNotFoundMsg
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern)); + suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
@ -1060,7 +1049,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
if (m_configuration.m_verbose) if (m_configuration.m_verbose)
printf("Failed to find %s\n", urlStr.c_str()); 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) return HTTP404Response(*this, request)
+ urlNotFoundMsg + urlNotFoundMsg
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern)); + suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));

View File

@ -151,7 +151,6 @@ class InternalServer {
private: // data private: // data
Server::Configuration m_configuration; Server::Configuration m_configuration;
std::string m_addr; std::string m_addr;
std::string m_root;
std::string m_indexTemplateString; std::string m_indexTemplateString;
std::shared_ptr<NameMapper> mp_nameMapper; std::shared_ptr<NameMapper> mp_nameMapper;
struct MHD_Daemon* mp_daemon; struct MHD_Daemon* mp_daemon;

View File

@ -370,6 +370,16 @@ std::string kiwix::gen_uuid(const std::string& s)
return kiwix::to_string(zim::Uuid::generate(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) kainjow::mustache::data kiwix::onlyAsNonEmptyMustacheValue(const std::string& s)
{ {
return s.empty() return s.empty()

View File

@ -51,6 +51,8 @@ namespace kiwix
std::string gen_date_str(); std::string gen_date_str();
std::string gen_uuid(const std::string& s); std::string gen_uuid(const std::string& s);
std::string normalizeRootUrl(std::string rootUrl);
// if s is empty then returns kainjow::mustache::data(false) // if s is empty then returns kainjow::mustache::data(false)
// otherwise kainjow::mustache::data(value) // otherwise kainjow::mustache::data(value)
kainjow::mustache::data onlyAsNonEmptyMustacheValue(const std::string& s); kainjow::mustache::data onlyAsNonEmptyMustacheValue(const std::string& s);