mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
Fixed: Handle download redirects to magnet links
This commit is contained in:
@@ -27,6 +27,7 @@ namespace NzbDrone.Core.Indexers
|
|||||||
where TSettings : IIndexerSettings, new()
|
where TSettings : IIndexerSettings, new()
|
||||||
{
|
{
|
||||||
protected const int MaxNumResultsPerQuery = 1000;
|
protected const int MaxNumResultsPerQuery = 1000;
|
||||||
|
private const int MaxRedirects = 5;
|
||||||
|
|
||||||
protected readonly IIndexerHttpClient _httpClient;
|
protected readonly IIndexerHttpClient _httpClient;
|
||||||
protected readonly IEventAggregator _eventAggregator;
|
protected readonly IEventAggregator _eventAggregator;
|
||||||
@@ -239,11 +240,47 @@ namespace NzbDrone.Core.Indexers
|
|||||||
request.RateLimit = RateLimit;
|
request.RateLimit = RateLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request.AllowAutoRedirect = false;
|
||||||
|
|
||||||
byte[] fileData;
|
byte[] fileData;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await _httpClient.ExecuteProxiedAsync(request, Definition);
|
var response = await _httpClient.ExecuteProxiedAsync(request, Definition);
|
||||||
|
|
||||||
|
if (response.StatusCode is HttpStatusCode.MovedPermanently or HttpStatusCode.Found or HttpStatusCode.SeeOther)
|
||||||
|
{
|
||||||
|
var autoRedirectChain = new List<string> { request.Url.ToString() };
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
var redirectUrl = response.RedirectUrl;
|
||||||
|
|
||||||
|
_logger.Debug("Download request is being redirected to: {0}", redirectUrl);
|
||||||
|
|
||||||
|
if (redirectUrl.IsNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
throw new WebException("Remote website tried to redirect without providing a location.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (redirectUrl.StartsWith("magnet:"))
|
||||||
|
{
|
||||||
|
return await Download(new Uri(redirectUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
request.Url = new HttpUri(redirectUrl);
|
||||||
|
autoRedirectChain.Add(request.Url.ToString());
|
||||||
|
|
||||||
|
if (autoRedirectChain.Count > MaxRedirects)
|
||||||
|
{
|
||||||
|
throw new WebException($"Too many download redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError);
|
||||||
|
}
|
||||||
|
|
||||||
|
response = await _httpClient.ExecuteProxiedAsync(request, Definition);
|
||||||
|
}
|
||||||
|
while (response.StatusCode is HttpStatusCode.MovedPermanently or HttpStatusCode.Found or HttpStatusCode.SeeOther);
|
||||||
|
}
|
||||||
|
|
||||||
fileData = response.ResponseData;
|
fileData = response.ResponseData;
|
||||||
|
|
||||||
_logger.Debug("Downloaded for release finished ({0} bytes from {1})", fileData.Length, link.AbsoluteUri);
|
_logger.Debug("Downloaded for release finished ({0} bytes from {1})", fileData.Length, link.AbsoluteUri);
|
||||||
@@ -287,10 +324,7 @@ namespace NzbDrone.Core.Indexers
|
|||||||
|
|
||||||
protected virtual Task<HttpRequest> GetDownloadRequest(Uri link)
|
protected virtual Task<HttpRequest> GetDownloadRequest(Uri link)
|
||||||
{
|
{
|
||||||
var requestBuilder = new HttpRequestBuilder(link.AbsoluteUri)
|
var requestBuilder = new HttpRequestBuilder(link.AbsoluteUri);
|
||||||
{
|
|
||||||
AllowAutoRedirect = FollowRedirect
|
|
||||||
};
|
|
||||||
|
|
||||||
if (Cookies != null)
|
if (Cookies != null)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user