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

CORS middleware should set the Vary header #199

Merged
merged 1 commit into from
Jan 3, 2022
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
17 changes: 14 additions & 3 deletions tower-http/src/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ pub struct CorsLayer {

const WILDCARD: &str = "*";

const VARY_HEADERS: [HeaderName; 3] = [
header::ORIGIN,
header::ACCESS_CONTROL_REQUEST_METHOD,
header::ACCESS_CONTROL_REQUEST_HEADERS,
];

impl CorsLayer {
/// Create a new `CorsLayer`.
///
Expand Down Expand Up @@ -769,26 +775,31 @@ where
expose_headers,
} => {
let mut response: Response<B> = ready!(future.poll(cx))?;
let headers = response.headers_mut();

response.headers_mut().insert(
headers.insert(
header::ACCESS_CONTROL_ALLOW_ORIGIN,
response_origin(allow_origin.take().unwrap(), origin),
);

if let Some(allow_credentials) = allow_credentials {
response.headers_mut().insert(
headers.insert(
header::ACCESS_CONTROL_ALLOW_CREDENTIALS,
allow_credentials.clone(),
);
}

if let Some(expose_headers) = expose_headers {
response.headers_mut().insert(
headers.insert(
header::ACCESS_CONTROL_EXPOSE_HEADERS,
expose_headers.clone(),
);
}

for h in &VARY_HEADERS {
headers.append(header::VARY, HeaderValue::from_static(h.as_str()));
}

Poll::Ready(Ok(response))
}
KindProj::NonCorsCall { future } => future.poll(cx),
Expand Down