Skip to content

Commit

Permalink
Add ability to configure CORS header (#1345)
Browse files Browse the repository at this point in the history
## Issue Addressed

#1177

## Proposed Changes

Add a command line option (`--http-allow-origin`) and a config item for configuring the `Access-Control-Allow-Origin` response header.  This should unblock making XMLHttpRequests.
  • Loading branch information
adaszko committed Jul 16, 2020
1 parent 4a01f44 commit fc5e6cb
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 163 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.

1 change: 1 addition & 0 deletions beacon_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ eth2_libp2p = { path = "./eth2_libp2p" }
eth2_ssz = "0.1.2"
serde = "1.0.110"
clap_utils = { path = "../common/clap_utils" }
hyper = "0.13.5"
4 changes: 4 additions & 0 deletions beacon_node/rest_api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub struct Config {
pub listen_address: Ipv4Addr,
/// The port the REST API HTTP server will listen on.
pub port: u16,
/// If something else than "", a 'Access-Control-Allow-Origin' header will be present in
/// responses. Put *, to allow any origin.
pub allow_origin: String,
}

impl Default for Config {
Expand All @@ -46,6 +49,7 @@ impl Default for Config {
enabled: false,
listen_address: Ipv4Addr::new(127, 0, 0, 1),
port: 5052,
allow_origin: "".to_string(),
}
}
}
8 changes: 8 additions & 0 deletions beacon_node/rest_api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum ApiError {
UnsupportedType(String),
ImATeapot(String), // Just in case.
ProcessingError(String), // A 202 error, for when a block/attestation cannot be processed, but still transmitted.
InvalidHeaderValue(String),
}

pub type ApiResult = Result<Response<Body>, ApiError>;
Expand All @@ -26,6 +27,7 @@ impl ApiError {
ApiError::UnsupportedType(desc) => (StatusCode::UNSUPPORTED_MEDIA_TYPE, desc),
ApiError::ImATeapot(desc) => (StatusCode::IM_A_TEAPOT, desc),
ApiError::ProcessingError(desc) => (StatusCode::ACCEPTED, desc),
ApiError::InvalidHeaderValue(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
}
}
}
Expand Down Expand Up @@ -77,6 +79,12 @@ impl From<std::io::Error> for ApiError {
}
}

impl From<hyper::header::InvalidHeaderValue> for ApiError {
fn from(e: hyper::header::InvalidHeaderValue) -> ApiError {
ApiError::InvalidHeaderValue(format!("Invalid CORS header value: {:?}", e))
}
}

impl StdError for ApiError {
fn cause(&self) -> Option<&dyn StdError> {
None
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/rest_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ pub fn start_server<T: BeaconChainTypes>(
) -> Result<SocketAddr, hyper::Error> {
let log = executor.log();
let inner_log = log.clone();
let rest_api_config = Arc::new(config.clone());
let eth2_config = Arc::new(eth2_config);

// Define the function that will build the request handler.
let make_service = make_service_fn(move |_socket: &AddrStream| {
let beacon_chain = beacon_chain.clone();
let log = inner_log.clone();
let rest_api_config = rest_api_config.clone();
let eth2_config = eth2_config.clone();
let network_globals = network_info.network_globals.clone();
let network_channel = network_info.network_chan.clone();
Expand All @@ -84,6 +86,7 @@ pub fn start_server<T: BeaconChainTypes>(
beacon_chain.clone(),
network_globals.clone(),
network_channel.clone(),
rest_api_config.clone(),
eth2_config.clone(),
log.clone(),
db_path.clone(),
Expand Down
Loading

0 comments on commit fc5e6cb

Please sign in to comment.