From 6aae0bd4849bc5faf04a194d89f19a38dc451b75 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 18 Sep 2020 15:48:18 +1000 Subject: [PATCH] Tidy cors header --- Cargo.lock | 1 + beacon_node/http_api/src/lib.rs | 14 +------------- beacon_node/http_metrics/Cargo.toml | 1 + beacon_node/http_metrics/src/lib.rs | 14 +------------- common/warp_utils/src/lib.rs | 1 + common/warp_utils/src/reply.rs | 15 +++++++++++++++ 6 files changed, 20 insertions(+), 26 deletions(-) create mode 100644 common/warp_utils/src/reply.rs diff --git a/Cargo.lock b/Cargo.lock index d763a62d58f..9821d14d93c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2348,6 +2348,7 @@ dependencies = [ "tokio 0.2.22", "types", "warp", + "warp_utils", ] [[package]] diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 5392a81a883..8bc5236dd65 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -1584,19 +1584,7 @@ pub fn serve( .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 = 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), diff --git a/beacon_node/http_metrics/Cargo.toml b/beacon_node/http_metrics/Cargo.toml index fd2f7975fb1..482f7a5debc 100644 --- a/beacon_node/http_metrics/Cargo.toml +++ b/beacon_node/http_metrics/Cargo.toml @@ -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"] } diff --git a/beacon_node/http_metrics/src/lib.rs b/beacon_node/http_metrics/src/lib.rs index eb846a99472..053ecaa501b 100644 --- a/beacon_node/http_metrics/src/lib.rs +++ b/beacon_node/http_metrics/src/lib.rs @@ -90,19 +90,7 @@ pub fn serve( ) }) .map(|reply| warp::reply::with_header(reply, "Server", &version_with_platform())) - .map(move |reply| { - let reply: Box = 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), diff --git a/common/warp_utils/src/lib.rs b/common/warp_utils/src/lib.rs index e181278ba37..22ada7426aa 100644 --- a/common/warp_utils/src/lib.rs +++ b/common/warp_utils/src/lib.rs @@ -1 +1,2 @@ pub mod reject; +pub mod reply; diff --git a/common/warp_utils/src/reply.rs b/common/warp_utils/src/reply.rs new file mode 100644 index 00000000000..dcec6214f0c --- /dev/null +++ b/common/warp_utils/src/reply.rs @@ -0,0 +1,15 @@ +/// Add CORS headers to `reply` only if `allow_origin.is_some()`. +pub fn maybe_cors( + reply: T, + allow_origin: Option<&String>, +) -> Box { + if let Some(allow_origin) = allow_origin { + Box::new(warp::reply::with_header( + reply, + "Access-Control-Allow-Origin", + allow_origin, + )) + } else { + Box::new(reply) + } +}