mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-14 23:50:32 +02:00
Compare commits
17 Commits
lets-encry
...
server-v0.
Author | SHA1 | Date | |
---|---|---|---|
|
4837a901e2 | ||
|
a02ff17cb1 | ||
|
dbf566928c | ||
|
ca982b2467 | ||
|
c859d13e3b | ||
|
41e49e8093 | ||
|
715a770d7a | ||
|
5469d8c910 | ||
|
8be5f773f4 | ||
|
b686b4c34e | ||
|
34a7b7f05a | ||
|
b1d9b06a87 | ||
|
94e673b50b | ||
|
1a644c6bb1 | ||
|
aad013f559 | ||
|
7a18d9da26 | ||
|
d59b8ce62e |
@@ -10,6 +10,7 @@ matrix:
|
|||||||
include:
|
include:
|
||||||
- rust: stable
|
- rust: stable
|
||||||
- rust: beta
|
- rust: beta
|
||||||
|
- rust: 1.36.0
|
||||||
- rust: nightly-2019-06-15
|
- rust: nightly-2019-06-15
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rust: nightly-2019-06-15
|
- rust: nightly-2019-06-15
|
||||||
|
68
CHANGES.md
68
CHANGES.md
@@ -1,68 +0,0 @@
|
|||||||
# Changes
|
|
||||||
|
|
||||||
## [0.3.0] - xxx
|
|
||||||
|
|
||||||
* Split `Service` trait to separate crate
|
|
||||||
|
|
||||||
* Use new `Service<Request>` trait
|
|
||||||
|
|
||||||
|
|
||||||
## [0.2.4] - 2018-11-21
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
* Allow to skip name resolution stage in Connector
|
|
||||||
|
|
||||||
|
|
||||||
## [0.2.3] - 2018-11-17
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
* Framed::is_write_buf_empty() checks if write buffer is flushed
|
|
||||||
|
|
||||||
## [0.2.2] - 2018-11-14
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
* Add low/high caps to Framed
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Refactor Connector and Resolver services
|
|
||||||
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
|
|
||||||
* Fix wrong service to socket binding
|
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2018-11-08
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
* Timeout service
|
|
||||||
|
|
||||||
* Added ServiceConfig and ServiceRuntime for server service configuration
|
|
||||||
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Connector has been refactored
|
|
||||||
|
|
||||||
* timer and LowResTimer renamed to time and LowResTime
|
|
||||||
|
|
||||||
* Refactored `Server::configure()` method
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.1] - 2018-10-10
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
- Set actix min version - 0.7.5
|
|
||||||
|
|
||||||
- Set trust-dns min version
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0] - 2018-10-08
|
|
||||||
|
|
||||||
* Initial impl
|
|
19
Cargo.toml
19
Cargo.toml
@@ -17,15 +17,13 @@ edition = "2018"
|
|||||||
members = [
|
members = [
|
||||||
"actix-codec",
|
"actix-codec",
|
||||||
"actix-connect",
|
"actix-connect",
|
||||||
"actix-lets-encrypt",
|
"actix-ioframe",
|
||||||
"actix-rt",
|
"actix-rt",
|
||||||
"actix-service",
|
"actix-service",
|
||||||
"actix-server",
|
"actix-server",
|
||||||
"actix-server-config",
|
"actix-server-config",
|
||||||
"actix-test-server",
|
"actix-testing",
|
||||||
"actix-threadpool",
|
"actix-threadpool",
|
||||||
"actix-tower",
|
|
||||||
"actix-ioframe",
|
|
||||||
"actix-utils",
|
"actix-utils",
|
||||||
"router",
|
"router",
|
||||||
]
|
]
|
||||||
@@ -40,3 +38,16 @@ futures = "0.1.25"
|
|||||||
openssl = "0.10"
|
openssl = "0.10"
|
||||||
tokio-tcp = "0.1"
|
tokio-tcp = "0.1"
|
||||||
tokio-openssl = "0.3"
|
tokio-openssl = "0.3"
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
actix-codec = { path = "actix-codec" }
|
||||||
|
actix-connect = { path = "actix-connect" }
|
||||||
|
actix-ioframe = { path = "actix-ioframe" }
|
||||||
|
actix-rt = { path = "actix-rt" }
|
||||||
|
actix-server = { path = "actix-server" }
|
||||||
|
actix-server-config = { path = "actix-server-config" }
|
||||||
|
actix-service = { path = "actix-service" }
|
||||||
|
actix-testing = { path = "actix-testing" }
|
||||||
|
actix-threadpool = { path = "actix-threadpool" }
|
||||||
|
actix-utils = { path = "actix-utils" }
|
||||||
|
actix-router = { path = "router" }
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# Actix net [](https://travis-ci.org/actix/actix-net) [](https://codecov.io/gh/actix/actix-net) [](https://crates.io/crates/actix-net) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
# Actix net [](https://travis-ci.org/actix/actix-net) [](https://codecov.io/gh/actix/actix-net) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
Actix net - framework for composable network services
|
Actix net - framework for composable network services
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ Actix net - framework for composable network services
|
|||||||
* [API Documentation (Development)](https://actix.rs/actix-net/actix_net/)
|
* [API Documentation (Development)](https://actix.rs/actix-net/actix_net/)
|
||||||
* [Chat on gitter](https://gitter.im/actix/actix)
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
* Cargo package: [actix-net](https://crates.io/crates/actix-net)
|
* Cargo package: [actix-net](https://crates.io/crates/actix-net)
|
||||||
* Minimum supported Rust version: 1.32 or later
|
* Minimum supported Rust version: 1.36 or later
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
@@ -1,5 +1,13 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.5] - 2019-09-05
|
||||||
|
|
||||||
|
* Add `TcpConnectService`
|
||||||
|
|
||||||
|
## [0.2.4] - 2019-09-02
|
||||||
|
|
||||||
|
* Use arbiter's storage for default async resolver
|
||||||
|
|
||||||
## [0.2.3] - 2019-08-05
|
## [0.2.3] - 2019-08-05
|
||||||
|
|
||||||
* Add `ConnectService` and `OpensslConnectService`
|
* Add `ConnectService` and `OpensslConnectService`
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-connect"
|
name = "actix-connect"
|
||||||
version = "0.2.3"
|
version = "0.2.5"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Connector - tcp connector service"
|
description = "Actix Connector - tcp connector service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -36,6 +36,7 @@ uri = ["http"]
|
|||||||
actix-service = "0.4.0"
|
actix-service = "0.4.0"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
actix-utils = "0.4.0"
|
actix-utils = "0.4.0"
|
||||||
|
actix-rt = "0.2.5"
|
||||||
derive_more = "0.15"
|
derive_more = "0.15"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
@@ -56,5 +57,5 @@ webpki = { version = "0.19", optional = true }
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
actix-test-server = { version="0.2.2", features=["ssl"] }
|
actix-testing = { version="0.1.0" }
|
||||||
actix-server-config = "0.1.0"
|
actix-server-config = "0.1.0"
|
||||||
|
@@ -52,7 +52,7 @@ impl<T: Address> NewService for TcpConnectorFactory<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tcp connector service
|
/// Tcp connector service
|
||||||
#[derive(Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct TcpConnector<T>(PhantomData<T>);
|
pub struct TcpConnector<T>(PhantomData<T>);
|
||||||
|
|
||||||
impl<T> TcpConnector<T> {
|
impl<T> TcpConnector<T> {
|
||||||
|
@@ -10,8 +10,6 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
mod connect;
|
mod connect;
|
||||||
mod connector;
|
mod connector;
|
||||||
mod error;
|
mod error;
|
||||||
@@ -30,8 +28,9 @@ pub use self::connect::{Address, Connect, Connection};
|
|||||||
pub use self::connector::{TcpConnector, TcpConnectorFactory};
|
pub use self::connector::{TcpConnector, TcpConnectorFactory};
|
||||||
pub use self::error::ConnectError;
|
pub use self::error::ConnectError;
|
||||||
pub use self::resolver::{Resolver, ResolverFactory};
|
pub use self::resolver::{Resolver, ResolverFactory};
|
||||||
pub use self::service::{ConnectService, ConnectServiceFactory};
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
||||||
|
|
||||||
|
use actix_rt::Arbiter;
|
||||||
use actix_service::{NewService, Service, ServiceExt};
|
use actix_service::{NewService, Service, ServiceExt};
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
@@ -41,16 +40,12 @@ pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver
|
|||||||
resolver
|
resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
struct DefaultResolver(AsyncResolver);
|
||||||
static DEFAULT_RESOLVER: RefCell<Option<AsyncResolver>> = RefCell::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
||||||
DEFAULT_RESOLVER.with(|cell| {
|
if Arbiter::contains_item::<DefaultResolver>() {
|
||||||
if let Some(ref resolver) = *cell.borrow() {
|
return Arbiter::get_item(|item: &DefaultResolver| item.0.clone());
|
||||||
return resolver.clone();
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
let (cfg, opts) = match read_system_conf() {
|
let (cfg, opts) = match read_system_conf() {
|
||||||
Ok((cfg, opts)) => (cfg, opts),
|
Ok((cfg, opts)) => (cfg, opts),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -62,9 +57,9 @@ pub(crate) fn get_default_resolver() -> AsyncResolver {
|
|||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
||||||
tokio_current_thread::spawn(bg);
|
tokio_current_thread::spawn(bg);
|
||||||
|
|
||||||
*cell.borrow_mut() = Some(resolver.clone());
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
||||||
resolver
|
resolver
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_default_resolver() -> AsyncResolver {
|
pub fn start_default_resolver() -> AsyncResolver {
|
||||||
|
@@ -113,17 +113,15 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
||||||
if req.addr.is_some() {
|
if req.addr.is_some() {
|
||||||
Either::B(ok(req))
|
Either::B(ok(req))
|
||||||
|
} else if let Ok(ip) = req.host().parse() {
|
||||||
|
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
||||||
|
Either::B(ok(req))
|
||||||
} else {
|
} else {
|
||||||
if let Ok(ip) = req.host().parse() {
|
trace!("DNS resolver: resolving host {:?}", req.host());
|
||||||
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
if self.resolver.is_none() {
|
||||||
Either::B(ok(req))
|
self.resolver = Some(get_default_resolver());
|
||||||
} else {
|
|
||||||
trace!("DNS resolver: resolving host {:?}", req.host());
|
|
||||||
if self.resolver.is_none() {
|
|
||||||
self.resolver = Some(get_default_resolver());
|
|
||||||
}
|
|
||||||
Either::A(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
|
||||||
}
|
}
|
||||||
|
Either::A(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -38,6 +38,14 @@ impl<T> ConnectServiceFactory<T> {
|
|||||||
resolver: self.resolver.service(),
|
resolver: self.resolver.service(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct new tcp stream service
|
||||||
|
pub fn tcp_service(&self) -> TcpConnectService<T> {
|
||||||
|
TcpConnectService {
|
||||||
|
tcp: self.tcp.service(),
|
||||||
|
resolver: self.resolver.service(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Default for ConnectServiceFactory<T> {
|
impl<T> Default for ConnectServiceFactory<T> {
|
||||||
@@ -121,3 +129,55 @@ impl<T: Address> Future for ConnectServiceResponse<T> {
|
|||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TcpConnectService<T> {
|
||||||
|
tcp: TcpConnector<T>,
|
||||||
|
resolver: Resolver<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Service for TcpConnectService<T> {
|
||||||
|
type Request = Connect<T>;
|
||||||
|
type Response = TcpStream;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Future = TcpConnectServiceResponse<T>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
|
TcpConnectServiceResponse {
|
||||||
|
fut1: Some(self.resolver.call(req)),
|
||||||
|
fut2: None,
|
||||||
|
tcp: self.tcp.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TcpConnectServiceResponse<T: Address> {
|
||||||
|
fut1: Option<<Resolver<T> as Service>::Future>,
|
||||||
|
fut2: Option<<TcpConnector<T> as Service>::Future>,
|
||||||
|
tcp: TcpConnector<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Future for TcpConnectServiceResponse<T> {
|
||||||
|
type Item = TcpStream;
|
||||||
|
type Error = ConnectError;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if let Some(ref mut fut) = self.fut1 {
|
||||||
|
let res = try_ready!(fut.poll());
|
||||||
|
let _ = self.fut1.take();
|
||||||
|
self.fut2 = Some(self.tcp.call(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref mut fut) = self.fut2 {
|
||||||
|
if let Async::Ready(conn) = fut.poll()? {
|
||||||
|
return Ok(Async::Ready(conn.into_parts().0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
use actix_codec::{BytesCodec, Framed};
|
use actix_codec::{BytesCodec, Framed};
|
||||||
use actix_server_config::Io;
|
use actix_server_config::Io;
|
||||||
use actix_service::{service_fn, NewService, Service};
|
use actix_service::{service_fn, NewService, Service};
|
||||||
use actix_test_server::TestServer;
|
use actix_testing::{self as test, TestServer};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::{future::lazy, Future, Sink};
|
use futures::{future::lazy, Future, Sink};
|
||||||
use http::{HttpTryFrom, Uri};
|
use http::{HttpTryFrom, Uri};
|
||||||
@@ -12,7 +12,7 @@ use actix_connect::{default_connector, Connect};
|
|||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "ssl")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string() {
|
fn test_string() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -22,14 +22,14 @@ fn test_string() {
|
|||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = default_connector();
|
||||||
let addr = format!("localhost:{}", srv.port());
|
let addr = format!("localhost:{}", srv.port());
|
||||||
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rust-tls")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rustls_string() {
|
fn test_rustls_string() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -39,12 +39,12 @@ fn test_rustls_string() {
|
|||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = default_connector();
|
||||||
let addr = format!("localhost:{}", srv.port());
|
let addr = format!("localhost:{}", srv.port());
|
||||||
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_str() {
|
fn test_static_str() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -52,33 +52,29 @@ fn test_static_str() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = srv
|
let resolver = test::block_on(lazy(
|
||||||
.block_on(lazy(
|
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
|
||||||
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
|
))
|
||||||
))
|
.unwrap();
|
||||||
.unwrap();
|
|
||||||
let mut conn = srv
|
|
||||||
.block_on(lazy(|| {
|
|
||||||
Ok::<_, ()>(actix_connect::new_connector(resolver.clone()))
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let con = srv
|
let mut conn = test::block_on(lazy(|| {
|
||||||
.block_on(conn.call(Connect::with("10", srv.addr())))
|
Ok::<_, ()>(actix_connect::new_connector(resolver.clone()))
|
||||||
.unwrap();
|
}))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
|
||||||
let connect = Connect::new(srv.host().to_owned());
|
let connect = Connect::new(srv.host().to_owned());
|
||||||
let mut conn = srv
|
let mut conn =
|
||||||
.block_on(lazy(|| Ok::<_, ()>(actix_connect::new_connector(resolver))))
|
test::block_on(lazy(|| Ok::<_, ()>(actix_connect::new_connector(resolver)))).unwrap();
|
||||||
.unwrap();
|
let con = test::block_on(conn.call(connect));
|
||||||
let con = srv.block_on(conn.call(connect));
|
|
||||||
assert!(con.is_err());
|
assert!(con.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new_service() {
|
fn test_new_service() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -86,31 +82,27 @@ fn test_new_service() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = srv
|
let resolver = test::block_on(lazy(|| {
|
||||||
.block_on(lazy(|| {
|
Ok::<_, ()>(actix_connect::start_resolver(
|
||||||
Ok::<_, ()>(actix_connect::start_resolver(
|
ResolverConfig::default(),
|
||||||
ResolverConfig::default(),
|
ResolverOpts::default(),
|
||||||
ResolverOpts::default(),
|
))
|
||||||
))
|
}))
|
||||||
}))
|
.unwrap();
|
||||||
.unwrap();
|
let factory = test::block_on(lazy(|| {
|
||||||
let factory = srv
|
Ok::<_, ()>(actix_connect::new_connector_factory(resolver))
|
||||||
.block_on(lazy(|| {
|
}))
|
||||||
Ok::<_, ()>(actix_connect::new_connector_factory(resolver))
|
.unwrap();
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut conn = srv.block_on(factory.new_service(&())).unwrap();
|
let mut conn = test::block_on(factory.new_service(&())).unwrap();
|
||||||
let con = srv
|
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
||||||
.block_on(conn.call(Connect::with("10", srv.addr())))
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "ssl")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uri() {
|
fn test_uri() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -120,14 +112,14 @@ fn test_uri() {
|
|||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = default_connector();
|
||||||
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
||||||
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rust-tls")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rustls_uri() {
|
fn test_rustls_uri() {
|
||||||
let mut srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
.send(Bytes::from_static(b"test"))
|
.send(Bytes::from_static(b"test"))
|
||||||
@@ -137,6 +129,6 @@ fn test_rustls_uri() {
|
|||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = default_connector();
|
||||||
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
||||||
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,7 @@ log = "0.4"
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "0.2.2"
|
||||||
actix-connect = "0.2.0"
|
actix-connect = "0.2.0"
|
||||||
actix-test-server = "0.2.2"
|
actix-testing = "0.1.0"
|
||||||
actix-server-config = "0.1.1"
|
actix-server-config = "0.1.1"
|
||||||
tokio-tcp = "0.1"
|
tokio-tcp = "0.1"
|
||||||
tokio-timer = "0.2"
|
tokio-timer = "0.2"
|
||||||
|
@@ -44,7 +44,7 @@ where
|
|||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
||||||
@@ -63,7 +63,7 @@ where
|
|||||||
service: F,
|
service: F,
|
||||||
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
||||||
sink: Sink<<U as Encoder>::Item>,
|
sink: Sink<<U as Encoder>::Item>,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
FramedDispatcher {
|
FramedDispatcher {
|
||||||
framed,
|
framed,
|
||||||
|
@@ -62,7 +62,7 @@ impl<St, Codec> Builder<St, Codec> {
|
|||||||
|
|
||||||
pub struct ServiceBuilder<St, C, Io, Codec> {
|
pub struct ServiceBuilder<St, C, Io, Codec> {
|
||||||
connect: C,
|
connect: C,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
_t: PhantomData<(St, Io, Codec)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ where
|
|||||||
|
|
||||||
pub struct NewServiceBuilder<St, C, Io, Codec> {
|
pub struct NewServiceBuilder<St, C, Io, Codec> {
|
||||||
connect: C,
|
connect: C,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
_t: PhantomData<(St, Io, Codec)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +170,7 @@ where
|
|||||||
pub(crate) struct FramedService<St, C, T, Io, Codec, Cfg> {
|
pub(crate) struct FramedService<St, C, T, Io, Codec, Cfg> {
|
||||||
connect: C,
|
connect: C,
|
||||||
handler: Rc<T>,
|
handler: Rc<T>,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ where
|
|||||||
type Error = ServiceError<C::Error, Codec>;
|
type Error = ServiceError<C::Error, Codec>;
|
||||||
type InitError = C::InitError;
|
type InitError = C::InitError;
|
||||||
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
||||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||||
let handler = self.handler.clone();
|
let handler = self.handler.clone();
|
||||||
@@ -221,7 +221,7 @@ where
|
|||||||
pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
||||||
connect: C,
|
connect: C,
|
||||||
handler: Rc<T>,
|
handler: Rc<T>,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
_t: PhantomData<(St, Io, Codec)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +280,7 @@ where
|
|||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
||||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||||
|
@@ -5,7 +5,7 @@ use std::time::Duration;
|
|||||||
use actix_codec::BytesCodec;
|
use actix_codec::BytesCodec;
|
||||||
use actix_server_config::Io;
|
use actix_server_config::Io;
|
||||||
use actix_service::{new_apply_fn, Service};
|
use actix_service::{new_apply_fn, Service};
|
||||||
use actix_test_server::TestServer;
|
use actix_testing::{self as test, TestServer};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_tcp::TcpStream;
|
||||||
use tokio_timer::sleep;
|
use tokio_timer::sleep;
|
||||||
@@ -19,7 +19,7 @@ fn test_disconnect() -> std::io::Result<()> {
|
|||||||
let disconnect = Arc::new(AtomicBool::new(false));
|
let disconnect = Arc::new(AtomicBool::new(false));
|
||||||
let disconnect1 = disconnect.clone();
|
let disconnect1 = disconnect.clone();
|
||||||
|
|
||||||
let mut srv = TestServer::with(move || {
|
let srv = TestServer::with(move || {
|
||||||
let disconnect1 = disconnect1.clone();
|
let disconnect1 = disconnect1.clone();
|
||||||
|
|
||||||
new_apply_fn(
|
new_apply_fn(
|
||||||
@@ -41,15 +41,14 @@ fn test_disconnect() -> std::io::Result<()> {
|
|||||||
})
|
})
|
||||||
.finish(|_t| Ok(None));
|
.finish(|_t| Ok(None));
|
||||||
|
|
||||||
let conn = srv
|
let conn = test::block_on(
|
||||||
.block_on(
|
actix_connect::default_connector()
|
||||||
actix_connect::default_connector()
|
.call(actix_connect::Connect::with(String::new(), srv.addr())),
|
||||||
.call(actix_connect::Connect::with(String::new(), srv.addr())),
|
)
|
||||||
)
|
.unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
srv.block_on(client.call(conn.into_parts().0)).unwrap();
|
test::block_on(client.call(conn.into_parts().0)).unwrap();
|
||||||
let _ = srv.block_on(
|
let _ = test::block_on(
|
||||||
sleep(Duration::from_millis(100))
|
sleep(Duration::from_millis(100))
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| ()),
|
.map_err(|_| ()),
|
||||||
|
@@ -1,21 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix-lets-encrypt"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Jordan Deitch <jd@rsa.pub>"]
|
|
||||||
description = "Actix Let's Encrypt"
|
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
|
||||||
homepage = "https://actix.rs"
|
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
|
||||||
documentation = "https://docs.rs/actix-lets-encrypt/"
|
|
||||||
categories = ["network-programming", "asynchronous"]
|
|
||||||
license = "MIT/Apache-2.0"
|
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
|
||||||
edition = "2018"
|
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "actix_lets_encrypt"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
acme-client = {version = "0.5", default-features = false}
|
|
@@ -1,64 +0,0 @@
|
|||||||
use acme_client::Directory;
|
|
||||||
|
|
||||||
struct CertificateError {
|
|
||||||
message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for CertificateError {
|
|
||||||
fn description(&self) -> &str { self.message.as_str() }
|
|
||||||
fn cause(&self) -> Option<&dyn std::error::Error> { None }
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for CertificateError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(f, "An Error Occurred, Please Try Again!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for CertificateError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(f, "{{ file: {}, line: {} }}", file!(), line!())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CertificateError {
|
|
||||||
fn new(message: String) -> Self {
|
|
||||||
CertificateError { message }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::convert::From<acme_client::error::Error> for CertificateError {
|
|
||||||
fn from(e: acme_client::error::Error) -> Self {
|
|
||||||
return CertificateError::new(e.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CertificateRequest<'a> {
|
|
||||||
domain: &'a str,
|
|
||||||
email: &'a str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> CertificateRequest<'a> {
|
|
||||||
fn new(email: &'a str, domain: &'a str) -> Self {
|
|
||||||
return CertificateRequest { domain, email };
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sign(self: &Self) -> Result<(), CertificateError> {
|
|
||||||
let directory = Directory::lets_encrypt()?;
|
|
||||||
let account = directory.account_registration()
|
|
||||||
.email(self.email)
|
|
||||||
.register()?;
|
|
||||||
let authorization = account.authorization(self.domain)?;
|
|
||||||
|
|
||||||
let http_challenge = authorization.get_http_challenge().ok_or("HTTP challenge failed")?;
|
|
||||||
http_challenge.save_key_authorization("/var/www")?;
|
|
||||||
http_challenge.validate()?;
|
|
||||||
|
|
||||||
let cert = account.certificate_signer(&[self.domain]).sign_certificate()?;
|
|
||||||
cert.save_signed_certificate("certificate.pem")?;
|
|
||||||
cert.save_private_key("certificate.key")?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,2 +0,0 @@
|
|||||||
mod certificate_signer;
|
|
||||||
mod authorization;
|
|
@@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.5] - 2019-09-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add arbiter specific storage
|
||||||
|
|
||||||
|
|
||||||
## [0.2.4] - 2019-07-17
|
## [0.2.4] - 2019-07-17
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-rt"
|
name = "actix-rt"
|
||||||
version = "0.2.4"
|
version = "0.2.5"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix runtime"
|
description = "Actix runtime"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
use std::any::{Any, TypeId};
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
@@ -16,15 +17,16 @@ use copyless::BoxHelper;
|
|||||||
thread_local!(
|
thread_local!(
|
||||||
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
||||||
static RUNNING: Cell<bool> = Cell::new(false);
|
static RUNNING: Cell<bool> = Cell::new(false);
|
||||||
static Q: RefCell<Vec<Box<Future<Item = (), Error = ()>>>> = RefCell::new(Vec::new());
|
static Q: RefCell<Vec<Box<dyn Future<Item = (), Error = ()>>>> = RefCell::new(Vec::new());
|
||||||
|
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
||||||
);
|
);
|
||||||
|
|
||||||
pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);
|
pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
pub(crate) enum ArbiterCommand {
|
pub(crate) enum ArbiterCommand {
|
||||||
Stop,
|
Stop,
|
||||||
Execute(Box<Future<Item = (), Error = ()> + Send>),
|
Execute(Box<dyn Future<Item = (), Error = ()> + Send>),
|
||||||
ExecuteFn(Box<FnExec>),
|
ExecuteFn(Box<dyn FnExec>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for ArbiterCommand {
|
impl fmt::Debug for ArbiterCommand {
|
||||||
@@ -56,6 +58,7 @@ impl Arbiter {
|
|||||||
let arb = Arbiter(tx);
|
let arb = Arbiter(tx);
|
||||||
ADDR.with(|cell| *cell.borrow_mut() = Some(arb.clone()));
|
ADDR.with(|cell| *cell.borrow_mut() = Some(arb.clone()));
|
||||||
RUNNING.with(|cell| cell.set(false));
|
RUNNING.with(|cell| cell.set(false));
|
||||||
|
STORAGE.with(|cell| cell.borrow_mut().clear());
|
||||||
Arbiter::spawn(ArbiterController { stop: None, rx });
|
Arbiter::spawn(ArbiterController { stop: None, rx });
|
||||||
|
|
||||||
arb
|
arb
|
||||||
@@ -90,6 +93,7 @@ impl Arbiter {
|
|||||||
|
|
||||||
let (stop, stop_rx) = channel();
|
let (stop, stop_rx) = channel();
|
||||||
RUNNING.with(|cell| cell.set(true));
|
RUNNING.with(|cell| cell.set(true));
|
||||||
|
STORAGE.with(|cell| cell.borrow_mut().clear());
|
||||||
|
|
||||||
System::set_current(sys);
|
System::set_current(sys);
|
||||||
|
|
||||||
@@ -180,7 +184,7 @@ impl Arbiter {
|
|||||||
let _ = self
|
let _ = self
|
||||||
.0
|
.0
|
||||||
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
|
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
|
||||||
let _ = f();
|
f();
|
||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +206,50 @@ impl Arbiter {
|
|||||||
})));
|
})));
|
||||||
rx
|
rx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set item to arbiter storage
|
||||||
|
pub fn set_item<T: 'static>(item: T) {
|
||||||
|
STORAGE.with(move |cell| cell.borrow_mut().insert(TypeId::of::<T>(), Box::new(item)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if arbiter storage contains item
|
||||||
|
pub fn contains_item<T: 'static>() -> bool {
|
||||||
|
STORAGE.with(move |cell| cell.borrow().get(&TypeId::of::<T>()).is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a reference to a type previously inserted on this arbiter's storage.
|
||||||
|
///
|
||||||
|
/// Panics is item is not inserted
|
||||||
|
pub fn get_item<T: 'static, F, R>(mut f: F) -> R
|
||||||
|
where
|
||||||
|
F: FnMut(&T) -> R,
|
||||||
|
{
|
||||||
|
STORAGE.with(move |cell| {
|
||||||
|
let st = cell.borrow();
|
||||||
|
let item = st
|
||||||
|
.get(&TypeId::of::<T>())
|
||||||
|
.and_then(|boxed| (&**boxed as &(dyn Any + 'static)).downcast_ref())
|
||||||
|
.unwrap();
|
||||||
|
f(item)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a mutable reference to a type previously inserted on this arbiter's storage.
|
||||||
|
///
|
||||||
|
/// Panics is item is not inserted
|
||||||
|
pub fn get_mut_item<T: 'static, F, R>(mut f: F) -> R
|
||||||
|
where
|
||||||
|
F: FnMut(&mut T) -> R,
|
||||||
|
{
|
||||||
|
STORAGE.with(move |cell| {
|
||||||
|
let mut st = cell.borrow_mut();
|
||||||
|
let item = st
|
||||||
|
.get_mut(&TypeId::of::<T>())
|
||||||
|
.and_then(|boxed| (&mut **boxed as &mut (dyn Any + 'static)).downcast_mut())
|
||||||
|
.unwrap();
|
||||||
|
f(item)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ArbiterController {
|
struct ArbiterController {
|
||||||
@@ -312,7 +360,7 @@ impl<F> FnExec for F
|
|||||||
where
|
where
|
||||||
F: FnOnce() + Send + 'static,
|
F: FnOnce() + Send + 'static,
|
||||||
{
|
{
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
|
#[allow(clippy::boxed_local)]
|
||||||
fn call_box(self: Box<Self>) {
|
fn call_box(self: Box<Self>) {
|
||||||
(*self)()
|
(*self)()
|
||||||
}
|
}
|
||||||
|
@@ -40,7 +40,7 @@ impl Error for RunError {
|
|||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
self.inner.description()
|
self.inner.description()
|
||||||
}
|
}
|
||||||
fn cause(&self) -> Option<&Error> {
|
fn cause(&self) -> Option<&dyn Error> {
|
||||||
self.inner.source()
|
self.inner.source()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.6.1] - 2019-09-25
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add UDS listening support to `ServerBuilder`
|
||||||
|
|
||||||
|
|
||||||
## [0.6.0] - 2019-07-18
|
## [0.6.0] - 2019-07-18
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-server"
|
name = "actix-server"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix server - General purpose tcp server"
|
description = "Actix server - General purpose tcp server"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
@@ -185,20 +185,41 @@ impl ServerBuilder {
|
|||||||
|
|
||||||
#[cfg(all(unix, feature = "uds"))]
|
#[cfg(all(unix, feature = "uds"))]
|
||||||
/// Add new unix domain service to the server.
|
/// Add new unix domain service to the server.
|
||||||
pub fn bind_uds<F, U, N>(mut self, name: N, addr: U, factory: F) -> io::Result<Self>
|
pub fn bind_uds<F, U, N>(self, name: N, addr: U, factory: F) -> io::Result<Self>
|
||||||
where
|
where
|
||||||
F: ServiceFactory<tokio_uds::UnixStream>,
|
F: ServiceFactory<tokio_uds::UnixStream>,
|
||||||
N: AsRef<str>,
|
N: AsRef<str>,
|
||||||
U: AsRef<std::path::Path>,
|
U: AsRef<std::path::Path>,
|
||||||
{
|
{
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|
||||||
use std::os::unix::net::UnixListener;
|
use std::os::unix::net::UnixListener;
|
||||||
|
|
||||||
// TODO: need to do something with existing paths
|
// The path must not exist when we try to bind.
|
||||||
let _ = std::fs::remove_file(addr.as_ref());
|
// Try to remove it to avoid bind error.
|
||||||
|
if let Err(e) = std::fs::remove_file(addr.as_ref()) {
|
||||||
|
// NotFound is expected and not an issue. Anything else is.
|
||||||
|
if e.kind() != std::io::ErrorKind::NotFound {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let lst = UnixListener::bind(addr)?;
|
let lst = UnixListener::bind(addr)?;
|
||||||
|
self.listen_uds(name, lst, factory)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(unix, feature = "uds"))]
|
||||||
|
/// Add new unix domain service to the server.
|
||||||
|
/// Useful when running as a systemd service and
|
||||||
|
/// a socket FD can be acquired using the systemd crate.
|
||||||
|
pub fn listen_uds<F, N: AsRef<str>>(
|
||||||
|
mut self,
|
||||||
|
name: N,
|
||||||
|
lst: std::os::unix::net::UnixListener,
|
||||||
|
factory: F,
|
||||||
|
) -> io::Result<Self>
|
||||||
|
where
|
||||||
|
F: ServiceFactory<tokio_uds::UnixStream>,
|
||||||
|
{
|
||||||
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||||
let token = self.token.next();
|
let token = self.token.next();
|
||||||
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
||||||
self.services.push(StreamNewService::create(
|
self.services.push(StreamNewService::create(
|
||||||
|
@@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.4.2] - 2019-08-27
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Check service readiness for `new_apply_cfg` combinator
|
||||||
|
|
||||||
|
|
||||||
## [0.4.1] - 2019-06-06
|
## [0.4.1] - 2019-06-06
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.4.1"
|
version = "0.4.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Service"
|
description = "Actix Service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
@@ -49,6 +49,7 @@ where
|
|||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: NewService<Config = ()>,
|
||||||
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: IntoFuture<Error = T::InitError>,
|
||||||
R::Item: IntoService<S>,
|
R::Item: IntoService<S>,
|
||||||
S: Service,
|
S: Service,
|
||||||
@@ -179,6 +180,7 @@ where
|
|||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: NewService<Config = ()>,
|
||||||
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: IntoFuture<Error = T::InitError>,
|
||||||
R::Item: IntoService<S>,
|
R::Item: IntoService<S>,
|
||||||
S: Service,
|
S: Service,
|
||||||
@@ -196,8 +198,9 @@ where
|
|||||||
ApplyConfigNewServiceFut {
|
ApplyConfigNewServiceFut {
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
cfg: cfg.clone(),
|
cfg: cfg.clone(),
|
||||||
srv: Some(self.srv.get_ref().new_service(&())),
|
|
||||||
fut: None,
|
fut: None,
|
||||||
|
srv: None,
|
||||||
|
srv_fut: Some(self.srv.get_ref().new_service(&())),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -208,13 +211,15 @@ where
|
|||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: NewService<Config = ()>,
|
||||||
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: IntoFuture<Error = T::InitError>,
|
||||||
R::Item: IntoService<S>,
|
R::Item: IntoService<S>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
cfg: C,
|
cfg: C,
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
srv: Option<T::Future>,
|
srv: Option<T::Service>,
|
||||||
|
srv_fut: Option<T::Future>,
|
||||||
fut: Option<R::Future>,
|
fut: Option<R::Future>,
|
||||||
_t: PhantomData<(S,)>,
|
_t: PhantomData<(S,)>,
|
||||||
}
|
}
|
||||||
@@ -224,6 +229,7 @@ where
|
|||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: NewService<Config = ()>,
|
||||||
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: IntoFuture<Error = T::InitError>,
|
||||||
R::Item: IntoService<S>,
|
R::Item: IntoService<S>,
|
||||||
S: Service,
|
S: Service,
|
||||||
@@ -232,12 +238,12 @@ where
|
|||||||
type Error = R::Error;
|
type Error = R::Error;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
if let Some(ref mut fut) = self.srv {
|
if let Some(ref mut fut) = self.srv_fut {
|
||||||
match fut.poll()? {
|
match fut.poll()? {
|
||||||
Async::NotReady => return Ok(Async::NotReady),
|
Async::NotReady => return Ok(Async::NotReady),
|
||||||
Async::Ready(mut srv) => {
|
Async::Ready(srv) => {
|
||||||
let _ = self.srv.take();
|
let _ = self.srv_fut.take();
|
||||||
self.fut = Some(self.f.get_mut()(&self.cfg, &mut srv).into_future());
|
self.srv = Some(srv);
|
||||||
return self.poll();
|
return self.poll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,6 +251,14 @@ where
|
|||||||
|
|
||||||
if let Some(ref mut fut) = self.fut {
|
if let Some(ref mut fut) = self.fut {
|
||||||
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
|
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
|
||||||
|
} else if let Some(ref mut srv) = self.srv {
|
||||||
|
match srv.poll_ready()? {
|
||||||
|
Async::NotReady => Ok(Async::NotReady),
|
||||||
|
Async::Ready(_) => {
|
||||||
|
self.fut = Some(self.f.get_mut()(&self.cfg, srv).into_future());
|
||||||
|
return self.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ use futures::{Async, Future, IntoFuture, Poll};
|
|||||||
use crate::{NewService, Service};
|
use crate::{NewService, Service};
|
||||||
|
|
||||||
pub type BoxedService<Req, Res, Err> = Box<
|
pub type BoxedService<Req, Res, Err> = Box<
|
||||||
Service<
|
dyn Service<
|
||||||
Request = Req,
|
Request = Req,
|
||||||
Response = Res,
|
Response = Res,
|
||||||
Error = Err,
|
Error = Err,
|
||||||
@@ -13,7 +13,7 @@ pub type BoxedService<Req, Res, Err> = Box<
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
pub type BoxedServiceResponse<Res, Err> =
|
pub type BoxedServiceResponse<Res, Err> =
|
||||||
Either<FutureResult<Res, Err>, Box<Future<Item = Res, Error = Err>>>;
|
Either<FutureResult<Res, Err>, Box<dyn Future<Item = Res, Error = Err>>>;
|
||||||
|
|
||||||
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
||||||
|
|
||||||
@@ -46,14 +46,14 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Inner<C, Req, Res, Err, InitErr> = Box<
|
type Inner<C, Req, Res, Err, InitErr> = Box<
|
||||||
NewService<
|
dyn NewService<
|
||||||
Config = C,
|
Config = C,
|
||||||
Request = Req,
|
Request = Req,
|
||||||
Response = Res,
|
Response = Res,
|
||||||
Error = Err,
|
Error = Err,
|
||||||
InitError = InitErr,
|
InitError = InitErr,
|
||||||
Service = BoxedService<Req, Res, Err>,
|
Service = BoxedService<Req, Res, Err>,
|
||||||
Future = Box<Future<Item = BoxedService<Req, Res, Err>, Error = InitErr>>,
|
Future = Box<dyn Future<Item = BoxedService<Req, Res, Err>, Error = InitErr>>,
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ where
|
|||||||
type InitError = InitErr;
|
type InitError = InitErr;
|
||||||
type Config = C;
|
type Config = C;
|
||||||
type Service = BoxedService<Req, Res, Err>;
|
type Service = BoxedService<Req, Res, Err>;
|
||||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
self.0.new_service(cfg)
|
self.0.new_service(cfg)
|
||||||
@@ -99,7 +99,7 @@ where
|
|||||||
type InitError = InitErr;
|
type InitError = InitErr;
|
||||||
type Config = C;
|
type Config = C;
|
||||||
type Service = BoxedService<Req, Res, Err>;
|
type Service = BoxedService<Req, Res, Err>;
|
||||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
Box::new(
|
Box::new(
|
||||||
@@ -133,7 +133,7 @@ where
|
|||||||
type Error = Err;
|
type Error = Err;
|
||||||
type Future = Either<
|
type Future = Either<
|
||||||
FutureResult<Self::Response, Self::Error>,
|
FutureResult<Self::Response, Self::Error>,
|
||||||
Box<Future<Item = Self::Response, Error = Self::Error>>,
|
Box<dyn Future<Item = Self::Response, Error = Self::Error>>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
@@ -34,6 +34,7 @@ impl<T> Cell<T> {
|
|||||||
unsafe { &mut *self.inner.as_ref().get() }
|
unsafe { &mut *self.inner.as_ref().get() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::mut_from_ref)]
|
||||||
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
|
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
|
||||||
&mut *self.inner.as_ref().get()
|
&mut *self.inner.as_ref().get()
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-test-server"
|
name = "actix-test-server"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix test server"
|
description = "Actix test server"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -11,31 +11,16 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
features = ["ssl", "tls", "rust-tls"]
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_test_server"
|
name = "actix_test_server"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
|
|
||||||
# tls
|
|
||||||
tls = ["native-tls", "actix-server/tls"]
|
|
||||||
|
|
||||||
# openssl
|
|
||||||
ssl = ["openssl", "actix-server/ssl"]
|
|
||||||
|
|
||||||
# rustls
|
|
||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.1"
|
actix-rt = "0.2.1"
|
||||||
actix-server = "0.5.0"
|
actix-server = "0.5.0"
|
||||||
actix-server-config = "0.1.0"
|
actix-server-config = "0.1.0"
|
||||||
|
actix-testing = "0.1.0"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
@@ -43,17 +28,5 @@ futures = "0.1"
|
|||||||
tokio-tcp = "0.1"
|
tokio-tcp = "0.1"
|
||||||
tokio-reactor = "0.1"
|
tokio-reactor = "0.1"
|
||||||
|
|
||||||
# native-tls
|
|
||||||
native-tls = { version="0.2", optional = true }
|
|
||||||
|
|
||||||
# openssl
|
|
||||||
openssl = { version="0.10", optional = true }
|
|
||||||
|
|
||||||
#rustls
|
|
||||||
rustls = { version = "^0.15", optional = true }
|
|
||||||
tokio-rustls = { version = "^0.9", optional = true }
|
|
||||||
webpki = { version = "0.19", optional = true }
|
|
||||||
webpki-roots = { version = "0.16", optional = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.0"
|
||||||
|
3
actix-test-server/README.md
Normal file
3
actix-test-server/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Actix test server (Deprecated)
|
||||||
|
|
||||||
|
Use [actix-testing](https://docs.rs/actix-testing/) instead
|
@@ -1,149 +1,2 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
//! Various helpers for Actix applications to use during testing.
|
||||||
use std::sync::mpsc;
|
pub use actix_testing::*;
|
||||||
use std::{net, thread};
|
|
||||||
|
|
||||||
use actix_rt::{Runtime, System};
|
|
||||||
use actix_server::{Server, StreamServiceFactory};
|
|
||||||
pub use actix_server_config::{Io, ServerConfig};
|
|
||||||
|
|
||||||
use futures::future::{lazy, Future, IntoFuture};
|
|
||||||
use net2::TcpBuilder;
|
|
||||||
use tokio_reactor::Handle;
|
|
||||||
use tokio_tcp::TcpStream;
|
|
||||||
|
|
||||||
/// The `TestServer` type.
|
|
||||||
///
|
|
||||||
/// `TestServer` is very simple test server that simplify process of writing
|
|
||||||
/// integration tests for actix-net applications.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_service::{service_fn, IntoNewService};
|
|
||||||
/// use actix_test_server::TestServer;
|
|
||||||
///
|
|
||||||
/// fn main() {
|
|
||||||
/// let srv = TestServer::with(|| service_fn(
|
|
||||||
/// |sock| {
|
|
||||||
/// println!("New connection: {:?}", sock);
|
|
||||||
/// Ok::<_, ()>(())
|
|
||||||
/// }
|
|
||||||
/// ));
|
|
||||||
///
|
|
||||||
/// println!("SOCKET: {:?}", srv.connect());
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub struct TestServer;
|
|
||||||
|
|
||||||
/// Test server runstime
|
|
||||||
pub struct TestServerRuntime {
|
|
||||||
addr: net::SocketAddr,
|
|
||||||
host: String,
|
|
||||||
port: u16,
|
|
||||||
rt: Runtime,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestServer {
|
|
||||||
/// Start new test server with application factory
|
|
||||||
pub fn with<F: StreamServiceFactory>(factory: F) -> TestServerRuntime {
|
|
||||||
let (tx, rx) = mpsc::channel();
|
|
||||||
|
|
||||||
// run server in separate thread
|
|
||||||
thread::spawn(move || {
|
|
||||||
let sys = System::new("actix-test-server");
|
|
||||||
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
||||||
let local_addr = tcp.local_addr().unwrap();
|
|
||||||
|
|
||||||
Server::build()
|
|
||||||
.listen("test", tcp, factory)?
|
|
||||||
.workers(1)
|
|
||||||
.disable_signals()
|
|
||||||
.start();
|
|
||||||
|
|
||||||
tx.send((System::current(), local_addr)).unwrap();
|
|
||||||
sys.run()
|
|
||||||
});
|
|
||||||
|
|
||||||
let (system, addr) = rx.recv().unwrap();
|
|
||||||
System::set_current(system);
|
|
||||||
|
|
||||||
let rt = Runtime::new().unwrap();
|
|
||||||
let host = format!("{}", addr.ip());
|
|
||||||
let port = addr.port();
|
|
||||||
|
|
||||||
TestServerRuntime {
|
|
||||||
addr,
|
|
||||||
rt,
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get firat available unused local address
|
|
||||||
pub fn unused_addr() -> net::SocketAddr {
|
|
||||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
|
||||||
let socket = TcpBuilder::new_v4().unwrap();
|
|
||||||
socket.bind(&addr).unwrap();
|
|
||||||
socket.reuse_address(true).unwrap();
|
|
||||||
let tcp = socket.to_tcp_listener().unwrap();
|
|
||||||
tcp.local_addr().unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestServerRuntime {
|
|
||||||
/// Execute future on current runtime
|
|
||||||
pub fn block_on<F, I, E>(&mut self, fut: F) -> Result<I, E>
|
|
||||||
where
|
|
||||||
F: Future<Item = I, Error = E>,
|
|
||||||
{
|
|
||||||
self.rt.block_on(fut)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Runs the provided function, with runtime enabled.
|
|
||||||
pub fn run_on<F, R>(&mut self, f: F) -> Result<R::Item, R::Error>
|
|
||||||
where
|
|
||||||
F: FnOnce() -> R,
|
|
||||||
R: IntoFuture,
|
|
||||||
{
|
|
||||||
self.rt.block_on(lazy(|| f().into_future()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Spawn future to the current runtime
|
|
||||||
pub fn spawn<F>(&mut self, fut: F)
|
|
||||||
where
|
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
|
||||||
{
|
|
||||||
self.rt.spawn(fut);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Test server host
|
|
||||||
pub fn host(&self) -> &str {
|
|
||||||
&self.host
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Test server port
|
|
||||||
pub fn port(&self) -> u16 {
|
|
||||||
self.port
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get test server address
|
|
||||||
pub fn addr(&self) -> net::SocketAddr {
|
|
||||||
self.addr
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stop http server
|
|
||||||
fn stop(&mut self) {
|
|
||||||
System::current().stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connect to server, return tokio TcpStream
|
|
||||||
pub fn connect(&self) -> std::io::Result<TcpStream> {
|
|
||||||
TcpStream::from_std(net::TcpStream::connect(self.addr)?, &Handle::default())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for TestServerRuntime {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
5
actix-testing/CHANGES.md
Normal file
5
actix-testing/CHANGES.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.0] - 2019-09-25
|
||||||
|
|
||||||
|
* Initial impl
|
29
actix-testing/Cargo.toml
Normal file
29
actix-testing/Cargo.toml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-testing"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
description = "Actix testing utils"
|
||||||
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
homepage = "https://actix.rs"
|
||||||
|
repository = "https://github.com/actix/actix-net.git"
|
||||||
|
documentation = "https://docs.rs/actix-testing/"
|
||||||
|
categories = ["network-programming", "asynchronous"]
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "actix_testing"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-rt = "0.2.1"
|
||||||
|
actix-server = "0.6.0"
|
||||||
|
actix-server-config = "0.1.0"
|
||||||
|
actix-service = "0.4.0"
|
||||||
|
|
||||||
|
log = "0.4"
|
||||||
|
net2 = "0.2"
|
||||||
|
futures = "0.1"
|
||||||
|
tokio-tcp = "0.1"
|
||||||
|
tokio-reactor = "0.1"
|
9
actix-testing/README.md
Normal file
9
actix-testing/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Actix test utilities [](https://crates.io/crates/actix-testint) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
## Documentation & community resources
|
||||||
|
|
||||||
|
* [User Guide](https://actix.rs/docs/)
|
||||||
|
* [API Documentation](https://docs.rs/actix-testing/)
|
||||||
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
|
* Cargo package: [actix-http-test](https://crates.io/crates/actix-testing)
|
||||||
|
* Minimum supported Rust version: 1.37 or later
|
152
actix-testing/src/lib.rs
Normal file
152
actix-testing/src/lib.rs
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
//! Various helpers for Actix applications to use during testing.
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::{net, thread};
|
||||||
|
|
||||||
|
use actix_rt::System;
|
||||||
|
use actix_server::{Server, ServerBuilder, StreamServiceFactory};
|
||||||
|
pub use actix_server_config::{Io, ServerConfig};
|
||||||
|
|
||||||
|
use net2::TcpBuilder;
|
||||||
|
use tokio_reactor::Handle;
|
||||||
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
|
mod rt;
|
||||||
|
pub use self::rt::*;
|
||||||
|
|
||||||
|
/// The `TestServer` type.
|
||||||
|
///
|
||||||
|
/// `TestServer` is very simple test server that simplify process of writing
|
||||||
|
/// integration tests for actix-net applications.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use actix_service::{service_fn, IntoNewService};
|
||||||
|
/// use actix_testing::TestServer;
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let srv = TestServer::with(|| service_fn(
|
||||||
|
/// |sock| {
|
||||||
|
/// println!("New connection: {:?}", sock);
|
||||||
|
/// Ok::<_, ()>(())
|
||||||
|
/// }
|
||||||
|
/// ));
|
||||||
|
///
|
||||||
|
/// println!("SOCKET: {:?}", srv.connect());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub struct TestServer;
|
||||||
|
|
||||||
|
/// Test server runstime
|
||||||
|
pub struct TestServerRuntime {
|
||||||
|
addr: net::SocketAddr,
|
||||||
|
host: String,
|
||||||
|
port: u16,
|
||||||
|
system: System,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServer {
|
||||||
|
/// Start new server with server builder
|
||||||
|
pub fn new<F>(mut factory: F) -> TestServerRuntime
|
||||||
|
where
|
||||||
|
F: FnMut(ServerBuilder) -> ServerBuilder + Send + 'static,
|
||||||
|
{
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
// run server in separate thread
|
||||||
|
thread::spawn(move || {
|
||||||
|
let sys = System::new("actix-test-server");
|
||||||
|
factory(Server::build())
|
||||||
|
.workers(1)
|
||||||
|
.disable_signals()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
tx.send(System::current()).unwrap();
|
||||||
|
sys.run()
|
||||||
|
});
|
||||||
|
let system = rx.recv().unwrap();
|
||||||
|
|
||||||
|
TestServerRuntime {
|
||||||
|
system,
|
||||||
|
addr: "127.0.0.1:0".parse().unwrap(),
|
||||||
|
host: "127.0.0.1".to_string(),
|
||||||
|
port: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start new test server with application factory
|
||||||
|
pub fn with<F: StreamServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
// run server in separate thread
|
||||||
|
thread::spawn(move || {
|
||||||
|
let sys = System::new("actix-test-server");
|
||||||
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let local_addr = tcp.local_addr().unwrap();
|
||||||
|
|
||||||
|
Server::build()
|
||||||
|
.listen("test", tcp, factory)?
|
||||||
|
.workers(1)
|
||||||
|
.disable_signals()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
tx.send((System::current(), local_addr)).unwrap();
|
||||||
|
sys.run()
|
||||||
|
});
|
||||||
|
|
||||||
|
let (system, addr) = rx.recv().unwrap();
|
||||||
|
|
||||||
|
let host = format!("{}", addr.ip());
|
||||||
|
let port = addr.port();
|
||||||
|
|
||||||
|
TestServerRuntime {
|
||||||
|
system,
|
||||||
|
addr,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get firat available unused local address
|
||||||
|
pub fn unused_addr() -> net::SocketAddr {
|
||||||
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
|
let socket = TcpBuilder::new_v4().unwrap();
|
||||||
|
socket.bind(&addr).unwrap();
|
||||||
|
socket.reuse_address(true).unwrap();
|
||||||
|
let tcp = socket.to_tcp_listener().unwrap();
|
||||||
|
tcp.local_addr().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServerRuntime {
|
||||||
|
/// Test server host
|
||||||
|
pub fn host(&self) -> &str {
|
||||||
|
&self.host
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test server port
|
||||||
|
pub fn port(&self) -> u16 {
|
||||||
|
self.port
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get test server address
|
||||||
|
pub fn addr(&self) -> net::SocketAddr {
|
||||||
|
self.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stop http server
|
||||||
|
fn stop(&mut self) {
|
||||||
|
self.system.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to server, return tokio TcpStream
|
||||||
|
pub fn connect(&self) -> std::io::Result<TcpStream> {
|
||||||
|
TcpStream::from_std(net::TcpStream::connect(self.addr)?, &Handle::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TestServerRuntime {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.stop()
|
||||||
|
}
|
||||||
|
}
|
116
actix-testing/src/rt.rs
Normal file
116
actix-testing/src/rt.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//! Various helpers for Actix applications to use during testing.
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
use actix_rt::{System, SystemRunner};
|
||||||
|
use actix_service::Service;
|
||||||
|
use futures::future::{lazy, Future, IntoFuture};
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static RT: RefCell<Inner> = {
|
||||||
|
RefCell::new(Inner(Some(System::builder().build())))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Inner(Option<SystemRunner>);
|
||||||
|
|
||||||
|
impl Inner {
|
||||||
|
fn get_mut(&mut self) -> &mut SystemRunner {
|
||||||
|
self.0.as_mut().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Inner {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
std::mem::forget(self.0.take().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the provided future, blocking the current thread until the future
|
||||||
|
/// completes.
|
||||||
|
///
|
||||||
|
/// This function can be used to synchronously block the current thread
|
||||||
|
/// until the provided `future` has resolved either successfully or with an
|
||||||
|
/// error. The result of the future is then returned from this function
|
||||||
|
/// call.
|
||||||
|
///
|
||||||
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
|
/// This function panics on nested call.
|
||||||
|
pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
|
||||||
|
where
|
||||||
|
F: IntoFuture,
|
||||||
|
{
|
||||||
|
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f.into_future()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the provided function, blocking the current thread until the result
|
||||||
|
/// future completes.
|
||||||
|
///
|
||||||
|
/// This function can be used to synchronously block the current thread
|
||||||
|
/// until the provided `future` has resolved either successfully or with an
|
||||||
|
/// error. The result of the future is then returned from this function
|
||||||
|
/// call.
|
||||||
|
///
|
||||||
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
|
/// This function panics on nested call.
|
||||||
|
pub fn block_fn<F, R>(f: F) -> Result<R::Item, R::Error>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> R,
|
||||||
|
R: IntoFuture,
|
||||||
|
{
|
||||||
|
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(f)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn future to the current test runtime.
|
||||||
|
pub fn spawn<F>(fut: F)
|
||||||
|
where
|
||||||
|
F: Future<Item = (), Error = ()> + 'static,
|
||||||
|
{
|
||||||
|
run_on(move || {
|
||||||
|
actix_rt::spawn(fut);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the provided function, with runtime enabled.
|
||||||
|
///
|
||||||
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
|
/// This function panics on nested call.
|
||||||
|
pub fn run_on<F, R>(f: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce() -> R,
|
||||||
|
{
|
||||||
|
RT.with(move |rt| {
|
||||||
|
rt.borrow_mut()
|
||||||
|
.get_mut()
|
||||||
|
.block_on(lazy(|| Ok::<_, ()>(f())))
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calls service and waits for response future completion.
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// use actix_web::{test, App, HttpResponse, http::StatusCode};
|
||||||
|
/// use actix_service::Service;
|
||||||
|
///
|
||||||
|
/// #[test]
|
||||||
|
/// fn test_response() {
|
||||||
|
/// let mut app = test::init_service(
|
||||||
|
/// App::new()
|
||||||
|
/// .service(web::resource("/test").to(|| HttpResponse::Ok()))
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// // Create request object
|
||||||
|
/// let req = test::TestRequest::with_uri("/test").to_request();
|
||||||
|
///
|
||||||
|
/// // Call application
|
||||||
|
/// let resp = test::call_service(&mut app, req);
|
||||||
|
/// assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn call_service<S, R>(app: &mut S, req: R) -> S::Response
|
||||||
|
where
|
||||||
|
S: Service<Request = R>,
|
||||||
|
S::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
block_on(run_on(move || app.call(req))).unwrap()
|
||||||
|
}
|
@@ -1,2 +0,0 @@
|
|||||||
# Changes
|
|
||||||
|
|
@@ -1,29 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix-tower"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Marcus Griep <marcus@griep.us>"]
|
|
||||||
description = "Actix Tower"
|
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
|
||||||
homepage = "https://actix.rs"
|
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
|
||||||
documentation = "https://docs.rs/actix-tower/"
|
|
||||||
categories = ["network-programming", "asynchronous"]
|
|
||||||
license = "MIT/Apache-2.0"
|
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
|
||||||
edition = "2018"
|
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[badges]
|
|
||||||
travis-ci = { repository = "actix/actix-tower", branch = "master" }
|
|
||||||
appveyor = { repository = "actix/actix-net" }
|
|
||||||
codecov = { repository = "actix/actix-tower", branch = "master", service = "github" }
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "actix_tower"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-service = "0.3.6"
|
|
||||||
futures = "0.1.24"
|
|
||||||
tower-service = "0.2.0"
|
|
||||||
|
|
@@ -1,580 +0,0 @@
|
|||||||
//! Utilities to provide interoperability between services based on the
|
|
||||||
//! `actix-service` and `tower-service` crates.
|
|
||||||
//!
|
|
||||||
//! ## Example
|
|
||||||
//!
|
|
||||||
//! In the following example, we take a `RandomService`—which will always
|
|
||||||
//! return 4—and wraps it with a middleware that will always add 1 to the
|
|
||||||
//! result. This pattern can be further used to wrap services from either
|
|
||||||
//! `tower-service` or `actix-service` with middleware provided by the other.
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use actix_tower::ActixServiceExt;
|
|
||||||
//! # use futures::{Async, Future};
|
|
||||||
//! use actix_service::Service;
|
|
||||||
//!
|
|
||||||
//! struct RandomService;
|
|
||||||
//! impl Service for RandomService {
|
|
||||||
//! // …
|
|
||||||
//! # type Request = ();
|
|
||||||
//! # type Response = u32;
|
|
||||||
//! # type Error = ();
|
|
||||||
//! # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
|
||||||
//! #
|
|
||||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
//! # Ok(Async::Ready(()))
|
|
||||||
//! # }
|
|
||||||
//! #
|
|
||||||
//! # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
|
||||||
//! # futures::finished(4)
|
|
||||||
//! # }
|
|
||||||
//! }
|
|
||||||
//!
|
|
||||||
//! struct AddOneMiddleware<S>(S);
|
|
||||||
//! impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
|
||||||
//! where
|
|
||||||
//! S: tower_service::Service<R, Response = u32>,
|
|
||||||
//! S::Future: 'static,
|
|
||||||
//! {
|
|
||||||
//! /// …
|
|
||||||
//! # type Response = u32;
|
|
||||||
//! # type Error = S::Error;
|
|
||||||
//! # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
|
||||||
//! #
|
|
||||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
//! # self.0.poll_ready()
|
|
||||||
//! # }
|
|
||||||
//! #
|
|
||||||
//! # fn call(&mut self, req: R) -> Self::Future {
|
|
||||||
//! # let fut = self.0.call(req).map(|x| x + 1);
|
|
||||||
//! # Box::new(fut)
|
|
||||||
//! # }
|
|
||||||
//! }
|
|
||||||
//!
|
|
||||||
//! let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
|
||||||
//! assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
//! assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
|
||||||
//! ```
|
|
||||||
|
|
||||||
use actix_service::Service as ActixService;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use tower_service::Service as TowerService;
|
|
||||||
|
|
||||||
/// Compatibility wrapper associating a `tower_service::Service` with a particular
|
|
||||||
/// `Request` type, so that it can be used as an `actix_service::Service`.
|
|
||||||
///
|
|
||||||
/// Generally created through convenience methods on the `TowerServiceExt<R>` trait.
|
|
||||||
pub struct ActixCompat<S, R> {
|
|
||||||
inner: S,
|
|
||||||
_phantom: PhantomData<R>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, R> ActixCompat<S, R> {
|
|
||||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
|
||||||
pub fn new(inner: S) -> Self {
|
|
||||||
ActixCompat {
|
|
||||||
inner,
|
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extension trait for wrapping a `tower_service::Service` instance for use as
|
|
||||||
/// an `actix_service::Service`.
|
|
||||||
pub trait TowerServiceExt<R>: TowerService<R> + Sized {
|
|
||||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_service::Service;
|
|
||||||
/// use actix_tower::TowerServiceExt;
|
|
||||||
/// # use futures::{Async, Future};
|
|
||||||
///
|
|
||||||
/// struct RandomService;
|
|
||||||
/// impl<R> tower_service::Service<R> for RandomService {
|
|
||||||
/// // …
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = ();
|
|
||||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # Ok(Async::Ready(()))
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
|
||||||
/// # futures::finished(4)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// let mut s = RandomService.into_actix_service();
|
|
||||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
|
||||||
/// ```
|
|
||||||
fn into_actix_service(self) -> ActixCompat<Self, R> {
|
|
||||||
ActixCompat::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Takes a function that, when provided with an `actix_service::Service` wraps it
|
|
||||||
/// and returns a new service. Useful for wrapping a `tower_service::Service` with
|
|
||||||
/// middleware built for `actix_service`.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_tower::TowerServiceExt;
|
|
||||||
/// # use futures::{Async, Future};
|
|
||||||
/// use tower_service::Service;
|
|
||||||
///
|
|
||||||
/// struct RandomService;
|
|
||||||
/// impl<R> Service<R> for RandomService {
|
|
||||||
/// // …
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = ();
|
|
||||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # Ok(Async::Ready(()))
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
|
||||||
/// # futures::finished(4)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// struct AddOneTransform<S>(S);
|
|
||||||
/// impl<S> actix_service::Service for AddOneTransform<S>
|
|
||||||
/// where
|
|
||||||
/// S: actix_service::Service<Response = u32>,
|
|
||||||
/// S::Future: 'static,
|
|
||||||
/// {
|
|
||||||
/// /// …
|
|
||||||
/// # type Request = S::Request;
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = S::Error;
|
|
||||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # self.0.poll_ready()
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
||||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
|
||||||
/// # Box::new(fut)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// let mut s = RandomService.wrap_with_actix_middleware(AddOneTransform);
|
|
||||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
|
||||||
/// ```
|
|
||||||
fn wrap_with_actix_middleware<F, U>(self, f: F) -> TowerCompat<U>
|
|
||||||
where
|
|
||||||
F: FnOnce(ActixCompat<Self, R>) -> U,
|
|
||||||
U: ActixService<Request = R>,
|
|
||||||
{
|
|
||||||
f(self.into_actix_service()).into_tower_service()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, R> TowerServiceExt<R> for S where S: TowerService<R> + Sized {}
|
|
||||||
|
|
||||||
impl<S, R> ActixService for ActixCompat<S, R>
|
|
||||||
where
|
|
||||||
S: TowerService<R>,
|
|
||||||
{
|
|
||||||
type Request = R;
|
|
||||||
type Response = S::Response;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = S::Future;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
TowerService::poll_ready(&mut self.inner)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
||||||
TowerService::call(&mut self.inner, req)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compatibility wrapper associating an `actix_service::Service` with a particular
|
|
||||||
/// `Request` type, so that it can be used as a `tower_service::Service`.
|
|
||||||
///
|
|
||||||
/// Generally created through convenience methods on the `ActixServiceExt<R>` trait.
|
|
||||||
pub struct TowerCompat<S> {
|
|
||||||
inner: S,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> TowerCompat<S> {
|
|
||||||
/// Wraps an `actix_service::Service` in a compatibility wrapper.
|
|
||||||
pub fn new(inner: S) -> Self {
|
|
||||||
TowerCompat { inner }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extension trait for wrapping an `actix_service::Service` instance for use as
|
|
||||||
/// a `tower_service::Service`.
|
|
||||||
pub trait ActixServiceExt: ActixService + Sized {
|
|
||||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_tower::ActixServiceExt;
|
|
||||||
/// # use futures::{Async, Future};
|
|
||||||
/// use tower_service::Service;
|
|
||||||
///
|
|
||||||
/// struct RandomService;
|
|
||||||
/// impl actix_service::Service for RandomService {
|
|
||||||
/// // …
|
|
||||||
/// # type Request = ();
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = ();
|
|
||||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # Ok(Async::Ready(()))
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
|
||||||
/// # futures::finished(4)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// let mut s = RandomService.into_tower_service();
|
|
||||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
|
||||||
/// ```
|
|
||||||
fn into_tower_service(self) -> TowerCompat<Self> {
|
|
||||||
TowerCompat::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Takes a function that, when provided with a `tower_service::Service` wraps it
|
|
||||||
/// and returns a new service. Useful for wrapping an `actix_service::Service` with
|
|
||||||
/// middleware built for `tower_service`.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_tower::ActixServiceExt;
|
|
||||||
/// # use futures::{Async, Future};
|
|
||||||
/// use actix_service::Service;
|
|
||||||
///
|
|
||||||
/// struct RandomService;
|
|
||||||
/// impl Service for RandomService {
|
|
||||||
/// // …
|
|
||||||
/// # type Request = ();
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = ();
|
|
||||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # Ok(Async::Ready(()))
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
|
||||||
/// # futures::finished(4)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// struct AddOneMiddleware<S>(S);
|
|
||||||
/// impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
|
||||||
/// where
|
|
||||||
/// S: tower_service::Service<R, Response = u32>,
|
|
||||||
/// S::Future: 'static,
|
|
||||||
/// {
|
|
||||||
/// /// …
|
|
||||||
/// # type Response = u32;
|
|
||||||
/// # type Error = S::Error;
|
|
||||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
|
||||||
/// #
|
|
||||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
/// # self.0.poll_ready()
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn call(&mut self, req: R) -> Self::Future {
|
|
||||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
|
||||||
/// # Box::new(fut)
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
|
||||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
|
||||||
/// ```
|
|
||||||
fn wrap_with_tower_middleware<F, U>(self, f: F) -> ActixCompat<U, Self::Request>
|
|
||||||
where
|
|
||||||
F: FnOnce(TowerCompat<Self>) -> U,
|
|
||||||
U: TowerService<Self::Request>,
|
|
||||||
{
|
|
||||||
f(self.into_tower_service()).into_actix_service()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> ActixServiceExt for S where S: ActixService + Sized {}
|
|
||||||
|
|
||||||
impl<S> TowerService<S::Request> for TowerCompat<S>
|
|
||||||
where
|
|
||||||
S: ActixService,
|
|
||||||
{
|
|
||||||
type Response = S::Response;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = S::Future;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
|
||||||
ActixService::poll_ready(&mut self.inner)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: S::Request) -> Self::Future {
|
|
||||||
ActixService::call(&mut self.inner, req)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
mod tower_service_into_actix_service {
|
|
||||||
use crate::TowerServiceExt;
|
|
||||||
use actix_service::{Service as ActixService, ServiceExt, Transform};
|
|
||||||
use futures::{future::FutureResult, Async, Future, Poll};
|
|
||||||
use tower_service::Service as TowerService;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_returns_4() {
|
|
||||||
let mut s = RandomService.into_actix_service();
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_can_combine() {
|
|
||||||
let mut s = RandomService.into_actix_service().map(|x| x + 1);
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_can_use_actix_middleware() {
|
|
||||||
let mut s = RandomService.wrap_with_actix_middleware(DoMathTransform);
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_and_add_service_chained() {
|
|
||||||
let s1 = RandomService.into_actix_service();
|
|
||||||
let s2 = AddOneService.into_actix_service();
|
|
||||||
let s3 = AddOneService.into_actix_service();
|
|
||||||
|
|
||||||
let mut s = s1.and_then(s2).and_then(s3);
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(6)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
|
||||||
let s1 = RandomService.into_actix_service();
|
|
||||||
let s2 = AddOneService.into_actix_service();
|
|
||||||
let s3 = AddOneService.into_actix_service();
|
|
||||||
let s4 = RandomService.into_actix_service();
|
|
||||||
|
|
||||||
let mut s = s1.and_then(s2).and_then(s3).and_then(s4);
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_can_be_transformed_to_do_math() {
|
|
||||||
let transform = DoMath;
|
|
||||||
|
|
||||||
let mut s = transform
|
|
||||||
.new_transform(RandomService.into_actix_service())
|
|
||||||
.wait()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct RandomService;
|
|
||||||
impl<R> TowerService<R> for RandomService {
|
|
||||||
type Response = u32;
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _req: R) -> Self::Future {
|
|
||||||
futures::finished(4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct AddOneService;
|
|
||||||
impl TowerService<u32> for AddOneService {
|
|
||||||
type Response = u32;
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: u32) -> Self::Future {
|
|
||||||
futures::finished(req + 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DoMathTransform<S>(S);
|
|
||||||
impl<S> ActixService for DoMathTransform<S>
|
|
||||||
where
|
|
||||||
S: ActixService<Response = u32>,
|
|
||||||
S::Future: 'static,
|
|
||||||
{
|
|
||||||
type Request = S::Request;
|
|
||||||
type Response = u32;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
self.0.poll_ready()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
||||||
let fut = self.0.call(req).map(|x| x * 17);
|
|
||||||
Box::new(fut)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DoMath;
|
|
||||||
impl<S> Transform<S> for DoMath
|
|
||||||
where
|
|
||||||
S: ActixService<Response = u32>,
|
|
||||||
S::Future: 'static,
|
|
||||||
{
|
|
||||||
type Request = S::Request;
|
|
||||||
type Response = u32;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Transform = DoMathTransform<S>;
|
|
||||||
type InitError = ();
|
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
|
||||||
futures::finished(DoMathTransform(service))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod actix_service_into_tower_service {
|
|
||||||
use crate::{ActixServiceExt, TowerServiceExt};
|
|
||||||
use actix_service::{Service as ActixService, ServiceExt};
|
|
||||||
use futures::{future::FutureResult, Async, Future, Poll};
|
|
||||||
use tower_service::Service as TowerService;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_returns_4() {
|
|
||||||
let mut s = RandomService.into_tower_service();
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_can_use_tower_middleware() {
|
|
||||||
let mut s =
|
|
||||||
AddOneService::wrap(RandomService.into_tower_service()).into_actix_service();
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn do_math_service_can_use_tower_middleware() {
|
|
||||||
let mut s =
|
|
||||||
AddOneService::wrap(DoMathService.into_tower_service()).into_actix_service();
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(188)), s.call(11).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
|
||||||
let s1 = RandomService.wrap_with_tower_middleware(AddOneService::wrap);
|
|
||||||
let s2 = DoMathService.wrap_with_tower_middleware(AddOneService::wrap);
|
|
||||||
|
|
||||||
let mut s = s1.and_then(s2);
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
|
||||||
|
|
||||||
assert_eq!(Ok(Async::Ready(86)), s.call(()).poll());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct RandomService;
|
|
||||||
impl ActixService for RandomService {
|
|
||||||
type Request = ();
|
|
||||||
type Response = u32;
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _req: Self::Request) -> Self::Future {
|
|
||||||
futures::finished(4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct AddOneService<S> {
|
|
||||||
inner: S,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> AddOneService<S> {
|
|
||||||
fn wrap(inner: S) -> Self {
|
|
||||||
AddOneService { inner }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, R> TowerService<R> for AddOneService<S>
|
|
||||||
where
|
|
||||||
S: TowerService<R, Response = u32>,
|
|
||||||
S::Future: 'static,
|
|
||||||
{
|
|
||||||
type Response = u32;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
self.inner.poll_ready()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: R) -> Self::Future {
|
|
||||||
let fut = self.inner.call(req).map(|x| x + 1);
|
|
||||||
|
|
||||||
Box::new(fut)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DoMathService;
|
|
||||||
impl ActixService for DoMathService {
|
|
||||||
type Request = u32;
|
|
||||||
type Response = u32;
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
||||||
futures::finished(req * 17)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -119,7 +119,7 @@ mod tests {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<Future<Item = (), Error = ()>>;
|
type Future = Box<dyn Future<Item = (), Error = ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
|
@@ -214,7 +214,7 @@ mod tests {
|
|||||||
type Request = oneshot::Receiver<usize>;
|
type Request = oneshot::Receiver<usize>;
|
||||||
type Response = usize;
|
type Response = usize;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<Future<Item = usize, Error = ()>>;
|
type Future = Box<dyn Future<Item = usize, Error = ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
|
@@ -45,7 +45,7 @@ where
|
|||||||
type Request = S;
|
type Request = S;
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = Box<Future<Item = (), Error = E>>;
|
type Future = Box<dyn Future<Item = (), Error = E>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
|
@@ -190,7 +190,7 @@ mod tests {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<Future<Item = (), Error = ()>>;
|
type Future = Box<dyn Future<Item = (), Error = ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
|
@@ -151,7 +151,7 @@ impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T>
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
if self.path.len() < 1 {
|
if self.path.is_empty() {
|
||||||
Err(de::value::Error::custom(
|
Err(de::value::Error::custom(
|
||||||
"expeceted at least one parameters",
|
"expeceted at least one parameters",
|
||||||
))
|
))
|
||||||
|
@@ -93,7 +93,7 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
/// Skip first `n` chars in path
|
/// Skip first `n` chars in path
|
||||||
pub fn skip(&mut self, n: u16) {
|
pub fn skip(&mut self, n: u16) {
|
||||||
self.skip = self.skip + n;
|
self.skip += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add(&mut self, name: Rc<String>, value: PathItem) {
|
pub(crate) fn add(&mut self, name: Rc<String>, value: PathItem) {
|
||||||
|
@@ -207,7 +207,7 @@ impl ResourceDef {
|
|||||||
"Dynamic path match but not all segments found: {}",
|
"Dynamic path match but not all segments found: {}",
|
||||||
name
|
name
|
||||||
);
|
);
|
||||||
false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -279,7 +279,7 @@ impl ResourceDef {
|
|||||||
"Dynamic path match but not all segments found: {}",
|
"Dynamic path match but not all segments found: {}",
|
||||||
name
|
name
|
||||||
);
|
);
|
||||||
false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user