Skip to content

Commit

Permalink
Use IndexOfAnyValues in Cookie DomainCharsTest (#78896)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Nov 28, 2022
1 parent f35444a commit 0fc047c
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public sealed class Cookie
// Space (' ') should be reserved as well per RFCs, but major web browsers support it and some web sites use it - so we support it too
private static readonly IndexOfAnyValues<char> s_reservedToNameChars = IndexOfAnyValues.Create("\t\r\n=;,");

private static readonly IndexOfAnyValues<char> s_domainChars =
IndexOfAnyValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz");

private string m_comment = string.Empty; // Do not rename (binary serialization)
private Uri? m_commentUri; // Do not rename (binary serialization)
private CookieVariant m_cookieVariant = CookieVariant.Plain; // Do not rename (binary serialization)
Expand Down Expand Up @@ -560,22 +563,9 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma

// Very primitive test to make sure that the name does not have illegal characters
// as per RFC 952 (relaxed on first char could be a digit and string can have '_').
private static bool DomainCharsTest(string name)
{
if (name == null || name.Length == 0)
{
return false;
}
for (int i = 0; i < name.Length; ++i)
{
char ch = name[i];
if (!(char.IsAsciiLetterOrDigit(ch) || ch == '.' || ch == '-' || ch == '_'))
{
return false;
}
}
return true;
}
private static bool DomainCharsTest(string name) =>
!string.IsNullOrEmpty(name) &&
name.AsSpan().IndexOfAnyExcept(s_domainChars) < 0;

[AllowNull]
public string Port
Expand Down

0 comments on commit 0fc047c

Please sign in to comment.