diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-05 00:00:33 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-05 00:00:33 +0200 |
| commit | 30d8beca2757159b5103be3f231cd3ef03d4f1bb (patch) | |
| tree | bc6d36c68a472f63fcb4e49ec6a6d87592dab464 | |
| parent | 55e4bf4197d5992d05296bff3bb339da17ed0a39 (diff) | |
| download | kristall-30d8beca2757159b5103be3f231cd3ef03d4f1bb.tar.gz | |
Fixes a bug in gemini client: When remote host closes TLS session, the client closes the socket.
| -rw-r--r-- | geminiclient.cpp | 34 | ||||
| -rw-r--r-- | geminiclient.hpp | 5 |
2 files changed, 34 insertions, 5 deletions
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<const QList<QSslError> &>::of(&QSslSocket::sslErrors), this, &GeminiClient::sslErrors); + connect(&socket, QOverload<QAbstractSocket::SocketError>::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 = "<invalid>"; 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<QSslError> &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(); + } +} diff --git a/geminiclient.hpp b/geminiclient.hpp index 71256c0..590eb5b 100644 --- a/geminiclient.hpp +++ b/geminiclient.hpp @@ -36,6 +36,8 @@ private: public: explicit GeminiClient(QObject *parent = nullptr); + ~GeminiClient() override; + bool startRequest(QUrl const & url); bool isInProgress() const; @@ -71,6 +73,9 @@ private slots: void sslErrors(const QList<QSslError> &errors); + void socketError(QAbstractSocket::SocketError socketError); + + private: bool is_receiving_body; |
