Skip to content

Commit

Permalink
Ensure that webrequest has been fully processed, don't just look at i…
Browse files Browse the repository at this point in the history
…ts state
  • Loading branch information
Blake-Madden committed Mar 24, 2024
1 parent f425708 commit 313196c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/util/downloadfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ bool FileDownload::Download(const wxString& url, const wxString& localDownloadPa
}
wxLogVerbose(L"Downloading '%s'", url);
m_downloadPath = localDownloadPath;
m_lastStatus = 404;
m_downloadSuccessful = false;
m_statusHasBeenProcessed = false;

wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
m_handler, url);
request.SetStorage(wxWebRequest::Storage_File);
request.SetHeader(L"User-Agent", GetUserAgent());
request.SetHeader(L"Sec-Fetch-Mode", L"navigate");
request.DisablePeerVerify(IsPeerVerifyDisabled());
m_lastStatus = 404;
m_downloadSuccessful = false;
request.Start();

wxProgressDialog* progressDlg = m_showProgress ?
Expand All @@ -186,7 +188,7 @@ bool FileDownload::Download(const wxString& url, const wxString& localDownloadPa
const auto startTime = std::chrono::system_clock::now();
bool timedOut{ false };
bool fileTooSmall{ false };
while (request.GetState() == wxWebRequest::State_Active)
while (!m_statusHasBeenProcessed)
{
wxYield();
if (request.GetBytesExpectedToReceive() != 0 &&
Expand Down Expand Up @@ -259,18 +261,20 @@ void FileDownload::RequestResponse(const wxString& url)
wxLogVerbose(L"Requesting response from '%s'", url);
m_downloadPath.clear();
m_buffer.clear();
m_lastStatus = 404;
m_statusHasBeenProcessed = false;

wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
m_handler, url);
request.SetStorage(wxWebRequest::Storage_None);
request.SetHeader(L"User-Agent", GetUserAgent());
request.SetHeader(L"Sec-Fetch-Mode", L"navigate");
request.DisablePeerVerify(IsPeerVerifyDisabled());
m_lastStatus = 404;
request.Start();

const auto startTime = std::chrono::system_clock::now();
bool timedOut{ false };
while (request.GetState() == wxWebRequest::State_Active)
while (!m_statusHasBeenProcessed)
{
wxYield();
/* Some misconfigured webpages cause ProcessRequest() to not be called
Expand Down Expand Up @@ -317,19 +321,20 @@ bool FileDownload::Read(const wxString& url)
wxLogVerbose(L"Reading '%s'", url);
m_downloadPath.clear();
m_buffer.clear();
m_lastStatus = 404;
m_statusHasBeenProcessed = false;

wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
m_handler, url);
request.SetStorage(wxWebRequest::Storage_Memory);
request.SetHeader(L"User-Agent", GetUserAgent());
request.SetHeader(L"Sec-Fetch-Mode", L"navigate");
request.DisablePeerVerify(IsPeerVerifyDisabled());
m_lastStatus = 404;
request.Start();

const auto startTime = std::chrono::system_clock::now();
bool timedOut{ false };
while (request.GetState() == wxWebRequest::State_Active)
while (!m_statusHasBeenProcessed)
{
wxYield();
/* Sometimes a connection failure will cause ProcessRequest to not be called,
Expand Down Expand Up @@ -429,6 +434,7 @@ void FileDownload::ProcessRequest(wxWebRequestEvent& evt)
evt.GetResponse().GetStream()->ReadAll(&m_buffer[0], m_buffer.size() - 1);
}
}
m_statusHasBeenProcessed = true;
break;
}
case wxWebRequest::State_Failed:
Expand All @@ -443,8 +449,10 @@ void FileDownload::ProcessRequest(wxWebRequestEvent& evt)
wxLogError(L"Web Request failed: %s",
evt.GetErrorDescription());
}
m_statusHasBeenProcessed = true;
break;
case wxWebRequest::State_Cancelled:
m_statusHasBeenProcessed = true;
break;
case wxWebRequest::State_Unauthorized:
{
Expand Down Expand Up @@ -476,9 +484,11 @@ void FileDownload::ProcessRequest(wxWebRequestEvent& evt)
{
wxLogStatus(L"Authentication challenge canceled");
}
m_statusHasBeenProcessed = true;
break;
}
// Nothing special to do for these states.
// Nothing special to do for these states
// (and nothing has been processed)
case wxWebRequest::State_Active:
[[fallthrough]];
case wxWebRequest::State_Idle:
Expand Down
1 change: 1 addition & 0 deletions src/util/downloadfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class FileDownload
bool m_downloadSuccessful{ false };
bool m_showProgress{ false };
bool m_disablePeerVerify{ false };
bool m_statusHasBeenProcessed{ false };
wxEvtHandler* m_handler{ nullptr };
mutable std::mutex m_mutex;
wxString m_downloadPath;
Expand Down

0 comments on commit 313196c

Please sign in to comment.