Skip to content

Use SearchValues in HostString #49114

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

Merged
merged 1 commit into from
Jul 6, 2023
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
44 changes: 21 additions & 23 deletions src/Http/Http.Abstractions/src/HostString.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Http.Abstractions;
Expand All @@ -15,6 +16,15 @@ namespace Microsoft.AspNetCore.Http;
[DebuggerDisplay("{Value}")]
public readonly struct HostString : IEquatable<HostString>
{
// Allowed Characters:
// A-Z, a-z, 0-9, .,
// -, %, [, ], :
// Above for IPV6
private static readonly SearchValues<char> s_safeHostStringChars =
SearchValues.Create("%-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ[]abcdefghijklmnopqrstuvwxyz");

private static readonly IdnMapping s_idnMapping = new();

private readonly string _value;

/// <summary>
Expand Down Expand Up @@ -121,33 +131,23 @@ public override string ToString()
/// <returns>The <see cref="HostString"/> value formated for use in a URI or HTTP header.</returns>
public string ToUriComponent()
{
if (string.IsNullOrEmpty(_value))
if (!HasValue)
{
return string.Empty;
}

int i;
for (i = 0; i < _value.Length; ++i)
if (!_value.AsSpan().ContainsAnyExcept(s_safeHostStringChars))
{
if (!HostStringHelper.IsSafeHostStringChar(_value[i]))
{
break;
}
return _value;
}

if (i != _value.Length)
{
GetParts(_value, out var host, out var port);

var mapping = new IdnMapping();
var encoded = mapping.GetAscii(host.Buffer!, host.Offset, host.Length);
GetParts(_value, out var host, out var port);

return StringSegment.IsNullOrEmpty(port)
? encoded
: string.Concat(encoded, ":", port.ToString());
}
var encoded = s_idnMapping.GetAscii(host.Buffer!, host.Offset, host.Length);

return _value;
return StringSegment.IsNullOrEmpty(port)
? encoded
: string.Concat(encoded, ":", port.AsSpan());
}

/// <summary>
Expand Down Expand Up @@ -177,14 +177,12 @@ public static HostString FromUriComponent(string uriComponent)
if (index >= 0)
{
// Has a port
string port = uriComponent.Substring(index);
var mapping = new IdnMapping();
uriComponent = mapping.GetUnicode(uriComponent, 0, index) + port;
var port = uriComponent.AsSpan(index);
uriComponent = string.Concat(s_idnMapping.GetUnicode(uriComponent, 0, index), port);
}
else
{
var mapping = new IdnMapping();
uriComponent = mapping.GetUnicode(uriComponent);
uriComponent = s_idnMapping.GetUnicode(uriComponent);
}
}
}
Expand Down
35 changes: 0 additions & 35 deletions src/Http/Http.Abstractions/src/Internal/HostStringHelper.cs

This file was deleted.