| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Globalization; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Security |
| | | 8 | | { |
| | | 9 | | internal static class TargetHostNameHelper |
| | | 10 | | { |
| | 0 | 11 | | private static readonly IdnMapping s_idnMapping = new IdnMapping(); |
| | 0 | 12 | | private static readonly SearchValues<char> s_safeDnsChars = |
| | 0 | 13 | | SearchValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"); |
| | | 14 | | |
| | | 15 | | private static bool IsSafeDnsString(ReadOnlySpan<char> name) => |
| | 0 | 16 | | !name.ContainsAnyExcept(s_safeDnsChars); |
| | | 17 | | |
| | | 18 | | internal static string NormalizeHostName(string? targetHost) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (string.IsNullOrEmpty(targetHost)) |
| | 0 | 21 | | { |
| | 0 | 22 | | return string.Empty; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | // RFC 6066 section 3 says to exclude trailing dot from fully qualified DNS hostname |
| | 0 | 26 | | targetHost = targetHost.TrimEnd('.'); |
| | | 27 | | |
| | | 28 | | try |
| | 0 | 29 | | { |
| | 0 | 30 | | return s_idnMapping.GetAscii(targetHost); |
| | | 31 | | } |
| | 0 | 32 | | catch (ArgumentException) when (IsSafeDnsString(targetHost)) |
| | 0 | 33 | | { |
| | | 34 | | // Seems like name that does not conform to IDN but appears somewhat valid according to original DNS rfc |
| | 0 | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | return targetHost; |
| | 0 | 38 | | } |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |