Qt: Use a UA string for all HTTP requests

This commit is contained in:
Vicki Pfau 2022-11-05 03:11:33 -07:00
parent 91e62b8e72
commit f847502f4a
6 changed files with 33 additions and 15 deletions

View File

@ -8,16 +8,17 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "GBAApp.h"
using namespace QGBA;
AbstractUpdater::AbstractUpdater(QObject* parent)
: QObject(parent)
, m_netman(new QNetworkAccessManager(this))
{
}
void AbstractUpdater::checkUpdate() {
QNetworkReply* reply = m_netman->get(QNetworkRequest(manifestLocation()));
QNetworkReply* reply = GBAApp::app()->httpGet(manifestLocation());
chaseRedirects(reply, &AbstractUpdater::manifestDownloaded);
}
@ -36,7 +37,7 @@ void AbstractUpdater::downloadUpdate() {
return;
}
m_isUpdating = true;
QNetworkReply* reply = m_netman->get(QNetworkRequest(url));
QNetworkReply* reply = GBAApp::app()->httpGet(url);
chaseRedirects(reply, &AbstractUpdater::updateDownloaded);
}
@ -54,7 +55,7 @@ void AbstractUpdater::chaseRedirects(QNetworkReply* reply, void (AbstractUpdater
connect(reply, &QNetworkReply::finished, this, [this, reply, cb]() {
// TODO: check domains, etc
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() / 100 == 3) {
QNetworkReply* newReply = m_netman->get(QNetworkRequest(reply->header(QNetworkRequest::LocationHeader).toString()));
QNetworkReply* newReply = GBAApp::app()->httpGet(reply->header(QNetworkRequest::LocationHeader).toString());
chaseRedirects(newReply, cb);
} else {
(this->*cb)(reply);
@ -69,7 +70,7 @@ void AbstractUpdater::manifestDownloaded(QNetworkReply* reply) {
if (!url.isValid()) {
emit updateDone(false);
} else {
QNetworkReply* reply = m_netman->get(QNetworkRequest(url));
QNetworkReply* reply = GBAApp::app()->httpGet(url);
chaseRedirects(reply, &AbstractUpdater::updateDownloaded);
}
} else {

View File

@ -9,7 +9,6 @@
#include <QFile>
#include <QObject>
class QNetworkAccessManager;
class QNetworkReply;
namespace QGBA {
@ -44,7 +43,6 @@ private:
void updateDownloaded(QNetworkReply*);
bool m_isUpdating = false;
QNetworkAccessManager* m_netman;
QByteArray m_manifest;
};

View File

@ -11,6 +11,7 @@
#include <QNetworkReply>
#include "ConfigController.h"
#include "GBAApp.h"
#include "VFileDevice.h"
#include <mgba/core/version.h>
@ -29,10 +30,8 @@ const char* SUFFIX = "";
ForwarderController::ForwarderController(QObject* parent)
: QObject(parent)
, m_netman(new QNetworkAccessManager(this))
, m_originalPath(qgetenv("PATH"))
{
m_netman->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
connect(this, &ForwarderController::buildFailed, this, &ForwarderController::cleanup);
connect(this, &ForwarderController::buildComplete, this, &ForwarderController::cleanup);
}
@ -83,7 +82,7 @@ void ForwarderController::downloadForwarderKit() {
emit buildFailed();
return;
#endif
QNetworkReply* reply = m_netman->get(QNetworkRequest(QUrl(fkUrl)));
QNetworkReply* reply = GBAApp::app()->httpGet(QUrl(fkUrl));
connectReply(reply, FORWARDER_KIT, &ForwarderController::gotForwarderKit);
}
@ -132,7 +131,7 @@ void ForwarderController::gotForwarderKit(QNetworkReply* reply) {
}
void ForwarderController::downloadManifest() {
QNetworkReply* reply = m_netman->get(QNetworkRequest(QUrl("https://mgba.io/latest.ini")));
QNetworkReply* reply = GBAApp::app()->httpGet(QUrl("https://mgba.io/latest.ini"));
connectReply(reply, MANIFEST, &ForwarderController::gotManifest);
}
@ -173,7 +172,7 @@ void ForwarderController::downloadBuild(const QUrl& url) {
emit buildFailed();
return;
}
QNetworkReply* reply = m_netman->get(QNetworkRequest(url));
QNetworkReply* reply = GBAApp::app()->httpGet(url);
connectReply(reply, BASE, &ForwarderController::gotBuild);
connect(reply, &QNetworkReply::readyRead, this, [this, reply]() {

View File

@ -12,7 +12,6 @@
#include <memory>
class QNetworkAccessManager;
class QNetworkReply;
namespace QGBA {
@ -61,7 +60,6 @@ private:
QString m_channel{"dev"};
QString m_outFilename;
QNetworkAccessManager* m_netman;
std::unique_ptr<ForwarderGenerator> m_generator;
QFile m_sourceFile;
bool m_inProgress = false;

View File

@ -19,6 +19,7 @@
#include <QFontDatabase>
#include <QIcon>
#include <mgba/core/version.h>
#include <mgba/feature/updater.h>
#include <mgba-util/socket.h>
#include <mgba-util/vfs.h>
@ -81,6 +82,8 @@ GBAApp::GBAApp(int& argc, char* argv[], ConfigController* config)
m_configController->updateOption("useDiscordPresence");
#endif
m_netman.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
cleanupAfterUpdate();
connect(this, &GBAApp::aboutToQuit, this, &GBAApp::cleanup);
@ -240,6 +243,19 @@ bool GBAApp::reloadGameDB() {
}
#endif
QNetworkAccessManager* GBAApp::netman() {
return &m_netman;
}
QNetworkReply* GBAApp::httpGet(const QUrl& url) {
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::UserAgentHeader,
QString("%1/%2 (+https://mgba.io) is definitely not Mozilla/5.0")
.arg(projectName)
.arg(projectVersion));
return m_netman.get(req);
}
qint64 GBAApp::submitWorkerJob(std::function<void ()> job, std::function<void ()> callback) {
return submitWorkerJob(job, nullptr, callback);
}

View File

@ -11,7 +11,8 @@
#include <QList>
#include <QMap>
#include <QMultiMap>
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QRunnable>
#include <QString>
#include <QThreadPool>
@ -70,6 +71,9 @@ public:
const NoIntroDB* gameDB() const { return m_db; }
bool reloadGameDB();
QNetworkAccessManager* netman();
QNetworkReply* httpGet(const QUrl&);
qint64 submitWorkerJob(std::function<void ()> job, std::function<void ()> callback = {});
qint64 submitWorkerJob(std::function<void ()> job, QObject* context, std::function<void ()> callback);
bool removeWorkerJob(qint64 jobId);
@ -128,6 +132,8 @@ private:
QFont m_monospace;
NoIntroDB* m_db = nullptr;
QNetworkAccessManager m_netman;
};
}