Skip to content

Commit

Permalink
HTTPClient: Generalize Download to support GET and POST
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jun 17, 2023
1 parent 93fbb5c commit 601e767
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
30 changes: 24 additions & 6 deletions Common/Net/HTTPClient.cpp
Expand Up @@ -458,8 +458,8 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
return 0;
}

Download::Download(const std::string &url, const Path &outfile)
: progress_(&cancelled_), url_(url), outfile_(outfile) {
Download::Download(RequestMethod method, const std::string &url, const std::string &postData, const Path &outfile)
: method_(method), progress_(&cancelled_), url_(url), postData_(postData), outfile_(outfile) {
}

Download::~Download() {
Expand All @@ -484,7 +484,7 @@ void Download::SetFailed(int code) {
completed_ = true;
}

int Download::PerformGET(const std::string &url) {
int Download::Perform(const std::string &url) {
Url fileUrl(url);
if (!fileUrl.Valid()) {
return -1;
Expand Down Expand Up @@ -533,7 +533,7 @@ void Download::Do() {

std::string downloadURL = url_;
while (resultCode_ == 0) {
int resultCode = PerformGET(downloadURL);
int resultCode = Perform(downloadURL);
if (resultCode == -1) {
SetFailed(resultCode);
return;
Expand Down Expand Up @@ -575,7 +575,7 @@ void Download::Do() {
}

std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const Path &outfile, const char *acceptMime) {
std::shared_ptr<Download> dl(new Download(url, outfile));
std::shared_ptr<Download> dl(new Download(RequestMethod::GET, url, "", outfile));
if (acceptMime)
dl->SetAccept(acceptMime);
downloads_.push_back(dl);
Expand All @@ -588,7 +588,7 @@ std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
const Path &outfile,
std::function<void(Download &)> callback,
const char *acceptMime) {
std::shared_ptr<Download> dl(new Download(url, outfile));
std::shared_ptr<Download> dl(new Download(RequestMethod::GET, url, "", outfile));
if (acceptMime)
dl->SetAccept(acceptMime);
dl->SetCallback(callback);
Expand All @@ -597,6 +597,17 @@ std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
return dl;
}

std::shared_ptr<Download> Downloader::AsyncPostWithCallback(
const std::string &url,
const std::string &postData,
std::function<void(Download &)> callback) {
std::shared_ptr<Download> dl(new Download(RequestMethod::POST, url, postData, Path()));
dl->SetCallback(callback);
downloads_.push_back(dl);
dl->Start();
return dl;
}

void Downloader::Update() {
restart:
for (size_t i = 0; i < downloads_.size(); i++) {
Expand All @@ -610,6 +621,13 @@ void Downloader::Update() {
}
}

void Downloader::WaitForAll() {
// TODO: Should lock? Though, OK if called from main thread, where Update() is called from.
while (!downloads_.empty()) {
sleep_ms(10);
}
}

std::vector<float> Downloader::GetCurrentProgress() {
std::vector<float> progress;
for (size_t i = 0; i < downloads_.size(); i++) {
Expand Down
20 changes: 17 additions & 3 deletions Common/Net/HTTPClient.h
Expand Up @@ -97,10 +97,15 @@ class Client : public net::Connection {
double dataTimeout_ = 900.0;
};

// Not particularly efficient, but hey - it's a background download, that's pretty cool :P
enum class RequestMethod {
GET,
POST,
};

// Really an asynchronous request.
class Download {
public:
Download(const std::string &url, const Path &outfile);
Download(RequestMethod method, const std::string &url, const std::string &postData, const Path &outfile);
~Download();

void Start();
Expand Down Expand Up @@ -154,11 +159,13 @@ class Download {

private:
void Do(); // Actually does the download. Runs on thread.
int PerformGET(const std::string &url);
int Perform(const std::string &url);
std::string RedirectLocation(const std::string &baseUrl);
void SetFailed(int code);

RequestProgress progress_;
RequestMethod method_;
std::string postData_;
Buffer buffer_;
std::vector<std::string> responseHeaders_;
std::string url_;
Expand Down Expand Up @@ -190,10 +197,17 @@ class Downloader {
std::function<void(Download &)> callback,
const char *acceptMime = nullptr);

std::shared_ptr<Download> AsyncPostWithCallback(
const std::string &url,
const std::string &postData,
std::function<void(Download &)> callback);

// Drops finished downloads from the list.
void Update();
void CancelAll();

void WaitForAll();

std::vector<float> GetCurrentProgress();

private:
Expand Down

0 comments on commit 601e767

Please sign in to comment.