diff --git a/Cargo.toml b/Cargo.toml index 5a877b1..db8158e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ integration-tests = [] name = "tikv_client" [dependencies] +regex = "1" failure = "0.1" futures-preview = { version = "0.3.0-alpha.15", features = ["compat"] } grpcio = { version = "0.5.0-alpha", features = [ "secure", "prost-codec" ], default-features = false } diff --git a/src/errors.rs b/src/errors.rs index 716a4f3..65d12e1 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -95,7 +95,7 @@ pub enum ErrorKind { } impl Fail for Error { - fn cause(&self) -> Option<&Fail> { + fn cause(&self) -> Option<&dyn Fail> { self.inner.cause() } diff --git a/src/rpc/security.rs b/src/rpc/security.rs index ee8f338..20d97be 100644 --- a/src/rpc/security.rs +++ b/src/rpc/security.rs @@ -9,10 +9,16 @@ use std::{ }; use grpcio::{Channel, ChannelBuilder, ChannelCredentialsBuilder, Environment}; +use lazy_static::*; use log::*; +use regex::Regex; use crate::Result; +lazy_static! { + static ref SCHEME_REG: Regex = Regex::new(r"^\s*(https?://)").unwrap(); +} + fn check_pem_file(tag: &str, path: &Path) -> Result { File::open(path) .map_err(|e| internal_err!("failed to open {} to load {}: {:?}", path.display(), tag, e)) @@ -65,21 +71,21 @@ impl SecurityManager { Factory: FnOnce(Channel) -> Client, { info!("connect to rpc server at endpoint: {:?}", addr); - let addr = addr - .trim_start_matches("http://") - .trim_start_matches("https://"); + + let addr = SCHEME_REG.replace(addr, ""); + let cb = ChannelBuilder::new(env) .keepalive_time(Duration::from_secs(10)) .keepalive_timeout(Duration::from_secs(3)); let channel = if self.ca.is_empty() { - cb.connect(addr) + cb.connect(&addr) } else { let cred = ChannelCredentialsBuilder::new() .root_cert(self.ca.clone()) .cert(self.cert.clone(), load_pem_file("private key", &self.key)?) .build(); - cb.secure_connect(addr, cred) + cb.secure_connect(&addr, cred) }; Ok(factory(channel))