Skip to content

Commit

Permalink
http: Specify expected mime types in downloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Aug 22, 2021
1 parent 4c51f47 commit afcf6d8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
11 changes: 8 additions & 3 deletions Common/Net/HTTPClient.cpp
Expand Up @@ -509,7 +509,7 @@ int Download::PerformGET(const std::string &url) {
return -1;
}

RequestParams req(fileUrl.Resource(), "*/*");
RequestParams req(fileUrl.Resource(), acceptMime_);
return client.GET(req, &buffer_, responseHeaders_, &progress_);
}

Expand Down Expand Up @@ -571,8 +571,10 @@ void Download::Do() {
completed_ = true;
}

std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const Path &outfile) {
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));
if (acceptMime)
dl->SetAccept(acceptMime);
downloads_.push_back(dl);
dl->Start();
return dl;
Expand All @@ -581,8 +583,11 @@ std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, cons
std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
const std::string &url,
const Path &outfile,
std::function<void(Download &)> callback) {
std::function<void(Download &)> callback,
const char *acceptMime) {
std::shared_ptr<Download> dl(new Download(url, outfile));
if (acceptMime)
dl->SetAccept(acceptMime);
dl->SetCallback(callback);
downloads_.push_back(dl);
dl->Start();
Expand Down
10 changes: 8 additions & 2 deletions Common/Net/HTTPClient.h
Expand Up @@ -120,6 +120,10 @@ class Download {
std::string url() const { return url_; }
const Path &outfile() const { return outfile_; }

void SetAccept(const char *mime) {
acceptMime_ = mime;
}

// If not downloading to a file, access this to get the result.
Buffer &buffer() { return buffer_; }
const Buffer &buffer() const { return buffer_; }
Expand Down Expand Up @@ -160,6 +164,7 @@ class Download {
std::string url_;
Path outfile_;
std::thread thread_;
const char *acceptMime_ = "*/*";
int resultCode_ = 0;
bool completed_ = false;
bool failed_ = false;
Expand All @@ -177,12 +182,13 @@ class Downloader {
CancelAll();
}

std::shared_ptr<Download> StartDownload(const std::string &url, const Path &outfile);
std::shared_ptr<Download> StartDownload(const std::string &url, const Path &outfile, const char *acceptMime = nullptr);

std::shared_ptr<Download> StartDownloadWithCallback(
const std::string &url,
const Path &outfile,
std::function<void(Download &)> callback);
std::function<void(Download &)> callback,
const char *acceptMime = nullptr);

// Drops finished downloads from the list.
void Update();
Expand Down
5 changes: 3 additions & 2 deletions Core/Config.cpp
Expand Up @@ -1393,8 +1393,9 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
// splash screen quickly), but then we'll just show the notification next time instead, we store the
// upgrade number in the ini.
if (iRunCount % 10 == 0 && bCheckForNewVersion) {
std::shared_ptr<http::Download> dl = g_DownloadManager.StartDownloadWithCallback(
"http://www.ppsspp.org/version.json", Path(), &DownloadCompletedCallback);
const char *versionUrl = "http://www.ppsspp.org/version.json";
const char *acceptMime = "application/json, text/*; q=0.9, */*; q=0.8";
auto dl = g_DownloadManager.StartDownloadWithCallback(versionUrl, Path(), &DownloadCompletedCallback, acceptMime);
dl->SetHidden(true);
}

Expand Down
3 changes: 2 additions & 1 deletion Core/Util/GameManager.cpp
Expand Up @@ -100,7 +100,8 @@ bool GameManager::DownloadAndInstall(std::string storeFileUrl) {
}

Path filename = GetTempFilename();
curDownload_ = g_DownloadManager.StartDownload(storeFileUrl, filename);
const char *acceptMime = "application/zip, application/x-cso, application/x-iso9660-image, application/octet-stream; q=0.9, */*; q=0.8";
curDownload_ = g_DownloadManager.StartDownload(storeFileUrl, filename, acceptMime);
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion UI/DevScreens.cpp
Expand Up @@ -1206,7 +1206,8 @@ void FrameDumpTestScreen::update() {
UIScreen::update();

if (!listing_) {
listing_ = g_DownloadManager.StartDownload(framedumpsBaseUrl, Path());
const char *acceptMime = "text/html, */*; q=0.8";
listing_ = g_DownloadManager.StartDownload(framedumpsBaseUrl, Path(), acceptMime);
}

if (listing_ && listing_->Done() && files_.empty()) {
Expand Down
8 changes: 5 additions & 3 deletions UI/Store.cpp
Expand Up @@ -134,7 +134,9 @@ void HttpImageFileView::DownloadCompletedCallback(http::Download &download) {
void HttpImageFileView::Draw(UIContext &dc) {
using namespace Draw;
if (!texture_ && !textureFailed_ && !path_.empty() && !download_) {
download_ = downloader_->StartDownloadWithCallback(path_, Path(), std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1));
auto cb = std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1);
const char *acceptMime = "image/png, image/jpeg, image/*; q=0.9, */*; q=0.8";
download_ = downloader_->StartDownloadWithCallback(path_, Path(), cb, acceptMime);
download_->SetHidden(true);
}

Expand Down Expand Up @@ -366,8 +368,8 @@ StoreScreen::StoreScreen() {
loading_ = true;

std::string indexPath = storeBaseUrl + "index.json";

listing_ = g_DownloadManager.StartDownload(indexPath, Path());
const char *acceptMime = "application/json, */*; q=0.8";
listing_ = g_DownloadManager.StartDownload(indexPath, Path(), acceptMime);
}

StoreScreen::~StoreScreen() {
Expand Down

0 comments on commit afcf6d8

Please sign in to comment.