mirror of https://github.com/kiwix/libkiwix.git
[NEW] Make InternalServer inherite Server::Configuration.
This avoid a lot of silly `m_configuration.foo`.
This commit is contained in:
parent
422e71a017
commit
3e54e56291
|
@ -216,7 +216,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
||||||
try {
|
try {
|
||||||
auto bookName = request.get_argument("content");
|
auto bookName = request.get_argument("content");
|
||||||
try {
|
try {
|
||||||
const auto bookIds = Library::BookIdSet{m_configuration.mp_nameMapper->getIdForName(bookName)};
|
const auto bookIds = Library::BookIdSet{mp_nameMapper->getIdForName(bookName)};
|
||||||
const auto queryString = request.get_query([&](const std::string& key){return key == "content";}, true);
|
const auto queryString = request.get_query([&](const std::string& key){return key == "content";}, true);
|
||||||
return {queryString, bookIds};
|
return {queryString, bookIds};
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
|
@ -236,7 +236,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
||||||
for(const auto& bookId: id_vec) {
|
for(const auto& bookId: id_vec) {
|
||||||
try {
|
try {
|
||||||
// This is a silly way to check that bookId exists
|
// This is a silly way to check that bookId exists
|
||||||
m_configuration.mp_nameMapper->getNameForId(bookId);
|
mp_nameMapper->getNameForId(bookId);
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
throw Error(noSuchBookErrorMsg(bookId));
|
throw Error(noSuchBookErrorMsg(bookId));
|
||||||
}
|
}
|
||||||
|
@ -255,7 +255,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
||||||
Library::BookIdSet bookIds;
|
Library::BookIdSet bookIds;
|
||||||
for(const auto& bookName: name_vec) {
|
for(const auto& bookName: name_vec) {
|
||||||
try {
|
try {
|
||||||
bookIds.insert(m_configuration.mp_nameMapper->getIdForName(bookName));
|
bookIds.insert(mp_nameMapper->getIdForName(bookName));
|
||||||
} catch(const std::out_of_range&) {
|
} catch(const std::out_of_range&) {
|
||||||
throw Error(noSuchBookErrorMsg(bookName));
|
throw Error(noSuchBookErrorMsg(bookName));
|
||||||
}
|
}
|
||||||
|
@ -266,7 +266,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
||||||
|
|
||||||
// Check for filtering
|
// Check for filtering
|
||||||
Filter filter = get_search_filter(request, "books.filter.");
|
Filter filter = get_search_filter(request, "books.filter.");
|
||||||
auto id_vec = m_configuration.mp_library->filter(filter);
|
auto id_vec = mp_library->filter(filter);
|
||||||
if (id_vec.empty()) {
|
if (id_vec.empty()) {
|
||||||
throw Error(nonParameterizedMessage("no-book-found"));
|
throw Error(nonParameterizedMessage("no-book-found"));
|
||||||
}
|
}
|
||||||
|
@ -278,7 +278,7 @@ std::pair<std::string, Library::BookIdSet> InternalServer::selectBooks(const Req
|
||||||
SearchInfo InternalServer::getSearchInfo(const RequestContext& request) const
|
SearchInfo InternalServer::getSearchInfo(const RequestContext& request) const
|
||||||
{
|
{
|
||||||
auto bookIds = selectBooks(request);
|
auto bookIds = selectBooks(request);
|
||||||
checkBookNumber(bookIds.second, m_configuration.m_multizimSearchLimit);
|
checkBookNumber(bookIds.second, m_multizimSearchLimit);
|
||||||
auto pattern = request.get_optional_param<std::string>("pattern", "");
|
auto pattern = request.get_optional_param<std::string>("pattern", "");
|
||||||
GeoQuery geoQuery;
|
GeoQuery geoQuery;
|
||||||
|
|
||||||
|
@ -355,11 +355,11 @@ public:
|
||||||
|
|
||||||
|
|
||||||
InternalServer::InternalServer(const Server::Configuration& configuration) :
|
InternalServer::InternalServer(const Server::Configuration& configuration) :
|
||||||
m_configuration(configuration),
|
Server::Configuration(configuration),
|
||||||
m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString),
|
m_indexTemplateString(configuration.m_indexTemplateString.empty() ? RESOURCE::templates::index_html : configuration.m_indexTemplateString),
|
||||||
mp_daemon(nullptr),
|
mp_daemon(nullptr),
|
||||||
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) (m_configuration.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)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
@ -371,13 +371,13 @@ bool InternalServer::start() {
|
||||||
#else
|
#else
|
||||||
int flags = MHD_USE_POLL_INTERNALLY;
|
int flags = MHD_USE_POLL_INTERNALLY;
|
||||||
#endif
|
#endif
|
||||||
if (m_configuration.m_verbose)
|
if (m_verbose)
|
||||||
flags |= MHD_USE_DEBUG;
|
flags |= MHD_USE_DEBUG;
|
||||||
|
|
||||||
struct sockaddr_in sockAddr;
|
struct sockaddr_in sockAddr;
|
||||||
memset(&sockAddr, 0, sizeof(sockAddr));
|
memset(&sockAddr, 0, sizeof(sockAddr));
|
||||||
sockAddr.sin_family = AF_INET;
|
sockAddr.sin_family = AF_INET;
|
||||||
sockAddr.sin_port = htons(m_configuration.m_port);
|
sockAddr.sin_port = htons(m_port);
|
||||||
if (m_addr.empty()) {
|
if (m_addr.empty()) {
|
||||||
if (0 != INADDR_ANY) {
|
if (0 != INADDR_ANY) {
|
||||||
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
@ -390,17 +390,17 @@ bool InternalServer::start() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mp_daemon = MHD_start_daemon(flags,
|
mp_daemon = MHD_start_daemon(flags,
|
||||||
m_configuration.m_port,
|
m_port,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
&staticHandlerCallback,
|
&staticHandlerCallback,
|
||||||
this,
|
this,
|
||||||
MHD_OPTION_SOCK_ADDR, &sockAddr,
|
MHD_OPTION_SOCK_ADDR, &sockAddr,
|
||||||
MHD_OPTION_THREAD_POOL_SIZE, m_configuration.m_nbThreads,
|
MHD_OPTION_THREAD_POOL_SIZE, m_nbThreads,
|
||||||
MHD_OPTION_PER_IP_CONNECTION_LIMIT, m_configuration.m_ipConnectionLimit,
|
MHD_OPTION_PER_IP_CONNECTION_LIMIT, m_ipConnectionLimit,
|
||||||
MHD_OPTION_END);
|
MHD_OPTION_END);
|
||||||
if (mp_daemon == nullptr) {
|
if (mp_daemon == nullptr) {
|
||||||
std::cerr << "Unable to instantiate the HTTP daemon. The port " << m_configuration.m_port
|
std::cerr << "Unable to instantiate the HTTP daemon. The port " << m_port
|
||||||
<< " is maybe already occupied or need more permissions to be open. "
|
<< " is maybe already occupied or need more permissions to be open. "
|
||||||
"Please try as root or with a port number higher or equal to 1024."
|
"Please try as root or with a port number higher or equal to 1024."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
@ -446,14 +446,14 @@ MHD_Result InternalServer::handlerCallback(struct MHD_Connection* connection,
|
||||||
void** cont_cls)
|
void** cont_cls)
|
||||||
{
|
{
|
||||||
auto start_time = std::chrono::steady_clock::now();
|
auto start_time = std::chrono::steady_clock::now();
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("======================\n");
|
printf("======================\n");
|
||||||
printf("Requesting : \n");
|
printf("Requesting : \n");
|
||||||
printf("full_url : %s\n", url);
|
printf("full_url : %s\n", url);
|
||||||
}
|
}
|
||||||
RequestContext request(connection, m_configuration.m_root, url, method, version);
|
RequestContext request(connection, m_root, url, method, version);
|
||||||
|
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
request.print_debug_info();
|
request.print_debug_info();
|
||||||
}
|
}
|
||||||
/* Unexpected method */
|
/* Unexpected method */
|
||||||
|
@ -469,7 +469,7 @@ MHD_Result InternalServer::handlerCallback(struct MHD_Connection* connection,
|
||||||
|
|
||||||
if (response->getReturnCode() == MHD_HTTP_INTERNAL_SERVER_ERROR) {
|
if (response->getReturnCode() == MHD_HTTP_INTERNAL_SERVER_ERROR) {
|
||||||
printf("========== INTERNAL ERROR !! ============\n");
|
printf("========== INTERNAL ERROR !! ============\n");
|
||||||
if (!m_configuration.m_verbose) {
|
if (!m_verbose) {
|
||||||
printf("Requesting : \n");
|
printf("Requesting : \n");
|
||||||
printf("full_url : %s\n", url);
|
printf("full_url : %s\n", url);
|
||||||
request.print_debug_info();
|
request.print_debug_info();
|
||||||
|
@ -482,7 +482,7 @@ MHD_Result InternalServer::handlerCallback(struct MHD_Connection* connection,
|
||||||
auto ret = response->send(request, connection);
|
auto ret = response->send(request, connection);
|
||||||
auto end_time = std::chrono::steady_clock::now();
|
auto end_time = std::chrono::steady_clock::now();
|
||||||
auto time_span = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
|
auto time_span = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("Request time : %fs\n", time_span.count());
|
printf("Request time : %fs\n", time_span.count());
|
||||||
printf("----------------------\n");
|
printf("----------------------\n");
|
||||||
}
|
}
|
||||||
|
@ -545,7 +545,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
|
||||||
if (isEndpointUrl(url, "catch"))
|
if (isEndpointUrl(url, "catch"))
|
||||||
return handle_catch(request);
|
return handle_catch(request);
|
||||||
|
|
||||||
std::string contentUrl = m_configuration.m_root + "/content" + url;
|
std::string contentUrl = m_root + "/content" + url;
|
||||||
const std::string query = request.get_query();
|
const std::string query = request.get_query();
|
||||||
if ( ! query.empty() )
|
if ( ! query.empty() )
|
||||||
contentUrl += "?" + query;
|
contentUrl += "?" + query;
|
||||||
|
@ -564,7 +564,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
|
||||||
MustacheData InternalServer::get_default_data() const
|
MustacheData InternalServer::get_default_data() const
|
||||||
{
|
{
|
||||||
MustacheData data;
|
MustacheData data;
|
||||||
data.set("root", m_configuration.m_root);
|
data.set("root", m_root);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,7 +615,7 @@ class InternalServer::LockableSuggestionSearcher : public zim::SuggestionSearche
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_suggest\n");
|
printf("** running handle_suggest\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -628,8 +628,8 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
|
||||||
std::shared_ptr<zim::Archive> archive;
|
std::shared_ptr<zim::Archive> archive;
|
||||||
try {
|
try {
|
||||||
bookName = request.get_argument("content");
|
bookName = request.get_argument("content");
|
||||||
bookId = m_configuration.mp_nameMapper->getIdForName(bookName);
|
bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
archive = m_configuration.mp_library->getArchiveById(bookId);
|
archive = mp_library->getArchiveById(bookId);
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
// error handled by the archive == nullptr check below
|
// error handled by the archive == nullptr check below
|
||||||
}
|
}
|
||||||
|
@ -646,7 +646,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
|
||||||
count = 10;
|
count = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("Searching suggestions for: \"%s\"\n", queryString.c_str());
|
printf("Searching suggestions for: \"%s\"\n", queryString.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -699,21 +699,21 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_viewer_settings(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_viewer_settings(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_viewer_settings\n");
|
printf("** running handle_viewer_settings\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
const kainjow::mustache::object data{
|
const kainjow::mustache::object data{
|
||||||
{"enable_toolbar", m_configuration.m_withTaskbar ? "true" : "false" },
|
{"enable_toolbar", m_withTaskbar ? "true" : "false" },
|
||||||
{"enable_link_blocking", m_configuration.m_blockExternalLinks ? "true" : "false" },
|
{"enable_link_blocking", m_blockExternalLinks ? "true" : "false" },
|
||||||
{"enable_library_button", m_configuration.m_withLibraryButton ? "true" : "false" }
|
{"enable_library_button", m_withLibraryButton ? "true" : "false" }
|
||||||
};
|
};
|
||||||
return ContentResponse::build(*this, RESOURCE::templates::viewer_settings_js, data, "application/javascript; charset=utf-8");
|
return ContentResponse::build(*this, RESOURCE::templates::viewer_settings_js, data, "application/javascript; charset=utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_skin(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_skin(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_skin\n");
|
printf("** running handle_skin\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -736,7 +736,7 @@ std::unique_ptr<Response> InternalServer::handle_skin(const RequestContext& requ
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_search\n");
|
printf("** running handle_search\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -758,20 +758,20 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
||||||
|
|
||||||
/* Make the search */
|
/* Make the search */
|
||||||
// Try to get a search from the searchInfo, else build it
|
// Try to get a search from the searchInfo, else build it
|
||||||
auto searcher = m_configuration.mp_library->getSearcherByIds(bookIds);
|
auto searcher = mp_library->getSearcherByIds(bookIds);
|
||||||
auto lock(searcher->getLock());
|
auto lock(searcher->getLock());
|
||||||
|
|
||||||
std::shared_ptr<zim::Search> search;
|
std::shared_ptr<zim::Search> search;
|
||||||
try {
|
try {
|
||||||
search = searchCache.getOrPut(searchInfo,
|
search = searchCache.getOrPut(searchInfo,
|
||||||
[=](){
|
[=](){
|
||||||
return make_shared<zim::Search>(searcher->search(searchInfo.getZimQuery(m_configuration.m_verbose)));
|
return make_shared<zim::Search>(searcher->search(searchInfo.getZimQuery(m_verbose)));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} catch(std::runtime_error& e) {
|
} catch(std::runtime_error& e) {
|
||||||
// Searcher->search will throw a runtime error if there is no valid xapian database to do the search.
|
// Searcher->search will throw a runtime error if there is no valid xapian database to do the search.
|
||||||
// (in case of zim file not containing a index)
|
// (in case of zim file not containing a index)
|
||||||
const auto cssUrl = renderUrl(m_configuration.m_root, RESOURCE::templates::url_of_search_results_css);
|
const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css);
|
||||||
HTTPErrorResponse response(*this, request, MHD_HTTP_NOT_FOUND,
|
HTTPErrorResponse response(*this, request, MHD_HTTP_NOT_FOUND,
|
||||||
"fulltext-search-unavailable",
|
"fulltext-search-unavailable",
|
||||||
"404-page-heading",
|
"404-page-heading",
|
||||||
|
@ -782,8 +782,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
||||||
/*
|
/*
|
||||||
if(bookIds.size() == 1) {
|
if(bookIds.size() == 1) {
|
||||||
auto bookId = *bookIds.begin();
|
auto bookId = *bookIds.begin();
|
||||||
auto bookName = m_configuration.mp_nameMapper->getNameForId(bookId);
|
auto bookName = mp_nameMapper->getNameForId(bookId);
|
||||||
response += TaskbarInfo(bookName, m_configuration.mp_library->getArchiveById(bookId).get());
|
response += TaskbarInfo(bookName, mp_library->getArchiveById(bookId).get());
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return response;
|
return response;
|
||||||
|
@ -807,12 +807,12 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the results */
|
/* Get the results */
|
||||||
SearchRenderer renderer(search->getResults(start-1, pageLength), m_configuration.mp_nameMapper, m_configuration.mp_library, start,
|
SearchRenderer renderer(search->getResults(start-1, pageLength), mp_nameMapper, mp_library, start,
|
||||||
search->getEstimatedMatches());
|
search->getEstimatedMatches());
|
||||||
renderer.setSearchPattern(searchInfo.pattern);
|
renderer.setSearchPattern(searchInfo.pattern);
|
||||||
renderer.setSearchBookQuery(searchInfo.bookFilterQuery);
|
renderer.setSearchBookQuery(searchInfo.bookFilterQuery);
|
||||||
renderer.setProtocolPrefix(m_configuration.m_root + "/content/");
|
renderer.setProtocolPrefix(m_root + "/content/");
|
||||||
renderer.setSearchProtocolPrefix(m_configuration.m_root + "/search");
|
renderer.setSearchProtocolPrefix(m_root + "/search");
|
||||||
renderer.setPageLength(pageLength);
|
renderer.setPageLength(pageLength);
|
||||||
if (request.get_requested_format() == "xml") {
|
if (request.get_requested_format() == "xml") {
|
||||||
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8");
|
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8");
|
||||||
|
@ -823,8 +823,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
||||||
/*
|
/*
|
||||||
if(bookIds.size() == 1) {
|
if(bookIds.size() == 1) {
|
||||||
auto bookId = *bookIds.begin();
|
auto bookId = *bookIds.begin();
|
||||||
auto bookName = m_configuration.mp_nameMapper->getNameForId(bookId);
|
auto bookName = mp_nameMapper->getNameForId(bookId);
|
||||||
response->set_taskbar(bookName, m_configuration.mp_library->getArchiveById(bookId).get());
|
response->set_taskbar(bookName, mp_library->getArchiveById(bookId).get());
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return std::move(response);
|
return std::move(response);
|
||||||
|
@ -837,7 +837,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_random\n");
|
printf("** running handle_random\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -850,8 +850,8 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
|
||||||
std::shared_ptr<zim::Archive> archive;
|
std::shared_ptr<zim::Archive> archive;
|
||||||
try {
|
try {
|
||||||
bookName = request.get_argument("content");
|
bookName = request.get_argument("content");
|
||||||
const std::string bookId = m_configuration.mp_nameMapper->getIdForName(bookName);
|
const std::string bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
archive = m_configuration.mp_library->getArchiveById(bookId);
|
archive = mp_library->getArchiveById(bookId);
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
// error handled by the archive == nullptr check below
|
// error handled by the archive == nullptr check below
|
||||||
}
|
}
|
||||||
|
@ -889,7 +889,7 @@ std::unique_ptr<Response> InternalServer::handle_captured_external(const Request
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_catch(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_catch(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_catch\n");
|
printf("** running handle_catch\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -903,7 +903,7 @@ std::unique_ptr<Response> InternalServer::handle_catch(const RequestContext& req
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_catalog");
|
printf("** running handle_catalog");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -932,13 +932,13 @@ std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& r
|
||||||
}
|
}
|
||||||
|
|
||||||
zim::Uuid uuid;
|
zim::Uuid uuid;
|
||||||
kiwix::OPDSDumper opdsDumper(m_configuration.mp_library, m_configuration.mp_nameMapper);
|
kiwix::OPDSDumper opdsDumper(mp_library, mp_nameMapper);
|
||||||
opdsDumper.setRootLocation(m_configuration.m_root);
|
opdsDumper.setRootLocation(m_root);
|
||||||
opdsDumper.setLibraryId(m_library_id);
|
opdsDumper.setLibraryId(m_library_id);
|
||||||
std::vector<std::string> bookIdsToDump;
|
std::vector<std::string> bookIdsToDump;
|
||||||
if (url == "root.xml") {
|
if (url == "root.xml") {
|
||||||
uuid = zim::Uuid::generate(host);
|
uuid = zim::Uuid::generate(host);
|
||||||
bookIdsToDump = m_configuration.mp_library->filter(kiwix::Filter().valid(true).local(true).remote(true));
|
bookIdsToDump = mp_library->filter(kiwix::Filter().valid(true).local(true).remote(true));
|
||||||
} else if (url == "search") {
|
} else if (url == "search") {
|
||||||
bookIdsToDump = search_catalog(request, opdsDumper);
|
bookIdsToDump = search_catalog(request, opdsDumper);
|
||||||
uuid = zim::Uuid::generate();
|
uuid = zim::Uuid::generate();
|
||||||
|
@ -959,7 +959,7 @@ InternalServer::search_catalog(const RequestContext& request,
|
||||||
const std::string q = filter.hasQuery()
|
const std::string q = filter.hasQuery()
|
||||||
? filter.getQuery()
|
? filter.getQuery()
|
||||||
: "<Empty query>";
|
: "<Empty query>";
|
||||||
std::vector<std::string> bookIdsToDump = m_configuration.mp_library->filter(filter);
|
std::vector<std::string> bookIdsToDump = mp_library->filter(filter);
|
||||||
const auto totalResults = bookIdsToDump.size();
|
const auto totalResults = bookIdsToDump.size();
|
||||||
const size_t count = request.get_optional_param("count", 10UL);
|
const size_t count = request.get_optional_param("count", 10UL);
|
||||||
const size_t startIndex = request.get_optional_param("start", 0UL);
|
const size_t startIndex = request.get_optional_param("start", 0UL);
|
||||||
|
@ -987,7 +987,7 @@ std::unique_ptr<Response>
|
||||||
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
|
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
|
||||||
{
|
{
|
||||||
const auto path = kiwix::urlEncode(item.getPath());
|
const auto path = kiwix::urlEncode(item.getPath());
|
||||||
const auto redirectUrl = m_configuration.m_root + "/content/" + bookName + "/" + path;
|
const auto redirectUrl = m_root + "/content/" + bookName + "/" + path;
|
||||||
return Response::build_redirect(*this, redirectUrl);
|
return Response::build_redirect(*this, redirectUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -995,7 +995,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
{
|
{
|
||||||
const std::string url = request.get_url();
|
const std::string url = request.get_url();
|
||||||
const std::string pattern = url.substr((url.find_last_of('/'))+1);
|
const std::string pattern = url.substr((url.find_last_of('/'))+1);
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_content\n");
|
printf("** running handle_content\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1006,12 +1006,12 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
|
|
||||||
std::shared_ptr<zim::Archive> archive;
|
std::shared_ptr<zim::Archive> archive;
|
||||||
try {
|
try {
|
||||||
const std::string bookId = m_configuration.mp_nameMapper->getIdForName(bookName);
|
const std::string bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
archive = m_configuration.mp_library->getArchiveById(bookId);
|
archive = mp_library->getArchiveById(bookId);
|
||||||
} catch (const std::out_of_range& e) {}
|
} catch (const std::out_of_range& e) {}
|
||||||
|
|
||||||
if (archive == nullptr) {
|
if (archive == nullptr) {
|
||||||
const std::string searchURL = m_configuration.m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
|
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
+ urlNotFoundMsg
|
+ urlNotFoundMsg
|
||||||
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
||||||
|
@ -1036,17 +1036,17 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
}
|
}
|
||||||
auto response = ItemResponse::build(*this, request, entry.getItem());
|
auto response = ItemResponse::build(*this, request, entry.getItem());
|
||||||
|
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("Found %s\n", entry.getPath().c_str());
|
printf("Found %s\n", entry.getPath().c_str());
|
||||||
printf("mimeType: %s\n", entry.getItem(true).getMimetype().c_str());
|
printf("mimeType: %s\n", entry.getItem(true).getMimetype().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch(zim::EntryNotFound& e) {
|
} catch(zim::EntryNotFound& e) {
|
||||||
if (m_configuration.m_verbose)
|
if (m_verbose)
|
||||||
printf("Failed to find %s\n", urlStr.c_str());
|
printf("Failed to find %s\n", urlStr.c_str());
|
||||||
|
|
||||||
std::string searchURL = m_configuration.m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
|
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
+ urlNotFoundMsg
|
+ urlNotFoundMsg
|
||||||
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
|
||||||
|
@ -1056,7 +1056,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_raw\n");
|
printf("** running handle_raw\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1078,8 +1078,8 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
|
||||||
|
|
||||||
std::shared_ptr<zim::Archive> archive;
|
std::shared_ptr<zim::Archive> archive;
|
||||||
try {
|
try {
|
||||||
const std::string bookId = m_configuration.mp_nameMapper->getIdForName(bookName);
|
const std::string bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
archive = m_configuration.mp_library->getArchiveById(bookId);
|
archive = mp_library->getArchiveById(bookId);
|
||||||
} catch (const std::out_of_range& e) {}
|
} catch (const std::out_of_range& e) {}
|
||||||
|
|
||||||
if (archive == nullptr) {
|
if (archive == nullptr) {
|
||||||
|
@ -1106,7 +1106,7 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
|
||||||
return ItemResponse::build(*this, request, entry.getItem());
|
return ItemResponse::build(*this, request, entry.getItem());
|
||||||
}
|
}
|
||||||
} catch (zim::EntryNotFound& e ) {
|
} catch (zim::EntryNotFound& e ) {
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("Failed to find %s\n", itemPath.c_str());
|
printf("Failed to find %s\n", itemPath.c_str());
|
||||||
}
|
}
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
|
@ -1122,13 +1122,13 @@ bool InternalServer::isLocallyCustomizedResource(const std::string& url) const
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_locally_customized_resource(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_locally_customized_resource(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_locally_customized_resource\n");
|
printf("** running handle_locally_customized_resource\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomizedResourceData& crd = m_customizedResources->at(request.get_url());
|
const CustomizedResourceData& crd = m_customizedResources->at(request.get_url());
|
||||||
|
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
std::cout << "Reading " << crd.resourceFilePath << std::endl;
|
std::cout << "Reading " << crd.resourceFilePath << std::endl;
|
||||||
}
|
}
|
||||||
const auto resourceData = getFileContent(crd.resourceFilePath);
|
const auto resourceData = getFileContent(crd.resourceFilePath);
|
||||||
|
|
|
@ -90,7 +90,7 @@ class SearchInfo {
|
||||||
typedef kainjow::mustache::data MustacheData;
|
typedef kainjow::mustache::data MustacheData;
|
||||||
class OPDSDumper;
|
class OPDSDumper;
|
||||||
|
|
||||||
class InternalServer {
|
class InternalServer : Server::Configuration {
|
||||||
public:
|
public:
|
||||||
InternalServer(const Server::Configuration& configuration);
|
InternalServer(const Server::Configuration& configuration);
|
||||||
virtual ~InternalServer();
|
virtual ~InternalServer();
|
||||||
|
@ -105,7 +105,7 @@ class InternalServer {
|
||||||
bool start();
|
bool start();
|
||||||
void stop();
|
void stop();
|
||||||
std::string getAddress() { return m_addr; }
|
std::string getAddress() { return m_addr; }
|
||||||
int getPort() { return m_configuration.m_port; }
|
int getPort() { return m_port; }
|
||||||
|
|
||||||
private: // functions
|
private: // functions
|
||||||
std::unique_ptr<Response> handle_request(const RequestContext& request);
|
std::unique_ptr<Response> handle_request(const RequestContext& request);
|
||||||
|
@ -148,7 +148,6 @@ class InternalServer {
|
||||||
typedef ConcurrentCache<std::string, std::shared_ptr<LockableSuggestionSearcher>> SuggestionSearcherCache;
|
typedef ConcurrentCache<std::string, std::shared_ptr<LockableSuggestionSearcher>> SuggestionSearcherCache;
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
Server::Configuration m_configuration;
|
|
||||||
std::string m_addr;
|
std::string m_addr;
|
||||||
std::string m_indexTemplateString;
|
std::string m_indexTemplateString;
|
||||||
struct MHD_Daemon* mp_daemon;
|
struct MHD_Daemon* mp_daemon;
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace kiwix {
|
||||||
|
|
||||||
std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext& request)
|
std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext& request)
|
||||||
{
|
{
|
||||||
if (m_configuration.m_verbose) {
|
if (m_verbose) {
|
||||||
printf("** running handle_catalog_v2");
|
printf("** running handle_catalog_v2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,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 = m_configuration.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}}),
|
||||||
|
@ -82,7 +82,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_root(const RequestCo
|
||||||
RESOURCE::templates::catalog_v2_root_xml,
|
RESOURCE::templates::catalog_v2_root_xml,
|
||||||
kainjow::mustache::object{
|
kainjow::mustache::object{
|
||||||
{"date", gen_date_str()},
|
{"date", gen_date_str()},
|
||||||
{"endpoint_root", m_configuration.m_root + "/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")},
|
||||||
{"partial_entries_feed_id", gen_uuid(m_library_id + "/partial_entries")},
|
{"partial_entries_feed_id", gen_uuid(m_library_id + "/partial_entries")},
|
||||||
|
@ -95,8 +95,8 @@ 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(m_configuration.mp_library, m_configuration.mp_nameMapper);
|
OPDSDumper opdsDumper(mp_library, mp_nameMapper);
|
||||||
opdsDumper.setRootLocation(m_configuration.m_root);
|
opdsDumper.setRootLocation(m_root);
|
||||||
opdsDumper.setLibraryId(m_library_id);
|
opdsDumper.setLibraryId(m_library_id);
|
||||||
const auto bookIds = search_catalog(request, opdsDumper);
|
const auto bookIds = search_catalog(request, opdsDumper);
|
||||||
const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query(), partial);
|
const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query(), partial);
|
||||||
|
@ -110,14 +110,14 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_entries(const Reques
|
||||||
std::unique_ptr<Response> InternalServer::handle_catalog_v2_complete_entry(const RequestContext& request, const std::string& entryId)
|
std::unique_ptr<Response> InternalServer::handle_catalog_v2_complete_entry(const RequestContext& request, const std::string& entryId)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
m_configuration.mp_library->getBookById(entryId);
|
mp_library->getBookById(entryId);
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
return HTTP404Response(*this, request)
|
return HTTP404Response(*this, request)
|
||||||
+ urlNotFoundMsg;
|
+ urlNotFoundMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
OPDSDumper opdsDumper(m_configuration.mp_library, m_configuration.mp_nameMapper);
|
OPDSDumper opdsDumper(mp_library, mp_nameMapper);
|
||||||
opdsDumper.setRootLocation(m_configuration.m_root);
|
opdsDumper.setRootLocation(m_root);
|
||||||
opdsDumper.setLibraryId(m_library_id);
|
opdsDumper.setLibraryId(m_library_id);
|
||||||
const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId);
|
const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId);
|
||||||
return ContentResponse::build(
|
return ContentResponse::build(
|
||||||
|
@ -129,8 +129,8 @@ 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(m_configuration.mp_library, m_configuration.mp_nameMapper);
|
OPDSDumper opdsDumper(mp_library, mp_nameMapper);
|
||||||
opdsDumper.setRootLocation(m_configuration.m_root);
|
opdsDumper.setRootLocation(m_root);
|
||||||
opdsDumper.setLibraryId(m_library_id);
|
opdsDumper.setLibraryId(m_library_id);
|
||||||
return ContentResponse::build(
|
return ContentResponse::build(
|
||||||
*this,
|
*this,
|
||||||
|
@ -141,8 +141,8 @@ 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(m_configuration.mp_library, m_configuration.mp_nameMapper);
|
OPDSDumper opdsDumper(mp_library, mp_nameMapper);
|
||||||
opdsDumper.setRootLocation(m_configuration.m_root);
|
opdsDumper.setRootLocation(m_root);
|
||||||
opdsDumper.setLibraryId(m_library_id);
|
opdsDumper.setLibraryId(m_library_id);
|
||||||
return ContentResponse::build(
|
return ContentResponse::build(
|
||||||
*this,
|
*this,
|
||||||
|
@ -155,7 +155,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_illustration(const R
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
const auto bookId = request.get_url_part(3);
|
const auto bookId = request.get_url_part(3);
|
||||||
auto book = m_configuration.mp_library->getBookByIdThreadSafe(bookId);
|
auto book = mp_library->getBookByIdThreadSafe(bookId);
|
||||||
auto size = request.get_argument<unsigned int>("size");
|
auto size = request.get_argument<unsigned int>("size");
|
||||||
auto illustration = book.getIllustration(size);
|
auto illustration = book.getIllustration(size);
|
||||||
return ContentResponse::build(
|
return ContentResponse::build(
|
||||||
|
|
|
@ -114,7 +114,7 @@ Response::Response(bool verbose)
|
||||||
|
|
||||||
std::unique_ptr<Response> Response::build(const InternalServer& server)
|
std::unique_ptr<Response> Response::build(const InternalServer& server)
|
||||||
{
|
{
|
||||||
return std::unique_ptr<Response>(new Response(server.m_configuration.m_verbose));
|
return std::unique_ptr<Response>(new Response(server.m_verbose));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Response> Response::build_304(const InternalServer& server, const ETag& etag)
|
std::unique_ptr<Response> Response::build_304(const InternalServer& server, const ETag& etag)
|
||||||
|
@ -389,8 +389,8 @@ std::unique_ptr<ContentResponse> ContentResponse::build(
|
||||||
const std::string& mimetype)
|
const std::string& mimetype)
|
||||||
{
|
{
|
||||||
return std::unique_ptr<ContentResponse>(new ContentResponse(
|
return std::unique_ptr<ContentResponse>(new ContentResponse(
|
||||||
server.m_configuration.m_root,
|
server.m_root,
|
||||||
server.m_configuration.m_verbose,
|
server.m_verbose,
|
||||||
content,
|
content,
|
||||||
mimetype));
|
mimetype));
|
||||||
}
|
}
|
||||||
|
@ -435,7 +435,7 @@ std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::unique_ptr<Response>(new ItemResponse(
|
return std::unique_ptr<Response>(new ItemResponse(
|
||||||
server.m_configuration.m_verbose,
|
server.m_verbose,
|
||||||
item,
|
item,
|
||||||
mimetype,
|
mimetype,
|
||||||
byteRange));
|
byteRange));
|
||||||
|
|
Loading…
Reference in New Issue