Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iframe-based content viewer #716

Merged
merged 42 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dea674e
Added resources of autoComplete.js to test/server.cpp
veloman-yunkan Aug 11, 2022
4db443e
Embryo of iframe-based viewer
veloman-yunkan Feb 22, 2022
e5f97d9
Handling of manual hash component change
veloman-yunkan Mar 19, 2022
4105be9
Improved browsing history tracking & traversal
veloman-yunkan Aug 7, 2022
228e31c
Handling of window size changes
veloman-yunkan Mar 19, 2022
7c6c315
/viewer# displays a blank page
veloman-yunkan Aug 7, 2022
5b545d8
viewer += static/templates/head_taskbar.html
veloman-yunkan Mar 20, 2022
58e4571
Copied static/skin/taskbar.js as viewer_taskbar.js
veloman-yunkan Aug 10, 2022
f41e71b
viewer_taskbar.js + viewer.html = BFF
veloman-yunkan Mar 20, 2022
7be7a8e
viewer += <!--static/templates/taskbar_part.html-->
veloman-yunkan Mar 20, 2022
0c4d9e8
Enabled the library button on the taskbar
veloman-yunkan Mar 20, 2022
17ff2a0
Enabled home button in the iframe-based viewer
veloman-yunkan Mar 20, 2022
a944671
Viewer respects the --urlRootLocation option
veloman-yunkan Aug 7, 2022
4a55b13
Enabled random page button in the iframe-based viewer
veloman-yunkan Aug 7, 2022
77d9777
Enabled searchbox in the iframe-based viewer
veloman-yunkan Aug 10, 2022
eadc0ac
Welcome page interoperates with iframe-based viewer
veloman-yunkan Aug 10, 2022
7ed01e7
Renamed static/skin/{viewer_taskbar -> viewer}.js
veloman-yunkan Aug 10, 2022
9484357
Moved all viewer JS code to viewer.js
veloman-yunkan Aug 10, 2022
29efb88
Superficial cleanup in static/skin/viewer.js
veloman-yunkan Aug 10, 2022
2083c39
Searchbox correctly tracks the current book
veloman-yunkan Aug 10, 2022
9a19373
Hiding of the suggestions drop-down list
veloman-yunkan Aug 10, 2022
40c496d
Removed old-style taskbar injection
veloman-yunkan Aug 11, 2022
0cf4850
Dropped TaskbarInfo
veloman-yunkan Jun 26, 2022
c73e6f9
Dropped unused params from ContentResponse ctor
veloman-yunkan Jun 26, 2022
c988511
Removed unused param from ContentResponse::build()
veloman-yunkan Jun 26, 2022
eb0a45b
Undefaulted bool params of ContentResponse::build()
veloman-yunkan Jun 26, 2022
0ce36e6
Got rid of isHomePage in ContentResponse::build()
veloman-yunkan Jun 26, 2022
685e7f8
Unconditional blocking of external links
veloman-yunkan Jun 29, 2022
a674561
Dropped root link injection
veloman-yunkan Jun 29, 2022
6cc677b
Dropped ContentResponse::contentDecorationAllowed()
veloman-yunkan Jun 29, 2022
b81cb3a
Got rid of raw mode in response generation
veloman-yunkan Jul 24, 2022
369406f
Viewer settings
veloman-yunkan Aug 10, 2022
2be9ac3
Partly respecting the kiwix-serve --nosearchbar option
veloman-yunkan Jul 10, 2022
da23e4e
Revert "Partly respecting the kiwix-serve --nosearchbar option"
veloman-yunkan Sep 4, 2022
ae01790
Introduced setupViewer()
veloman-yunkan Sep 4, 2022
796e729
Library button is disabled by setupViewer()
veloman-yunkan Sep 4, 2022
4e06bb6
Partly fixed auto-hiding of the toolbar
veloman-yunkan Sep 4, 2022
cac2d21
Respecting the --nosearchbar option of kiwix-serve
veloman-yunkan Sep 4, 2022
defa387
Fix cacheids after a rebase
veloman-yunkan Sep 21, 2022
fa67b45
Got rid of unused *pendToFirstOccurence() funcs
veloman-yunkan Sep 21, 2022
0994a8f
Dropped taskbarless test server
veloman-yunkan Sep 21, 2022
0a0f52f
Testing of the viewer settings endpoint
veloman-yunkan Sep 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,12 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
if (url == "/" )
return build_homepage(request);

