2021-08-31 04:25:56 +05:30
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "Application.h"
|
2021-08-31 04:25:56 +05:30
|
|
|
#include "AuthRequest.h"
|
|
|
|
#include "katabasis/Globals.h"
|
2021-11-20 20:52:22 +05:30
|
|
|
|
2021-08-31 04:25:56 +05:30
|
|
|
AuthRequest::AuthRequest(QObject *parent): QObject(parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthRequest::~AuthRequest() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::get(const QNetworkRequest &req, int timeout/* = 60*1000*/) {
|
|
|
|
setup(req, QNetworkAccessManager::GetOperation);
|
2021-11-22 03:51:12 +05:30
|
|
|
reply_ = APPLICATION->network()->get(request_);
|
2021-08-31 04:25:56 +05:30
|
|
|
status_ = Requesting;
|
|
|
|
timedReplies_.add(new Katabasis::Reply(reply_, timeout));
|
2022-05-17 00:04:07 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply_, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(onRequestError(QNetworkReply::NetworkError)));
|
|
|
|
#else
|
2021-08-31 04:25:56 +05:30
|
|
|
connect(reply_, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onRequestError(QNetworkReply::NetworkError)));
|
2022-05-17 00:04:07 +05:30
|
|
|
#endif
|
2021-08-31 04:25:56 +05:30
|
|
|
connect(reply_, SIGNAL(finished()), this, SLOT(onRequestFinished()));
|
|
|
|
connect(reply_, &QNetworkReply::sslErrors, this, &AuthRequest::onSslErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::post(const QNetworkRequest &req, const QByteArray &data, int timeout/* = 60*1000*/) {
|
|
|
|
setup(req, QNetworkAccessManager::PostOperation);
|
|
|
|
data_ = data;
|
|
|
|
status_ = Requesting;
|
2021-11-22 03:51:12 +05:30
|
|
|
reply_ = APPLICATION->network()->post(request_, data_);
|
2021-08-31 04:25:56 +05:30
|
|
|
timedReplies_.add(new Katabasis::Reply(reply_, timeout));
|
2022-05-17 00:04:07 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply_, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(onRequestError(QNetworkReply::NetworkError)));
|
|
|
|
#else
|
2021-08-31 04:25:56 +05:30
|
|
|
connect(reply_, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onRequestError(QNetworkReply::NetworkError)));
|
2022-05-17 00:04:07 +05:30
|
|
|
#endif
|
2021-08-31 04:25:56 +05:30
|
|
|
connect(reply_, SIGNAL(finished()), this, SLOT(onRequestFinished()));
|
|
|
|
connect(reply_, &QNetworkReply::sslErrors, this, &AuthRequest::onSslErrors);
|
|
|
|
connect(reply_, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(onUploadProgress(qint64,qint64)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::onRequestFinished() {
|
|
|
|
if (status_ == Idle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply_ != qobject_cast<QNetworkReply *>(sender())) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 17:16:20 +05:30
|
|
|
httpStatus_ = reply_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2021-08-31 04:25:56 +05:30
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::onRequestError(QNetworkReply::NetworkError error) {
|
|
|
|
qWarning() << "AuthRequest::onRequestError: Error" << (int)error;
|
|
|
|
if (status_ == Idle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply_ != qobject_cast<QNetworkReply *>(sender())) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-04 05:48:05 +05:30
|
|
|
errorString_ = reply_->errorString();
|
|
|
|
httpStatus_ = reply_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2021-08-31 04:25:56 +05:30
|
|
|
error_ = error;
|
2021-12-04 05:48:05 +05:30
|
|
|
qWarning() << "AuthRequest::onRequestError: Error string: " << errorString_;
|
|
|
|
qWarning() << "AuthRequest::onRequestError: HTTP status" << httpStatus_ << reply_->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
|
2021-08-31 04:25:56 +05:30
|
|
|
|
|
|
|
// QTimer::singleShot(10, this, SLOT(finish()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::onSslErrors(QList<QSslError> errors) {
|
|
|
|
int i = 1;
|
|
|
|
for (auto error : errors) {
|
|
|
|
qCritical() << "LOGIN SSL Error #" << i << " : " << error.errorString();
|
|
|
|
auto cert = error.certificate();
|
|
|
|
qCritical() << "Certificate in question:\n" << cert.toText();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::onUploadProgress(qint64 uploaded, qint64 total) {
|
|
|
|
if (status_ == Idle) {
|
|
|
|
qWarning() << "AuthRequest::onUploadProgress: No pending request";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply_ != qobject_cast<QNetworkReply *>(sender())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Restart timeout because request in progress
|
|
|
|
Katabasis::Reply *o2Reply = timedReplies_.find(reply_);
|
|
|
|
if(o2Reply) {
|
|
|
|
o2Reply->start();
|
|
|
|
}
|
|
|
|
emit uploadProgress(uploaded, total);
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:52:22 +05:30
|
|
|
void AuthRequest::setup(const QNetworkRequest &req, QNetworkAccessManager::Operation operation, const QByteArray &verb) {
|
2021-08-31 04:25:56 +05:30
|
|
|
request_ = req;
|
|
|
|
operation_ = operation;
|
|
|
|
url_ = req.url();
|
|
|
|
|
|
|
|
QUrl url = url_;
|
|
|
|
request_.setUrl(url);
|
|
|
|
|
2021-11-20 20:52:22 +05:30
|
|
|
if (!verb.isEmpty()) {
|
|
|
|
request_.setRawHeader(Katabasis::HTTP_HTTP_HEADER, verb);
|
|
|
|
}
|
|
|
|
|
2021-08-31 04:25:56 +05:30
|
|
|
status_ = Requesting;
|
|
|
|
error_ = QNetworkReply::NoError;
|
2021-12-04 05:48:05 +05:30
|
|
|
errorString_.clear();
|
|
|
|
httpStatus_ = 0;
|
2021-08-31 04:25:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void AuthRequest::finish() {
|
|
|
|
QByteArray data;
|
|
|
|
if (status_ == Idle) {
|
|
|
|
qWarning() << "AuthRequest::finish: No pending request";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = reply_->readAll();
|
|
|
|
status_ = Idle;
|
|
|
|
timedReplies_.remove(reply_);
|
|
|
|
reply_->disconnect(this);
|
|
|
|
reply_->deleteLater();
|
|
|
|
QList<QNetworkReply::RawHeaderPair> headers = reply_->rawHeaderPairs();
|
|
|
|
emit finished(error_, data, headers);
|
|
|
|
}
|