HTTPClient: Generalize Download to support GET and POST

This commit is contained in:
Henrik Rydgård 2023-06-17 22:29:32 +02:00
parent 93fbb5cdda
commit 601e767e3b
2 changed files with 41 additions and 9 deletions

View file

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

View file

@ -97,10 +97,15 @@ protected:
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();
@ -154,11 +159,13 @@ public:
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_;
@ -190,10 +197,17 @@ public:
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: