Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(extension/fetch): request builder hook to init args #11546

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions extensions/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use reqwest::redirect::Policy;
use reqwest::Body;
use reqwest::Client;
use reqwest::Method;
use reqwest::RequestBuilder;
use reqwest::Response;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -60,7 +61,7 @@ pub fn init<P: FetchPermissions + 'static>(
user_agent: String,
ca_data: Option<Vec<u8>>,
proxy: Option<Proxy>,
frozen_headers: Option<HeaderMap>,
request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>,
) -> Extension {
Extension::builder()
.js(include_js_files!(
Expand Down Expand Up @@ -90,7 +91,7 @@ pub fn init<P: FetchPermissions + 'static>(
ca_data: ca_data.clone(),
user_agent: user_agent.clone(),
proxy: proxy.clone(),
frozen_headers: frozen_headers.clone(),
request_builder_hook,
});
Ok(())
})
Expand All @@ -101,7 +102,7 @@ pub struct HttpClientDefaults {
pub user_agent: String,
pub ca_data: Option<Vec<u8>>,
pub proxy: Option<Proxy>,
pub frozen_headers: Option<HeaderMap>,
pub request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>,
}

pub trait FetchPermissions {
Expand Down Expand Up @@ -217,13 +218,9 @@ where
}
}

// Set frozen_headers after the user provided headers, so the
// end user can't override them.
let defaults = state.borrow::<HttpClientDefaults>();
if let Some(frozen_headers) = &defaults.frozen_headers {
for (key, value) in frozen_headers {
request = request.header(key, value)
}
if let Some(request_builder_hook) = defaults.request_builder_hook {
request = request_builder_hook(request);
}

let cancel_handle = CancelHandle::new_rc();
Expand Down