Skip to content

Commit

Permalink
Cache permanent redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jun 8, 2021
1 parent c76bef5 commit 442cb62
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Core/Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ protected override WebRequest GetWebRequest(Uri address)
log.InfoFormat("Setting MIME type {0}", mimeType);
Headers.Add("Accept", mimeType);
}
if (permanentRedirects.TryGetValue(address, out Uri redirUri))
{
// Obey a previously received permanent redirect
address = redirUri;
}
var request = base.GetWebRequest(address);
if (request is HttpWebRequest hwr)
{
Expand Down Expand Up @@ -417,6 +422,11 @@ protected override WebResponse GetWebResponse(WebRequest request)
log.InfoFormat("Host mismatch, purging token for redirect");
Headers.Remove("Authorization");
}
// Moved or PermanentRedirect
if (statusCode == 301 || statusCode == 308)
{
permanentRedirects.Add(request.RequestUri, redirUri);
}
return GetWebResponse(GetWebRequest(redirUri));
}
}
Expand All @@ -425,6 +435,7 @@ protected override WebResponse GetWebResponse(WebRequest request)

private int timeout;
private string mimeType;
private static readonly Dictionary<Uri, Uri> permanentRedirects = new Dictionary<Uri, Uri>();
}

// HACK: The ancient WebClient doesn't support setting the request type to HEAD and WebRequest doesn't support
Expand Down

0 comments on commit 442cb62

Please sign in to comment.