Skip to content

Commit

Permalink
Add ability for Rewrites to overwrite request headers (vercel/turbore…
Browse files Browse the repository at this point in the history
…po#5139)

### Description

Adds a `request_headers` override to `RewriteVc`, allowing middleware to
modify the request headers for further lookups.

### Testing Instructions

#50567
Re: WEB-1121
  • Loading branch information
jridgewell authored Jun 6, 2023
1 parent 40dfbe7 commit 89f6d42
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/turbopack-dev-server/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeaderListVc>,

/// 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<HeaderListVc>,
}

pub struct RewriteBuilder {
Expand All @@ -544,6 +548,7 @@ impl RewriteBuilder {
path_and_query,
source: None,
response_headers: None,
request_headers: None,
},
}
}
Expand All @@ -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()
}
Expand Down
14 changes: 13 additions & 1 deletion crates/turbopack-dev-server/src/source/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-dev-server/src/source/wrapping_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl GetContentSourceContent for WrappedGetContentSourceContent {
.into(),
),
response_headers: rewrite.response_headers,
request_headers: rewrite.request_headers,
}
.cell(),
)
Expand Down

0 comments on commit 89f6d42

Please sign in to comment.