From 9bd2df2327cfb1a5594dd5020a05b4fadde0f7ee Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 25 Apr 2023 12:38:28 +0400 Subject: [PATCH 1/5] ServerTest.MimeTypes tests all OPDS endpoints --- test/server.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/server.cpp b/test/server.cpp index b7cfa8d66..cc75a7b7e 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -497,8 +497,13 @@ TEST_F(ServerTest, MimeTypes) { "/skin/blank.html", "text/html" }, { "/skin/index.css", "text/css" }, { "/skin/index.js", "application/javascript" }, + { "/catalog/root.xml", "application/atom+xml; profile=opds-catalog; kind=acquisition; charset=utf-8" }, { "/catalog/v2/searchdescription.xml", "application/opensearchdescription+xml" }, { "/catalog/v2/root.xml", "application/atom+xml;profile=opds-catalog;kind=navigation" }, + { "/catalog/v2/languages", "application/atom+xml;profile=opds-catalog;kind=navigation" }, + { "/catalog/v2/categories", "application/atom+xml;profile=opds-catalog;kind=navigation" }, + { "/catalog/v2/entries", "application/atom+xml;profile=opds-catalog;kind=acquisition" }, + { "/catalog/v2/entry/6f1d19d0-633f-087b-fb55-7ac324ff9baf", "application/atom+xml;type=entry;profile=opds-catalog" }, { "/skin/search-icon.svg", "image/svg+xml" }, { "/skin/bittorrent.png", "image/png" }, { "/skin/favicon/favicon.ico", "image/x-icon" }, From 1273570e01f91113df540b00bd39dbcf9441c31a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 25 Apr 2023 12:15:28 +0400 Subject: [PATCH 2/5] Deduplication of OPDS MIME type strings --- src/server/internalServer_catalog_v2.cpp | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/server/internalServer_catalog_v2.cpp b/src/server/internalServer_catalog_v2.cpp index c0cb69ece..4a281d225 100644 --- a/src/server/internalServer_catalog_v2.cpp +++ b/src/server/internalServer_catalog_v2.cpp @@ -33,6 +33,24 @@ namespace kiwix { +namespace +{ + +enum OPDSResponseKind +{ + OPDS_ENTRY, + OPDS_NAVIGATION_FEED, + OPDS_ACQUISITION_FEED +}; + +const std::string opdsMimeType[] = { + "application/atom+xml;type=entry;profile=opds-catalog", + "application/atom+xml;profile=opds-catalog;kind=navigation", + "application/atom+xml;profile=opds-catalog;kind=acquisition" +}; + +} // unnamed namespace + std::unique_ptr InternalServer::handle_catalog_v2(const RequestContext& request) { if (m_verbose.load()) { @@ -90,7 +108,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_root(const RequestCo {"category_list_feed_id", gen_uuid(libraryId + "/categories")}, {"language_list_feed_id", gen_uuid(libraryId + "/languages")} }, - "application/atom+xml;profile=opds-catalog;kind=navigation" + opdsMimeType[OPDS_NAVIGATION_FEED] ); } @@ -104,7 +122,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_entries(const Reques return ContentResponse::build( *this, opdsFeed, - "application/atom+xml;profile=opds-catalog;kind=acquisition" + opdsMimeType[OPDS_ACQUISITION_FEED] ); } @@ -124,7 +142,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_complete_entry(const return ContentResponse::build( *this, opdsFeed, - "application/atom+xml;type=entry;profile=opds-catalog" + opdsMimeType[OPDS_ENTRY] ); } @@ -136,7 +154,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_categories(const Req return ContentResponse::build( *this, opdsDumper.categoriesOPDSFeed(), - "application/atom+xml;profile=opds-catalog;kind=navigation" + opdsMimeType[OPDS_NAVIGATION_FEED] ); } @@ -148,7 +166,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_languages(const Requ return ContentResponse::build( *this, opdsDumper.languagesOPDSFeed(), - "application/atom+xml;profile=opds-catalog;kind=navigation" + opdsMimeType[OPDS_NAVIGATION_FEED] ); } From 8c190cf34fcaef968fe6151714fd921e6dbd920e Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 25 Apr 2023 12:23:51 +0400 Subject: [PATCH 3/5] Moved InternalServer::handle_catalog() --- src/server/internalServer.cpp | 50 ------------------------ src/server/internalServer_catalog_v2.cpp | 50 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 67ddb987d..4896cd5d4 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -1046,56 +1046,6 @@ std::unique_ptr InternalServer::handle_catch(const RequestContext& req + urlNotFoundMsg; } -std::unique_ptr InternalServer::handle_catalog(const RequestContext& request) -{ - if (m_verbose.load()) { - printf("** running handle_catalog"); - } - - std::string host; - std::string url; - try { - host = request.get_header("Host"); - url = request.get_url_part(1); - } catch (const std::out_of_range&) { - return HTTP404Response(*this, request) - + urlNotFoundMsg; - } - - if (url == "v2") { - return handle_catalog_v2(request); - } - - if (url != "searchdescription.xml" && url != "root.xml" && url != "search") { - return HTTP404Response(*this, request) - + urlNotFoundMsg; - } - - if (url == "searchdescription.xml") { - auto response = ContentResponse::build(*this, RESOURCE::opensearchdescription_xml, get_default_data(), "application/opensearchdescription+xml"); - return std::move(response); - } - - zim::Uuid uuid; - kiwix::OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); - opdsDumper.setLibraryId(getLibraryId()); - std::vector bookIdsToDump; - if (url == "root.xml") { - uuid = zim::Uuid::generate(host); - bookIdsToDump = mp_library->filter(kiwix::Filter().valid(true).local(true).remote(true)); - } else if (url == "search") { - bookIdsToDump = search_catalog(request, opdsDumper); - uuid = zim::Uuid::generate(); - } - - auto response = ContentResponse::build( - *this, - opdsDumper.dumpOPDSFeed(bookIdsToDump, request.get_query()), - "application/atom+xml; profile=opds-catalog; kind=acquisition; charset=utf-8"); - return std::move(response); -} - std::vector InternalServer::search_catalog(const RequestContext& request, kiwix::OPDSDumper& opdsDumper) diff --git a/src/server/internalServer_catalog_v2.cpp b/src/server/internalServer_catalog_v2.cpp index 4a281d225..4f3e732f7 100644 --- a/src/server/internalServer_catalog_v2.cpp +++ b/src/server/internalServer_catalog_v2.cpp @@ -51,6 +51,56 @@ const std::string opdsMimeType[] = { } // unnamed namespace +std::unique_ptr InternalServer::handle_catalog(const RequestContext& request) +{ + if (m_verbose.load()) { + printf("** running handle_catalog"); + } + + std::string host; + std::string url; + try { + host = request.get_header("Host"); + url = request.get_url_part(1); + } catch (const std::out_of_range&) { + return HTTP404Response(*this, request) + + urlNotFoundMsg; + } + + if (url == "v2") { + return handle_catalog_v2(request); + } + + if (url != "searchdescription.xml" && url != "root.xml" && url != "search") { + return HTTP404Response(*this, request) + + urlNotFoundMsg; + } + + if (url == "searchdescription.xml") { + auto response = ContentResponse::build(*this, RESOURCE::opensearchdescription_xml, get_default_data(), "application/opensearchdescription+xml"); + return std::move(response); + } + + zim::Uuid uuid; + kiwix::OPDSDumper opdsDumper(mp_library, mp_nameMapper); + opdsDumper.setRootLocation(m_root); + opdsDumper.setLibraryId(getLibraryId()); + std::vector bookIdsToDump; + if (url == "root.xml") { + uuid = zim::Uuid::generate(host); + bookIdsToDump = mp_library->filter(kiwix::Filter().valid(true).local(true).remote(true)); + } else if (url == "search") { + bookIdsToDump = search_catalog(request, opdsDumper); + uuid = zim::Uuid::generate(); + } + + auto response = ContentResponse::build( + *this, + opdsDumper.dumpOPDSFeed(bookIdsToDump, request.get_query()), + "application/atom+xml; profile=opds-catalog; kind=acquisition; charset=utf-8"); + return std::move(response); +} + std::unique_ptr InternalServer::handle_catalog_v2(const RequestContext& request) { if (m_verbose.load()) { From 9994302312d330d50eb27bfe90531eba7f9e661a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 25 Apr 2023 12:42:03 +0400 Subject: [PATCH 4/5] Explicit charset in OPDS response MIME types --- src/server/internalServer_catalog_v2.cpp | 8 ++++---- test/server.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/server/internalServer_catalog_v2.cpp b/src/server/internalServer_catalog_v2.cpp index 4f3e732f7..a43d968e9 100644 --- a/src/server/internalServer_catalog_v2.cpp +++ b/src/server/internalServer_catalog_v2.cpp @@ -44,9 +44,9 @@ enum OPDSResponseKind }; const std::string opdsMimeType[] = { - "application/atom+xml;type=entry;profile=opds-catalog", - "application/atom+xml;profile=opds-catalog;kind=navigation", - "application/atom+xml;profile=opds-catalog;kind=acquisition" + "application/atom+xml;type=entry;profile=opds-catalog;charset=utf-8", + "application/atom+xml;profile=opds-catalog;kind=navigation;charset=utf-8", + "application/atom+xml;profile=opds-catalog;kind=acquisition;charset=utf-8" }; } // unnamed namespace @@ -97,7 +97,7 @@ std::unique_ptr InternalServer::handle_catalog(const RequestContext& r auto response = ContentResponse::build( *this, opdsDumper.dumpOPDSFeed(bookIdsToDump, request.get_query()), - "application/atom+xml; profile=opds-catalog; kind=acquisition; charset=utf-8"); + opdsMimeType[OPDS_ACQUISITION_FEED]); return std::move(response); } diff --git a/test/server.cpp b/test/server.cpp index cc75a7b7e..961ef9a06 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -497,13 +497,13 @@ TEST_F(ServerTest, MimeTypes) { "/skin/blank.html", "text/html" }, { "/skin/index.css", "text/css" }, { "/skin/index.js", "application/javascript" }, - { "/catalog/root.xml", "application/atom+xml; profile=opds-catalog; kind=acquisition; charset=utf-8" }, + { "/catalog/root.xml", "application/atom+xml;profile=opds-catalog;kind=acquisition;charset=utf-8" }, { "/catalog/v2/searchdescription.xml", "application/opensearchdescription+xml" }, - { "/catalog/v2/root.xml", "application/atom+xml;profile=opds-catalog;kind=navigation" }, - { "/catalog/v2/languages", "application/atom+xml;profile=opds-catalog;kind=navigation" }, - { "/catalog/v2/categories", "application/atom+xml;profile=opds-catalog;kind=navigation" }, - { "/catalog/v2/entries", "application/atom+xml;profile=opds-catalog;kind=acquisition" }, - { "/catalog/v2/entry/6f1d19d0-633f-087b-fb55-7ac324ff9baf", "application/atom+xml;type=entry;profile=opds-catalog" }, + { "/catalog/v2/root.xml", "application/atom+xml;profile=opds-catalog;kind=navigation;charset=utf-8" }, + { "/catalog/v2/languages", "application/atom+xml;profile=opds-catalog;kind=navigation;charset=utf-8" }, + { "/catalog/v2/categories", "application/atom+xml;profile=opds-catalog;kind=navigation;charset=utf-8" }, + { "/catalog/v2/entries", "application/atom+xml;profile=opds-catalog;kind=acquisition;charset=utf-8" }, + { "/catalog/v2/entry/6f1d19d0-633f-087b-fb55-7ac324ff9baf", "application/atom+xml;type=entry;profile=opds-catalog;charset=utf-8" }, { "/skin/search-icon.svg", "image/svg+xml" }, { "/skin/bittorrent.png", "image/png" }, { "/skin/favicon/favicon.ico", "image/x-icon" }, From dc58e278c7c435cae2e701ec7683ad17255e5f61 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 25 Apr 2023 12:44:06 +0400 Subject: [PATCH 5/5] git mv src/server/internalServer_catalog{_v2,}.cpp --- src/meson.build | 2 +- ...internalServer_catalog_v2.cpp => internalServer_catalog.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/server/{internalServer_catalog_v2.cpp => internalServer_catalog.cpp} (100%) diff --git a/src/meson.build b/src/meson.build index 21486bc4e..818eb4f9f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -26,7 +26,7 @@ kiwix_sources = [ 'server/request_context.cpp', 'server/response.cpp', 'server/internalServer.cpp', - 'server/internalServer_catalog_v2.cpp', + 'server/internalServer_catalog.cpp', 'server/i18n.cpp', 'opds_catalog.cpp', 'version.cpp' diff --git a/src/server/internalServer_catalog_v2.cpp b/src/server/internalServer_catalog.cpp similarity index 100% rename from src/server/internalServer_catalog_v2.cpp rename to src/server/internalServer_catalog.cpp