From 00626a91aa0651140c7c6ca0663dbdc1ab9e89bb Mon Sep 17 00:00:00 2001 From: zneix Date: Wed, 21 Jul 2021 22:54:27 +0200 Subject: [PATCH] Initial work for the server handling http request --- src/widgets/dialogs/LoginDialog.cpp | 96 +++++++++++++++++++++++++++-- src/widgets/dialogs/LoginDialog.hpp | 31 ++++++++++ 2 files changed, 121 insertions(+), 6 deletions(-) diff --git a/src/widgets/dialogs/LoginDialog.cpp b/src/widgets/dialogs/LoginDialog.cpp index b056f60cd..1fc9611dd 100644 --- a/src/widgets/dialogs/LoginDialog.cpp +++ b/src/widgets/dialogs/LoginDialog.cpp @@ -79,9 +79,68 @@ 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() { - const QString logInLink = "https://chatterino.com/client_login"; + // init tcp server + this->loginServer_ = new LoginServer(this); + + const QString loginLink = "http://localhost:1234"; this->setLayout(&this->ui_.layout); this->ui_.loginButton.setText("Log in (Opens in browser)"); @@ -94,7 +153,7 @@ BasicLoginWidget::BasicLoginWidget() QString("An error occurred while attempting to open the " "log in link (%1) - open it manually in your browser and " "proceed from there.") - .arg(logInLink)); + .arg(loginLink)); this->ui_.unableToOpenBrowserHelper.setOpenExternalLinks(true); this->ui_.horizontalLayout.addWidget(&this->ui_.loginButton); @@ -103,12 +162,24 @@ BasicLoginWidget::BasicLoginWidget() this->ui_.layout.addLayout(&this->ui_.horizontalLayout); this->ui_.layout.addWidget(&this->ui_.unableToOpenBrowserHelper); - connect(&this->ui_.loginButton, &QPushButton::clicked, [this, logInLink]() { - qCDebug(chatterinoWidget) << "open login in browser"; - if (!QDesktopServices::openUrl(QUrl(logInLink))) + connect(&this->ui_.loginButton, &QPushButton::clicked, [this, loginLink]() { + qDebug() << "penis"; + + // Initialize the server + if (!this->loginServer_->getServer()->listen(QHostAddress::LocalHost, + 52107)) + { + qDebug() << "failed to start server"; + return; + } + qDebug() << "listening!"; + return; + + // Open login page + if (!QDesktopServices::openUrl(QUrl(loginLink))) { - qCWarning(chatterinoWidget) << "open login in browser failed"; this->ui_.unableToOpenBrowserHelper.show(); + return; } }); @@ -156,6 +227,12 @@ BasicLoginWidget::BasicLoginWidget() }); } +BasicLoginWidget::~BasicLoginWidget() +{ + qDebug() << "BasicLoinWidget was destroyed, closing connection"; + this->loginServer_->close(); +} + AdvancedLoginWidget::AdvancedLoginWidget() { this->setLayout(&this->ui_.layout); @@ -264,4 +341,11 @@ LoginWidget::LoginWidget(QWidget *parent) this->ui_.mainLayout.addWidget(&this->ui_.buttonBox); } +void LoginWidget::hideEvent(QHideEvent *event) +{ + // Make the port free + qDebug() << "closing server"; + this->ui_.basic.loginServer_->getServer()->close(); +} + } // namespace chatterino diff --git a/src/widgets/dialogs/LoginDialog.hpp b/src/widgets/dialogs/LoginDialog.hpp index 154e063c9..11467f1de 100644 --- a/src/widgets/dialogs/LoginDialog.hpp +++ b/src/widgets/dialogs/LoginDialog.hpp @@ -14,15 +14,40 @@ #include #include #include +#include +#include #include #include 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 { public: BasicLoginWidget(); + ~BasicLoginWidget(); struct { QVBoxLayout layout; @@ -31,6 +56,10 @@ public: QPushButton pasteCodeButton; QLabel unableToOpenBrowserHelper; } ui_; + + //private: + // Local server listening to login data + LoginServer *loginServer_; }; class AdvancedLoginWidget : public QWidget @@ -78,6 +107,8 @@ private: AdvancedLoginWidget advanced; } ui_; + + void hideEvent(QHideEvent *e) override; }; } // namespace chatterino