From 0f74f280f9d44403ed5f0907891d142567d9e1b9 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 14 Mar 2019 11:15:32 -0700 Subject: [PATCH] impl Address for http::Uri --- .travis.yml | 2 +- actix-connect/Cargo.toml | 8 +++++-- actix-connect/src/connect.rs | 4 +++- actix-connect/src/lib.rs | 3 +++ actix-connect/src/uri.rs | 35 +++++++++++++++++++++++++++++ actix-connect/tests/test_connect.rs | 19 ++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 actix-connect/src/uri.rs diff --git a/.travis.yml b/.travis.yml index 7429ccc9..0215d4b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,7 +43,7 @@ script: fi - | if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-03-02" ]]; then - RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install cargo-tarpaulin + RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install --git https://github.com/xd009642/tarpaulin.git --branch develop cargo tarpaulin --features="ssl,tls,rust-tls" --out Xml bash <(curl -s https://codecov.io/bash) echo "Uploaded code coverage" diff --git a/actix-connect/Cargo.toml b/actix-connect/Cargo.toml index 30b998de..b5580899 100644 --- a/actix-connect/Cargo.toml +++ b/actix-connect/Cargo.toml @@ -14,18 +14,21 @@ edition = "2018" workspace = ".." [package.metadata.docs.rs] -features = ["ssl"] +features = ["ssl", "uri"] [lib] name = "actix_connect" path = "src/lib.rs" [features] -default = [] +default = ["uri"] # openssl ssl = ["openssl", "tokio-openssl"] +# support http::Uri as connect address +uri = ["http"] + [dependencies] actix-service = "0.3.4" actix-codec = "0.1.1" @@ -33,6 +36,7 @@ actix-utils = "0.3.4" derive_more = "0.14.0" either = "1.5.1" futures = "0.1.25" +http = { version = "0.1.16", optional = true } log = "0.4" tokio-tcp = "0.1.3" tokio-current-thread = "0.1.5" diff --git a/actix-connect/src/connect.rs b/actix-connect/src/connect.rs index 2fd05a17..0565cbea 100644 --- a/actix-connect/src/connect.rs +++ b/actix-connect/src/connect.rs @@ -61,7 +61,9 @@ impl Connect { } } - /// Set port + /// Use port if address does not provide one. + /// + /// By default it set to 0 pub fn set_port(mut self, port: u16) -> Self { self.port = port; self diff --git a/actix-connect/src/lib.rs b/actix-connect/src/lib.rs index 51bc3f79..11925cc4 100644 --- a/actix-connect/src/lib.rs +++ b/actix-connect/src/lib.rs @@ -14,6 +14,9 @@ mod error; mod resolver; pub mod ssl; +#[cfg(feature = "uri")] +mod uri; + pub use trust_dns_resolver::{error::ResolveError, AsyncResolver}; pub use self::connect::{Address, Connect, Connection}; diff --git a/actix-connect/src/uri.rs b/actix-connect/src/uri.rs new file mode 100644 index 00000000..4f992121 --- /dev/null +++ b/actix-connect/src/uri.rs @@ -0,0 +1,35 @@ +use http::Uri; + +use crate::Address; + +impl Address for Uri { + fn host(&self) -> &str { + self.host().unwrap_or("") + } + + fn port(&self) -> Option { + if let Some(port) = self.port_u16() { + Some(port) + } else { + port(self.scheme_str()) + } + } +} + +// TODO: load data from file +fn port(scheme: Option<&str>) -> Option { + if let Some(scheme) = scheme { + match scheme { + "http" => Some(80), + "https" => Some(443), + "ws" => Some(80), + "wss" => Some(443), + "amqp" => Some(5672), + "mqtt" => Some(1883), + "mqtts" => Some(8883), + _ => None, + } + } else { + None + } +} diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index 908b26e5..f2ec467a 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -4,6 +4,7 @@ use actix_service::{fn_service, NewService, Service}; use actix_test_server::TestServer; use bytes::Bytes; use futures::{future::lazy, Future, Sink}; +use http::{HttpTryFrom, Uri}; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; use actix_connect::{default_connector, Connect}; @@ -81,3 +82,21 @@ fn test_new_service() { .unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); } + +#[test] +fn test_uri() { + let mut srv = TestServer::with(|| { + fn_service(|io: Io| { + Framed::new(io.into_parts().0, BytesCodec) + .send(Bytes::from_static(b"test")) + .then(|_| Ok::<_, ()>(())) + }) + }); + + let mut conn = srv + .block_on(lazy(|| Ok::<_, ()>(default_connector()))) + .unwrap(); + let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); + let con = srv.block_on(conn.call(addr.into())).unwrap(); + assert_eq!(con.peer_addr().unwrap(), srv.addr()); +}