Skip to content

Commit

Permalink
Tidy cors header
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 18, 2020
1 parent 1f7445b commit 6aae0bd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1584,19 +1584,7 @@ pub fn serve<T: BeaconChainTypes>(
.with(slog_logging(log.clone()))
.with(prometheus_metrics())
.map(|reply| warp::reply::with_header(reply, "Server", &version_with_platform()))
.map(move |reply| {
let reply: Box<dyn warp::Reply> = if let Some(allow_origin) = allow_origin.as_ref() {
Box::new(warp::reply::with_header(
reply,
"Access-Control-Allow-Origin",
allow_origin,
))
} else {
Box::new(reply)
};

reply
});
.map(move |reply| warp_utils::reply::maybe_cors(reply, allow_origin.as_ref()));

let (listening_socket, server) = warp::serve(routes).try_bind_with_graceful_shutdown(
SocketAddrV4::new(config.listen_addr, config.listen_port),
Expand Down
1 change: 1 addition & 0 deletions beacon_node/http_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lighthouse_metrics = { path = "../../common/lighthouse_metrics" }
lazy_static = "1.4.0"
eth2 = { path = "../../common/eth2" }
lighthouse_version = { path = "../../common/lighthouse_version" }
warp_utils = { path = "../../common/warp_utils" }

[dev-dependencies]
tokio = { version = "0.2.21", features = ["sync"] }
Expand Down
14 changes: 1 addition & 13 deletions beacon_node/http_metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,7 @@ pub fn serve<T: BeaconChainTypes>(
)
})
.map(|reply| warp::reply::with_header(reply, "Server", &version_with_platform()))
.map(move |reply| {
let reply: Box<dyn warp::Reply> = if let Some(allow_origin) = allow_origin.as_ref() {
Box::new(warp::reply::with_header(
reply,
"Access-Control-Allow-Origin",
allow_origin,
))
} else {
Box::new(reply)
};

reply
});
.map(move |reply| warp_utils::reply::maybe_cors(reply, allow_origin.as_ref()));

let (listening_socket, server) = warp::serve(routes).try_bind_with_graceful_shutdown(
SocketAddrV4::new(config.listen_addr, config.listen_port),
Expand Down
1 change: 1 addition & 0 deletions common/warp_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod reject;
pub mod reply;
15 changes: 15 additions & 0 deletions common/warp_utils/src/reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Add CORS headers to `reply` only if `allow_origin.is_some()`.
pub fn maybe_cors<T: warp::Reply + 'static>(
reply: T,
allow_origin: Option<&String>,
) -> Box<dyn warp::Reply> {
if let Some(allow_origin) = allow_origin {
Box::new(warp::reply::with_header(
reply,
"Access-Control-Allow-Origin",
allow_origin,
))
} else {
Box::new(reply)
}
}

0 comments on commit 6aae0bd

Please sign in to comment.