Skip to content

Commit 5d5b8e3

Browse files
authored
Merge pull request #65 from dfinity/rjb/adapt-cors-headers
chore: separate cors layer for HTTP gateway
2 parents 2223a05 + 98883e4 commit 5d5b8e3

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/routing/middleware/cors.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::time::Duration;
44

55
use http::{
66
header::{
7-
ACCEPT_RANGES, CACHE_CONTROL, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, COOKIE, DNT,
8-
IF_MODIFIED_SINCE, IF_NONE_MATCH, RANGE, USER_AGENT,
7+
HeaderName, ACCEPT_RANGES, CACHE_CONTROL, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE,
8+
COOKIE, DNT, IF_MODIFIED_SINCE, IF_NONE_MATCH, RANGE, USER_AGENT,
99
},
1010
Method,
1111
};
@@ -14,6 +14,9 @@ use tower_http::cors::{Any, CorsLayer};
1414

1515
const MINUTE: Duration = Duration::from_secs(60);
1616

17+
const X_OC_JWT: HeaderName = HeaderName::from_static("x-oc-jwt");
18+
const X_OC_API_KEY: HeaderName = HeaderName::from_static("x-oc-api-key");
19+
1720
/*
1821
add_header "Access-Control-Allow-Origin" "*" always;
1922
add_header "Access-Control-Allow-Methods" "$cors_allow_methods" always;
@@ -47,3 +50,31 @@ pub fn layer(methods: &[Method]) -> CorsLayer {
4750
])
4851
.max_age(10 * MINUTE)
4952
}
53+
54+
pub fn http_gw_layer(methods: &[Method]) -> CorsLayer {
55+
CorsLayer::new()
56+
.allow_origin(Any)
57+
.allow_methods(methods.to_vec())
58+
.expose_headers([
59+
ACCEPT_RANGES,
60+
CONTENT_LENGTH,
61+
CONTENT_RANGE,
62+
X_REQUEST_ID,
63+
X_IC_CANISTER_ID,
64+
])
65+
.allow_headers([
66+
USER_AGENT,
67+
DNT,
68+
IF_NONE_MATCH,
69+
IF_MODIFIED_SINCE,
70+
CACHE_CONTROL,
71+
CONTENT_TYPE,
72+
RANGE,
73+
COOKIE,
74+
X_REQUESTED_WITH,
75+
X_IC_CANISTER_ID,
76+
X_OC_JWT,
77+
X_OC_API_KEY,
78+
])
79+
.max_age(10 * MINUTE)
80+
}

src/routing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ pub async fn setup_router(
397397
.put(handler::handler)
398398
.delete(handler::handler)
399399
.patch(handler::handler)
400-
.layer(cors::layer(&[
400+
.layer(cors::http_gw_layer(&[
401401
Method::HEAD,
402402
Method::GET,
403403
Method::POST,

0 commit comments

Comments
 (0)