From dbcbdff275f361f9a0f9528c67ae57e9405bc92c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 30 Jan 2022 17:09:20 +0400 Subject: [PATCH 1/2] Added an optional CSS link to error.html --- src/server/response.cpp | 4 +++- src/server/response.h | 3 ++- static/templates/error.html | 3 +++ test/server.cpp | 7 ++++--- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/server/response.cpp b/src/server/response.cpp index aefa0dc71..f87ffecb5 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -101,7 +101,8 @@ HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server, const RequestContext& request, int httpStatusCode, const std::string& pageTitleMsg, - const std::string& headingMsg) + const std::string& headingMsg, + const std::string& cssUrl) : ContentResponseBlueprint(&server, &request, httpStatusCode, @@ -110,6 +111,7 @@ HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server, { kainjow::mustache::list emptyList; this->m_data = kainjow::mustache::object{ + {"CSS_URL", onlyAsNonEmptyMustacheValue(cssUrl) }, {"PAGE_TITLE", pageTitleMsg}, {"PAGE_HEADING", headingMsg}, {"details", emptyList} diff --git a/src/server/response.h b/src/server/response.h index 710e2bc0f..1c93434e3 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -184,7 +184,8 @@ struct HTTPErrorHtmlResponse : ContentResponseBlueprint const RequestContext& request, int httpStatusCode, const std::string& pageTitleMsg, - const std::string& headingMsg); + const std::string& headingMsg, + const std::string& cssUrl = ""); using ContentResponseBlueprint::operator+; HTTPErrorHtmlResponse& operator+(const std::string& msg); diff --git a/static/templates/error.html b/static/templates/error.html index 5fcc0ff2c..711082096 100644 --- a/static/templates/error.html +++ b/static/templates/error.html @@ -3,6 +3,9 @@ {{PAGE_TITLE}} +{{#CSS_URL}} + +{{/CSS_URL}}

{{PAGE_HEADING}}

diff --git a/test/server.cpp b/test/server.cpp index 6467d44fa..de36057cd 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -430,7 +430,8 @@ std::string TestContentIn404HtmlResponse::expectedResponse() const R"FRAG( )FRAG", - R"FRAG( + R"FRAG( + @@ -497,8 +498,7 @@ std::string TestContentIn404HtmlResponse::pageCssLink() const return R"( -)"; + + R"(" rel="Stylesheet" />)"; } std::string TestContentIn404HtmlResponse::hiddenBookNameInput() const @@ -769,6 +769,7 @@ TEST_F(ServerTest, 500) Internal Server Error +

Internal Server Error

From ae1bf390235aa524be90f1bd7d57cdfa56d73ef2 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 30 Jan 2022 17:00:21 +0400 Subject: [PATCH 2/2] Got rid of static/templates/no_search_result.html The "Fulltext search unavailable" error page is now generated using the static/templates/error.html template. Also added two test cases checking that error page. --- src/server/internalServer.cpp | 17 +++++++++++------ static/resources_list.txt | 1 - static/skin/search_results.css | 2 +- static/templates/no_search_result.html | 15 --------------- test/server.cpp | 12 ++++++++++++ 5 files changed, 24 insertions(+), 23 deletions(-) delete mode 100644 static/templates/no_search_result.html diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 05c2d559c..8d1b5027a 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -447,6 +447,11 @@ std::string noSuchBookErrorMsg(const std::string& bookName) return "No such book: " + bookName; } +std::string noSearchResultsMsg() +{ + return "The fulltext search engine is not available for this content."; +} + } // unnamed namespace std::unique_ptr InternalServer::handle_suggest(const RequestContext& request) @@ -591,12 +596,12 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re } catch(std::runtime_error& e) { // 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) - auto data = get_default_data(); - data.set("pattern", encodeDiples(searchInfo.pattern)); - auto response = ContentResponse::build(*this, RESOURCE::templates::no_search_result_html, data, "text/html; charset=utf-8"); - response->set_code(MHD_HTTP_NOT_FOUND); - response->set_taskbar(searchInfo.bookName, archive.get()); - return std::move(response); + return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND, + "Fulltext search unavailable", + "Not Found", + m_root + "/skin/search_results.css") + + noSearchResultsMsg() + + TaskbarInfo(searchInfo.bookName, archive.get()); } diff --git a/static/resources_list.txt b/static/resources_list.txt index 7444c48ae..15275ad19 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -34,7 +34,6 @@ skin/fonts/Roboto.ttf skin/block_external.js skin/search_results.css templates/search_result.html -templates/no_search_result.html templates/error.html templates/index.html templates/suggestion.json diff --git a/static/skin/search_results.css b/static/skin/search_results.css index 8fb0a84b1..9fc107e9c 100644 --- a/static/skin/search_results.css +++ b/static/skin/search_results.css @@ -18,7 +18,7 @@ a:hover { text-decoration: underline } -.header { +h1 { font-size: 120%; } diff --git a/static/templates/no_search_result.html b/static/templates/no_search_result.html deleted file mode 100644 index baa7aa2c7..000000000 --- a/static/templates/no_search_result.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - Fulltext search unavailable - - - -
Not found
-

- There is no article with the title "{{pattern}}" - and the fulltext search engine is not available for this content. -

- - diff --git a/test/server.cpp b/test/server.cpp index de36057cd..d4056dda0 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -686,6 +686,18 @@ TEST_F(ServerTest, 404WithBodyTesting) Cannot find content entry invalid-article

)" }, + + { /* url */ "/ROOT/search?content=poor&pattern=whatever", + expected_page_title=="Fulltext search unavailable" && + expected_css_url=="/ROOT/skin/search_results.css" && + book_name=="poor" && + book_title=="poor" && + expected_body==R"( +

Not Found

+

+ The fulltext search engine is not available for this content. +

+)" }, }; for ( const auto& t : testData ) {