Skip to content

Commit 55594df

Browse files
Replace options lists with arrays. (#74230)
Co-authored-by: Marek Fišera <mara@neptuo.com>
1 parent de3cd20 commit 55594df

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
133133
int headerCount = request.Headers.Count + request.Content?.Headers.Count ?? 0;
134134
List<string> headerNames = new List<string>(headerCount);
135135
List<string> headerValues = new List<string>(headerCount);
136-
List<string> optionNames = new List<string>();
137-
List<object?> optionValues = new List<object?>();
138-
139136
JSObject abortController = BrowserHttpInterop.CreateAbortController();
140137
CancellationTokenRegistration? abortRegistration = cancellationToken.Register(() =>
141138
{
@@ -147,12 +144,27 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
147144
});
148145
try
149146
{
150-
optionNames.Add("method");
151-
optionValues.Add(request.Method.Method);
147+
if (request.RequestUri == null)
148+
{
149+
throw new ArgumentNullException(nameof(request.RequestUri));
150+
}
151+
152+
string uri = request.RequestUri.IsAbsoluteUri ? request.RequestUri.AbsoluteUri : request.RequestUri.ToString();
153+
154+
bool hasFetchOptions = request.Options.TryGetValue(FetchOptions, out IDictionary<string, object>? fetchOptions);
155+
int optionCount = 1 + (allowAutoRedirect.HasValue ? 1 : 0) + (hasFetchOptions && fetchOptions != null ? fetchOptions.Count : 0);
156+
int optionIndex = 0;
157+
string[] optionNames = new string[optionCount];
158+
object?[] optionValues = new object?[optionCount];
159+
160+
optionNames[optionIndex] = "method";
161+
optionValues[optionIndex] = request.Method.Method;
162+
optionIndex++;
152163
if (allowAutoRedirect.HasValue)
153164
{
154-
optionNames.Add("redirect");
155-
optionValues.Add(allowAutoRedirect.Value ? "follow" : "manual");
165+
optionNames[optionIndex] = "redirect";
166+
optionValues[optionIndex] = allowAutoRedirect.Value ? "follow" : "manual";
167+
optionIndex++;
156168
}
157169

158170
foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
@@ -176,21 +188,16 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
176188
}
177189
}
178190

179-
if (request.Options.TryGetValue(FetchOptions, out IDictionary<string, object>? fetchOptions))
191+
if (hasFetchOptions && fetchOptions != null)
180192
{
181193
foreach (KeyValuePair<string, object> item in fetchOptions)
182194
{
183-
optionNames.Add(item.Key);
184-
optionValues.Add(item.Value);
195+
optionNames[optionIndex] = item.Key;
196+
optionValues[optionIndex] = item.Value;
197+
optionIndex++;
185198
}
186199
}
187200

188-
if (request.RequestUri == null)
189-
{
190-
throw new ArgumentNullException(nameof(request.RequestUri));
191-
}
192-
193-
string uri = request.RequestUri.IsAbsoluteUri ? request.RequestUri.AbsoluteUri : request.RequestUri.ToString();
194201
Task<JSObject>? promise;
195202
cancellationToken.ThrowIfCancellationRequested();
196203
if (request.Content != null)
@@ -201,21 +208,22 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
201208
.ConfigureAwait(true);
202209
cancellationToken.ThrowIfCancellationRequested();
203210

204-
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController, body);
211+
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController, body);
205212
}
206213
else
207214
{
208215
byte[] buffer = await request.Content.ReadAsByteArrayAsync(cancellationToken)
209216
.ConfigureAwait(true);
210217
cancellationToken.ThrowIfCancellationRequested();
211218

212-
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController, buffer);
219+
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController, buffer);
213220
}
214221
}
215222
else
216223
{
217-
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController);
224+
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController);
218225
}
226+
219227
cancellationToken.ThrowIfCancellationRequested();
220228
ValueTask<JSObject> wrappedTask = BrowserHttpInterop.CancelationHelper(promise, cancellationToken, abortController);
221229
JSObject fetchResponse = await wrappedTask.ConfigureAwait(true);

0 commit comments

Comments
 (0)