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

Use QHttpServer instead of cpp-httplib #1500

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 2 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ option(USE_BUNDLED_COEURL "Use a bundled version of the Curl wrapper"
option(USE_BUNDLED_LIBEVENT "Use the bundled version of libevent." ${HUNTER_ENABLED})
option(USE_BUNDLED_LIBCURL "Use the bundled version of libcurl." ${HUNTER_ENABLED})
option(USE_BUNDLED_RE2 "Use the bundled version of re2." ${HUNTER_ENABLED})
option(USE_BUNDLED_CPPHTTPLIB "Use the bundled version of cpp-httplib." ON)
option(USE_BUNDLED_BLURHASH "Use the bundled version of blurhash." ON)

include(CMakeDependentOption)
Expand Down Expand Up @@ -243,7 +242,7 @@ endif()
#
# Discover Qt dependencies.
#
find_package(Qt6 6.5 COMPONENTS Core Widgets Gui LinguistTools Svg Multimedia Qml QuickControls2 REQUIRED)
find_package(Qt6 6.5 COMPONENTS Core Widgets Gui LinguistTools Svg Multimedia Qml QuickControls2 HttpServer REQUIRED)
#find_package(Qt6QuickCompiler)
find_package(Qt6DBus)

Expand Down Expand Up @@ -834,14 +833,6 @@ endif()

target_include_directories(nheko PRIVATE src includes src/timeline/ src/ui/ src/encryption/ src/voip/)

if (USE_BUNDLED_CPPHTTPLIB)
target_include_directories(nheko PRIVATE third_party/cpp-httplib-0.5.12)
target_sources(nheko PRIVATE third_party/cpp-httplib-0.5.12/httplib.h)
else()
find_package(httplib REQUIRED)
target_link_libraries(nheko PRIVATE httplib::httplib)
endif()

if (USE_BUNDLED_BLURHASH)
target_include_directories(nheko PRIVATE third_party/blurhash)
set(BLURHASH_SRC_FILES
Expand Down Expand Up @@ -878,6 +869,7 @@ target_link_libraries(nheko PRIVATE
Qt::Multimedia
Qt::Qml
Qt::QuickControls2
Qt::HttpServer
qt6keychain
nlohmann_json::nlohmann_json
lmdbxx::lmdbxx
Expand Down
43 changes: 13 additions & 30 deletions src/SSOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,33 @@

#include "SSOHandler.h"

#include <QHttpServerResponse>
#include <QTimer>

#include <thread>

#include "Logging.h"

SSOHandler::SSOHandler(QObject *)
: server{new QHttpServer}
{
QTimer::singleShot(120000, this, &SSOHandler::ssoFailed);

using namespace httplib;

svr.set_logger([](const Request &req, const Response &res) {
nhlog::net()->info("req: {}, res: {}", req.path, res.status);
});

svr.Get("/sso", [this](const Request &req, Response &res) {
if (req.has_param("loginToken")) {
auto val = req.get_param_value("loginToken");
res.set_content("SSO success", "text/plain");
emit ssoSuccess(val);
server->route("/sso", [this](const QHttpServerRequest &req) {
if (req.query().hasQueryItem(QStringLiteral("loginToken"))) {
emit ssoSuccess(req.query().queryItemValue(QStringLiteral("loginToken")).toStdString());
return tr("SSO success");
} else {
res.set_content("Missing loginToken for SSO login!", "text/plain");
emit ssoFailed();
return tr("Missing loginToken for SSO login!");
}
});

std::thread t([this]() {
this->port = svr.bind_to_any_port("localhost");
svr.listen_after_bind();
});
t.detach();

while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
server->listen();
if (server->serverPorts().size() > 0)
this->port = server->serverPorts().first();
}

SSOHandler::~SSOHandler()
{
svr.stop();
while (svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// work around capturing a member of a deleted object
auto s = server;
QTimer::singleShot(1000, [s] { s->deleteLater(); });
}

std::string
Expand Down
6 changes: 4 additions & 2 deletions src/SSOHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "httplib.h"
// #include "httplib.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can delete this too :)


#include <QHttpServer>
#include <QObject>

#include <string>

class SSOHandler final : public QObject
Expand All @@ -23,6 +25,6 @@ class SSOHandler final : public QObject
void ssoFailed();

private:
httplib::Server svr;
QHttpServer *server;
int port = 0;
};
Loading