From a448b3bc9b576a2600d8c9467eefbfb96732e388 Mon Sep 17 00:00:00 2001 From: Nick Krantz Date: Tue, 20 May 2025 11:10:37 -0500 Subject: [PATCH] use custom http client that follows redirects --- src/Icons/Services/ChangePasswordUriService.cs | 16 ++-------------- src/Icons/Util/ServiceCollectionExtension.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Icons/Services/ChangePasswordUriService.cs b/src/Icons/Services/ChangePasswordUriService.cs index a89d9521d0..2ee0fef797 100644 --- a/src/Icons/Services/ChangePasswordUriService.cs +++ b/src/Icons/Services/ChangePasswordUriService.cs @@ -51,13 +51,7 @@ public class ChangePasswordUriService : IChangePasswordUriService Path = "/.well-known/resource-that-should-not-exist-whose-status-code-should-not-be-200" }; - var request = new HttpRequestMessage(HttpMethod.Get, url.ToString()) - { - Headers = - { - { "Cache-Control", "no-store" }, - } - }; + var request = new HttpRequestMessage(HttpMethod.Get, url.ToString()); var response = await _httpClient.SendAsync(request); return !response.IsSuccessStatusCode; @@ -84,13 +78,7 @@ public class ChangePasswordUriService : IChangePasswordUriService Path = "/.well-known/change-password" }; - var request = new HttpRequestMessage(HttpMethod.Get, url.ToString()) - { - Headers = - { - { "Cache-Control", "no-store" }, - } - }; + var request = new HttpRequestMessage(HttpMethod.Get, url.ToString()); var response = await _httpClient.SendAsync(request); return response.IsSuccessStatusCode ? url.ToString() : null; diff --git a/src/Icons/Util/ServiceCollectionExtension.cs b/src/Icons/Util/ServiceCollectionExtension.cs index 56ee24f45a..3bd3537198 100644 --- a/src/Icons/Util/ServiceCollectionExtension.cs +++ b/src/Icons/Util/ServiceCollectionExtension.cs @@ -28,6 +28,24 @@ public static class ServiceCollectionExtension AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, }); + + // The CreatePasswordUri handler wants similar headers as Icons to portray coming from a browser but + // needs to follow redirects to get the final URL. + services.AddHttpClient("ChangePasswordUri", client => + { + client.Timeout = TimeSpan.FromSeconds(20); + client.MaxResponseContentBufferSize = 5000000; // 5 MB + // Let's add some headers to look like we're coming from a web browser request. Some websites + // will block our request without these. + client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + + "(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"); + client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8"); + client.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); + client.DefaultRequestHeaders.Add("Pragma", "no-cache"); + }).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + }); } public static void AddHtmlParsing(this IServiceCollection services)