Fixed: Changes in http redirect logic causing failed grabs and >25% cpu usage.

pull/2/head
Leonardo Galli 7 years ago
parent 799a448544
commit a48793edd2

@ -132,11 +132,26 @@ namespace NzbDrone.Common.Test.Http
var request = new HttpRequest(string.Format("http://{0}/redirect/1", _httpBinHost));
request.AllowAutoRedirect = true;
Subject.Get(request);
var response = Subject.Get(request);
response.StatusCode.Should().Be(HttpStatusCode.OK);
ExceptionVerification.ExpectedErrors(0);
}
[Test]
public void should_not_follow_redirects()
{
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1");
request.AllowAutoRedirect = false;
var response = Subject.Get(request);
response.StatusCode.Should().Be(HttpStatusCode.Found);
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void should_follow_redirects_to_https()
{

@ -45,36 +45,33 @@ 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 && response.HasHttpRedirect)
{
if (request.AllowAutoRedirect)
var autoRedirectChain = new List<string>();
autoRedirectChain.Add(request.Url.ToString());
do
{
request.Url += new HttpUri(response.Headers.GetSingleValue("Location"));
autoRedirectChain.Add(request.Url.ToString());
_logger.Trace("Redirected to {0}", request.Url);
autoRedirectCount++;
if (autoRedirectCount > 3)
if (autoRedirectChain.Count > 3)
{
throw new WebException($"Too many automatic redirections were attempted for {string.Join(" -> ", autoRedirectChain)}", WebExceptionStatus.ProtocolError);
}
response = ExecuteRequest(request);
}
else if (!RuntimeInfoBase.IsProduction)
{
_logger.Error("Server requested a redirect to [{0}]. Update the request URL to avoid this redirect.", response.Headers["Location"]);
break;
}
while (response.HasHttpRedirect);
}
if (response.HasHttpRedirect && !RuntimeInfoBase.IsProduction)
{
_logger.Error("Server requested a redirect to [{0}] while in developer mode. Update the request URL to avoid this redirect.", response.Headers["Location"]);
}
if (!request.SuppressHttpError && response.HasHttpError)

@ -35,7 +35,7 @@ namespace NzbDrone.Common.Http
private string _content;
public string Content
public string Content
{
get
{
@ -51,6 +51,10 @@ namespace NzbDrone.Common.Http
public bool HasHttpError => (int)StatusCode >= 400;
public bool HasHttpRedirect => StatusCode == HttpStatusCode.Moved ||
StatusCode == HttpStatusCode.MovedPermanently ||
StatusCode == HttpStatusCode.Found;
public Dictionary<string, string> GetCookies()
{
var result = new Dictionary<string, string>();
@ -95,4 +99,4 @@ namespace NzbDrone.Common.Http
public T Resource { get; private set; }
}
}
}

Loading…
Cancel
Save