mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
refactor: remove deno_fetch::HttpClientDefaults (#12931)
More clean up that should have been in cc83ad3
This commit is contained in:
parent
1333334496
commit
50fd8ebe40
1 changed files with 11 additions and 32 deletions
|
@ -57,6 +57,7 @@ pub use reqwest;
|
||||||
|
|
||||||
pub use fs_fetch_handler::FsFetchHandler;
|
pub use fs_fetch_handler::FsFetchHandler;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
pub user_agent: String,
|
pub user_agent: String,
|
||||||
pub root_cert_store: Option<RootCertStore>,
|
pub root_cert_store: Option<RootCertStore>,
|
||||||
|
@ -67,8 +68,6 @@ pub struct Options {
|
||||||
pub file_fetch_handler: Box<dyn FetchHandler>,
|
pub file_fetch_handler: Box<dyn FetchHandler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BoxFetchHandler(Box<dyn FetchHandler>);
|
|
||||||
|
|
||||||
impl Default for Options {
|
impl Default for Options {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -108,6 +107,7 @@ where
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
.state(move |state| {
|
.state(move |state| {
|
||||||
|
state.put::<Options>(options.clone());
|
||||||
state.put::<reqwest::Client>({
|
state.put::<reqwest::Client>({
|
||||||
create_http_client(
|
create_http_client(
|
||||||
options.user_agent.clone(),
|
options.user_agent.clone(),
|
||||||
|
@ -119,33 +119,11 @@ where
|
||||||
)
|
)
|
||||||
.unwrap()
|
.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(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.build()
|
.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 =
|
pub type CancelableResponseFuture =
|
||||||
Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
|
Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
|
||||||
|
|
||||||
|
@ -251,8 +229,9 @@ where
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let BoxFetchHandler(file_fetch_handler) =
|
let Options {
|
||||||
state.borrow_mut::<BoxFetchHandler>();
|
file_fetch_handler, ..
|
||||||
|
} = state.borrow_mut::<Options>();
|
||||||
let (request, maybe_request_body, maybe_cancel_handle) =
|
let (request, maybe_request_body, maybe_cancel_handle) =
|
||||||
file_fetch_handler.fetch_file(url);
|
file_fetch_handler.fetch_file(url);
|
||||||
let request_rid = state.resource_table.add(FetchRequestResource(request));
|
let request_rid = state.resource_table.add(FetchRequestResource(request));
|
||||||
|
@ -317,8 +296,8 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let defaults = state.borrow::<HttpClientDefaults>();
|
let options = state.borrow::<Options>();
|
||||||
if let Some(request_builder_hook) = defaults.request_builder_hook {
|
if let Some(request_builder_hook) = options.request_builder_hook {
|
||||||
request = request_builder_hook(request);
|
request = request_builder_hook(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,7 +554,7 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let defaults = state.borrow::<HttpClientDefaults>();
|
let options = state.borrow::<Options>();
|
||||||
let ca_certs = args
|
let ca_certs = args
|
||||||
.ca_certs
|
.ca_certs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -583,11 +562,11 @@ where
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let client = create_http_client(
|
let client = create_http_client(
|
||||||
defaults.user_agent.clone(),
|
options.user_agent.clone(),
|
||||||
defaults.root_cert_store.clone(),
|
options.root_cert_store.clone(),
|
||||||
ca_certs,
|
ca_certs,
|
||||||
args.proxy,
|
args.proxy,
|
||||||
defaults.unsafely_ignore_certificate_errors.clone(),
|
options.unsafely_ignore_certificate_errors.clone(),
|
||||||
client_cert_chain_and_key,
|
client_cert_chain_and_key,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue