From 9535b1674044e6ae90763a7c12a4e312247d1a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 21 Jul 2023 17:50:49 +0200 Subject: [PATCH] Use the patched naett functions to implement progress updates --- Common/Net/HTTPClient.cpp | 23 +---------------------- Common/Net/HTTPClient.h | 2 -- Common/Net/HTTPNaettRequest.cpp | 13 ++++++++++--- Common/Net/HTTPNaettRequest.h | 1 - Common/Net/HTTPRequest.cpp | 25 +++++++++++++++++++++++++ Common/Net/HTTPRequest.h | 4 +++- Common/Net/NetBuffer.cpp | 12 ++++++++++++ Common/Net/NetBuffer.h | 11 +---------- 8 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index 5638e30529..9fc37de082 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -445,28 +445,7 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector responseHeaders_; @@ -136,7 +135,6 @@ private: bool completed_ = false; bool failed_ = false; bool cancelled_ = false; - ProgressBarMode progressBarMode_; bool joined_ = false; }; diff --git a/Common/Net/HTTPNaettRequest.cpp b/Common/Net/HTTPNaettRequest.cpp index f706de794d..55963e9558 100644 --- a/Common/Net/HTTPNaettRequest.cpp +++ b/Common/Net/HTTPNaettRequest.cpp @@ -5,6 +5,7 @@ #include "Common/Net/HTTPRequest.h" #include "Common/Net/HTTPNaettRequest.h" #include "Common/Thread/ThreadUtil.h" +#include "Common/StringUtils.h" #include "Common/Log.h" #include "ext/naett/naett.h" @@ -12,7 +13,7 @@ namespace http { HTTPSDownload::HTTPSDownload(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile, ProgressBarMode progressBarMode, const std::string &name) - : Download(url, name, &cancelled_), method_(method), postData_(postData), postMime_(postMime), outfile_(outfile), progressBarMode_(progressBarMode) { + : Download(method, url, name, &cancelled_, progressBarMode), method_(method), postData_(postData), postMime_(postMime), outfile_(outfile) { } HTTPSDownload::~HTTPSDownload() { @@ -40,10 +41,11 @@ void HTTPSDownload::Start() { // 30 s timeout - not sure what's reasonable? options.push_back(naettTimeout(30 * 1000)); // milliseconds - const naettOption **opts = (const naettOption **)options.data(); req_ = naettRequestWithOptions(url_.c_str(), (int)options.size(), opts); res_ = naettMake(req_); + + progress_.Update(0, 0, false); } void HTTPSDownload::Join() { @@ -66,7 +68,9 @@ bool HTTPSDownload::Done() { return true; if (!naettComplete(res_)) { - // Not done yet, return and try again later. + int total = 0; + int size = naettGetTotalBytesRead(res_, &total); + progress_.Update(size, total, false); return false; } @@ -95,6 +99,7 @@ bool HTTPSDownload::Done() { break; } failed_ = true; + progress_.Update(0, 0, true); } else if (resultCode_ == 200) { int bodyLength; const void *body = naettGetBody(res_, &bodyLength); @@ -103,9 +108,11 @@ bool HTTPSDownload::Done() { if (!outfile_.empty() && !buffer_.FlushToFile(outfile_)) { ERROR_LOG(IO, "Failed writing download to '%s'", outfile_.c_str()); } + progress_.Update(bodyLength, bodyLength, true); } else { WARN_LOG(IO, "Naett request failed: %d", resultCode_); failed_ = true; + progress_.Update(0, 0, true); } completed_ = true; diff --git a/Common/Net/HTTPNaettRequest.h b/Common/Net/HTTPNaettRequest.h index 064c9c70ed..4fa5c93d16 100644 --- a/Common/Net/HTTPNaettRequest.h +++ b/Common/Net/HTTPNaettRequest.h @@ -51,7 +51,6 @@ private: bool completed_ = false; bool failed_ = false; bool cancelled_ = false; - ProgressBarMode progressBarMode_; bool joined_ = false; // Naett state diff --git a/Common/Net/HTTPRequest.cpp b/Common/Net/HTTPRequest.cpp index 318470a09d..e982ff81f9 100644 --- a/Common/Net/HTTPRequest.cpp +++ b/Common/Net/HTTPRequest.cpp @@ -3,9 +3,34 @@ #include "Common/Net/HTTPNaettRequest.h" #include "Common/TimeUtil.h" #include "Common/StringUtils.h" +#include "Common/Log.h" +#include "Common/System/OSD.h" namespace http { +Download::Download(RequestMethod method, const std::string &url, const std::string &name, bool *cancelled, ProgressBarMode mode) : method_(method), url_(url), name_(name), progress_(cancelled), progressBarMode_(mode) { + progress_.callback = [=](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 (progressBarMode_ != ProgressBarMode::NONE) { + if (!done) { + g_OSD.SetProgressBar(url_, std::move(message), 0.0f, (float)contentLength, (float)bytes, progressBarMode_ == ProgressBarMode::DELAYED ? 3.0f : 0.0f); // delay 3 seconds before showing. + } else { + g_OSD.RemoveProgressBar(url_, Failed() ? false : true, 0.5f); + } + } + }; +} + bool RequestManager::IsHttpsUrl(const std::string &url) { return startsWith(url, "https:"); } diff --git a/Common/Net/HTTPRequest.h b/Common/Net/HTTPRequest.h index fc83c3f09b..0e7686009b 100644 --- a/Common/Net/HTTPRequest.h +++ b/Common/Net/HTTPRequest.h @@ -23,7 +23,7 @@ enum class ProgressBarMode { // Abstract request. class Download { public: - Download(const std::string &url, const std::string &name, bool *cancelled) : url_(url), name_(name), progress_(cancelled) {} + Download(RequestMethod method, const std::string &url, const std::string &name, bool *cancelled, ProgressBarMode mode); virtual ~Download() {} void SetAccept(const char *mime) { @@ -68,12 +68,14 @@ public: protected: std::function callback_; + RequestMethod method_; std::string url_; std::string name_; const char *acceptMime_ = "*/*"; std::string userAgent_; net::RequestProgress progress_; + ProgressBarMode progressBarMode_; private: }; diff --git a/Common/Net/NetBuffer.cpp b/Common/Net/NetBuffer.cpp index 3cdbbcd189..06310a6c83 100644 --- a/Common/Net/NetBuffer.cpp +++ b/Common/Net/NetBuffer.cpp @@ -22,6 +22,18 @@ namespace net { +void RequestProgress::Update(int64_t downloaded, int64_t totalBytes, bool done) { + if (totalBytes) { + progress = (double)downloaded / (double)totalBytes; + } else { + progress = 0.01f; + } + + if (callback) { + callback(downloaded, totalBytes, done); + } +} + bool Buffer::FlushSocket(uintptr_t sock, double timeout, bool *cancelled) { static constexpr float CANCEL_INTERVAL = 0.25f; for (size_t pos = 0, end = data_.size(); pos < end; ) { diff --git a/Common/Net/NetBuffer.h b/Common/Net/NetBuffer.h index 85b1421d28..32525ad3f7 100644 --- a/Common/Net/NetBuffer.h +++ b/Common/Net/NetBuffer.h @@ -12,16 +12,7 @@ public: RequestProgress() {} explicit RequestProgress(bool *c) : cancelled(c) {} - void Update(int64_t downloaded, int64_t totalBytes, bool done) { - if (totalBytes) { - progress = (double)downloaded / (double)totalBytes; - } else { - progress = 0.01f; - } - if (callback) { - callback(downloaded, totalBytes, done); - } - } + void Update(int64_t downloaded, int64_t totalBytes, bool done); float progress = 0.0f; float kBps = 0.0f;