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

This commit is contained in:
Leonardo Galli 2018-03-04 16:01:40 +01:00
parent 799a448544
commit a48793edd2
3 changed files with 34 additions and 18 deletions

View File

@ -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()
{

View File

@ -45,36 +45,33 @@ namespace NzbDrone.Common.Http
public HttpResponse Execute(HttpRequest request)
{
var autoRedirectCount = 0;
var response = ExecuteRequest(request);
if (request.AllowAutoRedirect && response.HasHttpRedirect)
{
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)
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)

View File

@ -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>();