diff --git a/crates/turbopack-dev-server/src/source/mod.rs b/crates/turbopack-dev-server/src/source/mod.rs index 0af0c8dc54b75..1df9c935719d0 100644 --- a/crates/turbopack-dev-server/src/source/mod.rs +++ b/crates/turbopack-dev-server/src/source/mod.rs @@ -531,6 +531,10 @@ pub struct Rewrite { /// A [Headers] which will be appended to the eventual, fully resolved /// content result. This overwrites any previous matching headers. pub response_headers: Option, + + /// A [HeaderList] which will overwrite the values used during the lookup + /// process. All headers not present in this list will be deleted. + pub request_headers: Option, } pub struct RewriteBuilder { @@ -544,6 +548,7 @@ impl RewriteBuilder { path_and_query, source: None, response_headers: None, + request_headers: None, }, } } @@ -563,6 +568,13 @@ impl RewriteBuilder { self } + /// Sets request headers to overwrite the headers used during the lookup + /// process. + pub fn request_headers(mut self, headers: HeaderListVc) -> Self { + self.rewrite.request_headers = Some(headers); + self + } + pub fn build(self) -> RewriteVc { self.rewrite.cell() } diff --git a/crates/turbopack-dev-server/src/source/resolve.rs b/crates/turbopack-dev-server/src/source/resolve.rs index 9ef1f2d9ad9f0..36cb62e240b84 100644 --- a/crates/turbopack-dev-server/src/source/resolve.rs +++ b/crates/turbopack-dev-server/src/source/resolve.rs @@ -4,7 +4,10 @@ use std::{ }; use anyhow::{bail, Result}; -use hyper::Uri; +use hyper::{ + header::{HeaderName as HyperHeaderName, HeaderValue as HyperHeaderValue}, + Uri, +}; use turbo_tasks::{TransientInstance, Value}; use super::{ @@ -71,6 +74,15 @@ pub async fn resolve_source_request( if let Some(headers) = &rewrite.response_headers { response_header_overwrites.extend(headers.await?.iter().cloned()); } + if let Some(headers) = &rewrite.request_headers { + request_overwrites.headers.clear(); + for (name, value) in &*headers.await? { + request_overwrites.headers.insert( + HyperHeaderName::try_from(name)?, + HyperHeaderValue::try_from(value)?, + ); + } + } current_asset_path = new_asset_path; data = ContentSourceData::default(); } diff --git a/crates/turbopack-dev-server/src/source/wrapping_source.rs b/crates/turbopack-dev-server/src/source/wrapping_source.rs index 852d587f2e392..f937b0b563993 100644 --- a/crates/turbopack-dev-server/src/source/wrapping_source.rs +++ b/crates/turbopack-dev-server/src/source/wrapping_source.rs @@ -155,6 +155,7 @@ impl GetContentSourceContent for WrappedGetContentSourceContent { .into(), ), response_headers: rewrite.response_headers, + request_headers: rewrite.request_headers, } .cell(), )