From e9d413c7dc75443784f03be17f99b36d6093c8cd Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sat, 8 Mar 2025 21:26:47 +0100 Subject: [PATCH] QTcpServer: Add success and port --- src/core/localredirectserver.cpp | 23 +++++++++++++++-------- src/core/localredirectserver.h | 14 +++++++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/core/localredirectserver.cpp b/src/core/localredirectserver.cpp index 999b12910..333c977b2 100644 --- a/src/core/localredirectserver.cpp +++ b/src/core/localredirectserver.cpp @@ -1,8 +1,7 @@ /* - * This file was part of Clementine. + * Strawberry Music Player * Copyright 2012, 2014, John Maguire - * Copyright 2014, Krzysztof Sobiecki - * Copyright 2018-2021, Jonas Kvinge + * Copyright 2018-2025, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * 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) : QTcpServer(parent), port_(0), - socket_(nullptr) {} + socket_(nullptr), + success_(false) {} LocalRedirectServer::~LocalRedirectServer() { if (isListening()) close(); @@ -53,6 +53,7 @@ LocalRedirectServer::~LocalRedirectServer() { bool LocalRedirectServer::Listen() { if (!listen(QHostAddress::LocalHost, port_)) { + success_ = false; error_ = errorString(); return false; } @@ -61,6 +62,7 @@ bool LocalRedirectServer::Listen() { url_.setHost(u"localhost"_s); url_.setPort(serverPort()); url_.setPath(u"/"_s); + port_ = serverPort(); QObject::connect(this, &QTcpServer::newConnection, this, &LocalRedirectServer::NewConnection); return true; @@ -77,7 +79,7 @@ void LocalRedirectServer::NewConnection() { void LocalRedirectServer::incomingConnection(qintptr socket_descriptor) { - if (socket_) { + if (socket_ != nullptr) { if (socket_->state() == QAbstractSocket::ConnectedState) socket_->close(); socket_->deleteLater(); socket_ = nullptr; @@ -88,6 +90,7 @@ void LocalRedirectServer::incomingConnection(qintptr socket_descriptor) { if (!tcp_socket->setSocketDescriptor(socket_descriptor)) { delete tcp_socket; close(); + success_ = false; error_ = "Unable to set socket descriptor"_L1; Q_EMIT Finished(); return; @@ -115,6 +118,10 @@ void LocalRedirectServer::ReadyRead() { socket_->deleteLater(); socket_ = nullptr; request_url_ = ParseUrlFromRequest(buffer_); + success_ = request_url_.isValid(); + if (!request_url_.isValid()) { + error_ = "Invalid request URL"_L1; + } close(); Q_EMIT Finished(); } @@ -169,9 +176,9 @@ QUrl LocalRedirectServer::ParseUrlFromRequest(const QByteArray &request) const { const QByteArrayList lines = request.split('\r'); const QByteArray &request_line = lines[0]; - QByteArray path = request_line.split(' ')[1]; - QUrl base_url = url_; - QUrl request_url(base_url.toString() + QString::fromLatin1(path.mid(1)), QUrl::StrictMode); + const QByteArray path = request_line.split(' ')[1]; + const QUrl base_url = url_; + const QUrl request_url(base_url.toString() + QString::fromLatin1(path.mid(1)), QUrl::StrictMode); return request_url; diff --git a/src/core/localredirectserver.h b/src/core/localredirectserver.h index 80a841607..840fe429d 100644 --- a/src/core/localredirectserver.h +++ b/src/core/localredirectserver.h @@ -1,8 +1,7 @@ /* - * This file was part of Clementine. + * Strawberry Music Player * Copyright 2012, 2014, John Maguire - * Copyright 2014, Krzysztof Sobiecki - * Copyright 2018-2021, Jonas Kvinge + * Copyright 2018-2025, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * 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); ~LocalRedirectServer() override; - void set_port(const int port) { port_ = port; } - bool Listen(); const QUrl &url() const { return url_; } const QUrl &request_url() const { return request_url_; } + bool success() const { return success_; } const QString &error() const { return error_; } + int port() const { return port_; } + void set_port(const int port) { port_ = port; } + + bool Listen(); + Q_SIGNALS: void Finished(); @@ -66,6 +69,7 @@ class LocalRedirectServer : public QTcpServer { QUrl request_url_; QAbstractSocket *socket_; QByteArray buffer_; + bool success_; QString error_; };