Skip to content

Commit

Permalink
Add user agent to headers for all RetroAchievements server calls
Browse files Browse the repository at this point in the history
Requested agent syntax is "Dolphin/5.0-23456"
  • Loading branch information
LillyJadeKatrin committed Mar 31, 2024
1 parent a5e410d commit 3c5f0a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
12 changes: 10 additions & 2 deletions Source/Core/Common/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Common
{
#define EMULATOR_NAME "Dolphin"

#ifdef _DEBUG
#define BUILD_TYPE_STR "Debug "
#elif defined DEBUGFAST
Expand All @@ -19,9 +21,9 @@ namespace Common

const std::string& GetScmRevStr()
{
static const std::string scm_rev_str = "Dolphin "
static const std::string scm_rev_str = EMULATOR_NAME " "
#if !SCM_IS_MASTER
"[" SCM_BRANCH_STR "] "
"[" SCM_BRANCH_STR "] "
#endif

#ifdef __INTEL_COMPILER
Expand Down Expand Up @@ -50,6 +52,12 @@ const std::string& GetScmBranchStr()
return scm_branch_str;
}

const std::string& GetUserAgentStr()
{
static const std::string user_agent_str = std::string(EMULATOR_NAME "/") + SCM_DESC_STR;
return user_agent_str;
}

const std::string& GetScmDistributorStr()
{
static const std::string scm_distributor_str = SCM_DISTRIBUTOR_STR;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const std::string& GetScmDescStr();
const std::string& GetScmBranchStr();
const std::string& GetScmRevStr();
const std::string& GetScmRevGitStr();
const std::string& GetUserAgentStr();
const std::string& GetScmDistributorStr();
const std::string& GetScmUpdateTrackStr();
const std::string& GetNetplayDolphinVer();
Expand Down
10 changes: 7 additions & 3 deletions Source/Core/Core/AchievementManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Common/HttpRequest.h"
#include "Common/Image.h"
#include "Common/Logging/Log.h"
#include "Common/Version.h"
#include "Common/WorkQueueThread.h"
#include "Core/Config/AchievementSettings.h"
#include "Core/Core.h"
Expand All @@ -24,6 +25,9 @@
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/VideoEvents.h"

static const Common::HttpRequest::Headers USER_AGENT_HEADER = {
{"User-Agent", Common::GetUserAgentStr()}};

static std::unique_ptr<OSD::Icon> DecodeBadgeToOSDIcon(const AchievementManager::Badge& badge);

AchievementManager& AchievementManager::GetInstance()
Expand Down Expand Up @@ -1120,7 +1124,7 @@ AchievementManager::ResponseType AchievementManager::FetchGameData()
ERROR_LOG_FMT(ACHIEVEMENTS, "Invalid API request for game data.");
return ResponseType::INVALID_REQUEST;
}
auto http_response = http_request.Post(api_request.url, api_request.post_data);
auto http_response = http_request.Post(api_request.url, api_request.post_data, USER_AGENT_HEADER);
rc_api_destroy_request(&api_request);
if (!http_response.has_value() || http_response->size() == 0)
{
Expand Down Expand Up @@ -1637,7 +1641,7 @@ AchievementManager::ResponseType AchievementManager::Request(
ERROR_LOG_FMT(ACHIEVEMENTS, "Invalid API request.");
return ResponseType::INVALID_REQUEST;
}
auto http_response = http_request.Post(api_request.url, api_request.post_data);
auto http_response = http_request.Post(api_request.url, api_request.post_data, USER_AGENT_HEADER);
rc_api_destroy_request(&api_request);
if (http_response.has_value() && http_response->size() > 0)
{
Expand Down Expand Up @@ -1676,7 +1680,7 @@ AchievementManager::RequestImage(rc_api_fetch_image_request_t rc_request, Badge*
ERROR_LOG_FMT(ACHIEVEMENTS, "Invalid request for image.");
return ResponseType::INVALID_REQUEST;
}
auto http_response = http_request.Get(api_request.url);
auto http_response = http_request.Get(api_request.url, USER_AGENT_HEADER);
if (http_response.has_value() && http_response->size() > 0)
{
rc_api_destroy_request(&api_request);
Expand Down

0 comments on commit 3c5f0a5

Please sign in to comment.