Skip to content

Commit 6ab0f4e

Browse files
authored
Follow-up on IsLocalhost TLD local dev (#62838)
Changed `IsLocalhost` from an instance method to a static method. Updated the parameter to use `host` directly instead of `parsedAddress.Host`. Modified the check for the `.localhost` TLD to ensure the host length is greater than 10, optimizing the condition to avoid unnecessary checks for shorter hostnames.
1 parent 71edbbc commit 6ab0f4e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,21 @@ internal static ListenOptions ParseAddress(string address, out bool https)
142142

143143
return options;
144144

145-
bool IsLocalhost(string host, out string? prefix)
145+
static bool IsLocalhost(string host, out string? prefix)
146146
{
147147
// Check for "localhost"
148-
if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase))
148+
if (string.Equals(host, "localhost", StringComparison.OrdinalIgnoreCase))
149149
{
150150
prefix = null;
151151
return true;
152152
}
153153

154154
// Check for use of .localhost TLD (Top Level Domain)
155-
if (host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase)
156-
&& !string.Equals(parsedAddress.Host, ".localhost", StringComparison.OrdinalIgnoreCase))
155+
const string localhostTld = ".localhost";
156+
if (host.Length > localhostTld.Length && host.EndsWith(localhostTld, StringComparison.OrdinalIgnoreCase))
157157
{
158158
// Take all but the .localhost TLD as the prefix
159-
prefix = host[..^10]; // 10 is the length of ".localhost"
159+
prefix = host[..^localhostTld.Length];
160160
return true;
161161
}
162162

0 commit comments

Comments
 (0)