From 34c7b48337a3ea3aa8d14157e318c01cbed08414 Mon Sep 17 00:00:00 2001 From: mkiol Date: Tue, 10 May 2022 18:06:42 +0200 Subject: [PATCH] proxy for library.kiwix.org --- qml/FilesPage.qml | 10 +- qml/WebViewPage.qml | 4 +- scripts/library-kiwix-org.html | 527 +++++++++++++++++++++++++++++++++ src/utils.cpp | 8 +- src/utils.h | 2 + src/zimserver.cpp | 27 +- src/zimserver.h | 6 +- zimpedia.pro | 3 +- 8 files changed, 575 insertions(+), 12 deletions(-) create mode 100644 scripts/library-kiwix-org.html diff --git a/qml/FilesPage.qml b/qml/FilesPage.qml index 5ab7a03..fd2a8cc 100644 --- a/qml/FilesPage.qml +++ b/qml/FilesPage.qml @@ -42,7 +42,10 @@ Dialog { MenuItem { text: qsTr("Open %1").arg("library.kiwix.org") - onClicked: Qt.openUrlExternally("https://library.kiwix.org") + onClicked: { + // Qt.openUrlExternally("https://library.kiwix.org") + Qt.openUrlExternally(zimServer.libraryLocalUrl()) + } } MenuItem { enabled: !fileModel.busy @@ -89,7 +92,10 @@ Dialog { anchors.bottom: parent.bottom anchors.bottomMargin: Theme.iconSizeMedium text: qsTr("Open %1").arg("library.kiwix.org") - onClicked: Qt.openUrlExternally("https://library.kiwix.org") + onClicked: { + // Qt.openUrlExternally("https://library.kiwix.org") + Qt.openUrlExternally(zimServer.libraryLocalUrl()) + } } } diff --git a/qml/WebViewPage.qml b/qml/WebViewPage.qml index f4176e4..3a22683 100644 --- a/qml/WebViewPage.qml +++ b/qml/WebViewPage.qml @@ -45,10 +45,10 @@ WebViewPage { utils.readAsset("scripts/night_mode.js") + "\n" + utils.readAsset("scripts/zoom.js") + "\n" + "var res = {night_mode: false,zoom: false}\n" + - //"try {\n" + + "try {\n" + "res.night_mode = _night_mode_init()\n" + "res.zoom = _zoom_init()\n" + - //"} catch {}\n" + + "} catch {}\n" + "return res\n" view.runJavaScript(script, function(res) { console.log("js init done:", JSON.stringify(res)) diff --git a/scripts/library-kiwix-org.html b/scripts/library-kiwix-org.html new file mode 100644 index 0000000..4ece1b0 --- /dev/null +++ b/scripts/library-kiwix-org.html @@ -0,0 +1,527 @@ + + + + + + Welcome to Kiwix Server + + + + + + + + + + + + + +
+
+
+ +
+
+ +
+
+
+ + +
+
+
+

+
+
+
+
+
Powered by Kiwix
+ + + diff --git a/src/utils.cpp b/src/utils.cpp index d6e82af..7f13fbe 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -19,7 +19,7 @@ Utils::Utils(QObject *parent) : QObject{parent} { createCacheDir(); } -QString Utils::readAsset(const QString &path) const { +QByteArray Utils::readAssetStatic(const QString &path) { QFile file{SailfishApp::pathTo(path).toLocalFile()}; if (!file.open(QIODevice::ReadOnly)) { @@ -27,7 +27,11 @@ QString Utils::readAsset(const QString &path) const { return {}; } - return QString::fromUtf8(file.readAll()); + return file.readAll(); +} + +QString Utils::readAsset(const QString &path) const { + return QString::fromUtf8(readAssetStatic(path)); } QString Utils::homeDir() const { diff --git a/src/utils.h b/src/utils.h index 88547d9..a07ca83 100644 --- a/src/utils.h +++ b/src/utils.h @@ -8,6 +8,7 @@ #ifndef UTILS_H #define UTILS_H +#include #include #include #include @@ -18,6 +19,7 @@ class Utils : public QObject { public: explicit Utils(QObject *parent = nullptr); static bool createCacheDir(); + static QByteArray readAssetStatic(const QString &path); Q_INVOKABLE QString readAsset(const QString &path) const; Q_INVOKABLE QString homeDir() const; Q_INVOKABLE void copyToClipboard(const QString &text) const; diff --git a/src/zimserver.cpp b/src/zimserver.cpp index 5f2b626..852a44a 100644 --- a/src/zimserver.cpp +++ b/src/zimserver.cpp @@ -33,6 +33,7 @@ #include "articlemodel.h" #include "filemodel.h" #include "settings.h" +#include "utils.h" ZimServer *ZimServer::m_instance = nullptr; @@ -46,7 +47,7 @@ ZimServer *ZimServer::instance(QObject *parent) { ZimServer::ZimServer(QObject *parent) : QThread{parent}, m_server{parent} { connect(&m_server, &QHttpServer::newRequest, this, - &ZimServer::requestHandler); + &ZimServer::handleHttpRequest); connect(FileModel::instance(), &FileModel::busyChanged, this, &ZimServer::handleFilemodelChanged, Qt::QueuedConnection); connect( @@ -375,9 +376,24 @@ std::pair ZimServer::parseUrl(const QUrl &url) { return result; } -void ZimServer::requestHandler(QHttpRequest *req, QHttpResponse *resp) { +void ZimServer::handleLibraryRequest([[maybe_unused]] QHttpRequest *req, + QHttpResponse *resp) { + auto content = Utils::readAssetStatic("scripts/library-kiwix-org.html"); + resp->setHeader("Content-Length", QString::number(content.size())); + resp->setHeader("Content-Type", "text/html"); + resp->setHeader("Connection", "close"); + resp->writeHead(200); + resp->end(content); +} + +void ZimServer::handleHttpRequest(QHttpRequest *req, QHttpResponse *resp) { qDebug() << "http request:" << req->url().path(); + if (req->url().path() == "/library") { + handleLibraryRequest(req, resp); + return; + } + auto [uuid, path] = parseUrl(req->url()); if (path.isEmpty()) { @@ -435,6 +451,13 @@ void ZimServer::requestHandler(QHttpRequest *req, QHttpResponse *resp) { resp->end(art->content); } +QUrl ZimServer::libraryLocalUrl() const { + QUrl url{"http://localhost"}; + url.setPort(port); + url.setPath("/library"); + return url; +} + QUrl ZimServer::localUrl(const QString &path, const QString &uuid) { QUrl url{"http://localhost"}; url.setPort(port); diff --git a/src/zimserver.h b/src/zimserver.h index 5539212..4019797 100644 --- a/src/zimserver.h +++ b/src/zimserver.h @@ -67,6 +67,7 @@ class ZimServer : public QThread { Q_INVOKABLE void articleAsync(const QUrl &url); Q_INVOKABLE void openUrl(const QUrl &url, const QString &title); Q_INVOKABLE QString titleFromUrl(const QUrl &url) const; + Q_INVOKABLE QUrl libraryLocalUrl() const; QList search(QString phrase) const; signals: @@ -78,9 +79,6 @@ class ZimServer : public QThread { void urlReady(const QUrl &url, const QString &title); void busyChanged(); - private slots: - void requestHandler(QHttpRequest *req, QHttpResponse *resp); - private: enum class WorkType { none, article, loadZim }; static const int maxSearch = 20; // max size of search result @@ -130,6 +128,8 @@ class ZimServer : public QThread { void articleAsyncWork(); void loadZimAsyncWork(); QVariantList files() const; + void handleHttpRequest(QHttpRequest *req, QHttpResponse *resp); + void handleLibraryRequest(QHttpRequest *req, QHttpResponse *resp); }; #endif // ZIMSERVER_H diff --git a/zimpedia.pro b/zimpedia.pro index 4223205..1321107 100644 --- a/zimpedia.pro +++ b/zimpedia.pro @@ -45,7 +45,8 @@ include($${LIB_DIR}/qhttpserver/qhttpserver.pri) OTHER_FILES += \ $$files(qml/*.qml) \ - $$files(rpm/*) + $$files(rpm/*) \ + $$files(scripts/*) SOURCES += \ $${SRC_DIR}/main.cpp \