11use std:: fmt:: Debug ;
2- use std:: str:: FromStr ;
32use std:: sync:: { Arc , OnceLock } ;
43use std:: time:: Duration ;
54
@@ -17,8 +16,8 @@ use ethers::types::Address;
1716use ethers_signers:: Signer ;
1817use hyperlane_core:: rpc_clients:: FallbackProvider ;
1918use hyperlane_metric:: utils:: url_to_host_info;
20- use reqwest:: header:: { HeaderName , HeaderValue } ;
2119use reqwest:: { Client , Url } ;
20+ use reqwest_utils:: parse_custom_rpc_headers;
2221use thiserror:: Error ;
2322
2423use ethers_prometheus:: json_rpc_client:: { JsonRpcBlockGetter , PrometheusJsonRpcClient } ;
@@ -328,50 +327,14 @@ fn get_reqwest_client(url: &Url) -> ChainResult<Client> {
328327 if let Some ( client) = client_cache. get ( url) {
329328 return Ok ( client. clone ( ) ) ;
330329 }
331- let client = build_new_reqwest_client ( url. clone ( ) ) ?;
332- client_cache. insert ( url. clone ( ) , client. clone ( ) ) ;
333- Ok ( client)
334- }
335-
336- /// Builds a new reqwest client with the given URL.
337- /// Generally `get_reqwest_client` should be used instead of this function,
338- /// as it caches the client for reuse.
339- fn build_new_reqwest_client ( url : Url ) -> ChainResult < Client > {
340- let mut queries_to_keep = vec ! [ ] ;
341- let mut headers = reqwest:: header:: HeaderMap :: new ( ) ;
342-
343- // A hack to pass custom headers to the provider without
344- // requiring a bunch of changes to our configuration surface area.
345- // Any `custom_rpc_header` query parameter is expected to have the value
346- // format: `header_name:header_value`, will be added to the headers
347- // of the HTTP client, and removed from the URL params.
348- let mut updated_url = url. clone ( ) ;
349- for ( key, value) in url. query_pairs ( ) {
350- if key != "custom_rpc_header" {
351- queries_to_keep. push ( ( key. clone ( ) , value. clone ( ) ) ) ;
352- continue ;
353- }
354- if let Some ( ( header_name, header_value) ) = value. split_once ( ':' ) {
355- let header_name =
356- HeaderName :: from_str ( header_name) . map_err ( ChainCommunicationError :: from_other) ?;
357- let mut header_value =
358- HeaderValue :: from_str ( header_value) . map_err ( ChainCommunicationError :: from_other) ?;
359- header_value. set_sensitive ( true ) ;
360- headers. insert ( header_name, header_value) ;
361- }
362- }
363-
364- updated_url
365- . query_pairs_mut ( )
366- . clear ( )
367- . extend_pairs ( queries_to_keep) ;
368-
330+ let ( headers, _) =
331+ parse_custom_rpc_headers ( url) . map_err ( ChainCommunicationError :: from_other) ?;
369332 let client = Client :: builder ( )
370333 . timeout ( HTTP_CLIENT_TIMEOUT )
371334 . default_headers ( headers)
372335 . build ( )
373336 . map_err ( EthereumProviderConnectionError :: from) ?;
374-
337+ client_cache . insert ( url . clone ( ) , client . clone ( ) ) ;
375338 Ok ( client)
376339}
377340
0 commit comments