From 05adc64eb6354473723ca52da96ddc53b13445ad Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Mon, 2 Jan 2023 14:49:33 +0100 Subject: [PATCH] Use IndexOfAnyValues in MultipartContent (#79788) --- .../src/System/Net/Http/MultipartContent.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs index 49014faa89821..1e9ce91f2edf0 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs @@ -23,6 +23,9 @@ public class MultipartContent : HttpContent, IEnumerable private const int ColonSpaceLength = 2; private const int CommaSpaceLength = 2; + private static readonly IndexOfAnyValues s_allowedBoundaryChars = + IndexOfAnyValues.Create(" '()+,-./0123456789:=?ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"); + private readonly List _nestedContent; private readonly string _boundary; @@ -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)); } }