Root url is normalized once in the constructor

This commit is contained in:
Veloman Yunkan 2021-05-24 13:00:31 +04:00
parent 9ca6bd006f
commit f886c8c07b
3 changed files with 14 additions and 12 deletions

View File

@ -79,11 +79,14 @@ namespace kiwix {
namespace namespace
{ {
inline std::string normalizeRootUrl(const std::string& rootUrl) inline std::string normalizeRootUrl(std::string rootUrl)
{ {
return (rootUrl.empty() || rootUrl[0] == '/') while ( !rootUrl.empty() && rootUrl.back() == '/' )
? rootUrl rootUrl.pop_back();
: "/" + rootUrl;
while ( !rootUrl.empty() && rootUrl.front() == '/' )
rootUrl = rootUrl.substr(1);
return rootUrl.empty() ? rootUrl : "/" + rootUrl;
} }
} // unnamed namespace } // unnamed namespace
@ -112,7 +115,7 @@ InternalServer::InternalServer(Library* library,
bool blockExternalLinks) : bool blockExternalLinks) :
m_addr(addr), m_addr(addr),
m_port(port), m_port(port),
m_root(root), m_root(normalizeRootUrl(root)),
m_nbThreads(nbThreads), m_nbThreads(nbThreads),
m_verbose(verbose), m_verbose(verbose),
m_withTaskbar(withTaskbar), m_withTaskbar(withTaskbar),
@ -728,7 +731,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext
if (url == "root.xml") { if (url == "root.xml") {
return handle_catalog_v2_root(request); return handle_catalog_v2_root(request);
} else if (url == "searchdescription.xml") { } else if (url == "searchdescription.xml") {
const std::string endpoint_root = normalizeRootUrl(m_root) + "/catalog/v2"; const std::string endpoint_root = m_root + "/catalog/v2";
return ContentResponse::build(*this, return ContentResponse::build(*this,
RESOURCE::catalog_v2_searchdescription_xml, RESOURCE::catalog_v2_searchdescription_xml,
kainjow::mustache::object({{"endpoint_root", endpoint_root}}), kainjow::mustache::object({{"endpoint_root", endpoint_root}}),
@ -745,13 +748,12 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext
std::unique_ptr<Response> InternalServer::handle_catalog_v2_root(const RequestContext& request) std::unique_ptr<Response> InternalServer::handle_catalog_v2_root(const RequestContext& request)
{ {
const std::string root_url = normalizeRootUrl(m_root);
return ContentResponse::build( return ContentResponse::build(
*this, *this,
RESOURCE::catalog_v2_root_xml, RESOURCE::catalog_v2_root_xml,
kainjow::mustache::object{ kainjow::mustache::object{
{"date", gen_date_str()}, {"date", gen_date_str()},
{"endpoint_root", root_url + "/catalog/v2"}, {"endpoint_root", m_root + "/catalog/v2"},
{"feed_id", gen_uuid(m_library_id)}, {"feed_id", gen_uuid(m_library_id)},
{"all_entries_feed_id", gen_uuid(m_library_id + "/entries")}, {"all_entries_feed_id", gen_uuid(m_library_id + "/entries")},
{"category_list_feed_id", gen_uuid(m_library_id + "/categories")} {"category_list_feed_id", gen_uuid(m_library_id + "/categories")}
@ -767,7 +769,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_entries(const Reques
const size_t start = request.get_optional_param("start", 0UL); const size_t start = request.get_optional_param("start", 0UL);
const auto bookIds = subrange(mp_library->filter(filter), start, count); const auto bookIds = subrange(mp_library->filter(filter), start, count);
OPDSDumper opdsDumper(mp_library); OPDSDumper opdsDumper(mp_library);
opdsDumper.setRootLocation(normalizeRootUrl(m_root)); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(m_library_id); opdsDumper.setLibraryId(m_library_id);
const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query()); const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query());
return ContentResponse::build( return ContentResponse::build(
@ -780,7 +782,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_entries(const Reques
std::unique_ptr<Response> InternalServer::handle_catalog_v2_categories(const RequestContext& request) std::unique_ptr<Response> InternalServer::handle_catalog_v2_categories(const RequestContext& request)
{ {
OPDSDumper opdsDumper(mp_library); OPDSDumper opdsDumper(mp_library);
opdsDumper.setRootLocation(normalizeRootUrl(m_root)); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(m_library_id); opdsDumper.setLibraryId(m_library_id);
return ContentResponse::build( return ContentResponse::build(
*this, *this,

View File

@ -6,5 +6,5 @@
xmlns:atom="http://www.w3.org/2005/Atom" xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:k="http://kiwix.org/opensearchextension/1.0" xmlns:k="http://kiwix.org/opensearchextension/1.0"
indexOffset="0" indexOffset="0"
template="/{{root}}/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}&notag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}"/> template="{{root}}/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}&notag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}"/>
</OpenSearchDescription> </OpenSearchDescription>

View File

@ -715,7 +715,7 @@ TEST_F(LibraryServerTest, catalog_searchdescription_xml)
" xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" " xmlns:atom=\"http://www.w3.org/2005/Atom\"\n"
" xmlns:k=\"http://kiwix.org/opensearchextension/1.0\"\n" " xmlns:k=\"http://kiwix.org/opensearchextension/1.0\"\n"
" indexOffset=\"0\"\n" " indexOffset=\"0\"\n"
" template=\"//catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}&notag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}\"/>\n" " template=\"/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}&notag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}\"/>\n"
"</OpenSearchDescription>\n" "</OpenSearchDescription>\n"
); );
} }