From a5557eeb255a957ab63a6884cf35d41161657581 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 25 Sep 2023 16:15:32 +0200 Subject: [PATCH] Make the `Server` keep a `shared_ptr` instead of a raw Library pointer. We want to be sure that `Library` actually exists when we use it. While it is not a silver bullet (user can still create a shared_ptr on a raw pointer), making the `Server` keep `shared_ptr` on the library help us a lot here. --- include/server.h | 4 ++-- src/server.cpp | 2 +- src/server/internalServer.cpp | 6 +++--- src/server/internalServer.h | 4 ++-- src/server/internalServer_catalog.cpp | 10 +++++----- test/server_testing_tools.h | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/server.h b/include/server.h index 0cd579c57..bd96ed664 100644 --- a/include/server.h +++ b/include/server.h @@ -36,7 +36,7 @@ namespace kiwix * * @param library The library to serve. */ - Server(Library* library, NameMapper* nameMapper=nullptr); + Server(std::shared_ptr library, NameMapper* nameMapper=nullptr); virtual ~Server(); @@ -66,7 +66,7 @@ namespace kiwix std::string getAddress(); protected: - Library* mp_library; + std::shared_ptr mp_library; NameMapper* mp_nameMapper; std::string m_root = ""; std::string m_addr = ""; diff --git a/src/server.cpp b/src/server.cpp index e9373417c..1c331a406 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -29,7 +29,7 @@ namespace kiwix { -Server::Server(Library* library, NameMapper* nameMapper) : +Server::Server(std::shared_ptr library, NameMapper* nameMapper) : mp_library(library), mp_nameMapper(nameMapper), mp_server(nullptr) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 544f7e31f..098f476c5 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -406,7 +406,7 @@ public: }; -InternalServer::InternalServer(Library* library, +InternalServer::InternalServer(std::shared_ptr library, NameMapper* nameMapper, std::string addr, int port, @@ -787,7 +787,7 @@ std::unique_ptr InternalServer::handle_no_js(const RequestContext& req { const auto url = request.get_url(); const auto urlParts = kiwix::split(url, "/", true, false); - HTMLDumper htmlDumper(mp_library, mp_nameMapper); + HTMLDumper htmlDumper(mp_library.get(), mp_nameMapper); htmlDumper.setRootLocation(m_root); htmlDumper.setLibraryId(getLibraryId()); auto userLang = request.get_user_language(); @@ -958,7 +958,7 @@ std::unique_ptr InternalServer::handle_search_request(const RequestCon const auto pageLength = getSearchPageSize(request); /* Get the results */ - SearchRenderer renderer(search->getResults(start-1, pageLength), mp_nameMapper, mp_library, start, + SearchRenderer renderer(search->getResults(start-1, pageLength), mp_nameMapper, mp_library.get(), start, search->getEstimatedMatches()); renderer.setSearchPattern(searchInfo.pattern); renderer.setSearchBookQuery(searchInfo.bookFilterQuery); diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 867b324e0..149bda5a8 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -92,7 +92,7 @@ class OPDSDumper; class InternalServer { public: - InternalServer(Library* library, + InternalServer(std::shared_ptr library, NameMapper* nameMapper, std::string addr, int port, @@ -178,7 +178,7 @@ class InternalServer { int m_ipConnectionLimit; struct MHD_Daemon* mp_daemon; - Library* mp_library; + std::shared_ptr mp_library; NameMapper* mp_nameMapper; SearchCache searchCache; diff --git a/src/server/internalServer_catalog.cpp b/src/server/internalServer_catalog.cpp index a43d968e9..f4b8d096b 100644 --- a/src/server/internalServer_catalog.cpp +++ b/src/server/internalServer_catalog.cpp @@ -82,7 +82,7 @@ std::unique_ptr InternalServer::handle_catalog(const RequestContext& r } zim::Uuid uuid; - kiwix::OPDSDumper opdsDumper(mp_library, mp_nameMapper); + kiwix::OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(getLibraryId()); std::vector bookIdsToDump; @@ -164,7 +164,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_root(const RequestCo std::unique_ptr InternalServer::handle_catalog_v2_entries(const RequestContext& request, bool partial) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); + OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(getLibraryId()); const auto bookIds = search_catalog(request, opdsDumper); @@ -185,7 +185,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_complete_entry(const + urlNotFoundMsg; } - OPDSDumper opdsDumper(mp_library, mp_nameMapper); + OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(getLibraryId()); const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId); @@ -198,7 +198,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_complete_entry(const std::unique_ptr InternalServer::handle_catalog_v2_categories(const RequestContext& request) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); + OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(getLibraryId()); return ContentResponse::build( @@ -210,7 +210,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_categories(const Req std::unique_ptr InternalServer::handle_catalog_v2_languages(const RequestContext& request) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); + OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); opdsDumper.setRootLocation(m_root); opdsDumper.setLibraryId(getLibraryId()); return ContentResponse::build( diff --git a/test/server_testing_tools.h b/test/server_testing_tools.h index 6a797e345..575b0841d 100644 --- a/test/server_testing_tools.h +++ b/test/server_testing_tools.h @@ -140,7 +140,7 @@ void ZimFileServer::run(int serverPort, std::string indexTemplateString) } else { nameMapper.reset(new kiwix::HumanReadableNameMapper(*library, false)); } - server.reset(new kiwix::Server(library.get(), nameMapper.get())); + server.reset(new kiwix::Server(library, nameMapper.get())); server->setRoot(cfg.root); server->setAddress(address); server->setPort(serverPort);