mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Initial work for the server handling http request
This commit is contained in:
parent
ae9f92ded9
commit
00626a91aa
2 changed files with 121 additions and 6 deletions
|
@ -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 <a href=\"%1\">the "
|
||||
"log in link (%1)</a> - 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
|
||||
|
|
|
@ -14,15 +14,40 @@
|
|||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QTabWidget>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QVBoxLayout>
|
||||
#include <QtCore/QVariant>
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue