@@ -133,9 +133,6 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
133
133
int headerCount = request . Headers . Count + request . Content ? . Headers . Count ?? 0 ;
134
134
List < string > headerNames = new List < string > ( headerCount ) ;
135
135
List < string > headerValues = new List < string > ( headerCount ) ;
136
- List < string > optionNames = new List < string > ( ) ;
137
- List < object ? > optionValues = new List < object ? > ( ) ;
138
-
139
136
JSObject abortController = BrowserHttpInterop . CreateAbortController ( ) ;
140
137
CancellationTokenRegistration ? abortRegistration = cancellationToken . Register ( ( ) =>
141
138
{
@@ -147,12 +144,27 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
147
144
} ) ;
148
145
try
149
146
{
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 ++ ;
152
163
if ( allowAutoRedirect . HasValue )
153
164
{
154
- optionNames . Add ( "redirect" ) ;
155
- optionValues . Add ( allowAutoRedirect . Value ? "follow" : "manual" ) ;
165
+ optionNames [ optionIndex ] = "redirect" ;
166
+ optionValues [ optionIndex ] = allowAutoRedirect . Value ? "follow" : "manual" ;
167
+ optionIndex ++ ;
156
168
}
157
169
158
170
foreach ( KeyValuePair < string , IEnumerable < string > > header in request . Headers )
@@ -176,21 +188,16 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
176
188
}
177
189
}
178
190
179
- if ( request . Options . TryGetValue ( FetchOptions , out IDictionary < string , object > ? fetchOptions ) )
191
+ if ( hasFetchOptions && fetchOptions != null )
180
192
{
181
193
foreach ( KeyValuePair < string , object > item in fetchOptions )
182
194
{
183
- optionNames . Add ( item . Key ) ;
184
- optionValues . Add ( item . Value ) ;
195
+ optionNames [ optionIndex ] = item . Key ;
196
+ optionValues [ optionIndex ] = item . Value ;
197
+ optionIndex ++ ;
185
198
}
186
199
}
187
200
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 ( ) ;
194
201
Task < JSObject > ? promise ;
195
202
cancellationToken . ThrowIfCancellationRequested ( ) ;
196
203
if ( request . Content != null )
@@ -201,21 +208,22 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
201
208
. ConfigureAwait ( true ) ;
202
209
cancellationToken . ThrowIfCancellationRequested ( ) ;
203
210
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 ) ;
205
212
}
206
213
else
207
214
{
208
215
byte [ ] buffer = await request . Content . ReadAsByteArrayAsync ( cancellationToken )
209
216
. ConfigureAwait ( true ) ;
210
217
cancellationToken . ThrowIfCancellationRequested ( ) ;
211
218
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 ) ;
213
220
}
214
221
}
215
222
else
216
223
{
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 ) ;
218
225
}
226
+
219
227
cancellationToken . ThrowIfCancellationRequested ( ) ;
220
228
ValueTask < JSObject > wrappedTask = BrowserHttpInterop . CancelationHelper ( promise , cancellationToken , abortController ) ;
221
229
JSObject fetchResponse = await wrappedTask . ConfigureAwait ( true ) ;
0 commit comments