mirror of
https://github.com/bitwarden/server.git
synced 2024-11-25 12:45:18 +01:00
recursively follow redirects
This commit is contained in:
parent
16e1e8d78e
commit
221f5fa154
@ -53,32 +53,7 @@ namespace Bit.Icons.Controllers
|
||||
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..32..200" +
|
||||
$"&fallback_icon_url=https://raw.githubusercontent.com/bitwarden/web/master/src/images/fa-globe.png";
|
||||
var response = await _httpClient.GetAsync(iconUrl);
|
||||
|
||||
if(response.StatusCode == HttpStatusCode.Redirect && response.Headers.Contains("Location"))
|
||||
{
|
||||
var locationHeader = response.Headers.GetValues("Location").FirstOrDefault();
|
||||
if(!string.IsNullOrWhiteSpace(locationHeader) &&
|
||||
Uri.TryCreate(locationHeader, UriKind.Absolute, out Uri location))
|
||||
{
|
||||
var message = new HttpRequestMessage
|
||||
{
|
||||
RequestUri = location,
|
||||
Method = HttpMethod.Get
|
||||
};
|
||||
|
||||
// Let's add some headers to look like we're coming from a web browser request. Some websites
|
||||
// will block our request without these.
|
||||
message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
|
||||
message.Headers.Add("Accept-Language", "en-US,en;q=0.8");
|
||||
message.Headers.Add("Cache-Control", "no-cache");
|
||||
message.Headers.Add("Pragma", "no-cache");
|
||||
message.Headers.Add("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
|
||||
|
||||
response = await _httpClient.SendAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
response = await FollowRedirectsAsync(response, 1);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new NotFoundResult();
|
||||
@ -103,5 +78,42 @@ namespace Bit.Icons.Controllers
|
||||
|
||||
return new FileContentResult(icon.Image, icon.Format);
|
||||
}
|
||||
|
||||
private async Task<HttpResponseMessage> FollowRedirectsAsync(HttpResponseMessage response, int followCount)
|
||||
{
|
||||
if(response.IsSuccessStatusCode || followCount > 2)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
if((response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.MovedPermanently) &&
|
||||
response.Headers.Contains("Location"))
|
||||
{
|
||||
var locationHeader = response.Headers.GetValues("Location").FirstOrDefault();
|
||||
if(!string.IsNullOrWhiteSpace(locationHeader) &&
|
||||
Uri.TryCreate(locationHeader, UriKind.Absolute, out Uri location))
|
||||
{
|
||||
var message = new HttpRequestMessage
|
||||
{
|
||||
RequestUri = location,
|
||||
Method = HttpMethod.Get
|
||||
};
|
||||
|
||||
// Let's add some headers to look like we're coming from a web browser request. Some websites
|
||||
// will block our request without these.
|
||||
message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
|
||||
message.Headers.Add("Accept-Language", "en-US,en;q=0.8");
|
||||
message.Headers.Add("Cache-Control", "no-cache");
|
||||
message.Headers.Add("Pragma", "no-cache");
|
||||
message.Headers.Add("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
|
||||
|
||||
response = await _httpClient.SendAsync(message);
|
||||
response = await FollowRedirectsAsync(response, followCount++);
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user