From 84d9b7e75daaa2b799387af7c8edebf780609e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 14 Jul 2023 15:24:34 +0200 Subject: [PATCH] HTTP user agent plumbing --- Common/Net/HTTPClient.cpp | 12 +++++++++++- Common/Net/HTTPClient.h | 9 +++++++++ Core/Config.cpp | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index e8d3b6c7f5..74d3784ab7 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -180,7 +180,7 @@ void Connection::Disconnect() { namespace http { // TODO: do something sane here -constexpr const char *DEFAULT_USERAGENT = "NATIVEAPP 1.0"; +constexpr const char *DEFAULT_USERAGENT = "PPSSPP"; Client::Client() { httpVersion_ = "1.1"; @@ -491,6 +491,10 @@ int Download::Perform(const std::string &url) { } http::Client client; + if (!userAgent_.empty()) { + client.SetUserAgent(userAgent_); + } + if (!client.Resolve(fileUrl.Host().c_str(), fileUrl.Port())) { ERROR_LOG(IO, "Failed resolving %s", url.c_str()); return -1; @@ -580,6 +584,8 @@ void Download::Do() { std::shared_ptr Downloader::StartDownload(const std::string &url, const Path &outfile, const char *acceptMime) { std::shared_ptr dl(new Download(RequestMethod::GET, url, "", "", outfile)); + if (!userAgent_.empty()) + dl->SetUserAgent(userAgent_); if (acceptMime) dl->SetAccept(acceptMime); newDownloads_.push_back(dl); @@ -593,6 +599,8 @@ std::shared_ptr Downloader::StartDownloadWithCallback( std::function callback, const char *acceptMime) { std::shared_ptr dl(new Download(RequestMethod::GET, url, "", "", outfile)); + if (!userAgent_.empty()) + dl->SetUserAgent(userAgent_); if (acceptMime) dl->SetAccept(acceptMime); dl->SetCallback(callback); @@ -607,6 +615,8 @@ std::shared_ptr Downloader::AsyncPostWithCallback( const std::string &postMime, std::function callback) { std::shared_ptr dl(new Download(RequestMethod::POST, url, postData, postMime, Path())); + if (!userAgent_.empty()) + dl->SetUserAgent(userAgent_); dl->SetCallback(callback); newDownloads_.push_back(dl); dl->Start(); diff --git a/Common/Net/HTTPClient.h b/Common/Net/HTTPClient.h index 5cf57d1bd0..245eec0ee8 100644 --- a/Common/Net/HTTPClient.h +++ b/Common/Net/HTTPClient.h @@ -156,6 +156,9 @@ public: // Downloader::GetCurrentProgress won't return it in the results. bool IsHidden() const { return hidden_; } void SetHidden(bool hidden) { hidden_ = hidden; } + void SetUserAgent(const std::string &userAgent) { + userAgent_ = userAgent; + } private: void Do(); // Actually does the download. Runs on thread. @@ -166,6 +169,7 @@ private: RequestProgress progress_; RequestMethod method_; std::string postData_; + std::string userAgent_; Buffer buffer_; std::vector responseHeaders_; std::string url_; @@ -209,6 +213,9 @@ public: void CancelAll(); void WaitForAll(); + void SetUserAgent(const std::string &userAgent) { + userAgent_ = userAgent; + } std::vector GetCurrentProgress(); @@ -221,6 +228,8 @@ private: // These get copied to downloads_ in Update(). It's so that callbacks can add new downloads // while running. std::vector> newDownloads_; + + std::string userAgent_; }; } // http diff --git a/Core/Config.cpp b/Core/Config.cpp index c6472d9e46..1c2cd2cace 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -1050,6 +1050,8 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) { bUpdatedInstanceCounter = true; } + g_DownloadManager.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + UpdateIniLocation(iniFileName, controllerIniFilename); INFO_LOG(LOADER, "Loading config: %s", iniFilename_.c_str());