From 96a606839df3c5feca0117cb794df4bf933bfc28 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 17 Dec 2022 12:39:24 +0100 Subject: [PATCH] Use IndexOfAnyValues in MultipartContent --- .../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)); } }