QTcpServer: Add success and port
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* This file was part of Clementine.
|
* Strawberry Music Player
|
||||||
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
||||||
* Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
* Copyright 2018-2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
|
||||||
*
|
*
|
||||||
* Strawberry is free software: you can redistribute it and/or modify
|
* Strawberry is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -44,7 +43,8 @@ using namespace Qt::Literals::StringLiterals;
|
|||||||
LocalRedirectServer::LocalRedirectServer(QObject *parent)
|
LocalRedirectServer::LocalRedirectServer(QObject *parent)
|
||||||
: QTcpServer(parent),
|
: QTcpServer(parent),
|
||||||
port_(0),
|
port_(0),
|
||||||
socket_(nullptr) {}
|
socket_(nullptr),
|
||||||
|
success_(false) {}
|
||||||
|
|
||||||
LocalRedirectServer::~LocalRedirectServer() {
|
LocalRedirectServer::~LocalRedirectServer() {
|
||||||
if (isListening()) close();
|
if (isListening()) close();
|
||||||
@@ -53,6 +53,7 @@ LocalRedirectServer::~LocalRedirectServer() {
|
|||||||
bool LocalRedirectServer::Listen() {
|
bool LocalRedirectServer::Listen() {
|
||||||
|
|
||||||
if (!listen(QHostAddress::LocalHost, port_)) {
|
if (!listen(QHostAddress::LocalHost, port_)) {
|
||||||
|
success_ = false;
|
||||||
error_ = errorString();
|
error_ = errorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -61,6 +62,7 @@ bool LocalRedirectServer::Listen() {
|
|||||||
url_.setHost(u"localhost"_s);
|
url_.setHost(u"localhost"_s);
|
||||||
url_.setPort(serverPort());
|
url_.setPort(serverPort());
|
||||||
url_.setPath(u"/"_s);
|
url_.setPath(u"/"_s);
|
||||||
|
port_ = serverPort();
|
||||||
QObject::connect(this, &QTcpServer::newConnection, this, &LocalRedirectServer::NewConnection);
|
QObject::connect(this, &QTcpServer::newConnection, this, &LocalRedirectServer::NewConnection);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -77,7 +79,7 @@ void LocalRedirectServer::NewConnection() {
|
|||||||
|
|
||||||
void LocalRedirectServer::incomingConnection(qintptr socket_descriptor) {
|
void LocalRedirectServer::incomingConnection(qintptr socket_descriptor) {
|
||||||
|
|
||||||
if (socket_) {
|
if (socket_ != nullptr) {
|
||||||
if (socket_->state() == QAbstractSocket::ConnectedState) socket_->close();
|
if (socket_->state() == QAbstractSocket::ConnectedState) socket_->close();
|
||||||
socket_->deleteLater();
|
socket_->deleteLater();
|
||||||
socket_ = nullptr;
|
socket_ = nullptr;
|
||||||
@@ -88,6 +90,7 @@ void LocalRedirectServer::incomingConnection(qintptr socket_descriptor) {
|
|||||||
if (!tcp_socket->setSocketDescriptor(socket_descriptor)) {
|
if (!tcp_socket->setSocketDescriptor(socket_descriptor)) {
|
||||||
delete tcp_socket;
|
delete tcp_socket;
|
||||||
close();
|
close();
|
||||||
|
success_ = false;
|
||||||
error_ = "Unable to set socket descriptor"_L1;
|
error_ = "Unable to set socket descriptor"_L1;
|
||||||
Q_EMIT Finished();
|
Q_EMIT Finished();
|
||||||
return;
|
return;
|
||||||
@@ -115,6 +118,10 @@ void LocalRedirectServer::ReadyRead() {
|
|||||||
socket_->deleteLater();
|
socket_->deleteLater();
|
||||||
socket_ = nullptr;
|
socket_ = nullptr;
|
||||||
request_url_ = ParseUrlFromRequest(buffer_);
|
request_url_ = ParseUrlFromRequest(buffer_);
|
||||||
|
success_ = request_url_.isValid();
|
||||||
|
if (!request_url_.isValid()) {
|
||||||
|
error_ = "Invalid request URL"_L1;
|
||||||
|
}
|
||||||
close();
|
close();
|
||||||
Q_EMIT Finished();
|
Q_EMIT Finished();
|
||||||
}
|
}
|
||||||
@@ -169,9 +176,9 @@ QUrl LocalRedirectServer::ParseUrlFromRequest(const QByteArray &request) const {
|
|||||||
|
|
||||||
const QByteArrayList lines = request.split('\r');
|
const QByteArrayList lines = request.split('\r');
|
||||||
const QByteArray &request_line = lines[0];
|
const QByteArray &request_line = lines[0];
|
||||||
QByteArray path = request_line.split(' ')[1];
|
const QByteArray path = request_line.split(' ')[1];
|
||||||
QUrl base_url = url_;
|
const QUrl base_url = url_;
|
||||||
QUrl request_url(base_url.toString() + QString::fromLatin1(path.mid(1)), QUrl::StrictMode);
|
const QUrl request_url(base_url.toString() + QString::fromLatin1(path.mid(1)), QUrl::StrictMode);
|
||||||
|
|
||||||
return request_url;
|
return request_url;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* This file was part of Clementine.
|
* Strawberry Music Player
|
||||||
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
||||||
* Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
* Copyright 2018-2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
|
||||||
*
|
*
|
||||||
* Strawberry is free software: you can redistribute it and/or modify
|
* Strawberry is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -39,12 +38,16 @@ class LocalRedirectServer : public QTcpServer {
|
|||||||
explicit LocalRedirectServer(QObject *parent = nullptr);
|
explicit LocalRedirectServer(QObject *parent = nullptr);
|
||||||
~LocalRedirectServer() override;
|
~LocalRedirectServer() override;
|
||||||
|
|
||||||
void set_port(const int port) { port_ = port; }
|
|
||||||
bool Listen();
|
|
||||||
const QUrl &url() const { return url_; }
|
const QUrl &url() const { return url_; }
|
||||||
const QUrl &request_url() const { return request_url_; }
|
const QUrl &request_url() const { return request_url_; }
|
||||||
|
bool success() const { return success_; }
|
||||||
const QString &error() const { return error_; }
|
const QString &error() const { return error_; }
|
||||||
|
|
||||||
|
int port() const { return port_; }
|
||||||
|
void set_port(const int port) { port_ = port; }
|
||||||
|
|
||||||
|
bool Listen();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void Finished();
|
void Finished();
|
||||||
|
|
||||||
@@ -66,6 +69,7 @@ class LocalRedirectServer : public QTcpServer {
|
|||||||
QUrl request_url_;
|
QUrl request_url_;
|
||||||
QAbstractSocket *socket_;
|
QAbstractSocket *socket_;
|
||||||
QByteArray buffer_;
|
QByteArray buffer_;
|
||||||
|
bool success_;
|
||||||
QString error_;
|
QString error_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user