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 MultipartContent #79788

Merged
merged 1 commit into from
Jan 2, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class MultipartContent : HttpContent, IEnumerable<HttpContent>
private const int ColonSpaceLength = 2;
private const int CommaSpaceLength = 2;

private static readonly IndexOfAnyValues<char> s_allowedBoundaryChars =
IndexOfAnyValues.Create(" '()+,-./0123456789:=?ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz");

private readonly List<HttpContent> _nestedContent;
private readonly string _boundary;

Expand Down Expand Up @@ -85,15 +88,9 @@ private static void ValidateBoundary(string boundary)
throw new ArgumentException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, boundary), nameof(boundary));
}

const string AllowedMarks = @"'()+_,-./:=? ";

foreach (char ch in boundary)
if (boundary.AsSpan().IndexOfAnyExcept(s_allowedBoundaryChars) >= 0)
{
if (!char.IsAsciiLetterOrDigit(ch) &&
!AllowedMarks.Contains(ch)) // Marks.
{
throw new ArgumentException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, boundary), nameof(boundary));
}
throw new ArgumentException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, boundary), nameof(boundary));
}
}

Expand Down