From 30d8beca2757159b5103be3f231cd3ef03d4f1bb Mon Sep 17 00:00:00 2001 From: "Felix (xq) Queißner" Date: Fri, 5 Jun 2020 00:00:33 +0200 Subject: Fixes a bug in gemini client: When remote host closes TLS session, the client closes the socket. --- geminiclient.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'geminiclient.cpp') diff --git a/geminiclient.cpp b/geminiclient.cpp index 57e1679..9b8f6bb 100644 --- a/geminiclient.cpp +++ b/geminiclient.cpp @@ -8,6 +8,12 @@ GeminiClient::GeminiClient(QObject *parent) : QObject(parent) connect(&socket, &QSslSocket::readyRead, this, &GeminiClient::socketReadyRead); connect(&socket, &QSslSocket::disconnected, this, &GeminiClient::socketDisconnected); connect(&socket, QOverload &>::of(&QSslSocket::sslErrors), this, &GeminiClient::sslErrors); + connect(&socket, QOverload::of(&QSslSocket::error), this, &GeminiClient::socketError); +} + +GeminiClient::~GeminiClient() +{ + is_receiving_body = false; } bool GeminiClient::startRequest(const QUrl &url) @@ -17,14 +23,17 @@ bool GeminiClient::startRequest(const QUrl &url) socket.connectToHostEncrypted(url.host(), url.port(1965)); - buffer.resize(0); - body.resize(0); + buffer.clear(); + body.clear(); is_receiving_body = false; if(not socket.isOpen()) return false; + socket.setReadBufferSize(1); + target_url = url; + mime_type = ""; return true; } @@ -36,7 +45,10 @@ bool GeminiClient::isInProgress() const bool GeminiClient::cancelRequest() { - socket.close(); + this->is_receiving_body = false; + this->socket.close(); + this->buffer.clear(); + this->body.clear(); return true; } @@ -127,7 +139,7 @@ void GeminiClient::socketReadyRead() case 2: // success is_receiving_body = true; - mime_type = QString(meta); + mime_type = meta; return; case 3: { // redirect @@ -223,8 +235,20 @@ void GeminiClient::socketDisconnected() void GeminiClient::sslErrors(const QList &errors) { for(auto const & error : errors) { - qDebug() << error.errorString() ; + qWarning() << error.errorString() ; } socket.ignoreSslErrors(errors); } + +void GeminiClient::socketError(QAbstractSocket::SocketError socketError) +{ + // When remote host closes TLS session, the client closes the socket. + // This is more sane then erroring out here as it's a perfectly legal + // state and we know the TLS connection has ended. + if(socketError == QAbstractSocket::RemoteHostClosedError) { + socket.close(); + } else { + qWarning() << socketError << socket.errorString(); + } +} -- cgit v1.2.3