diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2021-11-29 10:29:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-29 10:29:41 -0500 |
commit | f3b74350da69cb8cc0aedb1c1570abe2c64741ba (patch) | |
tree | ef124a88dad366641ce697666a1c4bc484153995 /ext/fetch/lib.rs | |
parent | bd989143e1a23366edb53b6b543eeb0ae0ff3e51 (diff) |
refactor: remove deno_fetch::HttpClientDefaults (#12931)
More clean up that should have been in cc83ad3
Diffstat (limited to 'ext/fetch/lib.rs')
-rw-r--r-- | ext/fetch/lib.rs | 43 |
1 files changed, 11 insertions, 32 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 3d4c51f8f..ec2bc5e4e 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -57,6 +57,7 @@ pub use reqwest; pub use fs_fetch_handler::FsFetchHandler; +#[derive(Clone)] pub struct Options { pub user_agent: String, pub root_cert_store: Option<RootCertStore>, @@ -67,8 +68,6 @@ pub struct Options { pub file_fetch_handler: Box<dyn FetchHandler>, } -struct BoxFetchHandler(Box<dyn FetchHandler>); - impl Default for Options { fn default() -> Self { Self { @@ -108,6 +107,7 @@ where ), ]) .state(move |state| { + state.put::<Options>(options.clone()); state.put::<reqwest::Client>({ create_http_client( options.user_agent.clone(), @@ -119,33 +119,11 @@ where ) .unwrap() }); - state.put::<HttpClientDefaults>(HttpClientDefaults { - user_agent: options.user_agent.clone(), - root_cert_store: options.root_cert_store.clone(), - proxy: options.proxy.clone(), - request_builder_hook: options.request_builder_hook, - unsafely_ignore_certificate_errors: options - .unsafely_ignore_certificate_errors - .clone(), - client_cert_chain_and_key: options.client_cert_chain_and_key.clone(), - }); - state.put(BoxFetchHandler(dyn_clone::clone_box( - &*options.file_fetch_handler, - ))); Ok(()) }) .build() } -pub struct HttpClientDefaults { - pub user_agent: String, - pub root_cert_store: Option<RootCertStore>, - pub proxy: Option<Proxy>, - pub request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>, - pub unsafely_ignore_certificate_errors: Option<Vec<String>>, - pub client_cert_chain_and_key: Option<(String, String)>, -} - pub type CancelableResponseFuture = Pin<Box<dyn Future<Output = CancelableResponseResult>>>; @@ -251,8 +229,9 @@ where ))); } - let BoxFetchHandler(file_fetch_handler) = - state.borrow_mut::<BoxFetchHandler>(); + let Options { + file_fetch_handler, .. + } = state.borrow_mut::<Options>(); let (request, maybe_request_body, maybe_cancel_handle) = file_fetch_handler.fetch_file(url); let request_rid = state.resource_table.add(FetchRequestResource(request)); @@ -317,8 +296,8 @@ where } } - let defaults = state.borrow::<HttpClientDefaults>(); - if let Some(request_builder_hook) = defaults.request_builder_hook { + let options = state.borrow::<Options>(); + if let Some(request_builder_hook) = options.request_builder_hook { request = request_builder_hook(request); } @@ -575,7 +554,7 @@ where } }; - let defaults = state.borrow::<HttpClientDefaults>(); + let options = state.borrow::<Options>(); let ca_certs = args .ca_certs .into_iter() @@ -583,11 +562,11 @@ where .collect::<Vec<_>>(); let client = create_http_client( - defaults.user_agent.clone(), - defaults.root_cert_store.clone(), + options.user_agent.clone(), + options.root_cert_store.clone(), ca_certs, args.proxy, - defaults.unsafely_ignore_certificate_errors.clone(), + options.unsafely_ignore_certificate_errors.clone(), client_cert_chain_and_key, )?; |