Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Atc/Extensions/StringHasIsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,26 @@ public static bool IsUriOpcTcp(this string value)
/// Accepts single-label names (e.g. <c>localhost</c>) and an optional trailing dot (e.g. <c>example.com.</c>).
/// Each label is 1-63 ASCII alphanumeric/hyphen characters and may not start or end with a hyphen;
/// the total length is limited to 253 characters. Underscores and raw Unicode (non-punycode IDN) are not allowed.
/// Per RFC 1123 §2.1 the top-level (right-most) label must not be all-numeric, which also disambiguates
/// host names from IPv4 addresses (e.g. <c>192.168.0.27</c> is rejected).
/// This is a purely syntactic check and does not perform any DNS resolution.
/// </remarks>
public static bool IsHostName(this string value)
=> !string.IsNullOrEmpty(value) &&
RxHostName.Value.IsMatch(value);
{
if (string.IsNullOrEmpty(value) ||
!RxHostName.Value.IsMatch(value))
{
return false;
}

var host = value.EndsWith('.')
? value[..^1]
: value;

var topLevelLabel = host[(host.LastIndexOf('.') + 1)..];

return !topLevelLabel.All(char.IsDigit);
}

/// <summary>
/// Determines whether the specified value is a valid IPv4 address.
Expand Down
5 changes: 5 additions & 0 deletions test/Atc.Tests/Extensions/StringHasIsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public void IsUriOpcTcp(
[InlineData(true, "example.com.")]
[InlineData(true, "a.b.c.d.e.f")]
[InlineData(true, "xn--mnchen-3ya.de")]
[InlineData(true, "host123")]
[InlineData(false, "")]
[InlineData(false, " ")]
[InlineData(false, "-leadinghyphen.com")]
Expand All @@ -392,6 +393,10 @@ public void IsUriOpcTcp(
[InlineData(false, "double..dot.com")]
[InlineData(false, "space in.host")]
[InlineData(false, "münchen.de")]
[InlineData(false, "192.168.0.27")]
[InlineData(false, "1.2.3.4")]
[InlineData(false, "123")]
[InlineData(false, "example.123")]
public void IsHostName(
bool expected,
string input)
Expand Down
Loading