diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index 7ef0c15df1..82865e3410 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -458,8 +458,8 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector Downloader::StartDownload(const std::string &url, const Path &outfile, const char *acceptMime) { - std::shared_ptr dl(new Download(url, outfile)); + std::shared_ptr dl(new Download(RequestMethod::GET, url, "", outfile)); if (acceptMime) dl->SetAccept(acceptMime); downloads_.push_back(dl); @@ -588,7 +588,7 @@ std::shared_ptr Downloader::StartDownloadWithCallback( const Path &outfile, std::function callback, const char *acceptMime) { - std::shared_ptr dl(new Download(url, outfile)); + std::shared_ptr dl(new Download(RequestMethod::GET, url, "", outfile)); if (acceptMime) dl->SetAccept(acceptMime); dl->SetCallback(callback); @@ -597,6 +597,17 @@ std::shared_ptr Downloader::StartDownloadWithCallback( return dl; } +std::shared_ptr Downloader::AsyncPostWithCallback( + const std::string &url, + const std::string &postData, + std::function callback) { + std::shared_ptr 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 Downloader::GetCurrentProgress() { std::vector progress; for (size_t i = 0; i < downloads_.size(); i++) { diff --git a/Common/Net/HTTPClient.h b/Common/Net/HTTPClient.h index cafe4b08c5..d156a18301 100644 --- a/Common/Net/HTTPClient.h +++ b/Common/Net/HTTPClient.h @@ -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 responseHeaders_; std::string url_; @@ -190,10 +197,17 @@ public: std::function callback, const char *acceptMime = nullptr); + std::shared_ptr AsyncPostWithCallback( + const std::string &url, + const std::string &postData, + std::function callback); + // Drops finished downloads from the list. void Update(); void CancelAll(); + void WaitForAll(); + std::vector GetCurrentProgress(); private: