Skip to content

Commit

Permalink
Fix pack download url encoding woes
Browse files Browse the repository at this point in the history
Also remove a double free on shutdown in ~Download, update dlspeed in some situations it wasn't working, and add a remove_if that was missing in the pack download thread
  • Loading branch information
nico-abram committed Sep 11, 2022
1 parent f47c91b commit 1fdd5bf
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/Etterna/Singletons/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,19 @@ DownloadManager::DownloadAndInstallPack(DownloadablePack* pack, bool mirror)
DLMAN->DownloadQueue.push_back(std::make_pair(pack, mirror));
return nullptr;
}
auto dl = DownloadAndInstallPack(mirror ? pack->mirror : pack->url,
string& url = mirror ? pack->mirror
: pack->url;
auto last_slash = url.find_last_of('/');
auto base_url = url.substr(0, last_slash + 1);
auto filename = url.substr(last_slash + 1);
int outlength = 0;
char* unescaped_c_char_filename = curl_easy_unescape(
nullptr, filename.c_str(), filename.length(), &outlength);
std::string unescaped_filename(unescaped_c_char_filename, outlength);
curl_free(unescaped_c_char_filename);
string encoded_url = base_url + UrlEncode(unescaped_filename);

auto dl = DownloadAndInstallPack(encoded_url,
pack->name + ".zip");
dl->p_Pack = pack;
dl->p_Pack->downloading = true;
Expand Down Expand Up @@ -433,6 +445,16 @@ DownloadManager::init()
curl_easy_cleanup(msg->easy_handle);
}
if (!result_handles.empty()) {
std::remove_if(local_http_reqs.begin(),
local_http_reqs.end(),
[result_handles](CURL* x) {
return std::find_if(
result_handles.begin(),
result_handles.end(),
[x](auto pair) {
return pair.first == x;
}) != result_handles.end();
});
handle_count_changed = true;
{
const std::lock_guard<std::mutex> lock(
Expand Down Expand Up @@ -565,6 +587,14 @@ void
DownloadManager::UpdatePacks(float fDeltaSeconds)
{
timeSinceLastDownload += fDeltaSeconds;
for (auto& x : downloads) {
/*if (x.second == nullptr) {
Locator::getLogger()->warn("Pack download was null? URL: {}",
dl.first);
continue;
}*/
x.second->Update(fDeltaSeconds);
}
if (!pendingInstallDownloads.empty() && !gameplay) {
// Install all pending packs
for (auto i = pendingInstallDownloads.begin();
Expand Down Expand Up @@ -634,14 +664,6 @@ DownloadManager::UpdatePacks(float fDeltaSeconds)
if (finishedADownload && downloads.empty()) {
MESSAGEMAN->Broadcast("AllDownloadsCompleted");
}
for (auto& x : downloads) {
/*if (x.second == nullptr) {
Locator::getLogger()->warn("Pack download was null? URL: {}",
dl.first);
continue;
}*/
x.second->Update(fDeltaSeconds);
}
}

std::shared_ptr<Download>
Expand Down Expand Up @@ -2259,7 +2281,6 @@ DownloadManager::RefreshPackList(const string& url)
tmp.size = pack["size"].GetInt();
else
tmp.size = 0;

packlist.push_back(tmp);
}
if (MESSAGEMAN != nullptr)
Expand Down Expand Up @@ -2299,6 +2320,7 @@ Download::Download(std::string url, std::string filename)
return b;
}));
curl_easy_setopt(handle, CURLOPT_URL, m_Url.c_str());

curl_easy_setopt(handle, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(handle,
CURLOPT_XFERINFOFUNCTION,
Expand All @@ -2322,10 +2344,6 @@ Download::Download(std::string url, std::string filename)

Download::~Download()
{
if (handle != nullptr) {
curl_easy_cleanup(handle);
handle = nullptr;
}
FILEMAN->Remove(m_TempFileName);
}

Expand Down

0 comments on commit 1fdd5bf

Please sign in to comment.