-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C# Uri with underscore #44719
Comments
Tagging subscribers to this area: @dotnet/ncl Issue Details
|
Fails also on .NET 5 - here's minimal repro poking at various variants: static void Main()
{
PrintAbsoluteUri_IsValid("http://a.com");
PrintAbsoluteUri_IsValid("http://a._com");
PrintAbsoluteUri_IsValid("http://a.b._com");
PrintAbsoluteUri_IsValid("http://a.c_m");
PrintAbsoluteUri_IsValid("http://a.b.c_m");
PrintAbsoluteUri_IsValid("http://a.com_");
PrintAbsoluteUri_IsValid("http://a.b.com_");
PrintAbsoluteUri_IsValid("http://a_b.com");
PrintAbsoluteUri_IsValid("http://_a.com");
PrintAbsoluteUri_IsValid("http://a_.com");
PrintAbsoluteUri_IsValid("http://a._b.com");
PrintAbsoluteUri_IsValid("http://a.b_.com");
PrintAbsoluteUri_IsValid("http://a.a_b.com");
PrintAbsoluteUri_IsValid("http://_a.b.com");
}
static void PrintAbsoluteUri_IsValid(string uri)
{
Console.WriteLine(uri + " ... " + Uri.TryCreate(uri, UriKind.Absolute, out _));
}
@MihaZupan any thoughts? |
While original RFC did not allow '_', it is commonly used. |
I agree with Tomas, '_' is historically in use, there's no point in avoiding it outright. It isn't as simple as being accepted by Uri or not, the input examples also differ by HostNameType:
Dns will be matched first if possible. Every input that contains a label where _ is the first character is not matched by it. Specifically because of this The rest are matched by UNC, which shows up as 'basic' in The interesting case here is that |
Is there something we should do here @MihaZupan? If not, we should perhaps close it. While the DNS my not allow it, URL does not necessarily depend on it (e.g. it is only subset) |
Any updates on this? I'm facing this same issue in our use case where we are trying to create a URI object for (example) URL "http://test.test._test.pimvandijk.eu". This is an example-url, but we will use it like this a lot (the underscore at the third part of the url). Error is: Invalid URI: The hostname could not be parsed. You can check it via this small piece of code: The url works when I'm checking the txt-records via Google Dig: Checked on .net 7 & C# 11 Interesting is that, as @MihaZupan mentioned, "http://test._test.test.pimvandijk.eu" is working perfect, but putting the underscore on the next part of the url fails. |
I found what's happening. Let's take http://test1.test2._test3.pimvandijk.eu for example. In the UncNameHelper.IsValid it will first check "test1.". This is valid and will take us to the next for-loop. Here it checks "test2." and at the check of the . it will set validShortName to false. Coming to the _ of _test3, which will return false at
As mentioned in the comment on line 77 ("Subsequent segments must start with a letter or a digit") I think we have to add underscore to this check, but only if there are coming a letter or digit after the underscore. |
I recently hit this issue as well. As previous comments pointed out, the issue happens when the third or later segment of the URL starts with '_', like |
Description
I try to use Uri class to validate and extract host from URL.
Where are two cases:
Uri.TryCreate("http://_contoso._com", UriKind.Absolute, out _)
returnstrue
Uri.TryCreate("http://_c._contoso._com", UriKind.Absolute, out _)
returnsfalse
Based on Top Level Domain Name Specification:
So, looks like
http://_contoso._com
is invalid absolute URI.Configuration
C# (.NET Framework 4.8)
The text was updated successfully, but these errors were encountered: