Skip to content

Commit 51c1ada

Browse files
authored
Use SearchValues in HostString (#49114)
1 parent 2d1cb02 commit 51c1ada

File tree

2 files changed

+21
-58
lines changed

2 files changed

+21
-58
lines changed

src/Http/Http.Abstractions/src/HostString.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers;
45
using System.Diagnostics;
56
using System.Globalization;
67
using Microsoft.AspNetCore.Http.Abstractions;
@@ -15,6 +16,15 @@ namespace Microsoft.AspNetCore.Http;
1516
[DebuggerDisplay("{Value}")]
1617
public readonly struct HostString : IEquatable<HostString>
1718
{
19+
// Allowed Characters:
20+
// A-Z, a-z, 0-9, .,
21+
// -, %, [, ], :
22+
// Above for IPV6
23+
private static readonly SearchValues<char> s_safeHostStringChars =
24+
SearchValues.Create("%-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ[]abcdefghijklmnopqrstuvwxyz");
25+
26+
private static readonly IdnMapping s_idnMapping = new();
27+
1828
private readonly string _value;
1929

2030
/// <summary>
@@ -121,33 +131,23 @@ public override string ToString()
121131
/// <returns>The <see cref="HostString"/> value formated for use in a URI or HTTP header.</returns>
122132
public string ToUriComponent()
123133
{
124-
if (string.IsNullOrEmpty(_value))
134+
if (!HasValue)
125135
{
126136
return string.Empty;
127137
}
128138

129-
int i;
130-
for (i = 0; i < _value.Length; ++i)
139+
if (!_value.AsSpan().ContainsAnyExcept(s_safeHostStringChars))
131140
{
132-
if (!HostStringHelper.IsSafeHostStringChar(_value[i]))
133-
{
134-
break;
135-
}
141+
return _value;
136142
}
137143

138-
if (i != _value.Length)
139-
{
140-
GetParts(_value, out var host, out var port);
141-
142-
var mapping = new IdnMapping();
143-
var encoded = mapping.GetAscii(host.Buffer!, host.Offset, host.Length);
144+
GetParts(_value, out var host, out var port);
144145

145-
return StringSegment.IsNullOrEmpty(port)
146-
? encoded
147-
: string.Concat(encoded, ":", port.ToString());
148-
}
146+
var encoded = s_idnMapping.GetAscii(host.Buffer!, host.Offset, host.Length);
149147

150-
return _value;
148+
return StringSegment.IsNullOrEmpty(port)
149+
? encoded
150+
: string.Concat(encoded, ":", port.AsSpan());
151151
}
152152

153153
/// <summary>
@@ -177,14 +177,12 @@ public static HostString FromUriComponent(string uriComponent)
177177
if (index >= 0)
178178
{
179179
// Has a port
180-
string port = uriComponent.Substring(index);
181-
var mapping = new IdnMapping();
182-
uriComponent = mapping.GetUnicode(uriComponent, 0, index) + port;
180+
var port = uriComponent.AsSpan(index);
181+
uriComponent = string.Concat(s_idnMapping.GetUnicode(uriComponent, 0, index), port);
183182
}
184183
else
185184
{
186-
var mapping = new IdnMapping();
187-
uriComponent = mapping.GetUnicode(uriComponent);
185+
uriComponent = s_idnMapping.GetUnicode(uriComponent);
188186
}
189187
}
190188
}

src/Http/Http.Abstractions/src/Internal/HostStringHelper.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)