From 87eb0982a7976a4ed08791afba1b9a4448fb092e Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 27 Jul 2026 18:43:25 +0000 Subject: [PATCH] refactor: replace URI string concatenation with EndsWith check Replace new Uri(url.TrimEnd('/') + "/") pattern with explicit EndsWith('/') check to avoid string concatenation inside URI construction. Applies to 3 locations: - Program.cs (Heimdall.Web) - HttpClient base address - TokenRefreshHandler.cs - raw HttpClient for token refresh - Program.cs (Heimdall.Agent) - Central service client --- src/Heimdall.Agent/Program.cs | 3 ++- src/Heimdall.Web/Program.cs | 3 ++- src/Heimdall.Web/Services/TokenRefreshHandler.cs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Heimdall.Agent/Program.cs b/src/Heimdall.Agent/Program.cs index 4bbddae..5016f39 100644 --- a/src/Heimdall.Agent/Program.cs +++ b/src/Heimdall.Agent/Program.cs @@ -31,7 +31,8 @@ builder.Services.AddOptions() builder.Services.AddHttpClient("Central", (sp, client) => { var options = sp.GetRequiredService>().Value; - client.BaseAddress = new Uri(options.CentralUrl.TrimEnd('/') + "/"); + var baseUrl = options.CentralUrl.EndsWith('/') ? options.CentralUrl : options.CentralUrl + "/"; + client.BaseAddress = new Uri(baseUrl); client.Timeout = TimeSpan.FromSeconds(10); }); diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index ed79164..4484d5a 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -368,7 +368,8 @@ public partial class Program { ?? "http://localhost:8080"; builder.Services.AddHttpClient("HeimdallApi", client => { - client.BaseAddress = new Uri(apiBaseUrl.TrimEnd('/') + "/"); + var baseUrl = apiBaseUrl.EndsWith('/') ? apiBaseUrl : apiBaseUrl + "/"; + client.BaseAddress = new Uri(baseUrl); client.Timeout = TimeSpan.FromSeconds(30); }).AddHttpMessageHandler(); diff --git a/src/Heimdall.Web/Services/TokenRefreshHandler.cs b/src/Heimdall.Web/Services/TokenRefreshHandler.cs index cbd3b3a..793cbc6 100644 --- a/src/Heimdall.Web/Services/TokenRefreshHandler.cs +++ b/src/Heimdall.Web/Services/TokenRefreshHandler.cs @@ -81,7 +81,8 @@ public class TokenRefreshHandler : DelegatingHandler // Create a raw HttpClient (NOT the "HeimdallApi" named client which // includes this handler — would cause an infinite loop). - using var rawClient = new HttpClient { BaseAddress = new Uri(_apiBaseUrl.TrimEnd('/') + "/") }; + var baseUrl = _apiBaseUrl.EndsWith('/') ? _apiBaseUrl : _apiBaseUrl + "/"; + using var rawClient = new HttpClient { BaseAddress = new Uri(baseUrl) }; var response = await rawClient.PostAsJsonAsync( "api/auth/refresh", new { refreshToken = _authState.RefreshToken },