aboutsummaryrefslogtreecommitdiff
path: root/src/webclient.cpp
diff options
context:
space:
mode:
authorFelix (xq) Queißner <git@mq32.de>2020-06-07 01:06:07 +0200
committerFelix (xq) Queißner <git@mq32.de>2020-06-07 01:06:07 +0200
commit093bfcc50d5889358ed806096ac5652a9e925cfc (patch)
treef0276f86cf9b14309851b9d3136c370503ecea64 /src/webclient.cpp
parentd4d353dab0f7c2fe2e1d76f6666f848e077d07dd (diff)
downloadkristall-093bfcc50d5889358ed806096ac5652a9e925cfc.tar.gz
Implements multi-protocol support. Adds support for HTTP/HTTPS, adds settings to enable/disable protocols
Diffstat (limited to 'src/webclient.cpp')
-rw-r--r--src/webclient.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/webclient.cpp b/src/webclient.cpp
new file mode 100644
index 0000000..6e9b5ab
--- /dev/null
+++ b/src/webclient.cpp
@@ -0,0 +1,78 @@
+#include "webclient.hpp"
+
+#include <QNetworkRequest>
+#include <QNetworkReply>
+
+WebClient::WebClient(QObject *parent) :
+ QObject(parent),
+ current_reply(nullptr)
+{
+ manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
+}
+
+WebClient::~WebClient()
+{
+
+}
+
+bool WebClient::startRequest(const QUrl &url)
+{
+ if(this->current_reply != nullptr)
+ return true;
+
+ this->body.clear();
+
+ QNetworkRequest request(url);
+ request.setMaximumRedirectsAllowed(5);
+ request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
+
+ this->current_reply = manager.get(request);
+ if(this->current_reply == nullptr)
+ return false;
+
+ connect(this->current_reply, &QNetworkReply::readyRead, this, &WebClient::on_data);
+ connect(this->current_reply, &QNetworkReply::finished, this, &WebClient::on_finished);
+
+ return true;
+}
+
+bool WebClient::isInProgress() const
+{
+ return (this->current_reply != nullptr);
+}
+
+bool WebClient::cancelRequest()
+{
+ if(this->current_reply != nullptr)
+ {
+ this->current_reply->abort();
+ this->current_reply = nullptr;
+ }
+ this->body.clear();
+ return true;
+}
+
+void WebClient::on_data()
+{
+ this->body.append(this->current_reply->readAll());
+}
+
+void WebClient::on_finished()
+{
+ if(this->current_reply->error() != QNetworkReply::NoError)
+ {
+ emit this->requestFailed(this->current_reply->errorString());
+ }
+ else
+ {
+ auto mime = this->current_reply->header(QNetworkRequest::ContentTypeHeader).toString();
+
+ qDebug() << this->current_reply->url() << mime;
+
+ emit this->requestComplete(this->body, mime);
+
+ this->body.clear();
+ }
+ this->current_reply->deleteLater();
+ this->current_reply = nullptr;
+}