From a35b21a5d710a182d90760d32dd62ee6f63bd1f2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 30 Apr 2021 23:12:42 -0700 Subject: [PATCH] http: Set a more useful user agent. --- Common/File/PathBrowser.cpp | 2 ++ Common/Net/HTTPClient.cpp | 6 +++--- Common/Net/HTTPClient.h | 6 +++++- Core/FileLoaders/HTTPFileLoader.cpp | 3 +++ Core/Reporting.cpp | 2 ++ Core/WebServer.cpp | 2 ++ UI/RemoteISOScreen.cpp | 2 ++ 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Common/File/PathBrowser.cpp b/Common/File/PathBrowser.cpp index 5b968621b1..5bd0c43e88 100644 --- a/Common/File/PathBrowser.cpp +++ b/Common/File/PathBrowser.cpp @@ -22,6 +22,8 @@ bool LoadRemoteFileList(const std::string &url, bool *cancel, std::vector responseHeaders; + http.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + Url baseURL(url); if (!baseURL.Valid()) { return false; diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index 8bec924ea2..d4f4eb8df7 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -181,11 +181,11 @@ void Connection::Disconnect() { namespace http { // TODO: do something sane here -#define USERAGENT "NATIVEAPP 1.0" +constexpr const char *DEFAULT_USERAGENT = "NATIVEAPP 1.0"; Client::Client() { httpVersion_ = "1.1"; - userAgent_ = USERAGENT; + userAgent_ = DEFAULT_USERAGENT; } Client::~Client() { @@ -323,7 +323,7 @@ int Client::SendRequestWithData(const char *method, const char *resource, const buffer.Printf(tpl, method, resource, httpVersion_, host_.c_str(), - userAgent_, + userAgent_.c_str(), otherHeaders ? otherHeaders : ""); buffer.Append(data); bool flushed = buffer.FlushSocket(sock(), dataTimeout_); diff --git a/Common/Net/HTTPClient.h b/Common/Net/HTTPClient.h index b23a6d8eeb..9281863f1b 100644 --- a/Common/Net/HTTPClient.h +++ b/Common/Net/HTTPClient.h @@ -69,8 +69,12 @@ public: dataTimeout_ = t; } + void SetUserAgent(const std::string &&value) { + userAgent_ = value; + } + protected: - const char *userAgent_; + std::string userAgent_; const char *httpVersion_; double dataTimeout_ = -1.0; }; diff --git a/Core/FileLoaders/HTTPFileLoader.cpp b/Core/FileLoaders/HTTPFileLoader.cpp index 9cf2d653b6..5aa692b0d6 100644 --- a/Core/FileLoaders/HTTPFileLoader.cpp +++ b/Core/FileLoaders/HTTPFileLoader.cpp @@ -20,6 +20,7 @@ #include "Common/Common.h" #include "Common/Log.h" #include "Common/StringUtils.h" +#include "Core/Config.h" #include "Core/FileLoaders/HTTPFileLoader.h" HTTPFileLoader::HTTPFileLoader(const std::string &filename) @@ -28,6 +29,8 @@ HTTPFileLoader::HTTPFileLoader(const std::string &filename) void HTTPFileLoader::Prepare() { std::call_once(preparedFlag_, [this](){ + client_.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + std::vector responseHeaders; Url resourceURL = url_; int redirectsLeft = 20; diff --git a/Core/Reporting.cpp b/Core/Reporting.cpp index 8ac6025855..3b7cb2d31d 100644 --- a/Core/Reporting.cpp +++ b/Core/Reporting.cpp @@ -263,6 +263,8 @@ namespace Reporting http::Client http; Buffer theVoid; + http.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + if (output == NULL) output = &theVoid; diff --git a/Core/WebServer.cpp b/Core/WebServer.cpp index 54650c81d4..3efeedc020 100644 --- a/Core/WebServer.cpp +++ b/Core/WebServer.cpp @@ -65,6 +65,8 @@ static bool RegisterServer(int port) { http::Client http; Buffer theVoid; + http.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + char resource4[1024] = {}; if (http.Resolve(REPORT_HOSTNAME, REPORT_PORT, net::DNSType::IPV4)) { if (http.Connect()) { diff --git a/UI/RemoteISOScreen.cpp b/UI/RemoteISOScreen.cpp index d9c5e02fae..8a61fad262 100644 --- a/UI/RemoteISOScreen.cpp +++ b/UI/RemoteISOScreen.cpp @@ -117,6 +117,8 @@ bool RemoteISOConnectScreen::FindServer(std::string &resultHost, int &resultPort statusMessage_ = formatted; }; + http.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION)); + auto TryServer = [&](const std::string &host, int port) { SetStatus("Resolving [URL]...", host, port); if (!http.Resolve(host.c_str(), port)) {