2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-08-30 23:55:56 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QBuffer>
|
2021-08-30 23:55:56 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
#include "Application.h"
|
2021-08-30 23:55:56 +01:00
|
|
|
#include "AuthRequest.h"
|
|
|
|
#include "katabasis/Globals.h"
|
2021-11-20 15:22:22 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
AuthRequest::AuthRequest(QObject* parent) : QObject(parent) {}
|
2021-08-30 23:55:56 +01:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
AuthRequest::~AuthRequest() {}
|
2021-08-30 23:55:56 +01:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::get(const QNetworkRequest& req, int timeout /* = 60*1000*/)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
setup(req, QNetworkAccessManager::GetOperation);
|
2021-11-21 22:21:12 +00:00
|
|
|
reply_ = APPLICATION->network()->get(request_);
|
2021-08-30 23:55:56 +01:00
|
|
|
status_ = Requesting;
|
|
|
|
timedReplies_.add(new Katabasis::Reply(reply_, timeout));
|
2023-08-14 17:16:53 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, &QNetworkReply::errorOccurred, this, &AuthRequest::onRequestError);
|
2023-08-14 17:16:53 +01:00
|
|
|
#else // &QNetworkReply::error SIGNAL depricated
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &AuthRequest::onRequestError);
|
2022-05-16 19:34:07 +01:00
|
|
|
#endif
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, &QNetworkReply::finished, this, &AuthRequest::onRequestFinished);
|
2021-08-30 23:55:56 +01:00
|
|
|
connect(reply_, &QNetworkReply::sslErrors, this, &AuthRequest::onSslErrors);
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::post(const QNetworkRequest& req, const QByteArray& data, int timeout /* = 60*1000*/)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
setup(req, QNetworkAccessManager::PostOperation);
|
|
|
|
data_ = data;
|
|
|
|
status_ = Requesting;
|
2021-11-21 22:21:12 +00:00
|
|
|
reply_ = APPLICATION->network()->post(request_, data_);
|
2021-08-30 23:55:56 +01:00
|
|
|
timedReplies_.add(new Katabasis::Reply(reply_, timeout));
|
2023-08-14 17:16:53 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, &QNetworkReply::errorOccurred, this, &AuthRequest::onRequestError);
|
2023-08-14 17:16:53 +01:00
|
|
|
#else // &QNetworkReply::error SIGNAL depricated
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &AuthRequest::onRequestError);
|
2022-05-16 19:34:07 +01:00
|
|
|
#endif
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, &QNetworkReply::finished, this, &AuthRequest::onRequestFinished);
|
2021-08-30 23:55:56 +01:00
|
|
|
connect(reply_, &QNetworkReply::sslErrors, this, &AuthRequest::onSslErrors);
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(reply_, &QNetworkReply::uploadProgress, this, &AuthRequest::onUploadProgress);
|
2021-08-30 23:55:56 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::onRequestFinished()
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
if (status_ == Idle) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (reply_ != qobject_cast<QNetworkReply*>(sender())) {
|
2021-08-30 23:55:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-16 11:46:20 +00:00
|
|
|
httpStatus_ = reply_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2021-08-30 23:55:56 +01:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::onRequestError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
qWarning() << "AuthRequest::onRequestError: Error" << (int)error;
|
|
|
|
if (status_ == Idle) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (reply_ != qobject_cast<QNetworkReply*>(sender())) {
|
2021-08-30 23:55:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-12-04 00:18:05 +00:00
|
|
|
errorString_ = reply_->errorString();
|
|
|
|
httpStatus_ = reply_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2021-08-30 23:55:56 +01:00
|
|
|
error_ = error;
|
2021-12-04 00:18:05 +00:00
|
|
|
qWarning() << "AuthRequest::onRequestError: Error string: " << errorString_;
|
2023-08-14 17:16:53 +01:00
|
|
|
qWarning() << "AuthRequest::onRequestError: HTTP status" << httpStatus_
|
|
|
|
<< reply_->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
|
2021-08-30 23:55:56 +01:00
|
|
|
|
|
|
|
// QTimer::singleShot(10, this, SLOT(finish()));
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::onSslErrors(QList<QSslError> errors)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::onUploadProgress(qint64 uploaded, qint64 total)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
if (status_ == Idle) {
|
|
|
|
qWarning() << "AuthRequest::onUploadProgress: No pending request";
|
|
|
|
return;
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (reply_ != qobject_cast<QNetworkReply*>(sender())) {
|
2021-08-30 23:55:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Restart timeout because request in progress
|
2023-08-14 17:16:53 +01:00
|
|
|
Katabasis::Reply* o2Reply = timedReplies_.find(reply_);
|
|
|
|
if (o2Reply) {
|
2021-08-30 23:55:56 +01:00
|
|
|
o2Reply->start();
|
|
|
|
}
|
|
|
|
emit uploadProgress(uploaded, total);
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::setup(const QNetworkRequest& req, QNetworkAccessManager::Operation operation, const QByteArray& verb)
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
request_ = req;
|
|
|
|
operation_ = operation;
|
|
|
|
url_ = req.url();
|
|
|
|
|
|
|
|
QUrl url = url_;
|
|
|
|
request_.setUrl(url);
|
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
if (!verb.isEmpty()) {
|
|
|
|
request_.setRawHeader(Katabasis::HTTP_HTTP_HEADER, verb);
|
|
|
|
}
|
|
|
|
|
2021-08-30 23:55:56 +01:00
|
|
|
status_ = Requesting;
|
|
|
|
error_ = QNetworkReply::NoError;
|
2021-12-04 00:18:05 +00:00
|
|
|
errorString_.clear();
|
|
|
|
httpStatus_ = 0;
|
2021-08-30 23:55:56 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void AuthRequest::finish()
|
|
|
|
{
|
2021-08-30 23:55:56 +01:00
|
|
|
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);
|
|
|
|
}
|