HTTP request classes code cleanup - move common things up to the base class

This commit is contained in:
Henrik Rydgård 2025-01-23 09:45:17 +01:00
parent c4a432e467
commit 12adad0494
7 changed files with 30 additions and 62 deletions

View file

@ -485,8 +485,9 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
return 0;
}
HTTPRequest::HTTPRequest(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), postData_(postData), postMime_(postMime), outfile_(outfile) {
HTTPRequest::HTTPRequest(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), postData_(postData), postMime_(postMime) {
outfile_ = outfile;
}
HTTPRequest::~HTTPRequest() {

View file

@ -96,7 +96,7 @@ protected:
// Really an asynchronous request.
class HTTPRequest : public Request {
public:
HTTPRequest(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 = "");
HTTPRequest(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 = "");
~HTTPRequest();
void Start() override;
@ -105,23 +105,6 @@ public:
bool Done() override { return completed_; }
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:
void Do(); // Actually does the download. Runs on thread.
int Perform(const std::string &url);
@ -130,14 +113,10 @@ private:
std::string postData_;
Buffer buffer_;
std::vector<std::string> responseHeaders_;
Path outfile_;
std::thread thread_;
std::string postMime_;
int resultCode_ = 0;
bool completed_ = false;
bool failed_ = false;
bool cancelled_ = false;
bool joined_ = false;
};

View file

@ -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() {

View file

@ -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<std::string> responseHeaders_;
Path outfile_;
std::string postMime_;
int resultCode_ = 0;
bool completed_ = false;
bool failed_ = false;
bool cancelled_ = false;
// Naett state
naettReq *req_ = nullptr;

View file

@ -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;

View file

@ -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<void(Request &)> 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<std::string> responseHeaders_;
net::RequestProgress progress_;
ProgressBarMode progressBarMode_;
private:
std::function<void(Request &)> callback_;
};
using std::shared_ptr;

View file

@ -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());