if (isEndpointUrl(url, "skin"))
if (isEndpointUrl(url, "viewer") || isEndpointUrl(url, "skin"))
return handle_skin(request);

if (url == "/viewer_settings.js")
return handle_viewer_settings(request);

if (isEndpointUrl(url, "content"))
return handle_content(request);

Expand Down Expand Up @@ -623,7 +626,7 @@ InternalServer::get_matching_if_none_match_etag(const RequestContext& r) const

std::unique_ptr<Response> InternalServer::build_homepage(const RequestContext& request)
{
return ContentResponse::build(*this, m_indexTemplateString, get_default_data(), "text/html; charset=utf-8", true);
return ContentResponse::build(*this, m_indexTemplateString, get_default_data(), "text/html; charset=utf-8");
}

/**
Expand Down Expand Up @@ -653,8 +656,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r

if (archive == nullptr) {
return HTTP404Response(*this, request)
+ noSuchBookErrorMsg(bookName)
+ TaskbarInfo(bookName);
+ noSuchBookErrorMsg(bookName);
}

const auto queryString = request.get_optional_param("term", std::string());
Expand Down Expand Up @@ -714,13 +716,30 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
return std::move(response);
}

std::unique_ptr<Response> InternalServer::handle_viewer_settings(const RequestContext& request)
{
if (m_verbose.load()) {
printf("** running handle_viewer_settings\n");
}

const kainjow::mustache::object data{
{"enable_toolbar", m_withTaskbar ? "true" : "false" },
{"enable_link_blocking", m_blockExternalLinks ? "true" : "false" },
{"enable_library_button", m_withLibraryButton ? "true" : "false" }
};
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)
{
if (m_verbose.load()) {
printf("** running handle_skin\n");
}

auto resourceName = request.get_url().substr(1);
const bool isRequestForViewer = request.get_url() == "/viewer";
auto resourceName = isRequestForViewer
? "viewer.html"
: request.get_url().substr(1);
try {
auto response = ContentResponse::build(
*this,
Expand Down Expand Up @@ -777,11 +796,15 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
"404-page-heading",
cssUrl);
response += nonParameterizedMessage("no-search-results");
// XXX: Now this has to be handled by the iframe-based viewer which
// XXX: has to resolve if the book selection resulted in a single book.
/*
if(bookIds.size() == 1) {
auto bookId = *bookIds.begin();
auto bookName = mp_nameMapper->getNameForId(bookId);
response += TaskbarInfo(bookName, mp_library->getArchiveById(bookId).get());
}
*/
return response;
}

Expand Down Expand Up @@ -811,16 +834,18 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
renderer.setSearchProtocolPrefix(m_root + "/search");
renderer.setPageLength(pageLength);
if (request.get_requested_format() == "xml") {
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8",
/*isHomePage =*/false,
/*raw =*/true);
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8");
}
auto response = ContentResponse::build(*this, renderer.getHtml(), "text/html; charset=utf-8");
// XXX: Now this has to be handled by the iframe-based viewer which
// XXX: has to resolve if the book selection resulted in a single book.
/*
if(bookIds.size() == 1) {
auto bookId = *bookIds.begin();
auto bookName = mp_nameMapper->getNameForId(bookId);
response->set_taskbar(bookName, mp_library->getArchiveById(bookId).get());
}
*/
return std::move(response);
} catch (const Error& e) {
return HTTP400Response(*this, request)
Expand Down Expand Up @@ -852,17 +877,15 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re

if (archive == nullptr) {
return HTTP404Response(*this, request)
+ noSuchBookErrorMsg(bookName)
+ TaskbarInfo(bookName);
+ noSuchBookErrorMsg(bookName);
}

try {
auto entry = archive->getRandomEntry();
return build_redirect(bookName, getFinalItem(*archive, entry));
} catch(zim::EntryNotFound& e) {
return HTTP404Response(*this, request)
+ nonParameterizedMessage("random-article-failure")
+ TaskbarInfo(bookName, archive.get());
+ nonParameterizedMessage("random-article-failure");
}
}

Expand Down Expand Up @@ -1010,8 +1033,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
return HTTP404Response(*this, request)
+ urlNotFoundMsg
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern))
+ TaskbarInfo(bookName);
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
}

auto urlStr = url.substr(prefixLength + bookName.size());
Expand All @@ -1027,9 +1049,6 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
return build_redirect(bookName, getFinalItem(*archive, entry));
}
auto response = ItemResponse::build(*this, request, entry.getItem());
try {
dynamic_cast<ContentResponse&>(*response).set_taskbar(bookName, archive.get());
} catch (std::bad_cast& e) {}

if (m_verbose.load()) {
printf("Found %s\n", entry.getPath().c_str());
Expand All @@ -1044,8 +1063,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
return HTTP404Response(*this, request)
+ urlNotFoundMsg
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern))
+ TaskbarInfo(bookName, archive.get());
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
}
}

Expand Down Expand Up @@ -1093,13 +1111,13 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
try {
if (kind == "meta") {
auto item = archive->getMetadataItem(itemPath);
return ItemResponse::build(*this, request, item, /*raw=*/true);
return ItemResponse::build(*this, request, item);
} else {
auto entry = archive->getEntryByPath(itemPath);
if (entry.isRedirect()) {
return build_redirect(bookName, entry.getItem(true));
}
return ItemResponse::build(*this, request, entry.getItem(), /*raw=*/true);
return ItemResponse::build(*this, request, entry.getItem());
}
} catch (zim::EntryNotFound& e ) {
if (m_verbose.load()) {
Expand Down Expand Up @@ -1136,9 +1154,7 @@ std::unique_ptr<Response> InternalServer::handle_locally_customized_resource(con

return ContentResponse::build(*this,
resourceData,
crd.mimeType,
/*isHomePage=*/false,
/*raw=*/true);
crd.mimeType);
}

}
5 changes: 3 additions & 2 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class InternalServer {
std::unique_ptr<Response> handle_request(const RequestContext& request);
std::unique_ptr<Response> build_redirect(const std::string& bookName, const zim::Item& item) const;
std::unique_ptr<Response> build_homepage(const RequestContext& request);
std::unique_ptr<Response> handle_viewer_settings(const RequestContext& request);
std::unique_ptr<Response> handle_skin(const RequestContext& request);
std::unique_ptr<Response> handle_catalog(const RequestContext& request);
std::unique_ptr<Response> handle_catalog_v2(const RequestContext& request);
Expand Down Expand Up @@ -183,8 +184,8 @@ class InternalServer {
std::unique_ptr<CustomizedResources> m_customizedResources;

friend std::unique_ptr<Response> Response::build(const InternalServer& server);
friend std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage, bool raw);
friend std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item, bool raw);
friend std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype);
friend std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item);
};

}
Expand Down
6 changes: 5 additions & 1 deletion src/server/internalServer_catalog_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_illustration(const R
auto book = mp_library->getBookByIdThreadSafe(bookId);
auto size = request.get_argument<unsigned int>("size");
auto illustration = book.getIllustration(size);
return ContentResponse::build(*this, illustration->getData(), illustration->mimeType);
return ContentResponse::build(
*this,
illustration->getData(),
illustration->mimeType
);
} catch(...) {
return HTTP404Response(*this, request)
+ urlNotFoundMsg;
Expand Down
Loading