@@ -48,35 +48,13 @@ impl HttpOutcallClient {
4848 ///
4949 /// Transform context is used to sanitize HTTP responses before checking for consensus.
5050 ///
51- /// You can use [`sanitized`] method to set up default transform context. (Available with
52- /// Cargo feature `sanitize-http-outcall`)
53- ///
5451 /// # Arguments
5552 /// * `transform_context` - method to use to sanitize HTTP response
5653 pub fn with_transform ( mut self , transform_context : TransformContext ) -> Self {
5754 self . transform_context = Some ( transform_context) ;
5855 self
5956 }
6057
61- /// Sets default transform context for the client.
62- ///
63- /// The default sanitize drops most of HTTP headers that may prevent consensus on the response.
64- ///
65- /// Only available with Cargo feature `sanitize-http-outcall`.
66- #[ cfg( feature = "sanitize-http-outcall" ) ]
67- pub fn sanitized ( mut self ) -> Self {
68- use ic_exports:: ic_cdk:: management_canister:: TransformFunc ;
69-
70- self . transform_context = Some ( TransformContext {
71- function : TransformFunc ( candid:: Func {
72- method : "sanitize_http_response" . to_string ( ) ,
73- principal : ic_exports:: ic_cdk:: api:: canister_self ( ) ,
74- } ) ,
75- context : vec ! [ ] ,
76- } ) ;
77- self
78- }
79-
8058 /// The maximal size of the response in bytes. If None, 2MiB will be the
8159 /// limit.
8260 /// This value affects the cost of the http request and it is highly
@@ -90,17 +68,6 @@ impl HttpOutcallClient {
9068 }
9169}
9270
93- #[ cfg( feature = "sanitize-http-outcall" ) ]
94- fn sanitize_http_response ( raw_response : ic_exports:: ic_cdk:: management_canister:: TransformArgs ) -> ic_exports:: ic_cdk:: management_canister:: HttpRequestResult {
95- const USE_HEADERS : & [ & str ] = & [ "content-encoding" , "content-length" , "content-type" , "host" ] ;
96- let ic_exports:: ic_cdk:: management_canister:: TransformArgs { mut response, .. } = raw_response;
97- response
98- . headers
99- . retain ( |header| USE_HEADERS . iter ( ) . any ( |v| v == & header. name . to_lowercase ( ) ) ) ;
100-
101- response
102- }
103-
10471impl Client for HttpOutcallClient {
10572 fn send_rpc_request (
10673 & self ,
@@ -169,52 +136,3 @@ impl Client for HttpOutcallClient {
169136 } )
170137 }
171138}
172-
173- #[ cfg( test) ]
174- #[ cfg( feature = "sanitize-http-outcall" ) ]
175- mod tests {
176- use candid:: Nat ;
177- use ic_exports:: ic_cdk:: management_canister:: { HttpRequestResult , TransformArgs } ;
178-
179- use super :: * ;
180-
181- #[ test]
182- fn sanitize_http_response_removes_extra_headers ( ) {
183- let transform_args = TransformArgs {
184- response : HttpRequestResult {
185- status : 200u128 . into ( ) ,
186- headers : vec ! [
187- HttpHeader {
188- name: "content-type" . to_string( ) ,
189- value: "application/json" . to_string( ) ,
190- } ,
191- HttpHeader {
192- name: "content-length" . to_string( ) ,
193- value: "42" . to_string( ) ,
194- } ,
195- HttpHeader {
196- name: "content-encoding" . to_string( ) ,
197- value: "gzip" . to_string( ) ,
198- } ,
199- HttpHeader {
200- name: "date" . to_string( ) ,
201- value: "Fri, 11 Oct 2024 10:25:08 GMT" . to_string( ) ,
202- } ,
203- ] ,
204- body : vec ! [ ] ,
205- } ,
206- context : vec ! [ ] ,
207- } ;
208-
209- let sanitized: HttpRequestResult = sanitize_http_response ( transform_args) ;
210- assert_eq ! ( sanitized. headers. len( ) , 3 ) ;
211- assert_eq ! ( sanitized. status, Nat :: from( 200u128 ) ) ;
212- assert ! (
213- sanitized
214- . headers
215- . iter( )
216- . any( |header| header. name == "content-type" )
217- ) ;
218- assert ! ( !sanitized. headers. iter( ) . any( |header| header. name == "date" ) ) ;
219- }
220- }
0 commit comments