1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 23:42:56 +01:00

impl Address for http::Uri

This commit is contained in:
Nikolay Kim 2019-03-14 11:15:32 -07:00
parent eb37e15554
commit 0f74f280f9
6 changed files with 67 additions and 4 deletions

View File

@ -43,7 +43,7 @@ script:
fi fi
- | - |
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-03-02" ]]; then 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 cargo tarpaulin --features="ssl,tls,rust-tls" --out Xml
bash <(curl -s https://codecov.io/bash) bash <(curl -s https://codecov.io/bash)
echo "Uploaded code coverage" echo "Uploaded code coverage"

View File

@ -14,18 +14,21 @@ edition = "2018"
workspace = ".." workspace = ".."
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = ["ssl"] features = ["ssl", "uri"]
[lib] [lib]
name = "actix_connect" name = "actix_connect"
path = "src/lib.rs" path = "src/lib.rs"
[features] [features]
default = [] default = ["uri"]
# openssl # openssl
ssl = ["openssl", "tokio-openssl"] ssl = ["openssl", "tokio-openssl"]
# support http::Uri as connect address
uri = ["http"]
[dependencies] [dependencies]
actix-service = "0.3.4" actix-service = "0.3.4"
actix-codec = "0.1.1" actix-codec = "0.1.1"
@ -33,6 +36,7 @@ actix-utils = "0.3.4"
derive_more = "0.14.0" derive_more = "0.14.0"
either = "1.5.1" either = "1.5.1"
futures = "0.1.25" futures = "0.1.25"
http = { version = "0.1.16", optional = true }
log = "0.4" log = "0.4"
tokio-tcp = "0.1.3" tokio-tcp = "0.1.3"
tokio-current-thread = "0.1.5" tokio-current-thread = "0.1.5"

View File

@ -61,7 +61,9 @@ impl<T: Address> Connect<T> {
} }
} }
/// 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 { pub fn set_port(mut self, port: u16) -> Self {
self.port = port; self.port = port;
self self

View File

@ -14,6 +14,9 @@ mod error;
mod resolver; mod resolver;
pub mod ssl; pub mod ssl;
#[cfg(feature = "uri")]
mod uri;
pub use trust_dns_resolver::{error::ResolveError, AsyncResolver}; pub use trust_dns_resolver::{error::ResolveError, AsyncResolver};
pub use self::connect::{Address, Connect, Connection}; pub use self::connect::{Address, Connect, Connection};

35
actix-connect/src/uri.rs Normal file
View File

@ -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<u16> {
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<u16> {
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
}
}

View File

@ -4,6 +4,7 @@ use actix_service::{fn_service, NewService, Service};
use actix_test_server::TestServer; use actix_test_server::TestServer;
use bytes::Bytes; use bytes::Bytes;
use futures::{future::lazy, Future, Sink}; use futures::{future::lazy, Future, Sink};
use http::{HttpTryFrom, Uri};
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use actix_connect::{default_connector, Connect}; use actix_connect::{default_connector, Connect};
@ -81,3 +82,21 @@ fn test_new_service() {
.unwrap(); .unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }
#[test]
fn test_uri() {
let mut srv = TestServer::with(|| {
fn_service(|io: Io<tokio_tcp::TcpStream>| {
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());
}