Skip to content

Commit

Permalink
HTTPDownloader: Move to util project
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 6, 2023
1 parent 3c6b6c5 commit 0fe6e91
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 107 deletions.
22 changes: 4 additions & 18 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ add_library(common
hash_combine.h
heap_array.h
heterogeneous_containers.h
http_downloader.cpp
http_downloader.h
layered_settings_interface.cpp
layered_settings_interface.h
log.cpp
Expand Down Expand Up @@ -67,13 +65,11 @@ target_link_libraries(common PRIVATE stb zlib minizip Zstd::Zstd "${CMAKE_DL_LIB

if(WIN32)
target_sources(common PRIVATE
http_downloader_winhttp.cpp
http_downloader_winhttp.h
thirdparty/StackWalker.cpp
thirdparty/StackWalker.h
windows_headers.h
)
target_link_libraries(common PRIVATE winhttp.lib OneCore.lib)
target_link_libraries(common PRIVATE OneCore.lib)
endif()

if(MSVC)
Expand All @@ -98,19 +94,9 @@ if(APPLE)
target_link_libraries(common PRIVATE ${COCOA_LIBRARY})
endif()

if(NOT WIN32 AND NOT ANDROID)
target_sources(common PRIVATE
http_downloader_curl.cpp
http_downloader_curl.h
)
target_link_libraries(common PRIVATE
CURL::libcurl
)

if(LIBBACKTRACE_FOUND)
target_compile_definitions(common PRIVATE "-DENABLE_LIBBACKTRACE=1")
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
endif()
if(NOT WIN32 AND NOT ANDROID AND LIBBACKTRACE_FOUND)
target_compile_definitions(common PRIVATE "-DENABLE_LIBBACKTRACE=1")
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
endif()

if(ANDROID)
Expand Down
2 changes: 1 addition & 1 deletion src/common/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>%(AdditionalDependencies);winhttp.lib;OneCore.lib</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies);OneCore.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>
4 changes: 0 additions & 4 deletions src/common/common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
<ClInclude Include="file_system.h" />
<ClInclude Include="hash_combine.h" />
<ClInclude Include="heap_array.h" />
<ClInclude Include="http_downloader.h" />
<ClInclude Include="http_downloader_winhttp.h" />
<ClInclude Include="image.h" />
<ClInclude Include="intrin.h" />
<ClInclude Include="layered_settings_interface.h" />
Expand Down Expand Up @@ -51,8 +49,6 @@
<ClCompile Include="error.cpp" />
<ClCompile Include="fastjmp.cpp" />
<ClCompile Include="file_system.cpp" />
<ClCompile Include="http_downloader.cpp" />
<ClCompile Include="http_downloader_winhttp.cpp" />
<ClCompile Include="image.cpp" />
<ClCompile Include="layered_settings_interface.cpp" />
<ClCompile Include="log.cpp" />
Expand Down
4 changes: 0 additions & 4 deletions src/common/common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
<ClInclude Include="lru_cache.h" />
<ClInclude Include="easing.h" />
<ClInclude Include="error.h" />
<ClInclude Include="http_downloader_winhttp.h" />
<ClInclude Include="http_downloader.h" />
<ClInclude Include="path.h" />
<ClInclude Include="windows_headers.h" />
<ClInclude Include="settings_interface.h" />
Expand Down Expand Up @@ -62,8 +60,6 @@
</ClCompile>
<ClCompile Include="crash_handler.cpp" />
<ClCompile Include="error.cpp" />
<ClCompile Include="http_downloader_winhttp.cpp" />
<ClCompile Include="http_downloader.cpp" />
<ClCompile Include="layered_settings_interface.cpp" />
<ClCompile Include="memory_settings_interface.cpp" />
<ClCompile Include="threading.cpp" />
Expand Down
49 changes: 24 additions & 25 deletions src/core/achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "common/assert.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/http_downloader.h"
#include "common/log.h"
#include "common/md5_digest.h"
#include "common/path.h"
Expand All @@ -29,6 +28,7 @@
#include "common/string_util.h"

#include "util/cd_image.h"
#include "util/http_downloader.h"
#include "util/imgui_fullscreen.h"
#include "util/imgui_manager.h"
#include "util/platform_misc.h"
Expand Down Expand Up @@ -130,8 +130,8 @@ static void BeginLoadGame();
static void UpdateGameSummary();
static void DownloadImage(std::string url, std::string cache_filename);

static bool CreateClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http);
static void DestroyClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http);
static bool CreateClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http);
static void DestroyClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http);
static void ClientMessageCallback(const char* message, const rc_client_t* client);
static uint32_t ClientReadMemory(uint32_t address, uint8_t* buffer, uint32_t num_bytes, rc_client_t* client);
static void ClientServerCall(const rc_api_request_t* request, rc_client_server_callback_t callback, void* callback_data,
Expand Down Expand Up @@ -187,7 +187,7 @@ static bool s_using_raintegration = false;
static std::recursive_mutex s_achievements_mutex;
static rc_client_t* s_client;
static std::string s_image_directory;
static std::unique_ptr<Common::HTTPDownloader> s_http_downloader;
static std::unique_ptr<HTTPDownloader> s_http_downloader;

static std::string s_game_path;
static std::string s_game_hash;
Expand Down Expand Up @@ -305,9 +305,8 @@ std::string Achievements::GetGameHash(CDImage* image)

void Achievements::DownloadImage(std::string url, std::string cache_filename)
{
auto callback = [cache_filename](s32 status_code, std::string content_type,
Common::HTTPDownloader::Request::Data data) {
if (status_code != Common::HTTPDownloader::HTTP_STATUS_OK)
auto callback = [cache_filename](s32 status_code, std::string content_type, HTTPDownloader::Request::Data data) {
if (status_code != HTTPDownloader::HTTP_STATUS_OK)
return;

if (!FileSystem::WriteBinaryFile(cache_filename.c_str(), data.data(), data.size()))
Expand Down Expand Up @@ -430,9 +429,9 @@ bool Achievements::Initialize()
return true;
}

bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http)
bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http)
{
*http = Common::HTTPDownloader::Create(GetUserAgent().c_str());
*http = HTTPDownloader::Create(GetUserAgent().c_str());
if (!*http)
{
Host::ReportErrorAsync("Achievements Error", "Failed to create HTTPDownloader, cannot use achievements");
Expand All @@ -459,7 +458,7 @@ bool Achievements::CreateClient(rc_client_t** client, std::unique_ptr<Common::HT
return true;
}

void Achievements::DestroyClient(rc_client_t** client, std::unique_ptr<Common::HTTPDownloader>* http)
void Achievements::DestroyClient(rc_client_t** client, std::unique_ptr<HTTPDownloader>* http)
{
(*http)->WaitForAllRequests();

Expand Down Expand Up @@ -613,20 +612,20 @@ uint32_t Achievements::ClientReadMemory(uint32_t address, uint8_t* buffer, uint3
void Achievements::ClientServerCall(const rc_api_request_t* request, rc_client_server_callback_t callback,
void* callback_data, rc_client_t* client)
{
Common::HTTPDownloader::Request::Callback hd_callback =
[callback, callback_data](s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) {
rc_api_server_response_t rr;
rr.http_status_code = (status_code <= 0) ? (status_code == Common::HTTPDownloader::HTTP_STATUS_CANCELLED ?
RC_API_SERVER_RESPONSE_CLIENT_ERROR :
RC_API_SERVER_RESPONSE_RETRYABLE_CLIENT_ERROR) :
status_code;
rr.body_length = data.size();
rr.body = reinterpret_cast<const char*>(data.data());

callback(&rr, callback_data);
};
HTTPDownloader::Request::Callback hd_callback = [callback, callback_data](s32 status_code, std::string content_type,
HTTPDownloader::Request::Data data) {
rc_api_server_response_t rr;
rr.http_status_code = (status_code <= 0) ? (status_code == HTTPDownloader::HTTP_STATUS_CANCELLED ?
RC_API_SERVER_RESPONSE_CLIENT_ERROR :
RC_API_SERVER_RESPONSE_RETRYABLE_CLIENT_ERROR) :
status_code;
rr.body_length = data.size();
rr.body = reinterpret_cast<const char*>(data.data());

callback(&rr, callback_data);
};

Common::HTTPDownloader* http = static_cast<Common::HTTPDownloader*>(rc_client_get_userdata(client));
HTTPDownloader* http = static_cast<HTTPDownloader*>(rc_client_get_userdata(client));

// TODO: Content-type for post
if (request->post_data)
Expand Down Expand Up @@ -1598,9 +1597,9 @@ bool Achievements::Login(const char* username, const char* password, Error* erro

// We need to use a temporary client if achievements aren't currently active.
rc_client_t* client = s_client;
Common::HTTPDownloader* http = s_http_downloader.get();
HTTPDownloader* http = s_http_downloader.get();
const bool is_temporary_client = (client == nullptr);
std::unique_ptr<Common::HTTPDownloader> temporary_downloader;
std::unique_ptr<HTTPDownloader> temporary_downloader;
ScopedGuard temporary_client_guard = [&client, is_temporary_client, &temporary_downloader]() {
if (is_temporary_client)
DestroyClient(&client, &temporary_downloader);
Expand Down
19 changes: 9 additions & 10 deletions src/core/game_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include "system.h"

#include "util/cd_image.h"
#include "util/http_downloader.h"

#include "common/assert.h"
#include "common/byte_stream.h"
#include "common/file_system.h"
#include "common/heterogeneous_containers.h"
#include "common/http_downloader.h"
#include "common/log.h"
#include "common/path.h"
#include "common/progress_callback.h"
Expand Down Expand Up @@ -1116,15 +1116,14 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
{
std::string url(url_template);
if (has_title)
StringUtil::ReplaceAll(&url, "${title}", Common::HTTPDownloader::URLEncode(entry.title));
StringUtil::ReplaceAll(&url, "${title}", HTTPDownloader::URLEncode(entry.title));
if (has_file_title)
{
std::string display_name(FileSystem::GetDisplayNameFromPath(entry.path));
StringUtil::ReplaceAll(&url, "${filetitle}",
Common::HTTPDownloader::URLEncode(Path::GetFileTitle(display_name)));
StringUtil::ReplaceAll(&url, "${filetitle}", HTTPDownloader::URLEncode(Path::GetFileTitle(display_name)));
}
if (has_serial)
StringUtil::ReplaceAll(&url, "${serial}", Common::HTTPDownloader::URLEncode(entry.serial));
StringUtil::ReplaceAll(&url, "${serial}", HTTPDownloader::URLEncode(entry.serial));

download_urls.emplace_back(entry.path, std::move(url));
}
Expand All @@ -1136,7 +1135,7 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
return false;
}

std::unique_ptr<Common::HTTPDownloader> downloader(Common::HTTPDownloader::Create());
std::unique_ptr<HTTPDownloader> downloader(HTTPDownloader::Create());
if (!downloader)
{
progress->DisplayError("Failed to create HTTP downloader.");
Expand Down Expand Up @@ -1165,11 +1164,11 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
}

// we could actually do a few in parallel here...
std::string filename(Common::HTTPDownloader::URLDecode(url));
std::string filename(HTTPDownloader::URLDecode(url));
downloader->CreateRequest(
std::move(url), [use_serial, &save_callback, entry_path = std::move(entry_path), filename = std::move(filename)](
s32 status_code, std::string content_type, Common::HTTPDownloader::Request::Data data) {
if (status_code != Common::HTTPDownloader::HTTP_STATUS_OK || data.empty())
s32 status_code, std::string content_type, HTTPDownloader::Request::Data data) {
if (status_code != HTTPDownloader::HTTP_STATUS_OK || data.empty())
return;

std::unique_lock lock(s_mutex);
Expand All @@ -1180,7 +1179,7 @@ bool GameList::DownloadCovers(const std::vector<std::string>& url_templates, boo
// prefer the content type from the response for the extension
// otherwise, if it's missing, and the request didn't have an extension.. fall back to jpegs.
std::string template_filename;
std::string content_type_extension(Common::HTTPDownloader::GetExtensionForContentType(content_type));
std::string content_type_extension(HTTPDownloader::GetExtensionForContentType(content_type));

// don't treat the domain name as an extension..
const std::string::size_type last_slash = filename.find('/');
Expand Down
16 changes: 15 additions & 1 deletion src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ add_library(util
gpu_texture.h
host.cpp
host.h
http_downloader.cpp
http_downloader.h
imgui_fullscreen.cpp
imgui_fullscreen.h
imgui_manager.cpp
Expand Down Expand Up @@ -234,6 +236,8 @@ if(WIN32)
d3d12_texture.h
dinput_source.cpp
dinput_source.h
http_downloader_winhttp.cpp
http_downloader_winhttp.h
platform_misc_win32.cpp
win32_raw_input_source.cpp
win32_raw_input_source.h
Expand All @@ -243,7 +247,7 @@ if(WIN32)
xinput_source.h
)
target_link_libraries(util PRIVATE d3d12ma)
target_link_libraries(util PRIVATE d3d11.lib d3d12.lib d3dcompiler.lib dxgi.lib winmm.lib Dwmapi.lib)
target_link_libraries(util PRIVATE d3d11.lib d3d12.lib d3dcompiler.lib dxgi.lib winmm.lib Dwmapi.lib winhttp.lib)

if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
target_link_libraries(util PRIVATE WinPixEventRuntime::WinPixEventRuntime)
Expand Down Expand Up @@ -279,6 +283,16 @@ elseif(NOT ANDROID)
target_link_libraries(util PRIVATE ${DBUS_LINK_LIBRARIES})
endif()

if(NOT WIN32 AND NOT ANDROID)
target_sources(common PRIVATE
http_downloader_curl.cpp
http_downloader_curl.h
)
target_link_libraries(common PRIVATE
CURL::libcurl
)
endif()

function(add_util_resources target)
if(APPLE)
get_property(UTIL_METAL_SOURCES GLOBAL PROPERTY UTIL_METAL_SOURCES)
Expand Down
16 changes: 7 additions & 9 deletions src/common/http_downloader.cpp → src/util/http_downloader.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <[email protected]>
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#include "http_downloader.h"
#include "assert.h"
#include "log.h"
#include "string_util.h"
#include "timer.h"

#include "common/assert.h"
#include "common/log.h"
#include "common/string_util.h"
#include "common/timer.h"

Log_SetChannel(HTTPDownloader);

static constexpr float DEFAULT_TIMEOUT_IN_SECONDS = 30;
static constexpr u32 DEFAULT_MAX_ACTIVE_REQUESTS = 4;

namespace Common {

const char HTTPDownloader::DEFAULT_USER_AGENT[] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0";

Expand Down Expand Up @@ -355,5 +355,3 @@ std::string HTTPDownloader::GetExtensionForContentType(const std::string& conten
}
return ret;
}

} // namespace Common
8 changes: 3 additions & 5 deletions src/common/http_downloader.h → src/util/http_downloader.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <[email protected]>
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#pragma once

#include "common/types.h"

#include <atomic>
#include <functional>
#include <memory>
Expand All @@ -11,8 +13,6 @@
#include <string_view>
#include <vector>

namespace Common {

class HTTPDownloader
{
public:
Expand Down Expand Up @@ -94,5 +94,3 @@ class HTTPDownloader
std::mutex m_pending_http_request_lock;
std::vector<Request*> m_pending_http_requests;
};

} // namespace FrontendCommon
Loading

0 comments on commit 0fe6e91

Please sign in to comment.