From 5cb1d18439f562c8004a941b0f56ed034a44b052 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Fri, 28 Jul 2023 09:01:06 +0200 Subject: feat: Deno.createHttpClient allowHost (#19689) This adds an option to allow using the host header in a fetch call. Closes https://github.com/denoland/deno/issues/16840 Ref https://github.com/denoland/deno/issues/11017 --- ext/fetch/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'ext/fetch') diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index a3daff040..48f6a0294 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -237,11 +237,11 @@ pub fn op_fetch( where FP: FetchPermissions + 'static, { - let client = if let Some(rid) = client_rid { + let (client, allow_host) = if let Some(rid) = client_rid { let r = state.resource_table.get::(rid)?; - r.client.clone() + (r.client.clone(), r.allow_host) } else { - get_or_create_client_from_state(state)? + (get_or_create_client_from_state(state)?, false) }; let method = Method::from_bytes(&method)?; @@ -334,7 +334,7 @@ where let v = HeaderValue::from_bytes(&value) .map_err(|err| type_error(err.to_string()))?; - if !matches!(name, HOST | CONTENT_LENGTH) { + if (name != HOST || allow_host) && name != CONTENT_LENGTH { header_map.append(name, v); } } @@ -761,6 +761,7 @@ impl Resource for FetchResponseResource { pub struct HttpClientResource { pub client: Client, + pub allow_host: bool, } impl Resource for HttpClientResource { @@ -770,8 +771,8 @@ impl Resource for HttpClientResource { } impl HttpClientResource { - fn new(client: Client) -> Self { - Self { client } + fn new(client: Client, allow_host: bool) -> Self { + Self { client, allow_host } } } @@ -795,6 +796,8 @@ pub struct CreateHttpClientArgs { http1: bool, #[serde(default = "default_true")] http2: bool, + #[serde(default)] + allow_host: bool, } fn default_true() -> bool { @@ -860,7 +863,9 @@ where }, )?; - let rid = state.resource_table.add(HttpClientResource::new(client)); + let rid = state + .resource_table + .add(HttpClientResource::new(client, args.allow_host)); Ok(rid) } -- cgit v1.2.3