|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Buffers; |
4 | 5 | using System.Collections;
|
| 6 | +using System.Diagnostics; |
5 | 7 | using System.Diagnostics.CodeAnalysis;
|
6 | 8 | using System.Globalization;
|
7 | 9 | using System.Runtime.Serialization;
|
@@ -154,16 +156,26 @@ private bool IsMatchInBypassList(Uri input)
|
154 | 156 | {
|
155 | 157 | UpdateRegexList(canThrow: false);
|
156 | 158 |
|
157 |
| - if (_regexBypassList != null) |
| 159 | + if (_regexBypassList is Regex[] bypassList) |
158 | 160 | {
|
159 |
| - Span<char> stackBuffer = stackalloc char[128]; |
160 |
| - string matchUriString = input.IsDefaultPort ? |
161 |
| - string.Create(null, stackBuffer, $"{input.Scheme}://{input.Host}") : |
162 |
| - string.Create(null, stackBuffer, $"{input.Scheme}://{input.Host}:{(uint)input.Port}"); |
| 161 | + bool isDefaultPort = input.IsDefaultPort; |
| 162 | + int lengthRequired = input.Scheme.Length + 3 + input.Host.Length; |
| 163 | + if (!isDefaultPort) |
| 164 | + { |
| 165 | + lengthRequired += 1 + 5; // 1 for ':' and 5 for max formatted length of a port (16 bit value) |
| 166 | + } |
| 167 | + |
| 168 | + int charsWritten; |
| 169 | + Span<char> url = lengthRequired <= 256 ? stackalloc char[256] : new char[lengthRequired]; |
| 170 | + bool formatted = isDefaultPort ? |
| 171 | + url.TryWrite($"{input.Scheme}://{input.Host}", out charsWritten) : |
| 172 | + url.TryWrite($"{input.Scheme}://{input.Host}:{(uint)input.Port}", out charsWritten); |
| 173 | + Debug.Assert(formatted); |
| 174 | + url = url.Slice(0, charsWritten); |
163 | 175 |
|
164 |
| - foreach (Regex r in _regexBypassList) |
| 176 | + foreach (Regex r in bypassList) |
165 | 177 | {
|
166 |
| - if (r.IsMatch(matchUriString)) |
| 178 | + if (r.IsMatch(url)) |
167 | 179 | {
|
168 | 180 | return true;
|
169 | 181 | }
|
|
0 commit comments