HTTPDownloader: Fix user agent sending on Windows/Android

This commit is contained in:
Connor McLaughlin
2021-04-04 12:55:03 +10:00
parent 251043f11a
commit d41b5be908
10 changed files with 40 additions and 33 deletions

View File

@ -213,14 +213,13 @@ static std::string GetUserAgent()
bool Initialize(bool test_mode, bool use_first_disc_from_playlist, bool enable_rich_presence, bool challenge_mode)
{
s_http_downloader = FrontendCommon::HTTPDownloader::Create();
s_http_downloader = FrontendCommon::HTTPDownloader::Create(GetUserAgent().c_str());
if (!s_http_downloader)
{
Log_ErrorPrint("Failed to create HTTP downloader, cannot use cheevos");
return false;
}
s_http_downloader->SetUserAgent(GetUserAgent());
g_active = true;
g_challenge_mode = challenge_mode;
s_test_mode = test_mode;
@ -408,11 +407,11 @@ bool Login(const char* username, const char* password)
// create a temporary downloader if we're not initialized
Assert(!g_active);
std::unique_ptr<FrontendCommon::HTTPDownloader> http_downloader = FrontendCommon::HTTPDownloader::Create();
std::unique_ptr<FrontendCommon::HTTPDownloader> http_downloader =
FrontendCommon::HTTPDownloader::Create(GetUserAgent().c_str());
if (!http_downloader)
return false;
http_downloader->SetUserAgent(GetUserAgent());
SendLogin(username, password, http_downloader.get(), LoginCallback);
http_downloader->WaitForAllRequests();

View File

@ -4,26 +4,21 @@
#include "common/timer.h"
Log_SetChannel(HTTPDownloader);
static constexpr char DEFAULT_USER_AGENT[] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0";
static constexpr float DEFAULT_TIMEOUT_IN_SECONDS = 30;
static constexpr u32 DEFAULT_MAX_ACTIVE_REQUESTS = 4;
namespace FrontendCommon {
const char HTTPDownloader::DEFAULT_USER_AGENT[] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0";
HTTPDownloader::HTTPDownloader()
: m_user_agent(DEFAULT_USER_AGENT), m_timeout(DEFAULT_TIMEOUT_IN_SECONDS),
m_max_active_requests(DEFAULT_MAX_ACTIVE_REQUESTS)
: m_timeout(DEFAULT_TIMEOUT_IN_SECONDS), m_max_active_requests(DEFAULT_MAX_ACTIVE_REQUESTS)
{
}
HTTPDownloader::~HTTPDownloader() = default;
void HTTPDownloader::SetUserAgent(std::string name)
{
m_user_agent = std::move(name);
}
void HTTPDownloader::SetTimeout(float timeout)
{
m_timeout = timeout;

View File

@ -52,9 +52,8 @@ public:
HTTPDownloader();
virtual ~HTTPDownloader();
static std::unique_ptr<HTTPDownloader> Create();
static std::unique_ptr<HTTPDownloader> Create(const char* user_agent = DEFAULT_USER_AGENT);
void SetUserAgent(std::string name);
void SetTimeout(float timeout);
void SetMaxActiveRequests(u32 max_active_requests);
@ -63,6 +62,8 @@ public:
void PollRequests();
void WaitForAllRequests();
static const char DEFAULT_USER_AGENT[];
protected:
virtual Request* InternalCreateRequest() = 0;
virtual void InternalPollRequests() = 0;

View File

@ -13,10 +13,10 @@ HTTPDownloaderCurl::HTTPDownloaderCurl() : HTTPDownloader() {}
HTTPDownloaderCurl::~HTTPDownloaderCurl() = default;
std::unique_ptr<HTTPDownloader> HTTPDownloader::Create()
std::unique_ptr<HTTPDownloader> HTTPDownloader::Create(const char* user_agent)
{
std::unique_ptr<HTTPDownloaderCurl> instance(std::make_unique<HTTPDownloaderCurl>());
if (!instance->Initialize())
if (!instance->Initialize(user_agent))
return {};
return instance;
@ -25,7 +25,7 @@ std::unique_ptr<HTTPDownloader> HTTPDownloader::Create()
static bool s_curl_initialized = false;
static std::once_flag s_curl_initialized_once_flag;
bool HTTPDownloaderCurl::Initialize()
bool HTTPDownloaderCurl::Initialize(const char* user_agent)
{
if (!s_curl_initialized)
{
@ -45,6 +45,8 @@ bool HTTPDownloaderCurl::Initialize()
return false;
}
}
m_user_agent = user_agent;
m_thread_pool = std::make_unique<cb::ThreadPool>(m_max_active_requests);
return true;
}
@ -75,8 +77,8 @@ void HTTPDownloaderCurl::ProcessRequest(Request* req)
long response_code = 0;
curl_easy_getinfo(req->handle, CURLINFO_RESPONSE_CODE, &response_code);
req->status_code = static_cast<s32>(response_code);
Log_DevPrintf("Request for '%s' returned status code %d and %zu bytes",
req->url.c_str(), req->status_code, req->data.size());
Log_DevPrintf("Request for '%s' returned status code %d and %zu bytes", req->url.c_str(), req->status_code,
req->data.size());
}
else
{

View File

@ -14,7 +14,7 @@ public:
HTTPDownloaderCurl();
~HTTPDownloaderCurl() override;
bool Initialize();
bool Initialize(const char* user_agent);
protected:
Request* InternalCreateRequest() override;
@ -32,6 +32,7 @@ private:
static size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata);
void ProcessRequest(Request* req);
std::string m_user_agent;
std::unique_ptr<cb::ThreadPool> m_thread_pool;
std::mutex m_cancel_mutex;
};

View File

@ -22,16 +22,16 @@ HTTPDownloaderWinHttp::~HTTPDownloaderWinHttp()
}
}
std::unique_ptr<HTTPDownloader> HTTPDownloader::Create()
std::unique_ptr<HTTPDownloader> HTTPDownloader::Create(const char* user_agent)
{
std::unique_ptr<HTTPDownloaderWinHttp> instance(std::make_unique<HTTPDownloaderWinHttp>());
if (!instance->Initialize())
if (!instance->Initialize(user_agent))
return {};
return instance;
}
bool HTTPDownloaderWinHttp::Initialize()
bool HTTPDownloaderWinHttp::Initialize(const char* user_agent)
{
// WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY is not supported before Win8.1.
const DWORD dwAccessType =

View File

@ -13,7 +13,7 @@ public:
HTTPDownloaderWinHttp();
~HTTPDownloaderWinHttp() override;
bool Initialize();
bool Initialize(const char* user_agent);
protected:
Request* InternalCreateRequest() override;