From 9b914446389b2f5755625720eeb618a7e6bbcacd Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sat, 14 May 2016 13:29:26 +0200 Subject: [PATCH] comma delimeting multiple cors headers (#1078) --- Cargo.lock | 12 +++++++++++- parity/configuration.rs | 9 +++++---- parity/rpc.rs | 6 +++--- rpc/Cargo.toml | 2 +- rpc/src/lib.rs | 8 +++++--- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b3fbd44a..c409cf58a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ dependencies = [ "ethsync 1.2.0", "json-ipc-server 0.1.0 (git+https://github.com/ethcore/json-ipc-server.git)", "jsonrpc-core 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-http-server 5.1.0 (git+https://github.com/ethcore/jsonrpc-http-server.git)", + "jsonrpc-http-server 5.1.0 (git+https://github.com/ethcore/jsonrpc-http-server.git?branch=multiple_cors_domains)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -577,6 +577,16 @@ dependencies = [ "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "jsonrpc-http-server" +version = "5.1.0" +source = "git+https://github.com/ethcore/jsonrpc-http-server.git?branch=multiple_cors_domains#9c026feeb6573c82c99c8005c5d8244de68a2e30" +dependencies = [ + "hyper 0.9.3 (git+https://github.com/ethcore/hyper)", + "jsonrpc-core 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "kernel32-sys" version = "0.2.2" diff --git a/parity/configuration.rs b/parity/configuration.rs index 8d0eea9bb..c7c3480f9 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -263,10 +263,11 @@ impl Configuration { self.args.flag_rpcapi.clone().unwrap_or(self.args.flag_jsonrpc_apis.clone()) } - pub fn rpc_cors(&self) -> Option { - self.args.flag_jsonrpc_cors.clone().or(self.args.flag_rpccorsdomain.clone()) + pub fn rpc_cors(&self) -> Vec { + let cors = self.args.flag_jsonrpc_cors.clone().or(self.args.flag_rpccorsdomain.clone()); + cors.map_or_else(Vec::new, |c| c.split(',').map(|s| s.to_owned()).collect()) } - + fn geth_ipc_path() -> &'static str { if cfg!(target_os = "macos") { "$HOME/Library/Ethereum/geth.ipc" @@ -338,7 +339,7 @@ mod tests { assert_eq!(net.rpc_enabled, true); assert_eq!(net.rpc_interface, "all".to_owned()); assert_eq!(net.rpc_port, 8000); - assert_eq!(conf.rpc_cors(), Some("*".to_owned())); + assert_eq!(conf.rpc_cors(), vec!["*".to_owned()]); assert_eq!(conf.rpc_apis(), "web3,eth".to_owned()); } diff --git a/parity/rpc.rs b/parity/rpc.rs index a48af6401..e1782e9b6 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -41,7 +41,7 @@ pub struct HttpConfiguration { pub interface: String, pub port: u16, pub apis: String, - pub cors: Option, + pub cors: Vec, } pub struct IpcConfiguration { @@ -139,11 +139,11 @@ pub fn setup_http_rpc_server( pub fn setup_http_rpc_server( dependencies: &Arc, url: &SocketAddr, - cors_domain: Option, + cors_domains: Vec, apis: Vec<&str>, ) -> RpcServer { let server = setup_rpc_server(apis, dependencies); - let start_result = server.start_http(url, cors_domain); + let start_result = server.start_http(url, cors_domains); let deps = dependencies.clone(); match start_result { Err(RpcServerError::IoError(err)) => die_with_io_error("RPC", err), diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 326634ab2..c4de059bf 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -13,7 +13,7 @@ log = "0.3" serde = "0.7.0" serde_json = "0.7.0" jsonrpc-core = "2.0" -jsonrpc-http-server = { git = "https://github.com/ethcore/jsonrpc-http-server.git" } +jsonrpc-http-server = { git = "https://github.com/ethcore/jsonrpc-http-server.git", branch = "multiple_cors_domains" } ethcore-util = { path = "../util" } ethcore = { path = "../ethcore" } ethash = { path = "../ethash" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index d4998fdc3..7d9818615 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -59,9 +59,11 @@ impl RpcServer { } /// Start http server asynchronously and returns result with `Server` handle on success or an error. - pub fn start_http(&self, addr: &SocketAddr, cors_domain: Option) -> Result { - let cors_domain = cors_domain.to_owned(); - Server::start(addr, self.handler.clone(), cors_domain.map(jsonrpc_http_server::AccessControlAllowOrigin::Value)) + pub fn start_http(&self, addr: &SocketAddr, cors_domains: Vec) -> Result { + let cors_domains = cors_domains.into_iter() + .map(jsonrpc_http_server::AccessControlAllowOrigin::Value) + .collect(); + Server::start(addr, self.handler.clone(), cors_domains) } /// Start ipc server asynchronously and returns result with `Server` handle on success or an error.