Skip to content

Commit 78f8654

Browse files
committed
Check for download errors properly
Replace the hodgepodge of unreliable checks for download failures introduced in #114 and then #139 with proper check for errors: the dwError field in INTERNET_STATUS_REQUEST_COMPLETE. This makes the code simpler _and_ more reliable. Fixes #147.
1 parent a6fc8fb commit 78f8654

1 file changed

Lines changed: 4 additions & 25 deletions

File tree

src/download.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ std::wstring GetURLFileName(const char *url)
135135

136136
struct DownloadCallbackContext
137137
{
138-
DownloadCallbackContext(InetHandle *conn_) : conn(conn_), wasClosed(false) {}
138+
DownloadCallbackContext(InetHandle *conn_) : conn(conn_), lastError(ERROR_SUCCESS) {}
139139
InetHandle *conn;
140-
bool wasClosed;
140+
DWORD lastError;
141141
Event eventRequestComplete;
142142
};
143143

@@ -157,28 +157,9 @@ void CALLBACK DownloadInternetStatusCallback(_In_ HINTERNET hInternet,
157157
break;
158158

159159
case INTERNET_STATUS_REQUEST_COMPLETE:
160+
context->lastError = res->dwError;
160161
context->eventRequestComplete.Signal();
161162
break;
162-
163-
case INTERNET_STATUS_CONNECTION_CLOSED:
164-
// Mark the connection as closed, but don't close the handle yet.
165-
// This is because this may happen in at least two different
166-
// situations:
167-
// 1. Connection prematurely closed, we need to report an error.
168-
// 2. Redirect to another site (WinInet closes connection, starts
169-
// another one), in which case we DON'T want to error out.
170-
context->wasClosed = true;
171-
break;
172-
173-
case INTERNET_STATUS_REDIRECT:
174-
// Activity like this following INTERNET_STATUS_CONNECTION_CLOSED
175-
// implies that the handle is still doing useful work, so unflag
176-
// it. Otherwise we'd get an error at the very end of download.
177-
//
178-
// See https://github.com/vslavik/winsparkle/pull/139 for what this
179-
// mess is about.
180-
context->wasClosed = false;
181-
break;
182163
}
183164
}
184165

@@ -349,9 +330,7 @@ void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread,
349330

350331
if (ibuf.dwBufferLength == 0)
351332
{
352-
// This check is required in case the INTERNET_STATUS_CONNECTION_CLOSED event was
353-
// received (and the handle was closed) during the call to InternetReadFileEx()
354-
if (context.wasClosed)
333+
if (context.lastError != ERROR_SUCCESS)
355334
throw Win32Exception();
356335
else
357336
break; // all of the file was downloaded

0 commit comments

Comments
 (0)