diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 03e130dff..21b02310b 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -79,11 +79,14 @@ namespace kiwix { namespace { -inline std::string normalizeRootUrl(const std::string& rootUrl) +inline std::string normalizeRootUrl(std::string rootUrl) { - return (rootUrl.empty() || rootUrl[0] == '/') - ? rootUrl - : "/" + rootUrl; + while ( !rootUrl.empty() && rootUrl.back() == '/' ) + rootUrl.pop_back(); + + while ( !rootUrl.empty() && rootUrl.front() == '/' ) + rootUrl = rootUrl.substr(1); + return rootUrl.empty() ? rootUrl : "/" + rootUrl; } } // unnamed namespace @@ -112,7 +115,7 @@ InternalServer::InternalServer(Library* library, bool blockExternalLinks) : m_addr(addr), m_port(port), - m_root(root), + m_root(normalizeRootUrl(root)), m_nbThreads(nbThreads), m_verbose(verbose), m_withTaskbar(withTaskbar), @@ -728,7 +731,7 @@ std::unique_ptr InternalServer::handle_catalog_v2(const RequestContext if (url == "root.xml") { return handle_catalog_v2_root(request); } 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, RESOURCE::catalog_v2_searchdescription_xml, kainjow::mustache::object({{"endpoint_root", endpoint_root}}), @@ -745,13 +748,12 @@ std::unique_ptr InternalServer::handle_catalog_v2(const RequestContext std::unique_ptr InternalServer::handle_catalog_v2_root(const RequestContext& request) { - const std::string root_url = normalizeRootUrl(m_root); return ContentResponse::build( *this, RESOURCE::catalog_v2_root_xml, kainjow::mustache::object{ {"date", gen_date_str()}, - {"endpoint_root", root_url + "/catalog/v2"}, + {"endpoint_root", m_root + "/catalog/v2"}, {"feed_id", gen_uuid(m_library_id)}, {"all_entries_feed_id", gen_uuid(m_library_id + "/entries")}, {"category_list_feed_id", gen_uuid(m_library_id + "/categories")} @@ -767,7 +769,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_entries(const Reques const size_t start = request.get_optional_param("start", 0UL); const auto bookIds = subrange(mp_library->filter(filter), start, count); OPDSDumper opdsDumper(mp_library); - opdsDumper.setRootLocation(normalizeRootUrl(m_root)); + opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(m_library_id); const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query()); return ContentResponse::build( @@ -780,7 +782,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_entries(const Reques std::unique_ptr InternalServer::handle_catalog_v2_categories(const RequestContext& request) { OPDSDumper opdsDumper(mp_library); - opdsDumper.setRootLocation(normalizeRootUrl(m_root)); + opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(m_library_id); return ContentResponse::build( *this, diff --git a/static/opensearchdescription.xml b/static/opensearchdescription.xml index 47fd790b5..702152f8b 100644 --- a/static/opensearchdescription.xml +++ b/static/opensearchdescription.xml @@ -6,5 +6,5 @@ xmlns:atom="http://www.w3.org/2005/Atom" xmlns:k="http://kiwix.org/opensearchextension/1.0" indexOffset="0" - template="/{{root}}/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}¬ag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}"/> + template="{{root}}/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}¬ag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}"/> diff --git a/test/server.cpp b/test/server.cpp index 42401cc74..bce722add 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -715,7 +715,7 @@ TEST_F(LibraryServerTest, catalog_searchdescription_xml) " xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" " xmlns:k=\"http://kiwix.org/opensearchextension/1.0\"\n" " indexOffset=\"0\"\n" - " template=\"//catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}¬ag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}\"/>\n" + " template=\"/catalog/search?q={searchTerms?}&lang={language?}&name={k:name?}&tag={k:tag?}¬ag={k:notag?}&maxsize={k:maxsize?}&count={count?}&start={startIndex?}\"/>\n" "\n" ); }