From 433defee9a1cd3cc939e61d0837ba18a7d652f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 13 Jun 2026 15:21:05 +0200 Subject: [PATCH] Address some more freedback in HTTPRequest --- Common/Net/HTTPRequest.cpp | 40 +++++++++++++++++++------------------- Common/Net/HTTPRequest.h | 18 ++++++++++++----- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Common/Net/HTTPRequest.cpp b/Common/Net/HTTPRequest.cpp index e7cf1ffa2b..1dd8d06027 100644 --- a/Common/Net/HTTPRequest.cpp +++ b/Common/Net/HTTPRequest.cpp @@ -15,19 +15,19 @@ Request::Request(RequestMethod method, std::string_view url, std::string_view na INFO_LOG(Log::HTTP, "HTTP %s request: %.*s (%.*s)", RequestMethodToString(method), (int)url.size(), url.data(), (int)name.size(), name.data()); progress_.callback = [this](int64_t bytes, int64_t contentLength, bool done) { - std::string message; - if (!name_.empty()) { - message = name_; - } else { - std::size_t pos = url_.rfind('/'); - if (pos != std::string::npos) { - message = url_.substr(pos + 1); - } else { - message = url_; - } - } if (flags_ & RequestFlags::ProgressBar) { if (!done) { + std::string message; + if (!name_.empty()) { + message = name_; + } else { + std::size_t pos = url_.rfind('/'); + if (pos != std::string::npos) { + message = url_.substr(pos + 1); + } else { + message = url_; + } + } g_OSD.SetProgressBar(url_, std::move(message), 0.0f, (float)contentLength, (float)bytes, flags_ & RequestFlags::ProgressBarDelayed ? 3.0f : 0.0f); // delay 3 seconds before showing. } else { g_OSD.RemoveProgressBar(url_, Failed() ? false : true, 0.5f); @@ -40,7 +40,7 @@ static bool IsHttpsUrl(std::string_view url) { return startsWith(url, "https:"); } -Path UrlToCachePath(const Path &cacheDir, std::string_view url) { +static Path UrlToCachePath(const Path &cacheDir, std::string_view url) { std::string fn = "DLCACHE_"; for (auto c : url) { if (isalnum(c) || c == '.' || c == '-' || c == '_') { @@ -52,7 +52,7 @@ Path UrlToCachePath(const Path &cacheDir, std::string_view url) { return cacheDir / fn; } -Path RequestManager::UrlToCachePath(const std::string_view url) { +Path RequestManager::UrlToCachePath(const std::string_view url) const { if (cacheDir_.empty()) { return Path(); } @@ -72,13 +72,13 @@ static std::shared_ptr CreateRequest(RequestMethod method, std::string_ } // Compatible with StartDownload -// Synchronous (no callback). +// Synchronous (no completionCallback). bool RequestManager::ReadFileFromCache(std::string_view url, std::string *data) { Path cacheFile = UrlToCachePath(url); return File::ReadBinaryFileToString(cacheFile, data); } -std::shared_ptr RequestManager::StartDownload(std::string_view url, const Path &outfile, RequestFlags flags, const char *acceptMime, std::string_view name, std::function callback) { +std::shared_ptr RequestManager::StartDownload(std::string_view url, const Path &outfile, RequestFlags flags, const char *acceptMime, std::string_view name, RequestCompletionCallback completionCallback) { const bool enableCache = !cacheDir_.empty() && (flags & RequestFlags::Cached24H); // Come up with a cache file path. @@ -96,11 +96,11 @@ std::shared_ptr RequestManager::StartDownload(std::string_view url, con // to modify the calling code. std::string contents; if (File::ReadBinaryFileToString(cacheFile, &contents)) { - INFO_LOG(Log::HTTP, "Returning cached file for %.*s: %s", (int)url.size(), url.data(), cacheFile.c_str()); + INFO_LOG(Log::HTTP, "Returning cached file for %.*s: %s", STR_VIEW(url), cacheFile.c_str()); // All is well, but we've indented a bit much here. std::shared_ptr dl(new CachedRequest(RequestMethod::GET, url, KeepAfterLast(url, '/'), nullptr, flags, contents)); newDownloads_.push_back(dl); - dl->SetCallback(callback); + dl->SetCallback(completionCallback); return dl; } else { INFO_LOG(Log::HTTP, "Failed reading from cache, proceeding with request"); @@ -127,7 +127,7 @@ std::shared_ptr RequestManager::StartDownload(std::string_view url, con if (acceptMime) { dl->SetAccept(acceptMime); } - dl->SetCallback(callback); + dl->SetCallback(completionCallback); newDownloads_.push_back(dl); dl->Start(); return dl; @@ -138,12 +138,12 @@ std::shared_ptr RequestManager::AsyncPostWithCallback( std::string_view postData, std::string_view postMime, RequestFlags flags, - std::function callback, + RequestCompletionCallback completionCallback, std::string_view name) { std::shared_ptr dl = CreateRequest(RequestMethod::POST, url, postData, postMime, Path(), flags, nullptr, name); if (!userAgent_.empty()) dl->SetUserAgent(userAgent_); - dl->SetCallback(callback); + dl->SetCallback(completionCallback); newDownloads_.push_back(dl); dl->Start(); return dl; diff --git a/Common/Net/HTTPRequest.h b/Common/Net/HTTPRequest.h index e13fa405b1..0c2e80568f 100644 --- a/Common/Net/HTTPRequest.h +++ b/Common/Net/HTTPRequest.h @@ -23,6 +23,11 @@ enum class RequestFlags { }; ENUM_CLASS_BITOPS(RequestFlags); +class Request; + +// Note that these are executed both on success and failure, so need to handle both by inspecting the Request. +using RequestCompletionCallback = std::function; + // Abstract request. class Request { public: @@ -39,7 +44,7 @@ public: // NOTE: Completion callbacks (which these are) are deferred until RunCallback is called. This is so that // the call will end up on the thread that calls g_DownloadManager.Update(). - void SetCallback(std::function callback) { + void SetCallback(RequestCompletionCallback callback) { callback_ = callback; } void RunCallback() { @@ -97,7 +102,7 @@ protected: RequestFlags flags_; private: - std::function callback_; + RequestCompletionCallback callback_; }; class RequestManager { @@ -107,14 +112,14 @@ public: } // NOTE: This is the only version that supports the cache flag (for now). - std::shared_ptr StartDownload(std::string_view url, const Path &outfile, RequestFlags flags, const char *acceptMime = nullptr, std::string_view name = "", std::function callback = {}); + std::shared_ptr StartDownload(std::string_view url, const Path &outfile, RequestFlags flags, const char *acceptMime = nullptr, std::string_view name = "", RequestCompletionCallback completionCallback = {}); std::shared_ptr AsyncPostWithCallback( std::string_view url, std::string_view postData, std::string_view postMime, // Use postMime = "application/x-www-form-urlencoded" for standard form-style posts, such as used by retroachievements. For encoding form data manually we have MultipartFormDataEncoder. RequestFlags flags, - std::function callback, + RequestCompletionCallback completionCallback, std::string_view name = ""); // Drops finished downloads from the list. @@ -129,7 +134,10 @@ public: cacheDir_ = path; } - Path UrlToCachePath(const std::string_view url); + // Just computes the path, doesn't check availability. + Path UrlToCachePath(const std::string_view url) const; + + // Does the file read. Returns false if file missing or other error. bool ReadFileFromCache(std::string_view url, std::string *data); private: