diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-16 23:51:21 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-16 23:51:21 +0200 |
| commit | bc3bd1d7524dffcf9669ac252db0a79b7484ee80 (patch) | |
| tree | 50107aa8c7094afbeee328f55fa4a35788ac3083 /src | |
| parent | 62005f23ba21f1246c1f7768e60f401b2083da90 (diff) | |
| download | kristall-bc3bd1d7524dffcf9669ac252db0a79b7484ee80.tar.gz | |
Streamlines HTTP(S) interface and routes the redirects through BrowserTab as well. Prepares for unified protocol handling.
Diffstat (limited to 'src')
| -rw-r--r-- | src/documentstyle.cpp | 2 | ||||
| -rw-r--r-- | src/webclient.cpp | 42 | ||||
| -rw-r--r-- | src/webclient.hpp | 1 |
3 files changed, 33 insertions, 12 deletions
diff --git a/src/documentstyle.cpp b/src/documentstyle.cpp index e8a4751..6608d5b 100644 --- a/src/documentstyle.cpp +++ b/src/documentstyle.cpp @@ -285,6 +285,6 @@ QString DocumentStyle::toStyleSheet() const css += QString("h2 { color: %2; %1 }\n").arg(encodeCssFont (h2_font)).arg(h2_color.name()); css += QString("h3 { color: %2; %1 }\n").arg(encodeCssFont (h3_font)).arg(h3_color.name()); - qDebug() << "CSS → " << css; + // qDebug() << "CSS → " << css; return css; } diff --git a/src/webclient.cpp b/src/webclient.cpp index e8a9959..4babb76 100644 --- a/src/webclient.cpp +++ b/src/webclient.cpp @@ -39,8 +39,8 @@ bool WebClient::startRequest(const QUrl &url) // ssl_config.setCaCertificates(QList<QSslCertificate> { }); QNetworkRequest request(url); - request.setMaximumRedirectsAllowed(5); - request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); + // request.setMaximumRedirectsAllowed(5); + request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, false); request.setSslConfiguration(ssl_config); this->current_reply = manager.get(request); @@ -50,6 +50,7 @@ bool WebClient::startRequest(const QUrl &url) connect(this->current_reply, &QNetworkReply::readyRead, this, &WebClient::on_data); connect(this->current_reply, &QNetworkReply::finished, this, &WebClient::on_finished); connect(this->current_reply, &QNetworkReply::sslErrors, this, &WebClient::on_sslErrors); + connect(this->current_reply, &QNetworkReply::redirected, this, &WebClient::on_redirected); return true; } @@ -78,10 +79,15 @@ void WebClient::on_data() void WebClient::on_finished() { - if(this->current_reply->error() != QNetworkReply::NoError) + auto * const reply = this->current_reply; + this->current_reply = nullptr; + + reply->deleteLater(); + + if(reply->error() != QNetworkReply::NoError) { NetworkError error = UnknownError; - switch(this->current_reply->error()) + switch(reply->error()) { case QNetworkReply::ConnectionRefusedError: error = ConnectionRefused; break; case QNetworkReply::RemoteHostClosedError: error = ProtocolViolation; break; @@ -99,23 +105,32 @@ void WebClient::on_finished() case QNetworkReply::OperationNotImplementedError: error = InternalServerError; break; case QNetworkReply::ServiceUnavailableError: error = InternalServerError; break; default: - qDebug() << "Unhandled server error:" << this->current_reply->error(); + qDebug() << "Unhandled server error:" << reply->error(); break; } - qDebug() << "web network error" << this->current_reply->errorString(); - emit this->networkError(error, this->current_reply->errorString()); + qDebug() << "web network error" << reply->errorString(); + emit this->networkError(error, reply->errorString()); } else { - auto mime = this->current_reply->header(QNetworkRequest::ContentTypeHeader).toString(); + int statusCode =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - emit this->requestComplete(this->body, mime); + if(statusCode >= 200 and statusCode < 300) { + auto mime = reply->header(QNetworkRequest::ContentTypeHeader).toString(); + emit this->requestComplete(this->body, mime); + } + else if(statusCode >= 300 and statusCode < 400) { + auto url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); + + emit this->redirected(url, (statusCode == 301) or (statusCode == 308)); + } + else { + emit networkError(UnknownError, QString("Unhandled HTTP status code %1").arg(statusCode)); + } this->body.clear(); } - this->current_reply->deleteLater(); - this->current_reply = nullptr; } void WebClient::on_sslErrors(const QList<QSslError> &errors) @@ -125,3 +140,8 @@ void WebClient::on_sslErrors(const QList<QSslError> &errors) qDebug() << err; this->current_reply->ignoreSslErrors(); } + +void WebClient::on_redirected(const QUrl &url) +{ + qDebug() << "redirected to" << url; +} diff --git a/src/webclient.hpp b/src/webclient.hpp index 2a5b80d..55439b9 100644 --- a/src/webclient.hpp +++ b/src/webclient.hpp @@ -28,6 +28,7 @@ private slots: void on_data(); void on_finished(); void on_sslErrors(const QList<QSslError> &errors); + void on_redirected(const QUrl &url); private: QNetworkAccessManager manager; |
