Skip to content

Commit

Permalink
Use the patched naett functions to implement progress updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jul 21, 2023
1 parent c17a559 commit 9535b16
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 39 deletions.
23 changes: 1 addition & 22 deletions Common/Net/HTTPClient.cpp
Expand Up @@ -445,28 +445,7 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
}

HTTPDownload::HTTPDownload(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) {
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) {
INFO_LOG(IO, "Showing progress bar: %s", message.c_str());
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);
}
}
};
: Download(method, url, name, &cancelled_, progressBarMode), postData_(postData), postMime_(postMime), outfile_(outfile) {
}

HTTPDownload::~HTTPDownload() {
Expand Down
2 changes: 0 additions & 2 deletions Common/Net/HTTPClient.h
Expand Up @@ -125,7 +125,6 @@ class HTTPDownload : public Download {
std::string RedirectLocation(const std::string &baseUrl);
void SetFailed(int code);

RequestMethod method_;
std::string postData_;
Buffer buffer_;
std::vector<std::string> responseHeaders_;
Expand All @@ -136,7 +135,6 @@ class HTTPDownload : public Download {
bool completed_ = false;
bool failed_ = false;
bool cancelled_ = false;
ProgressBarMode progressBarMode_;
bool joined_ = false;
};

Expand Down
13 changes: 10 additions & 3 deletions Common/Net/HTTPNaettRequest.cpp
Expand Up @@ -5,14 +5,15 @@
#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"

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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Common/Net/HTTPNaettRequest.h
Expand Up @@ -51,7 +51,6 @@ class HTTPSDownload : public Download {
bool completed_ = false;
bool failed_ = false;
bool cancelled_ = false;
ProgressBarMode progressBarMode_;
bool joined_ = false;

// Naett state
Expand Down
25 changes: 25 additions & 0 deletions Common/Net/HTTPRequest.cpp
Expand Up @@ -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:");
}
Expand Down
4 changes: 3 additions & 1 deletion Common/Net/HTTPRequest.h
Expand Up @@ -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) {
Expand Down Expand Up @@ -68,12 +68,14 @@ class Download {

protected:
std::function<void(Download &)> callback_;
RequestMethod method_;
std::string url_;
std::string name_;
const char *acceptMime_ = "*/*";
std::string userAgent_;

net::RequestProgress progress_;
ProgressBarMode progressBarMode_;

private:
};
Expand Down
12 changes: 12 additions & 0 deletions Common/Net/NetBuffer.cpp
Expand Up @@ -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; ) {
Expand Down
11 changes: 1 addition & 10 deletions Common/Net/NetBuffer.h
Expand Up @@ -12,16 +12,7 @@ class RequestProgress {
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;
Expand Down

0 comments on commit 9535b16

Please sign in to comment.