Make the `Server` keep a `shared_ptr` instead of a raw NameMapper pointer.

Same as for `Library`, we want to be sure that the `NameMapper`
actually exists when the server is using it.
This commit is contained in:
Matthieu Gautier 2023-09-25 16:44:28 +02:00
parent a5557eeb25
commit 1316dec37c
6 changed files with 21 additions and 16 deletions

View File

@ -36,7 +36,7 @@ namespace kiwix
* *
* @param library The library to serve. * @param library The library to serve.
*/ */
Server(std::shared_ptr<Library> library, NameMapper* nameMapper=nullptr); Server(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper=nullptr);
virtual ~Server(); virtual ~Server();
@ -67,7 +67,7 @@ namespace kiwix
protected: protected:
std::shared_ptr<Library> mp_library; std::shared_ptr<Library> mp_library;
NameMapper* mp_nameMapper; std::shared_ptr<NameMapper> mp_nameMapper;
std::string m_root = ""; std::string m_root = "";
std::string m_addr = ""; std::string m_addr = "";
std::string m_indexTemplateString = ""; std::string m_indexTemplateString = "";

View File

@ -29,7 +29,7 @@
namespace kiwix { namespace kiwix {
Server::Server(std::shared_ptr<Library> library, NameMapper* nameMapper) : Server::Server(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper) :
mp_library(library), mp_library(library),
mp_nameMapper(nameMapper), mp_nameMapper(nameMapper),
mp_server(nullptr) mp_server(nullptr)

View File

@ -254,6 +254,11 @@ get_matching_if_none_match_etag(const RequestContext& r, const std::string& etag
} }
} }
struct NoDelete
{
template<class T> void operator()(T*) {}
};
} // unnamed namespace } // unnamed namespace
std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const RequestContext& request) const std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const RequestContext& request) const
@ -407,7 +412,7 @@ public:
InternalServer::InternalServer(std::shared_ptr<Library> library, InternalServer::InternalServer(std::shared_ptr<Library> library,
NameMapper* nameMapper, std::shared_ptr<NameMapper> nameMapper,
std::string addr, std::string addr,
int port, int port,
std::string root, std::string root,
@ -433,7 +438,7 @@ InternalServer::InternalServer(std::shared_ptr<Library> library,
m_ipConnectionLimit(ipConnectionLimit), m_ipConnectionLimit(ipConnectionLimit),
mp_daemon(nullptr), mp_daemon(nullptr),
mp_library(library), mp_library(library),
mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper), mp_nameMapper(nameMapper ? nameMapper : std::shared_ptr<NameMapper>(&defaultNameMapper, NoDelete())),
searchCache(getEnvVar<int>("KIWIX_SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)), searchCache(getEnvVar<int>("KIWIX_SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)),
suggestionSearcherCache(getEnvVar<int>("KIWIX_SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))), suggestionSearcherCache(getEnvVar<int>("KIWIX_SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))),
m_customizedResources(new CustomizedResources) m_customizedResources(new CustomizedResources)
@ -787,7 +792,7 @@ std::unique_ptr<Response> InternalServer::handle_no_js(const RequestContext& req
{ {
const auto url = request.get_url(); const auto url = request.get_url();
const auto urlParts = kiwix::split(url, "/", true, false); const auto urlParts = kiwix::split(url, "/", true, false);
HTMLDumper htmlDumper(mp_library.get(), mp_nameMapper); HTMLDumper htmlDumper(mp_library.get(), mp_nameMapper.get());
htmlDumper.setRootLocation(m_root); htmlDumper.setRootLocation(m_root);
htmlDumper.setLibraryId(getLibraryId()); htmlDumper.setLibraryId(getLibraryId());
auto userLang = request.get_user_language(); auto userLang = request.get_user_language();
@ -958,7 +963,7 @@ std::unique_ptr<Response> InternalServer::handle_search_request(const RequestCon
const auto pageLength = getSearchPageSize(request); const auto pageLength = getSearchPageSize(request);
/* Get the results */ /* Get the results */
SearchRenderer renderer(search->getResults(start-1, pageLength), mp_nameMapper, mp_library.get(), start, SearchRenderer renderer(search->getResults(start-1, pageLength), mp_nameMapper.get(), mp_library.get(), start,
search->getEstimatedMatches()); search->getEstimatedMatches());
renderer.setSearchPattern(searchInfo.pattern); renderer.setSearchPattern(searchInfo.pattern);
renderer.setSearchBookQuery(searchInfo.bookFilterQuery); renderer.setSearchBookQuery(searchInfo.bookFilterQuery);

View File

@ -93,7 +93,7 @@ class OPDSDumper;
class InternalServer { class InternalServer {
public: public:
InternalServer(std::shared_ptr<Library> library, InternalServer(std::shared_ptr<Library> library,
NameMapper* nameMapper, std::shared_ptr<NameMapper> nameMapper,
std::string addr, std::string addr,
int port, int port,
std::string root, std::string root,
@ -179,7 +179,7 @@ class InternalServer {
struct MHD_Daemon* mp_daemon; struct MHD_Daemon* mp_daemon;
std::shared_ptr<Library> mp_library; std::shared_ptr<Library> mp_library;
NameMapper* mp_nameMapper; std::shared_ptr<NameMapper> mp_nameMapper;
SearchCache searchCache; SearchCache searchCache;
SuggestionSearcherCache suggestionSearcherCache; SuggestionSearcherCache suggestionSearcherCache;

View File

@ -82,7 +82,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& r
} }
zim::Uuid uuid; zim::Uuid uuid;
kiwix::OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); kiwix::OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(getLibraryId()); opdsDumper.setLibraryId(getLibraryId());
std::vector<std::string> bookIdsToDump; std::vector<std::string> bookIdsToDump;
@ -164,7 +164,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_root(const RequestCo
std::unique_ptr<Response> InternalServer::handle_catalog_v2_entries(const RequestContext& request, bool partial) std::unique_ptr<Response> InternalServer::handle_catalog_v2_entries(const RequestContext& request, bool partial)
{ {
OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(getLibraryId()); opdsDumper.setLibraryId(getLibraryId());
const auto bookIds = search_catalog(request, opdsDumper); const auto bookIds = search_catalog(request, opdsDumper);
@ -185,7 +185,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_complete_entry(const
+ urlNotFoundMsg; + urlNotFoundMsg;
} }
OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(getLibraryId()); opdsDumper.setLibraryId(getLibraryId());
const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId); const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId);
@ -198,7 +198,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_complete_entry(const
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.get(), mp_nameMapper); OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(getLibraryId()); opdsDumper.setLibraryId(getLibraryId());
return ContentResponse::build( return ContentResponse::build(
@ -210,7 +210,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_categories(const Req
std::unique_ptr<Response> InternalServer::handle_catalog_v2_languages(const RequestContext& request) std::unique_ptr<Response> InternalServer::handle_catalog_v2_languages(const RequestContext& request)
{ {
OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper); OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setLibraryId(getLibraryId()); opdsDumper.setLibraryId(getLibraryId());
return ContentResponse::build( return ContentResponse::build(

View File

@ -100,7 +100,7 @@ private:
private: // data private: // data
std::shared_ptr<kiwix::Library> library; std::shared_ptr<kiwix::Library> library;
kiwix::Manager manager; kiwix::Manager manager;
std::unique_ptr<kiwix::NameMapper> nameMapper; std::shared_ptr<kiwix::NameMapper> nameMapper;
std::unique_ptr<kiwix::Server> server; std::unique_ptr<kiwix::Server> server;
std::unique_ptr<httplib::Client> client; std::unique_ptr<httplib::Client> client;
const Cfg cfg; const Cfg cfg;
@ -140,7 +140,7 @@ void ZimFileServer::run(int serverPort, std::string indexTemplateString)
} else { } else {
nameMapper.reset(new kiwix::HumanReadableNameMapper(*library, false)); nameMapper.reset(new kiwix::HumanReadableNameMapper(*library, false));
} }
server.reset(new kiwix::Server(library, nameMapper.get())); server.reset(new kiwix::Server(library, nameMapper));
server->setRoot(cfg.root); server->setRoot(cfg.root);
server->setAddress(address); server->setAddress(address);
server->setPort(serverPort); server->setPort(serverPort);