diff --git a/src/libraries/System.Net.WebProxy/src/System.Net.WebProxy.csproj b/src/libraries/System.Net.WebProxy/src/System.Net.WebProxy.csproj index 980f1552ded92f..fbe0e197d113c3 100644 --- a/src/libraries/System.Net.WebProxy/src/System.Net.WebProxy.csproj +++ b/src/libraries/System.Net.WebProxy/src/System.Net.WebProxy.csproj @@ -10,6 +10,7 @@ + diff --git a/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs b/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs index f2e8b5fdbb5748..9460000a28e752 100644 --- a/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs +++ b/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs @@ -1,7 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Buffers; using System.Collections; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.Serialization; @@ -154,16 +156,26 @@ private bool IsMatchInBypassList(Uri input) { UpdateRegexList(canThrow: false); - if (_regexBypassList != null) + if (_regexBypassList is Regex[] bypassList) { - Span stackBuffer = stackalloc char[128]; - string matchUriString = input.IsDefaultPort ? - string.Create(null, stackBuffer, $"{input.Scheme}://{input.Host}") : - string.Create(null, stackBuffer, $"{input.Scheme}://{input.Host}:{(uint)input.Port}"); + bool isDefaultPort = input.IsDefaultPort; + int lengthRequired = input.Scheme.Length + 3 + input.Host.Length; + if (!isDefaultPort) + { + lengthRequired += 1 + 5; // 1 for ':' and 5 for max formatted length of a port (16 bit value) + } + + int charsWritten; + Span url = lengthRequired <= 256 ? stackalloc char[256] : new char[lengthRequired]; + bool formatted = isDefaultPort ? + url.TryWrite($"{input.Scheme}://{input.Host}", out charsWritten) : + url.TryWrite($"{input.Scheme}://{input.Host}:{(uint)input.Port}", out charsWritten); + Debug.Assert(formatted); + url = url.Slice(0, charsWritten); - foreach (Regex r in _regexBypassList) + foreach (Regex r in bypassList) { - if (r.IsMatch(matchUriString)) + if (r.IsMatch(url)) { return true; }