diff --git a/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs b/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs index a329fc40..49f80ddf 100644 --- a/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs +++ b/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs @@ -620,19 +620,22 @@ public async Task TestResumeImmediatelyAfterCanceling() cancellationCompleted.TrySetResult(true); }; Downloader.DownloadProgressChanged += async (_, e) => { - try + if (canStopDownload && e.ProgressPercentage > 50) { - await semaphoreSlim.WaitAsync(); - if (canStopDownload && e.ProgressPercentage > 50) + try { - canStopDownload = false; - lastProgressPercentage = e.ProgressPercentage; - await Downloader.CancelTaskAsync(); + await semaphoreSlim.WaitAsync(); + if (canStopDownload) + { + canStopDownload = false; + lastProgressPercentage = e.ProgressPercentage; + await Downloader.CancelTaskAsync(); + } + } + finally + { + semaphoreSlim.Release(); } - } - finally - { - semaphoreSlim.Release(); } }; diff --git a/src/Downloader/Downloader.csproj b/src/Downloader/Downloader.csproj index ef6ee23c..d39a650d 100644 --- a/src/Downloader/Downloader.csproj +++ b/src/Downloader/Downloader.csproj @@ -2,7 +2,7 @@ net8.0;net9.0;net10.0; latestMajor - 5.1.1 + 5.2.0 Downloader Behzad Khosravifar bezzad @@ -11,6 +11,7 @@ https://github.com/bezzad/Downloader download-manager, downloader, download, idm, internet, streaming, download-file, stream-downloader, multipart-download + * Fixed issue #220: Some servers don't like the Range header and respond with errors like 403, 404 or 503. even though the file is perfectly downloadable with a normal request (no Range header). * Fixed issue #221: resolved file corruption when using ChunkCount on small files by ensuring the final byte of each chunk (especially the last chunk) is written correctly. true diff --git a/src/Downloader/SocketClient.cs b/src/Downloader/SocketClient.cs index 40c1bbcb..11d172ff 100644 --- a/src/Downloader/SocketClient.cs +++ b/src/Downloader/SocketClient.cs @@ -187,9 +187,12 @@ private async Task FetchResponseHeaders(Request request, bool addRange, Cancella } catch (HttpRequestException exp) { - if (addRange && exp.IsRequestedRangeNotSatisfiable()) + // issue #220: Some servers don't like the Range header and respond with errors like + // 403 (Forbidden), 404 (Not Found), or 503 (Service Unavailable) + // even though the file is perfectly downloadable with a normal request (no Range header). + if (addRange && (exp.IsRequestedRangeNotSatisfiable() || !exp.IsRedirectError())) { - await FetchResponseHeaders(request, false, cancelToken).ConfigureAwait(false); + await FetchResponseHeaders(request, false, cancelToken); } else if (request.Configuration.AllowAutoRedirect && exp.IsRedirectError() &&