11// Licensed to the .NET Foundation under one or more agreements. 
22// The .NET Foundation licenses this file to you under the MIT license. 
33
4- using  System . IO ; 
4+ using  System . Buffers ; 
55using  System . Net . Http . Headers ; 
66using  System . Runtime . InteropServices . JavaScript ; 
77using  System . Threading ; 
@@ -17,47 +17,53 @@ internal static partial class BrowserHttpInterop
1717        [ JSImport ( "INTERNAL.http_wasm_supports_streaming_response" ) ] 
1818        public  static partial  bool  SupportsStreamingResponse ( ) ; 
1919
20-         [ JSImport ( "INTERNAL.http_wasm_create_abort_controler " ) ] 
21-         public  static partial  JSObject  CreateAbortController ( ) ; 
20+         [ JSImport ( "INTERNAL.http_wasm_create_controller " ) ] 
21+         public  static partial  JSObject  CreateController ( ) ; 
2222
2323        [ JSImport ( "INTERNAL.http_wasm_abort_request" ) ] 
2424        public  static partial  void  AbortRequest ( 
25-             JSObject  abortController ) ; 
25+             JSObject  httpController ) ; 
2626
2727        [ JSImport ( "INTERNAL.http_wasm_abort_response" ) ] 
2828        public  static partial  void  AbortResponse ( 
29-             JSObject  fetchResponse ) ; 
30- 
31-         [ JSImport ( "INTERNAL.http_wasm_create_transform_stream" ) ] 
32-         public  static partial  JSObject  CreateTransformStream ( ) ; 
29+             JSObject  httpController ) ; 
3330
3431        [ JSImport ( "INTERNAL.http_wasm_transform_stream_write" ) ] 
3532        public  static partial  Task  TransformStreamWrite ( 
36-             JSObject  transformStream , 
33+             JSObject  httpController , 
3734            IntPtr  bufferPtr , 
3835            int  bufferLength ) ; 
3936
37+         public  static unsafe  Task  TransformStreamWriteUnsafe ( JSObject  httpController ,  ReadOnlyMemory < byte >  buffer ,  Buffers . MemoryHandle  handle ) 
38+             =>  TransformStreamWrite ( httpController ,  ( nint ) handle . Pointer ,  buffer . Length ) ; 
39+ 
4040        [ JSImport ( "INTERNAL.http_wasm_transform_stream_close" ) ] 
4141        public  static partial  Task  TransformStreamClose ( 
42-             JSObject  transformStream ) ; 
43- 
44-         [ JSImport ( "INTERNAL.http_wasm_transform_stream_abort" ) ] 
45-         public  static partial  void  TransformStreamAbort ( 
46-             JSObject  transformStream ) ; 
42+             JSObject  httpController ) ; 
4743
4844        [ JSImport ( "INTERNAL.http_wasm_get_response_header_names" ) ] 
4945        private  static partial  string [ ]  _GetResponseHeaderNames ( 
50-             JSObject  fetchResponse ) ; 
46+             JSObject  httpController ) ; 
5147
5248        [ JSImport ( "INTERNAL.http_wasm_get_response_header_values" ) ] 
5349        private  static partial  string [ ]  _GetResponseHeaderValues ( 
54-             JSObject  fetchResponse ) ; 
50+             JSObject  httpController ) ; 
51+ 
52+         [ JSImport ( "INTERNAL.http_wasm_get_response_status" ) ] 
53+         public  static partial  int  GetResponseStatus ( 
54+             JSObject  httpController ) ; 
55+ 
56+         [ JSImport ( "INTERNAL.http_wasm_get_response_type" ) ] 
57+         public  static partial  string  GetResponseType ( 
58+             JSObject  httpController ) ; 
5559
56-         public  static void  GetResponseHeaders ( JSObject  fetchResponse ,  HttpHeaders  resposeHeaders ,  HttpHeaders  contentHeaders ) 
60+         public  static void  GetResponseHeaders ( JSObject  httpController ,  HttpHeaders  resposeHeaders ,  HttpHeaders  contentHeaders ) 
5761        { 
58-             string [ ]  headerNames  =  _GetResponseHeaderNames ( fetchResponse ) ; 
59-             string [ ]  headerValues  =  _GetResponseHeaderValues ( fetchResponse ) ; 
62+             string [ ]  headerNames  =  _GetResponseHeaderNames ( httpController ) ; 
63+             string [ ]  headerValues  =  _GetResponseHeaderValues ( httpController ) ; 
6064
65+             // Some of the headers may not even be valid header types in .NET thus we use TryAddWithoutValidation 
66+             // CORS will only allow access to certain headers on browser. 
6167            for  ( int  i  =  0 ;  i  <  headerNames . Length ;  i ++ ) 
6268            { 
6369                if  ( ! resposeHeaders . TryAddWithoutValidation ( headerNames [ i ] ,  headerValues [ i ] ) ) 
@@ -67,43 +73,38 @@ public static void GetResponseHeaders(JSObject fetchResponse, HttpHeaders respos
6773            } 
6874        } 
6975
70- 
7176        [ JSImport ( "INTERNAL.http_wasm_fetch" ) ] 
72-         public  static partial  Task < JSObject >  Fetch ( 
77+         public  static partial  Task  Fetch ( 
78+             JSObject  httpController , 
7379            string  uri , 
7480            string [ ]  headerNames , 
7581            string [ ]  headerValues , 
7682            string [ ]  optionNames , 
77-             [ JSMarshalAs < JSType . Array < JSType . Any > > ]  object ? [ ]  optionValues , 
78-             JSObject  abortControler ) ; 
83+             [ JSMarshalAs < JSType . Array < JSType . Any > > ]  object ? [ ]  optionValues ) ; 
7984
8085        [ JSImport ( "INTERNAL.http_wasm_fetch_stream" ) ] 
81-         public  static partial  Task < JSObject >  Fetch ( 
86+         public  static partial  Task  FetchStream ( 
87+             JSObject  httpController , 
8288            string  uri , 
8389            string [ ]  headerNames , 
8490            string [ ]  headerValues , 
8591            string [ ]  optionNames , 
86-             [ JSMarshalAs < JSType . Array < JSType . Any > > ]  object ? [ ]  optionValues , 
87-             JSObject  abortControler , 
88-             JSObject  transformStream ) ; 
92+             [ JSMarshalAs < JSType . Array < JSType . Any > > ]  object ? [ ]  optionValues ) ; 
8993
9094        [ JSImport ( "INTERNAL.http_wasm_fetch_bytes" ) ] 
91-         private  static partial  Task < JSObject >  FetchBytes ( 
95+         private  static partial  Task  FetchBytes ( 
96+             JSObject  httpController , 
9297            string  uri , 
9398            string [ ]  headerNames , 
9499            string [ ]  headerValues , 
95100            string [ ]  optionNames , 
96101            [ JSMarshalAs < JSType . Array < JSType . Any > > ]  object ? [ ]  optionValues , 
97-             JSObject  abortControler , 
98102            IntPtr  bodyPtr , 
99103            int  bodyLength ) ; 
100104
101-         public  static unsafe  Task < JSObject >   Fetch ( string  uri ,  string [ ]  headerNames ,  string [ ]  headerValues ,  string [ ]  optionNames ,  object ? [ ]  optionValues ,  JSObject   abortControler ,   byte [ ]   body ) 
105+         public  static unsafe  Task   FetchBytes ( JSObject   httpController ,   string  uri ,  string [ ]  headerNames ,  string [ ]  headerValues ,  string [ ]  optionNames ,  object ? [ ]  optionValues ,  MemoryHandle   pinBuffer ,   int   bodyLength ) 
102106        { 
103-             fixed ( byte *  ptr  =  body ) 
104-             { 
105-                 return  FetchBytes ( uri ,  headerNames ,  headerValues ,  optionNames ,  optionValues ,  abortControler ,  ( IntPtr ) ptr ,  body . Length ) ; 
106-             } 
107+             return  FetchBytes ( httpController ,  uri ,  headerNames ,  headerValues ,  optionNames ,  optionValues ,  ( IntPtr ) pinBuffer . Pointer ,  bodyLength ) ; 
107108        } 
108109
109110        [ JSImport ( "INTERNAL.http_wasm_get_streamed_response_bytes" ) ] 
@@ -112,6 +113,10 @@ public static partial Task<int> GetStreamedResponseBytes(
112113            IntPtr  bufferPtr , 
113114            int  bufferLength ) ; 
114115
116+         public  static unsafe  Task < int >  GetStreamedResponseBytesUnsafe ( JSObject  jsController ,  Memory < byte >  buffer ,  MemoryHandle  handle ) 
117+             =>  GetStreamedResponseBytes ( jsController ,  ( IntPtr ) handle . Pointer ,  buffer . Length ) ; 
118+ 
119+ 
115120        [ JSImport ( "INTERNAL.http_wasm_get_response_length" ) ] 
116121        public  static partial  Task < int >  GetResponseLength ( 
117122            JSObject  fetchResponse ) ; 
@@ -122,8 +127,10 @@ public static partial int GetResponseBytes(
122127            [ JSMarshalAs < JSType . MemoryView > ]  Span < byte >  buffer ) ; 
123128
124129
125-         public  static async  ValueTask   CancelationHelper ( Task  promise ,  CancellationToken  cancellationToken ,  JSObject ?   fetchResponse   =   null ) 
130+         public  static async  Task   CancellationHelper ( Task  promise ,  CancellationToken  cancellationToken ,  JSObject   jsController ) 
126131        { 
132+             Http . CancellationHelper . ThrowIfCancellationRequested ( cancellationToken ) ; 
133+ 
127134            if  ( promise . IsCompletedSuccessfully ) 
128135            { 
129136                return ; 
@@ -132,46 +139,43 @@ public static async ValueTask CancelationHelper(Task promise, CancellationToken
132139            { 
133140                using  ( var  operationRegistration  =  cancellationToken . Register ( static  s => 
134141                { 
135-                     ( Task  _promise ,  JSObject ?   _fetchResponse )  =  ( ( Task ,  JSObject ? ) ) s ! ; 
136-                     CancelablePromise . CancelPromise ( _promise ,  static  ( JSObject ?   __fetchResponse )  => 
142+                     ( Task  _promise ,  JSObject   _jsController )  =  ( ( Task ,  JSObject ) ) s ! ; 
143+                     CancelablePromise . CancelPromise ( _promise ,  static  ( JSObject   __jsController )  => 
137144                    { 
138-                         if  ( __fetchResponse   !=   null ) 
145+                         if  ( ! __jsController . IsDisposed ) 
139146                        { 
140-                             AbortResponse ( __fetchResponse ) ; 
147+                             AbortResponse ( __jsController ) ; 
141148                        } 
142-                     } ,  _fetchResponse ) ; 
143-                 } ,  ( promise ,  fetchResponse ) ) ) 
149+                     } ,  _jsController ) ; 
150+                 } ,  ( promise ,  jsController ) ) ) 
144151                { 
145152                    await  promise . ConfigureAwait ( true ) ; 
146153                } 
147154            } 
148155            catch  ( OperationCanceledException  oce )  when  ( cancellationToken . IsCancellationRequested ) 
149156            { 
150-                 throw   CancellationHelper . CreateOperationCanceledException ( oce ,  cancellationToken ) ; 
157+                 Http . CancellationHelper . ThrowIfCancellationRequested ( oce ,  cancellationToken ) ; 
151158            } 
152159            catch  ( JSException  jse ) 
153160            { 
154161                if  ( jse . Message . StartsWith ( "AbortError" ,  StringComparison . Ordinal ) ) 
155162                { 
156-                     throw  CancellationHelper . CreateOperationCanceledException ( jse ,  CancellationToken . None ) ; 
157-                 } 
158-                 if  ( cancellationToken . IsCancellationRequested ) 
159-                 { 
160-                     throw  CancellationHelper . CreateOperationCanceledException ( jse ,  cancellationToken ) ; 
163+                     throw  Http . CancellationHelper . CreateOperationCanceledException ( jse ,  CancellationToken . None ) ; 
161164                } 
165+                 Http . CancellationHelper . ThrowIfCancellationRequested ( jse ,  cancellationToken ) ; 
162166                throw  new  HttpRequestException ( jse . Message ,  jse ) ; 
163167            } 
164168        } 
165169
166-         public  static async  ValueTask < T >  CancelationHelper < T > ( Task < T >  promise ,  CancellationToken  cancellationToken ,  JSObject ?   fetchResponse   =   null ) 
170+         public  static async  Task < T >  CancellationHelper < T > ( Task < T >  promise ,  CancellationToken  cancellationToken ,  JSObject   jsController ) 
167171        { 
172+             Http . CancellationHelper . ThrowIfCancellationRequested ( cancellationToken ) ; 
168173            if  ( promise . IsCompletedSuccessfully ) 
169174            { 
170175                return  promise . Result ; 
171176            } 
172-             await  CancelationHelper ( ( Task ) promise ,  cancellationToken ,  fetchResponse ) . ConfigureAwait ( true ) ; 
173-             return  await   promise . ConfigureAwait ( true ) ; 
177+             await  CancellationHelper ( ( Task ) promise ,  cancellationToken ,  jsController ) . ConfigureAwait ( false ) ; 
178+             return  promise . Result ; 
174179        } 
175180    } 
176- 
177181} 
0 commit comments