From 12adad04942cae3ffef17cf70904e56487e8a8e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 23 Jan 2025 09:45:17 +0100 Subject: [PATCH] HTTP request classes code cleanup - move common things up to the base class --- Common/Net/HTTPClient.cpp | 5 +++-- Common/Net/HTTPClient.h | 23 +---------------------- Common/Net/HTTPNaettRequest.cpp | 5 +++-- Common/Net/HTTPNaettRequest.h | 24 +----------------------- Common/Net/HTTPRequest.cpp | 4 ++-- Common/Net/HTTPRequest.h | 29 +++++++++++++++++++---------- Core/Util/GameManager.cpp | 2 +- 7 files changed, 30 insertions(+), 62 deletions(-) diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index c8abcc463f..3ca4094b7d 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -485,8 +485,9 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector responseHeaders_; - Path outfile_; std::thread thread_; std::string postMime_; - int resultCode_ = 0; bool completed_ = false; bool failed_ = false; - bool cancelled_ = false; bool joined_ = false; }; diff --git a/Common/Net/HTTPNaettRequest.cpp b/Common/Net/HTTPNaettRequest.cpp index 9c14c6a49b..be87fecd98 100644 --- a/Common/Net/HTTPNaettRequest.cpp +++ b/Common/Net/HTTPNaettRequest.cpp @@ -12,8 +12,9 @@ namespace http { -HTTPSRequest::HTTPSRequest(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile, ProgressBarMode progressBarMode, std::string_view name) - : Request(method, url, name, &cancelled_, progressBarMode), method_(method), postData_(postData), postMime_(postMime), outfile_(outfile) { +HTTPSRequest::HTTPSRequest(RequestMethod method, std::string_view url, std::string_view postData, std::string_view postMime, const Path &outfile, ProgressBarMode progressBarMode, std::string_view name) + : Request(method, url, name, &cancelled_, progressBarMode), method_(method), postData_(postData), postMime_(postMime) { + outfile_ = outfile; } HTTPSRequest::~HTTPSRequest() { diff --git a/Common/Net/HTTPNaettRequest.h b/Common/Net/HTTPNaettRequest.h index 03447ca66a..5cbbe8f3b0 100644 --- a/Common/Net/HTTPNaettRequest.h +++ b/Common/Net/HTTPNaettRequest.h @@ -14,7 +14,7 @@ namespace http { // Really an asynchronous request. class HTTPSRequest : public Request { public: - HTTPSRequest(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile, ProgressBarMode progressBarMode = ProgressBarMode::DELAYED, std::string_view name = ""); + HTTPSRequest(RequestMethod method, std::string_view url, std::string_view postData, std::string_view postMime, const Path &outfile, ProgressBarMode progressBarMode = ProgressBarMode::DELAYED, std::string_view name = ""); ~HTTPSRequest(); void Start() override; @@ -24,34 +24,12 @@ public: bool Done() override; bool Failed() const override { return failed_; } - // NOTE! The value of ResultCode is INVALID until Done() returns true. - int ResultCode() const override { return resultCode_; } - - const Path &outfile() const override { return outfile_; } - - // If not downloading to a file, access this to get the result. - Buffer &buffer() override { return buffer_; } - const Buffer &buffer() const override { return buffer_; } - - void Cancel() override { - cancelled_ = true; - } - - bool IsCancelled() const override { - return cancelled_; - } - private: RequestMethod method_; std::string postData_; - Buffer buffer_; - std::vector responseHeaders_; - Path outfile_; std::string postMime_; - int resultCode_ = 0; bool completed_ = false; bool failed_ = false; - bool cancelled_ = false; // Naett state naettReq *req_ = nullptr; diff --git a/Common/Net/HTTPRequest.cpp b/Common/Net/HTTPRequest.cpp index 5709ce9d6c..2a6d295c8c 100644 --- a/Common/Net/HTTPRequest.cpp +++ b/Common/Net/HTTPRequest.cpp @@ -9,9 +9,9 @@ namespace http { -Request::Request(RequestMethod method, const std::string &url, std::string_view name, bool *cancelled, ProgressBarMode mode) +Request::Request(RequestMethod method, std::string_view url, std::string_view name, bool *cancelled, ProgressBarMode mode) : method_(method), url_(url), name_(name), progress_(cancelled), progressBarMode_(mode) { - INFO_LOG(Log::HTTP, "HTTP %s request: %s (%.*s)", RequestMethodToString(method), url.c_str(), (int)name.size(), name.data()); + INFO_LOG(Log::HTTP, "HTTP %s request: %.*s (%.*s)", RequestMethodToString(method), (int)url.size(), url.data(), (int)name.size(), name.data()); progress_.callback = [=](int64_t bytes, int64_t contentLength, bool done) { std::string message; diff --git a/Common/Net/HTTPRequest.h b/Common/Net/HTTPRequest.h index 88a6eb1fbe..79bd597aed 100644 --- a/Common/Net/HTTPRequest.h +++ b/Common/Net/HTTPRequest.h @@ -23,7 +23,7 @@ enum class ProgressBarMode { // Abstract request. class Request { public: - Request(RequestMethod method, const std::string &url, std::string_view name, bool *cancelled, ProgressBarMode mode); + Request(RequestMethod method, std::string_view url, std::string_view name, bool *cancelled, ProgressBarMode mode); virtual ~Request() {} void SetAccept(const char *mime) { @@ -51,31 +51,40 @@ public: virtual bool Done() = 0; virtual bool Failed() const = 0; - virtual int ResultCode() const = 0; - // Returns 1.0 when done. That one value can be compared exactly - or just use Done(). float Progress() const { return progress_.progress; } float SpeedKBps() const { return progress_.kBps; } std::string url() const { return url_; } - virtual const Path &outfile() const = 0; - virtual void Cancel() = 0; - virtual bool IsCancelled() const = 0; + const Path &OutFile() const { return outfile_; } - // Response - virtual Buffer &buffer() = 0; - virtual const Buffer &buffer() const = 0; + void Cancel() { cancelled_ = true; } + bool IsCancelled() const { return cancelled_; } + + // If not downloading to a file, access this to get the result. + Buffer &buffer() { return buffer_; } + const Buffer &buffer() const { return buffer_; } + + // NOTE! The value of ResultCode is INVALID until Done() returns true. + int ResultCode() const { return resultCode_; } protected: - std::function callback_; RequestMethod method_; std::string url_; std::string name_; const char *acceptMime_ = "*/*"; std::string userAgent_; + Path outfile_; + Buffer buffer_; + bool cancelled_ = false; + int resultCode_ = 0; + std::vector responseHeaders_; net::RequestProgress progress_; ProgressBarMode progressBarMode_; + +private: + std::function callback_; }; using std::shared_ptr; diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index f5a93cc4ea..a5109bd113 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -163,7 +163,7 @@ void GameManager::UninstallGame(const std::string &name) { void GameManager::Update() { if (curDownload_.get() && curDownload_->Done()) { INFO_LOG(Log::HLE, "Download completed! Status = %d", curDownload_->ResultCode()); - Path fileName = curDownload_->outfile(); + Path fileName = curDownload_->OutFile(); if (curDownload_->ResultCode() == 200) { if (!File::Exists(fileName)) { ERROR_LOG(Log::HLE, "Downloaded file '%s' does not exist :(", fileName.c_str());