From 3b9f28b2b5e5eb474e28ea0f829ad2ba4e397567 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 14 Apr 2022 22:37:11 +0400 Subject: [PATCH] Applied cache-id to search_results.css The story of search_results.css static/skin/search_results.css was extracted from static/templates/no_search_result.html before the latter was dropped. static/templates/no_search_result.html in turn seems to be a copied and edited version of static/templates/search_result.html. In the context of exploratory work on the internationalization of kiwix-serve (PR #679) I noticed duplication of inline CSS across those two templates and intended to eliminated it. That goal was not fully accomplished (static/templates/search_result.html remained untouched) because by that time PR #679 grew too big and the efforts were diverted into splitting it into smaller ones. Thus search_results.css slipped into one of those small PRs, without making much sense because nothing really justifies preserving custom CSS in the "Fulltext search unavailable" error page. At the same time, it served as the only case where a link to a cacheable resource is generated in C++ code (rather than found in a template). This poses certain problems to the handling of cache-ids. A workaround is to expel the URL into a template so that it is processed by `kiwix-resources`. This commit merely demonstrates that solution. But whether it should be preserved (or rather the "Fulltext search unavailable" page should be deprived of CSS) is questionable. --- scripts/kiwix-resources | 4 ++-- src/server/internalServer.cpp | 13 ++++++++++++- static/resources_list.txt | 1 + static/templates/url_of_search_results_css | 1 + test/server.cpp | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 static/templates/url_of_search_results_css diff --git a/scripts/kiwix-resources b/scripts/kiwix-resources index 72e029a20..9c607dede 100755 --- a/scripts/kiwix-resources +++ b/scripts/kiwix-resources @@ -43,7 +43,7 @@ def fill_resource_revisions(resource_file_path): for resource in read_resource_file(resource_file_path): resource_revisions[resource] = get_resource_revision(base_dir, resource) -RESOURCE_WITH_CACHEID_URL_PATTERN=r'"([^"?]+)\?KIWIXCACHEID([^"]*)"' +RESOURCE_WITH_CACHEID_URL_PATTERN=r'([^"?]+)\?KIWIXCACHEID([^"]*)' def set_cacheid(resource_matchobj): path = resource_matchobj.group(1) @@ -53,7 +53,7 @@ def set_cacheid(resource_matchobj): resource = resource[len(root_prefix):] extra_query = resource_matchobj.group(2) cacheid = 'cacheid=' + resource_revisions[resource] - return f'"{path}?{cacheid}{extra_query}"' + return f'{path}?{cacheid}{extra_query}' def preprocess_line(line): if 'KIWIXCACHEID' in line: diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 748765615..95f999f99 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -442,6 +442,16 @@ SuggestionsList_t getSuggestions(SuggestionSearcherCache& cache, const zim::Arch namespace { +std::string renderUrl(const std::string& root, const std::string& urlTemplate) +{ + MustacheData data; + data.set("root", root); + auto url = kainjow::mustache::mustache(urlTemplate).render(data); + if ( url.back() == '\n' ) + url.pop_back(); + return url; +} + std::string makeFulltextSearchSuggestion(const std::string& lang, const std::string& queryString) { return i18n::expandParameterizedString(lang, "suggest-full-text-search", @@ -622,10 +632,11 @@ 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) + const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css); return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND, "fulltext-search-unavailable", "404-page-heading", - m_root + "/skin/search_results.css") + cssUrl) + nonParameterizedMessage("no-search-results") + TaskbarInfo(searchInfo.bookName, archive.get()); } diff --git a/static/resources_list.txt b/static/resources_list.txt index 15275ad19..bc1eb1af3 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -47,5 +47,6 @@ templates/catalog_v2_entries.xml templates/catalog_v2_entry.xml templates/catalog_v2_categories.xml templates/catalog_v2_languages.xml +templates/url_of_search_results_css opensearchdescription.xml catalog_v2_searchdescription.xml diff --git a/static/templates/url_of_search_results_css b/static/templates/url_of_search_results_css new file mode 100644 index 000000000..f0ecb87d8 --- /dev/null +++ b/static/templates/url_of_search_results_css @@ -0,0 +1 @@ +{{root}}/skin/search_results.css?KIWIXCACHEID diff --git a/test/server.cpp b/test/server.cpp index 422cc2620..7a6ddf307 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -345,7 +345,7 @@ R"EXPECTEDRESULT( +R"EXPECTEDRESULT( @@ -847,7 +847,7 @@ TEST_F(ServerTest, 404WithBodyTesting) { /* url */ "/ROOT/search?content=poor&pattern=whatever", expected_page_title=="Fulltext search unavailable" && - expected_css_url=="/ROOT/skin/search_results.css" && + expected_css_url=="/ROOT/skin/search_results.css?cacheid=76d39c84" && book_name=="poor" && book_title=="poor" && expected_body==R"(