|
|
|
@ -7,6 +7,7 @@ using System.Net;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
|
using NzbDrone.Common.Http.Dispatchers;
|
|
|
|
|
using NzbDrone.Common.TPL;
|
|
|
|
|
|
|
|
|
@ -50,6 +51,57 @@ namespace NzbDrone.Common.Http
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HttpResponse Execute(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
var autoRedirectCount = 0;
|
|
|
|
|
var autoRedirectChain = new List<string>();
|
|
|
|
|
autoRedirectChain.Add(request.Url.ToString());
|
|
|
|
|
|
|
|
|
|
var response = ExecuteRequest(request);
|
|
|
|
|
|
|
|
|
|
while (response.StatusCode == HttpStatusCode.Moved ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.MovedPermanently ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.Found)
|
|
|
|
|
{
|
|
|
|
|
if (request.AllowAutoRedirect)
|
|
|
|
|
{
|
|
|
|
|
request.Url += new HttpUri(response.Headers.GetSingleValue("Location"));
|
|
|
|
|
autoRedirectChain.Add(request.Url.ToString());
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Redirected to {0}", request.Url);
|
|
|
|
|
|
|
|
|
|
autoRedirectCount++;
|
|
|
|
|
if (autoRedirectCount > 3)
|
|
|
|
|
{
|
|
|
|
|
throw new WebException($"Too many automatic redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = ExecuteRequest(request);
|
|
|
|
|
}
|
|
|
|
|
else if (!RuntimeInfo.IsProduction)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Server requested a redirect to [{0}]. Update the request URL to avoid this redirect.", response.Headers["Location"]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!request.SuppressHttpError && response.HasHttpError)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("HTTP Error - {0}", response);
|
|
|
|
|
|
|
|
|
|
if ((int)response.StatusCode == 429)
|
|
|
|
|
{
|
|
|
|
|
throw new TooManyRequestsException(request, response);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpException(request, response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpResponse ExecuteRequest(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
foreach (var interceptor in _requestInterceptors)
|
|
|
|
|
{
|
|
|
|
@ -85,28 +137,6 @@ namespace NzbDrone.Common.Http
|
|
|
|
|
_logger.Trace("Response content ({0} bytes): {1}", response.ResponseData.Length, response.Content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!RuntimeInfo.IsProduction &&
|
|
|
|
|
(response.StatusCode == HttpStatusCode.Moved ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.MovedPermanently ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.Found))
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Server requested a redirect to [{0}]. Update the request URL to avoid this redirect.", response.Headers["Location"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!request.SuppressHttpError && response.HasHttpError)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("HTTP Error - {0}", response);
|
|
|
|
|
|
|
|
|
|
if ((int)response.StatusCode == 429)
|
|
|
|
|
{
|
|
|
|
|
throw new TooManyRequestsException(request, response);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpException(request, response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|