Change to QHttpServer

This commit is contained in:
zneix 2021-07-22 19:46:40 +02:00
parent 00626a91aa
commit 86da2abb88
No known key found for this signature in database
GPG key ID: 911916E0523B22F6
5 changed files with 54 additions and 98 deletions

View file

@ -40,6 +40,7 @@ find_package(Qt5 REQUIRED
Multimedia Multimedia
Svg Svg
Concurrent Concurrent
HttpServer
) )
if (WIN32) if (WIN32)

View file

@ -20,7 +20,7 @@ MINIMUM_REQUIRED_QT_VERSION = 5.12.0
error("You're trying to compile with Qt $$QT_VERSION, but minimum required Qt version is $$MINIMUM_REQUIRED_QT_VERSION") error("You're trying to compile with Qt $$QT_VERSION, but minimum required Qt version is $$MINIMUM_REQUIRED_QT_VERSION")
} }
QT += widgets core gui network multimedia svg concurrent QT += widgets core gui network multimedia svg concurrent httpserver
CONFIG += communi CONFIG += communi
COMMUNI += core model util COMMUNI += core model util

View file

@ -483,6 +483,7 @@ target_link_libraries(${LIBRARY_PROJECT}
Qt5::Multimedia Qt5::Multimedia
Qt5::Svg Qt5::Svg
Qt5::Concurrent Qt5::Concurrent
Qt5::HttpServer
LibCommuni::LibCommuni LibCommuni::LibCommuni
qt5keychain qt5keychain

View file

@ -17,6 +17,7 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QMessageBox> #include <QMessageBox>
#include <QUrl> #include <QUrl>
#include <QtHttpServer/QHttpServer>
#include <pajlada/settings/setting.hpp> #include <pajlada/settings/setting.hpp>
namespace chatterino { namespace chatterino {
@ -79,66 +80,27 @@ namespace {
} // namespace } // namespace
LoginServer::LoginServer(QObject *parent)
: QTcpServer(parent)
{
server_ = new QTcpServer(this);
connect(server_, &QTcpServer::newConnection, this,
&LoginServer::slotNewConnection);
}
QTcpServer *LoginServer::getServer()
{
return this->server_;
}
void LoginServer::slotNewConnection()
{
qDebug() << "HONDETEDTED!!!";
socket_ = server_->nextPendingConnection();
connect(socket_, &QTcpSocket::readyRead, this,
&LoginServer::slotServerRead);
connect(socket_, &QTcpSocket::disconnected, this,
&LoginServer::slotClientDisconnected);
}
void LoginServer::slotServerRead()
{
qDebug() << "reading data...";
while (socket_->bytesAvailable() > 0)
{
QByteArray array = socket_->readAll();
qDebug() << array;
}
// Write data back
socket_->write(
"HTTP/1.1 200 OK\r\nServer: nginx/1.14.2\r\nDate: Wed, 21 Jul "
"2021 20:19:05 GMT\r\nContent-Type: text/plain\r\nContent-Length: "
"4\r\nConnection: close\r\n\r\nxd\r\n");
socket_->waitForBytesWritten(5000);
socket_->close();
// socket_->write("HTTP/1.1 204 No Content\r\n");
}
void LoginServer::slotBytesWritten()
{
qDebug() << "bytes written!";
}
void LoginServer::slotClientDisconnected()
{
qDebug() << "HEDISCONNECTED!";
socket_->close();
}
BasicLoginWidget::BasicLoginWidget() BasicLoginWidget::BasicLoginWidget()
{ {
// init tcp server // Initialize HTTP server and its routes
this->loginServer_ = new LoginServer(this); qCDebug(chatterinoWidget) << "Creating new HTTP server";
this->httpServer_ = new QHttpServer(this);
this->tcpServer_ = new QTcpServer(this->httpServer_);
this->httpServer_->bind(this->tcpServer_);
qCDebug(chatterinoWidget) << "Initializing HTTP server's routes";
this->httpServer_->route(
"/code", QHttpServerRequest::Method::GET,
[this](const QHttpServerRequest &req, QHttpServerResponder &&resp) {
qDebug() << "got credentials!";
resp.write("KKona", "text/plain",
QHttpServerResponder::StatusCode::Ok);
qDebug() << req.url().fragment();
qDebug() << req.url();
this->ui_.loginButton.setText("Logged in!");
this->ui_.loginButton.setEnabled(true);
});
const QString loginLink = "http://localhost:1234"; const QString loginLink = "http://localhost:1234";
this->setLayout(&this->ui_.layout); this->setLayout(&this->ui_.layout);
@ -163,17 +125,20 @@ BasicLoginWidget::BasicLoginWidget()
this->ui_.layout.addWidget(&this->ui_.unableToOpenBrowserHelper); this->ui_.layout.addWidget(&this->ui_.unableToOpenBrowserHelper);
connect(&this->ui_.loginButton, &QPushButton::clicked, [this, loginLink]() { connect(&this->ui_.loginButton, &QPushButton::clicked, [this, loginLink]() {
qDebug() << "penis"; // Start listening for credentials
qDebug() << this->tcpServer_->isListening();
// Initialize the server if (!this->tcpServer_->listen(serverAddress, serverPort))
if (!this->loginServer_->getServer()->listen(QHostAddress::LocalHost,
52107))
{ {
qDebug() << "failed to start server"; qCWarning(chatterinoWidget) << "Failed to start HTTP server";
return; }
else
{
qInfo(chatterinoWidget) << QString("HTTP server Listening on %1:%2")
.arg(serverAddress.toString())
.arg(serverPort);
this->ui_.loginButton.setText("Listening...");
this->ui_.loginButton.setDisabled(true);
} }
qDebug() << "listening!";
return;
// Open login page // Open login page
if (!QDesktopServices::openUrl(QUrl(loginLink))) if (!QDesktopServices::openUrl(QUrl(loginLink)))
@ -227,10 +192,17 @@ BasicLoginWidget::BasicLoginWidget()
}); });
} }
BasicLoginWidget::~BasicLoginWidget() void BasicLoginWidget::closeHttpServer()
{ {
qDebug() << "BasicLoinWidget was destroyed, closing connection"; // Revert login button
this->loginServer_->close(); this->ui_.loginButton.setText("Log in (Opens in browser)");
this->ui_.loginButton.setEnabled(true);
qCDebug(chatterinoWidget) << "Closing TCP servers bind to HTTP server";
for (const auto &server : this->httpServer_->servers())
{
server->close();
}
} }
AdvancedLoginWidget::AdvancedLoginWidget() AdvancedLoginWidget::AdvancedLoginWidget()
@ -344,8 +316,7 @@ LoginWidget::LoginWidget(QWidget *parent)
void LoginWidget::hideEvent(QHideEvent *event) void LoginWidget::hideEvent(QHideEvent *event)
{ {
// Make the port free // Make the port free
qDebug() << "closing server"; this->ui_.basic.closeHttpServer();
this->ui_.basic.loginServer_->getServer()->close();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -18,36 +18,14 @@
#include <QTcpSocket> #include <QTcpSocket>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtHttpServer/QHttpServer>
namespace chatterino { namespace chatterino {
class LoginServer : public QTcpServer
{
public:
static constexpr int chatterinoPort = 52107;
explicit LoginServer(QObject *parent = {});
QTcpServer *getServer();
// void incomingConnection(qintptr handle) override;
public slots:
void slotNewConnection();
void slotServerRead();
void slotBytesWritten();
void slotClientDisconnected();
//public slots:
// void newConnection();
private:
QTcpServer *server_;
QTcpSocket *socket_;
};
class BasicLoginWidget : public QWidget class BasicLoginWidget : public QWidget
{ {
public: public:
BasicLoginWidget(); BasicLoginWidget();
~BasicLoginWidget();
struct { struct {
QVBoxLayout layout; QVBoxLayout layout;
@ -57,9 +35,14 @@ public:
QLabel unableToOpenBrowserHelper; QLabel unableToOpenBrowserHelper;
} ui_; } ui_;
//private: void closeHttpServer();
private:
// Local server listening to login data // Local server listening to login data
LoginServer *loginServer_; const QHostAddress serverAddress{QHostAddress::LocalHost};
static const int serverPort = 52107;
QHttpServer *httpServer_;
QTcpServer *tcpServer_;
}; };
class AdvancedLoginWidget : public QWidget class AdvancedLoginWidget : public QWidget