aboutsummaryrefslogtreecommitdiff
path: root/src/webclient.cpp
diff options
context:
space:
mode:
authorFelix (xq) Queißner <git@mq32.de>2020-06-16 23:51:21 +0200
committerFelix (xq) Queißner <git@mq32.de>2020-06-16 23:51:21 +0200
commitbc3bd1d7524dffcf9669ac252db0a79b7484ee80 (patch)
tree50107aa8c7094afbeee328f55fa4a35788ac3083 /src/webclient.cpp
parent62005f23ba21f1246c1f7768e60f401b2083da90 (diff)
downloadkristall-bc3bd1d7524dffcf9669ac252db0a79b7484ee80.tar.gz
Streamlines HTTP(S) interface and routes the redirects through BrowserTab as well. Prepares for unified protocol handling.
Diffstat (limited to 'src/webclient.cpp')
-rw-r--r--src/webclient.cpp42
1 files changed, 31 insertions, 11 deletions
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;
+}