From 493367c89fca2465944f857a698a5eb805c3acb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 23 Jan 2025 10:13:36 +0100 Subject: [PATCH] More http code cleanup --- Common/Net/HTTPRequest.cpp | 44 +++++++++++++------------------------- Common/Net/HTTPRequest.h | 12 +++++------ 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/Common/Net/HTTPRequest.cpp b/Common/Net/HTTPRequest.cpp index 2a6d295c8c..7b0fdffbf2 100644 --- a/Common/Net/HTTPRequest.cpp +++ b/Common/Net/HTTPRequest.cpp @@ -35,21 +35,24 @@ Request::Request(RequestMethod method, std::string_view url, std::string_view na }; } -bool RequestManager::IsHttpsUrl(const std::string &url) { +static bool IsHttpsUrl(std::string_view url) { return startsWith(url, "https:"); } -std::shared_ptr RequestManager::StartDownload(const std::string &url, const Path &outfile, ProgressBarMode mode, const char *acceptMime) { - std::shared_ptr dl; +std::shared_ptr CreateRequest(RequestMethod method, std::string_view url, std::string_view postdata, std::string_view postMime, const Path &outfile, ProgressBarMode mode, std::string_view name) { if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) { #ifndef HTTPS_NOT_AVAILABLE - dl.reset(new HTTPSRequest(RequestMethod::GET, url, "", "", outfile, mode)); + return std::shared_ptr(new HTTPSRequest(method, url, postdata, postMime, outfile, mode, name)); #else return std::shared_ptr(); #endif } else { - dl.reset(new HTTPRequest(RequestMethod::GET, url, "", "", outfile, mode)); + return std::shared_ptr(new HTTPRequest(method, url, postdata, postMime, outfile, mode, name)); } +} + +std::shared_ptr RequestManager::StartDownload(std::string_view url, const Path &outfile, ProgressBarMode mode, const char *acceptMime) { + std::shared_ptr dl = CreateRequest(RequestMethod::GET, url, "", "", outfile, mode, ""); if (!userAgent_.empty()) dl->SetUserAgent(userAgent_); @@ -61,22 +64,14 @@ std::shared_ptr RequestManager::StartDownload(const std::string &url, c } std::shared_ptr RequestManager::StartDownloadWithCallback( - const std::string &url, + std::string_view url, const Path &outfile, ProgressBarMode mode, std::function callback, std::string_view name, const char *acceptMime) { - std::shared_ptr dl; - if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) { -#ifndef HTTPS_NOT_AVAILABLE - dl.reset(new HTTPSRequest(RequestMethod::GET, url, "", "", outfile, mode, name)); -#else - return std::shared_ptr(); -#endif - } else { - dl.reset(new HTTPRequest(RequestMethod::GET, url, "", "", outfile, mode, name)); - } + std::shared_ptr dl = CreateRequest(RequestMethod::GET, url, "", "", outfile, mode, name); + if (!userAgent_.empty()) dl->SetUserAgent(userAgent_); if (acceptMime) @@ -88,22 +83,13 @@ std::shared_ptr RequestManager::StartDownloadWithCallback( } std::shared_ptr RequestManager::AsyncPostWithCallback( - const std::string &url, - const std::string &postData, - const std::string &postMime, + std::string_view url, + std::string_view postData, + std::string_view postMime, ProgressBarMode mode, std::function callback, std::string_view name) { - std::shared_ptr dl; - if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) { -#ifndef HTTPS_NOT_AVAILABLE - dl.reset(new HTTPSRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name)); -#else - return std::shared_ptr(); -#endif - } else { - dl.reset(new HTTPRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name)); - } + std::shared_ptr dl = CreateRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name); if (!userAgent_.empty()) dl->SetUserAgent(userAgent_); dl->SetCallback(callback); diff --git a/Common/Net/HTTPRequest.h b/Common/Net/HTTPRequest.h index 79bd597aed..b63b4746e4 100644 --- a/Common/Net/HTTPRequest.h +++ b/Common/Net/HTTPRequest.h @@ -95,10 +95,10 @@ public: CancelAll(); } - std::shared_ptr StartDownload(const std::string &url, const Path &outfile, ProgressBarMode mode, const char *acceptMime = nullptr); + std::shared_ptr StartDownload(std::string_view url, const Path &outfile, ProgressBarMode mode, const char *acceptMime = nullptr); std::shared_ptr StartDownloadWithCallback( - const std::string &url, + std::string_view url, const Path &outfile, ProgressBarMode mode, std::function callback, @@ -106,9 +106,9 @@ public: const char *acceptMime = nullptr); std::shared_ptr AsyncPostWithCallback( - const std::string &url, - const std::string &postData, - const std::string &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. + 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. ProgressBarMode mode, std::function callback, std::string_view name = ""); @@ -122,8 +122,6 @@ public: } private: - static bool IsHttpsUrl(const std::string &url); - std::vector> downloads_; // These get copied to downloads_ in Update(). It's so that callbacks can add new downloads // while running.