Initial work for the server handling http request

This commit is contained in:
zneix 2021-07-21 22:54:27 +02:00
parent ae9f92ded9
commit 00626a91aa
No known key found for this signature in database
GPG key ID: 911916E0523B22F6
2 changed files with 121 additions and 6 deletions

View file

@ -79,9 +79,68 @@ 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()
{ {
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->setLayout(&this->ui_.layout);
this->ui_.loginButton.setText("Log in (Opens in browser)"); this->ui_.loginButton.setText("Log in (Opens in browser)");
@ -94,7 +153,7 @@ BasicLoginWidget::BasicLoginWidget()
QString("An error occurred while attempting to open <a href=\"%1\">the " QString("An error occurred while attempting to open <a href=\"%1\">the "
"log in link (%1)</a> - open it manually in your browser and " "log in link (%1)</a> - open it manually in your browser and "
"proceed from there.") "proceed from there.")
.arg(logInLink)); .arg(loginLink));
this->ui_.unableToOpenBrowserHelper.setOpenExternalLinks(true); this->ui_.unableToOpenBrowserHelper.setOpenExternalLinks(true);
this->ui_.horizontalLayout.addWidget(&this->ui_.loginButton); this->ui_.horizontalLayout.addWidget(&this->ui_.loginButton);
@ -103,12 +162,24 @@ BasicLoginWidget::BasicLoginWidget()
this->ui_.layout.addLayout(&this->ui_.horizontalLayout); this->ui_.layout.addLayout(&this->ui_.horizontalLayout);
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]() {
qCDebug(chatterinoWidget) << "open login in browser"; qDebug() << "penis";
if (!QDesktopServices::openUrl(QUrl(logInLink)))
// 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(); this->ui_.unableToOpenBrowserHelper.show();
return;
} }
}); });
@ -156,6 +227,12 @@ BasicLoginWidget::BasicLoginWidget()
}); });
} }
BasicLoginWidget::~BasicLoginWidget()
{
qDebug() << "BasicLoinWidget was destroyed, closing connection";
this->loginServer_->close();
}
AdvancedLoginWidget::AdvancedLoginWidget() AdvancedLoginWidget::AdvancedLoginWidget()
{ {
this->setLayout(&this->ui_.layout); this->setLayout(&this->ui_.layout);
@ -264,4 +341,11 @@ LoginWidget::LoginWidget(QWidget *parent)
this->ui_.mainLayout.addWidget(&this->ui_.buttonBox); 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 } // namespace chatterino

View file

@ -14,15 +14,40 @@
#include <QLineEdit> #include <QLineEdit>
#include <QPushButton> #include <QPushButton>
#include <QTabWidget> #include <QTabWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QtCore/QVariant> #include <QtCore/QVariant>
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;
@ -31,6 +56,10 @@ public:
QPushButton pasteCodeButton; QPushButton pasteCodeButton;
QLabel unableToOpenBrowserHelper; QLabel unableToOpenBrowserHelper;
} ui_; } ui_;
//private:
// Local server listening to login data
LoginServer *loginServer_;
}; };
class AdvancedLoginWidget : public QWidget class AdvancedLoginWidget : public QWidget
@ -78,6 +107,8 @@ private:
AdvancedLoginWidget advanced; AdvancedLoginWidget advanced;
} ui_; } ui_;
void hideEvent(QHideEvent *e) override;
}; };
} // namespace chatterino } // namespace chatterino