2023-07-20 11:10:32 +02:00
|
|
|
#include "Common/Net/HTTPRequest.h"
|
|
|
|
#include "Common/Net/HTTPClient.h"
|
2023-07-20 11:25:27 +02:00
|
|
|
#include "Common/Net/HTTPNaettRequest.h"
|
2023-07-20 11:10:32 +02:00
|
|
|
#include "Common/TimeUtil.h"
|
2023-07-20 11:25:27 +02:00
|
|
|
#include "Common/StringUtils.h"
|
2023-07-20 11:10:32 +02:00
|
|
|
|
|
|
|
namespace http {
|
|
|
|
|
2023-07-20 11:25:27 +02:00
|
|
|
bool RequestManager::IsHttpsUrl(const std::string &url) {
|
|
|
|
return startsWith(url, "https:");
|
|
|
|
}
|
|
|
|
|
2023-07-20 11:10:32 +02:00
|
|
|
std::shared_ptr<Download> RequestManager::StartDownload(const std::string &url, const Path &outfile, ProgressBarMode mode, const char *acceptMime) {
|
2023-07-20 11:25:27 +02:00
|
|
|
std::shared_ptr<Download> dl;
|
|
|
|
if (IsHttpsUrl(url)) {
|
2023-07-20 16:01:51 +02:00
|
|
|
#ifndef HTTPS_NOT_AVAILABLE
|
2023-07-20 11:25:27 +02:00
|
|
|
dl.reset(new HTTPSDownload(RequestMethod::GET, url, "", "", outfile, mode));
|
2023-07-20 16:01:51 +02:00
|
|
|
#else
|
|
|
|
return std::shared_ptr<Download>();
|
|
|
|
#endif
|
2023-07-20 11:25:27 +02:00
|
|
|
} else {
|
|
|
|
dl.reset(new HTTPDownload(RequestMethod::GET, url, "", "", outfile, mode));
|
|
|
|
}
|
2023-07-20 11:10:32 +02:00
|
|
|
|
|
|
|
if (!userAgent_.empty())
|
|
|
|
dl->SetUserAgent(userAgent_);
|
|
|
|
if (acceptMime)
|
|
|
|
dl->SetAccept(acceptMime);
|
|
|
|
newDownloads_.push_back(dl);
|
|
|
|
dl->Start();
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Download> RequestManager::StartDownloadWithCallback(
|
|
|
|
const std::string &url,
|
|
|
|
const Path &outfile,
|
|
|
|
ProgressBarMode mode,
|
|
|
|
std::function<void(Download &)> callback,
|
|
|
|
const std::string &name,
|
|
|
|
const char *acceptMime) {
|
2023-07-20 11:25:27 +02:00
|
|
|
std::shared_ptr<Download> dl;
|
|
|
|
if (IsHttpsUrl(url)) {
|
2023-07-20 16:01:51 +02:00
|
|
|
#ifndef HTTPS_NOT_AVAILABLE
|
2023-07-20 11:25:27 +02:00
|
|
|
dl.reset(new HTTPSDownload(RequestMethod::GET, url, "", "", outfile, mode, name));
|
2023-07-20 16:01:51 +02:00
|
|
|
#else
|
|
|
|
return std::shared_ptr<Download>();
|
|
|
|
#endif
|
2023-07-20 11:25:27 +02:00
|
|
|
} else {
|
|
|
|
dl.reset(new HTTPDownload(RequestMethod::GET, url, "", "", outfile, mode, name));
|
|
|
|
}
|
2023-07-20 11:10:32 +02:00
|
|
|
if (!userAgent_.empty())
|
|
|
|
dl->SetUserAgent(userAgent_);
|
|
|
|
if (acceptMime)
|
|
|
|
dl->SetAccept(acceptMime);
|
|
|
|
dl->SetCallback(callback);
|
|
|
|
newDownloads_.push_back(dl);
|
|
|
|
dl->Start();
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Download> RequestManager::AsyncPostWithCallback(
|
|
|
|
const std::string &url,
|
|
|
|
const std::string &postData,
|
|
|
|
const std::string &postMime,
|
|
|
|
ProgressBarMode mode,
|
|
|
|
std::function<void(Download &)> callback,
|
|
|
|
const std::string &name) {
|
2023-07-20 11:25:27 +02:00
|
|
|
std::shared_ptr<Download> dl;
|
|
|
|
if (IsHttpsUrl(url)) {
|
2023-07-20 16:01:51 +02:00
|
|
|
#ifndef HTTPS_NOT_AVAILABLE
|
2023-07-20 11:25:27 +02:00
|
|
|
dl.reset(new HTTPSDownload(RequestMethod::POST, url, postData, postMime, Path(), mode, name));
|
2023-07-20 16:01:51 +02:00
|
|
|
#else
|
|
|
|
return std::shared_ptr<Download>();
|
|
|
|
#endif
|
2023-07-20 11:25:27 +02:00
|
|
|
} else {
|
|
|
|
dl.reset(new HTTPDownload(RequestMethod::POST, url, postData, postMime, Path(), mode, name));
|
|
|
|
}
|
2023-07-20 11:10:32 +02:00
|
|
|
if (!userAgent_.empty())
|
|
|
|
dl->SetUserAgent(userAgent_);
|
|
|
|
dl->SetCallback(callback);
|
|
|
|
newDownloads_.push_back(dl);
|
|
|
|
dl->Start();
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestManager::Update() {
|
|
|
|
for (auto iter : newDownloads_) {
|
|
|
|
downloads_.push_back(iter);
|
|
|
|
}
|
|
|
|
newDownloads_.clear();
|
|
|
|
|
|
|
|
restart:
|
|
|
|
for (size_t i = 0; i < downloads_.size(); i++) {
|
|
|
|
auto dl = downloads_[i];
|
|
|
|
if (dl->Done()) {
|
|
|
|
dl->RunCallback();
|
|
|
|
dl->Join();
|
|
|
|
downloads_.erase(downloads_.begin() + i);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestManager::WaitForAll() {
|
|
|
|
// TODO: Should lock? Though, OK if called from main thread, where Update() is called from.
|
|
|
|
while (!downloads_.empty()) {
|
|
|
|
Update();
|
|
|
|
sleep_ms(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestManager::CancelAll() {
|
|
|
|
for (size_t i = 0; i < downloads_.size(); i++) {
|
|
|
|
downloads_[i]->Cancel();
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < downloads_.size(); i++) {
|
|
|
|
downloads_[i]->Join();
|
|
|
|
}
|
|
|
|
downloads_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|