Skip to content
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

Use IndexOfAnyValues in Cookie DomainCharsTest #78896

Merged
merged 1 commit into from
Nov 28, 2022
Merged
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
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");
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved

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