mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-14 10:00:31 +02:00
Compare commits
83 Commits
router-v0.
...
test-serve
Author | SHA1 | Date | |
---|---|---|---|
|
d3208bf7a8 | ||
|
21507d3da1 | ||
|
b9d8a215b4 | ||
|
51c4dfe5cb | ||
|
a60112c71e | ||
|
bd814d6f80 | ||
|
a4e0c71baa | ||
|
b9ea445e70 | ||
|
ba2901269d | ||
|
5cbc29306a | ||
|
810fa869ae | ||
|
33cd51aabf | ||
|
629ed05f82 | ||
|
5e8ae210f7 | ||
|
3add90628f | ||
|
02ab804e0b | ||
|
feac0b43d9 | ||
|
1441355d4f | ||
|
7c5afc09a6 | ||
|
16856c7d3f | ||
|
95d02659d5 | ||
|
bcbd7e6ddf | ||
|
e0d3581239 | ||
|
ef1bdb2eb2 | ||
|
10301ff49d | ||
|
27c28d6597 | ||
|
b290273e81 | ||
|
720230b852 | ||
|
44c2639fd6 | ||
|
9a5705d1b6 | ||
|
7ff923a58f | ||
|
6659b192d3 | ||
|
1146d9cf30 | ||
|
b7b76c47e5 | ||
|
d23dc6f6af | ||
|
9b6a955da4 | ||
|
f3aa48309f | ||
|
c9b86712e5 | ||
|
0f74f280f9 | ||
|
eb37e15554 | ||
|
ad007b8b42 | ||
|
7c0d1f2273 | ||
|
d82bc7c52b | ||
|
265229b44b | ||
|
38545dedc7 | ||
|
6ebff22601 | ||
|
2c9b91b366 | ||
|
b483200037 | ||
|
a73600fbcd | ||
|
084a28ca07 | ||
|
a7c74c53ea | ||
|
3e7d737e73 | ||
|
87db4bf741 | ||
|
8b0fe6f796 | ||
|
52a45fda53 | ||
|
2c7de7e0fb | ||
|
1fcc0734b5 | ||
|
b6f952b036 | ||
|
0fdac38307 | ||
|
a3c4637372 | ||
|
ae9bc5ae78 | ||
|
21c289d7e4 | ||
|
5e6eed905c | ||
|
6801a38de5 | ||
|
39356690b0 | ||
|
f6f292a678 | ||
|
b3366bc1af | ||
|
2c1f8f0b96 | ||
|
e7465bfa2e | ||
|
755d4958c5 | ||
|
825117fd4c | ||
|
7033b50fed | ||
|
ef9bfb8981 | ||
|
bef199f831 | ||
|
f1d4bcef4b | ||
|
8e13ba7bce | ||
|
9a9b3e9ca9 | ||
|
5567fb41d2 | ||
|
ad50595ece | ||
|
2430c7247b | ||
|
1bf0f1e1a5 | ||
|
9887aef6e8 | ||
|
787255d030 |
36
.travis.yml
36
.travis.yml
@@ -10,9 +10,9 @@ matrix:
|
|||||||
include:
|
include:
|
||||||
- rust: stable
|
- rust: stable
|
||||||
- rust: beta
|
- rust: beta
|
||||||
- rust: nightly
|
- rust: nightly-2019-03-02
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rust: nightly
|
- rust: nightly-2019-03-02
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
@@ -24,34 +24,26 @@ before_install:
|
|||||||
- sudo apt-get update -qq
|
- sudo apt-get update -qq
|
||||||
- sudo apt-get install -y openssl libssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev
|
- sudo apt-get install -y openssl libssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev
|
||||||
|
|
||||||
|
before_cache: |
|
||||||
|
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-03-02" ]]; then
|
||||||
|
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install cargo-tarpaulin
|
||||||
|
fi
|
||||||
|
|
||||||
# Add clippy
|
# Add clippy
|
||||||
before_script:
|
before_script:
|
||||||
- export PATH=$PATH:~/.cargo/bin
|
- export PATH=$PATH:~/.cargo/bin
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" != "nightly" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" != "nightly-2019-03-02" ]]; then
|
||||||
cargo clean
|
cargo clean
|
||||||
cargo test --features="ssl,tls,rust-tls" -- --nocapture
|
cargo test --all --all-features -- --nocapture
|
||||||
cd actix-codec && cargo test && cd ..
|
|
||||||
cd actix-service && cargo test && cd ..
|
|
||||||
cd actix-server && cargo test --features="ssl,tls,rust-tls" -- --nocapture && cd ..
|
|
||||||
cd actix-rt && cargo test && cd ..
|
|
||||||
cd actix-connector && cargo test && cd ..
|
|
||||||
cd actix-utils && cargo test && cd ..
|
|
||||||
cd router && cargo test && cd ..
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
after_success:
|
||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-03-02" ]]; then
|
||||||
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install -f cargo-tarpaulin
|
taskset -c 0 cargo tarpaulin --all --all-features --out Xml
|
||||||
cargo tarpaulin --features="ssl,tls,rust-tls" --out Xml
|
|
||||||
bash <(curl -s https://codecov.io/bash)
|
|
||||||
echo "Uploaded code coverage"
|
echo "Uploaded code coverage"
|
||||||
cd actix-service && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
bash <(curl -s https://codecov.io/bash)
|
||||||
cd actix-rt && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
cd actix-connector && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
cd actix-codec && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
cd actix-server && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
cd actix-utils && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
cd router && cargo tarpaulin --out Xml && bash <(curl -s https://codecov.io/bash) && cd ..
|
|
||||||
fi
|
fi
|
||||||
|
@@ -16,12 +16,13 @@ edition = "2018"
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"actix-codec",
|
"actix-codec",
|
||||||
"actix-connector",
|
"actix-connect",
|
||||||
"actix-rt",
|
"actix-rt",
|
||||||
"actix-service",
|
"actix-service",
|
||||||
"actix-server",
|
"actix-server",
|
||||||
"actix-server-config",
|
"actix-server-config",
|
||||||
"actix-test-server",
|
"actix-test-server",
|
||||||
|
"actix-threadpool",
|
||||||
"actix-utils",
|
"actix-utils",
|
||||||
"router",
|
"router",
|
||||||
]
|
]
|
||||||
|
49
README.md
49
README.md
@@ -1,20 +1,18 @@
|
|||||||
# 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://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 - framework for composable network services (experimental)
|
Actix net - framework for composable network services
|
||||||
|
|
||||||
## Documentation & community resources
|
## Documentation & community resources
|
||||||
|
|
||||||
* [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.26 or later
|
* Minimum supported Rust version: 1.32 or later
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() -> io::Result<()> {
|
||||||
let sys = actix_rt::System::new("test");
|
|
||||||
|
|
||||||
// load ssl keys
|
// load ssl keys
|
||||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||||
builder.set_private_key_file("./examples/key.pem", SslFiletype::PEM).unwrap();
|
builder.set_private_key_file("./examples/key.pem", SslFiletype::PEM).unwrap();
|
||||||
@@ -26,7 +24,7 @@ fn main() {
|
|||||||
// bind socket address and start workers. By default server uses number of
|
// bind socket address and start workers. By default server uses number of
|
||||||
// available logical cpu as threads count. actix net start separate
|
// available logical cpu as threads count. actix net start separate
|
||||||
// instances of service pipeline in each worker.
|
// instances of service pipeline in each worker.
|
||||||
actix_server::build()
|
Server::build()
|
||||||
.bind(
|
.bind(
|
||||||
// configure service pipeline
|
// configure service pipeline
|
||||||
"basic", "0.0.0.0:8443",
|
"basic", "0.0.0.0:8443",
|
||||||
@@ -35,28 +33,23 @@ fn main() {
|
|||||||
let acceptor = acceptor.clone();
|
let acceptor = acceptor.clone();
|
||||||
|
|
||||||
// service for converting incoming TcpStream to a SslStream<TcpStream>
|
// service for converting incoming TcpStream to a SslStream<TcpStream>
|
||||||
(move |stream| {
|
fn_service(move |stream: Io<tokio_tcp::TcpStream>| {
|
||||||
SslAcceptorExt::accept_async(&acceptor, stream)
|
SslAcceptorExt::accept_async(&acceptor, stream.into_parts().0)
|
||||||
.map_err(|e| println!("Openssl error: {}", e))
|
.map_err(|e| println!("Openssl error: {}", e))
|
||||||
})
|
})
|
||||||
// convert closure to a `NewService`
|
// .and_then() combinator uses other service to convert incoming `Request` to a
|
||||||
.into_new_service()
|
// `Response` and then uses that response as an input for next
|
||||||
|
// service. in this case, on success we use `logger` service
|
||||||
// .and_then() combinator uses other service to convert incoming `Request` to a `Response`
|
.and_then(fn_service(logger))
|
||||||
// and then uses that response as an input for next service.
|
// Next service counts number of connections
|
||||||
// in this case, on success we use `logger` service
|
.and_then(move |_| {
|
||||||
.and_then(logger)
|
let num = num.fetch_add(1, Ordering::Relaxed);
|
||||||
|
println!("got ssl connection {:?}", num);
|
||||||
// Next service counts number of connections
|
future::ok(())
|
||||||
.and_then(move |req| {
|
})
|
||||||
let num = num.fetch_add(1, Ordering::Relaxed);
|
},
|
||||||
println!("processed {:?} connections", num);
|
)?
|
||||||
future::ok(())
|
.run()
|
||||||
})
|
|
||||||
}).unwrap()
|
|
||||||
.start();
|
|
||||||
|
|
||||||
sys.run();
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -1,6 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.1.0] - 2019-03-06
|
## [0.1.2] - 2019-03-27
|
||||||
|
|
||||||
|
* Added `Framed::map_io()` method.
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.1] - 2019-03-06
|
||||||
|
|
||||||
* Added `FramedParts::with_read_buffer()` method.
|
* Added `FramedParts::with_read_buffer()` method.
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-codec"
|
name = "actix-codec"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Utilities for encoding and decoding frames"
|
description = "Utilities for encoding and decoding frames"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -11,15 +11,15 @@ 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 = "../"
|
workspace = ".."
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_codec"
|
name = "actix_codec"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4.12"
|
||||||
futures = "0.1.24"
|
futures = "0.1.24"
|
||||||
tokio-io = "0.1"
|
tokio-io = "0.1.12"
|
||||||
tokio-codec = "0.1"
|
tokio-codec = "0.1.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
@@ -167,6 +167,22 @@ impl<T, U> Framed<T, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Consume the `Frame`, returning `Frame` with different io.
|
||||||
|
pub fn map_io<F, T2>(self, f: F) -> Framed<T2, U>
|
||||||
|
where
|
||||||
|
F: Fn(T) -> T2,
|
||||||
|
{
|
||||||
|
let (inner, read_buf) = self.inner.into_parts();
|
||||||
|
let (inner, write_buf, lw, hw) = inner.into_parts();
|
||||||
|
|
||||||
|
Framed {
|
||||||
|
inner: framed_read2_with_buffer(
|
||||||
|
framed_write2_with_buffer(Fuse(f(inner.0), inner.1), write_buf, lw, hw),
|
||||||
|
read_buf,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Consume the `Frame`, returning `Frame` with different codec.
|
/// Consume the `Frame`, returning `Frame` with different codec.
|
||||||
pub fn map_codec<F, U2>(self, f: F) -> Framed<T, U2>
|
pub fn map_codec<F, U2>(self, f: F) -> Framed<T, U2>
|
||||||
where
|
where
|
||||||
|
38
actix-connect/CHANGES.md
Normal file
38
actix-connect/CHANGES.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.4] - 2019-04-12
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Do not start default resolver immediately for default connector.
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.3] - 2019-04-11
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Start trust-dns default resolver on first use
|
||||||
|
|
||||||
|
## [0.1.2] - 2019-04-04
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Log error if dns system config could not be loaded.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Rename connect Connector to TcpConnector #10
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.1] - 2019-03-15
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fix error handling for single address
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.0] - 2019-03-14
|
||||||
|
|
||||||
|
* Refactor resolver and connector services
|
||||||
|
|
||||||
|
* Rename crate
|
55
actix-connect/Cargo.toml
Normal file
55
actix-connect/Cargo.toml
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-connect"
|
||||||
|
version = "0.1.4"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
description = "Actix Connector - tcp connector service"
|
||||||
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
homepage = "https://actix.rs"
|
||||||
|
repository = "https://github.com/actix/actix-net.git"
|
||||||
|
documentation = "https://docs.rs/actix-connect/"
|
||||||
|
categories = ["network-programming", "asynchronous"]
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
features = ["ssl", "uri"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "actix_connect"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["uri"]
|
||||||
|
|
||||||
|
# openssl
|
||||||
|
ssl = ["openssl", "tokio-openssl"]
|
||||||
|
|
||||||
|
# support http::Uri as connect address
|
||||||
|
uri = ["http"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-service = "0.3.6"
|
||||||
|
actix-codec = "0.1.2"
|
||||||
|
actix-utils = "0.3.5"
|
||||||
|
derive_more = "0.14.0"
|
||||||
|
either = "1.5.1"
|
||||||
|
futures = "0.1.25"
|
||||||
|
http = { version = "0.1.16", optional = true }
|
||||||
|
log = "0.4"
|
||||||
|
tokio-tcp = "0.1.3"
|
||||||
|
tokio-current-thread = "0.1.5"
|
||||||
|
trust-dns-resolver = { version="0.11.0-alpha.3", default-features = false }
|
||||||
|
|
||||||
|
# openssl
|
||||||
|
openssl = { version="0.10", optional = true }
|
||||||
|
tokio-openssl = { version="0.3", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
bytes = "0.4"
|
||||||
|
#actix-test-server = { version="0.2.0", features=["ssl"] }
|
||||||
|
actix-test-server = { path="../actix-test-server", features=["ssl"] }
|
||||||
|
actix-server-config = "0.1.0"
|
||||||
|
actix-utils = "0.3.4"
|
||||||
|
tokio-tcp = "0.1"
|
172
actix-connect/src/connect.rs
Normal file
172
actix-connect/src/connect.rs
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::fmt;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
|
/// Connect request
|
||||||
|
pub trait Address {
|
||||||
|
/// Host name of the request
|
||||||
|
fn host(&self) -> &str;
|
||||||
|
|
||||||
|
/// Port of the request
|
||||||
|
fn port(&self) -> Option<u16>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Address for String {
|
||||||
|
fn host(&self) -> &str {
|
||||||
|
&self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn port(&self) -> Option<u16> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Address for &'static str {
|
||||||
|
fn host(&self) -> &str {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn port(&self) -> Option<u16> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect request
|
||||||
|
#[derive(Eq, PartialEq, Debug, Hash)]
|
||||||
|
pub struct Connect<T> {
|
||||||
|
pub(crate) req: T,
|
||||||
|
pub(crate) port: u16,
|
||||||
|
pub(crate) addr: Option<Either<SocketAddr, VecDeque<SocketAddr>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Connect<T> {
|
||||||
|
/// Create `Connect` instance by spliting the string by ':' and convert the second part to u16
|
||||||
|
pub fn new(req: T) -> Connect<T> {
|
||||||
|
let (_, port) = parse(req.host());
|
||||||
|
Connect {
|
||||||
|
req,
|
||||||
|
port: port.unwrap_or(0),
|
||||||
|
addr: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create new `Connect` instance from host and address. Connector skips name resolution stage for such connect messages.
|
||||||
|
pub fn with(req: T, addr: SocketAddr) -> Connect<T> {
|
||||||
|
Connect {
|
||||||
|
req,
|
||||||
|
port: 0,
|
||||||
|
addr: Some(Either::Left(addr)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Use port if address does not provide one.
|
||||||
|
///
|
||||||
|
/// By default it set to 0
|
||||||
|
pub fn set_port(mut self, port: u16) -> Self {
|
||||||
|
self.port = port;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Host name
|
||||||
|
pub fn host(&self) -> &str {
|
||||||
|
self.req.host()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Port of the request
|
||||||
|
pub fn port(&self) -> u16 {
|
||||||
|
self.req.port().unwrap_or(self.port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> From<T> for Connect<T> {
|
||||||
|
fn from(addr: T) -> Self {
|
||||||
|
Connect::new(addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> fmt::Display for Connect<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}:{}", self.host(), self.port())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(host: &str) -> (&str, Option<u16>) {
|
||||||
|
let mut parts_iter = host.splitn(2, ':');
|
||||||
|
if let Some(host) = parts_iter.next() {
|
||||||
|
let port_str = parts_iter.next().unwrap_or("");
|
||||||
|
if let Ok(port) = port_str.parse::<u16>() {
|
||||||
|
(host, Some(port))
|
||||||
|
} else {
|
||||||
|
(host, None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(host, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Connection<T, U> {
|
||||||
|
io: U,
|
||||||
|
req: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> Connection<T, U> {
|
||||||
|
pub fn new(io: U, req: T) -> Self {
|
||||||
|
Self { io, req }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> Connection<T, U> {
|
||||||
|
/// Reconstruct from a parts.
|
||||||
|
pub fn from_parts(io: U, req: T) -> Self {
|
||||||
|
Self { io, req }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deconstruct into a parts.
|
||||||
|
pub fn into_parts(self) -> (U, T) {
|
||||||
|
(self.io, self.req)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace inclosed object, return new Stream and old object
|
||||||
|
pub fn replace<Y>(self, io: Y) -> (U, Connection<T, Y>) {
|
||||||
|
(self.io, Connection { io, req: self.req })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a shared reference to the underlying stream.
|
||||||
|
pub fn get_ref(&self) -> &U {
|
||||||
|
&self.io
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the underlying stream.
|
||||||
|
pub fn get_mut(&mut self) -> &mut U {
|
||||||
|
&mut self.io
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address, U> Connection<T, U> {
|
||||||
|
/// Get request
|
||||||
|
pub fn host(&self) -> &str {
|
||||||
|
&self.req.host()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> std::ops::Deref for Connection<T, U> {
|
||||||
|
type Target = U;
|
||||||
|
|
||||||
|
fn deref(&self) -> &U {
|
||||||
|
&self.io
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> std::ops::DerefMut for Connection<T, U> {
|
||||||
|
fn deref_mut(&mut self) -> &mut U {
|
||||||
|
&mut self.io
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U: fmt::Debug> fmt::Debug for Connection<T, U> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Stream {{{:?}}}", self.io)
|
||||||
|
}
|
||||||
|
}
|
156
actix-connect/src/connector.rs
Normal file
156
actix-connect/src/connector.rs
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use actix_service::{NewService, Service};
|
||||||
|
use futures::future::{err, ok, Either, FutureResult};
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
use tokio_tcp::{ConnectFuture, TcpStream};
|
||||||
|
|
||||||
|
use super::connect::{Address, Connect, Connection};
|
||||||
|
use super::error::ConnectError;
|
||||||
|
|
||||||
|
/// Tcp connector service factory
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TcpConnectorFactory<T>(PhantomData<T>);
|
||||||
|
|
||||||
|
impl<T> TcpConnectorFactory<T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
TcpConnectorFactory(PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for TcpConnectorFactory<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
TcpConnectorFactory(PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> NewService for TcpConnectorFactory<T> {
|
||||||
|
type Request = Connect<T>;
|
||||||
|
type Response = Connection<T, TcpStream>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Service = TcpConnector<T>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
|
ok(TcpConnector(PhantomData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tcp connector service
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TcpConnector<T>(PhantomData<T>);
|
||||||
|
|
||||||
|
impl<T> TcpConnector<T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
TcpConnector(PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for TcpConnector<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
TcpConnector(PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Service for TcpConnector<T> {
|
||||||
|
type Request = Connect<T>;
|
||||||
|
type Response = Connection<T, TcpStream>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Future = Either<TcpConnectorResponse<T>, FutureResult<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
|
let port = req.port();
|
||||||
|
let Connect { req, addr, .. } = req;
|
||||||
|
|
||||||
|
if let Some(addr) = addr {
|
||||||
|
Either::A(TcpConnectorResponse::new(req, port, addr))
|
||||||
|
} else {
|
||||||
|
error!("TCP connector: got unresolved address");
|
||||||
|
Either::B(err(ConnectError::Unresolverd))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
/// Tcp stream connector response future
|
||||||
|
pub struct TcpConnectorResponse<T> {
|
||||||
|
req: Option<T>,
|
||||||
|
port: u16,
|
||||||
|
addrs: Option<VecDeque<SocketAddr>>,
|
||||||
|
stream: Option<ConnectFuture>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> TcpConnectorResponse<T> {
|
||||||
|
pub fn new(
|
||||||
|
req: T,
|
||||||
|
port: u16,
|
||||||
|
addr: either::Either<SocketAddr, VecDeque<SocketAddr>>,
|
||||||
|
) -> TcpConnectorResponse<T> {
|
||||||
|
trace!(
|
||||||
|
"TCP connector - connecting to {:?} port:{}",
|
||||||
|
req.host(),
|
||||||
|
port
|
||||||
|
);
|
||||||
|
|
||||||
|
match addr {
|
||||||
|
either::Either::Left(addr) => TcpConnectorResponse {
|
||||||
|
req: Some(req),
|
||||||
|
port,
|
||||||
|
addrs: None,
|
||||||
|
stream: Some(TcpStream::connect(&addr)),
|
||||||
|
},
|
||||||
|
either::Either::Right(addrs) => TcpConnectorResponse {
|
||||||
|
req: Some(req),
|
||||||
|
port,
|
||||||
|
addrs: Some(addrs),
|
||||||
|
stream: None,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Future for TcpConnectorResponse<T> {
|
||||||
|
type Item = Connection<T, TcpStream>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
// connect
|
||||||
|
loop {
|
||||||
|
if let Some(new) = self.stream.as_mut() {
|
||||||
|
match new.poll() {
|
||||||
|
Ok(Async::Ready(sock)) => {
|
||||||
|
let req = self.req.take().unwrap();
|
||||||
|
trace!(
|
||||||
|
"TCP connector - successfully connected to connecting to {:?} - {:?}",
|
||||||
|
req.host(), sock.peer_addr()
|
||||||
|
);
|
||||||
|
return Ok(Async::Ready(Connection::new(sock, req)));
|
||||||
|
}
|
||||||
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||||
|
Err(err) => {
|
||||||
|
trace!(
|
||||||
|
"TCP connector - failed to connect to connecting to {:?} port: {}",
|
||||||
|
self.req.as_ref().unwrap().host(),
|
||||||
|
self.port,
|
||||||
|
);
|
||||||
|
if self.addrs.is_none() || self.addrs.as_ref().unwrap().is_empty() {
|
||||||
|
return Err(err.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to connect
|
||||||
|
self.stream = Some(TcpStream::connect(
|
||||||
|
&self.addrs.as_mut().unwrap().pop_front().unwrap(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
actix-connect/src/error.rs
Normal file
26
actix-connect/src/error.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
|
use derive_more::{Display, From};
|
||||||
|
use trust_dns_resolver::error::ResolveError;
|
||||||
|
|
||||||
|
#[derive(Debug, From, Display)]
|
||||||
|
pub enum ConnectError {
|
||||||
|
/// Failed to resolve the hostname
|
||||||
|
#[display(fmt = "Failed resolving hostname: {}", _0)]
|
||||||
|
Resolver(ResolveError),
|
||||||
|
|
||||||
|
/// No dns records
|
||||||
|
#[display(fmt = "No dns records found for the input")]
|
||||||
|
NoRecords,
|
||||||
|
|
||||||
|
/// Invalid input
|
||||||
|
InvalidInput,
|
||||||
|
|
||||||
|
/// Unresolved host name
|
||||||
|
#[display(fmt = "Connector received `Connect` method with unresolved host")]
|
||||||
|
Unresolverd,
|
||||||
|
|
||||||
|
/// Connection io error
|
||||||
|
#[display(fmt = "{}", _0)]
|
||||||
|
Io(io::Error),
|
||||||
|
}
|
116
actix-connect/src/lib.rs
Normal file
116
actix-connect/src/lib.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//! Actix connect - tcp connector service
|
||||||
|
//!
|
||||||
|
//! ## Package feature
|
||||||
|
//!
|
||||||
|
//! * `ssl` - enables ssl support via `openssl` crate
|
||||||
|
//! * `rust-tls` - enables ssl support via `rustls` crate
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
mod connect;
|
||||||
|
mod connector;
|
||||||
|
mod error;
|
||||||
|
mod resolver;
|
||||||
|
pub mod ssl;
|
||||||
|
|
||||||
|
#[cfg(feature = "uri")]
|
||||||
|
mod uri;
|
||||||
|
|
||||||
|
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
|
pub use trust_dns_resolver::system_conf::read_system_conf;
|
||||||
|
pub use trust_dns_resolver::{error::ResolveError, AsyncResolver};
|
||||||
|
|
||||||
|
pub use self::connect::{Address, Connect, Connection};
|
||||||
|
pub use self::connector::{TcpConnector, TcpConnectorFactory};
|
||||||
|
pub use self::error::ConnectError;
|
||||||
|
pub use self::resolver::{Resolver, ResolverFactory};
|
||||||
|
|
||||||
|
use actix_service::{NewService, Service, ServiceExt};
|
||||||
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.1.2", note = "please use `actix_connect::TcpConnector`")]
|
||||||
|
pub type Connector<T> = TcpConnector<T>;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(
|
||||||
|
since = "0.1.2",
|
||||||
|
note = "please use `actix_connect::TcpConnectorFactory`"
|
||||||
|
)]
|
||||||
|
pub type ConnectorFactory<T> = TcpConnectorFactory<T>;
|
||||||
|
|
||||||
|
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
||||||
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
||||||
|
tokio_current_thread::spawn(bg);
|
||||||
|
resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static DEFAULT_RESOLVER: RefCell<Option<AsyncResolver>> = RefCell::new(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
||||||
|
DEFAULT_RESOLVER.with(|cell| {
|
||||||
|
if let Some(ref resolver) = *cell.borrow() {
|
||||||
|
return resolver.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let (cfg, opts) = match read_system_conf() {
|
||||||
|
Ok((cfg, opts)) => (cfg, opts),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("TRust-DNS can not load system config: {}", e);
|
||||||
|
(ResolverConfig::default(), ResolverOpts::default())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
||||||
|
tokio_current_thread::spawn(bg);
|
||||||
|
|
||||||
|
*cell.borrow_mut() = Some(resolver.clone());
|
||||||
|
resolver
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_default_resolver() -> AsyncResolver {
|
||||||
|
get_default_resolver()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create tcp connector service
|
||||||
|
pub fn new_connector<T: Address>(
|
||||||
|
resolver: AsyncResolver,
|
||||||
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
||||||
|
+ Clone {
|
||||||
|
Resolver::new(resolver).and_then(TcpConnector::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create tcp connector service
|
||||||
|
pub fn new_connector_factory<T: Address>(
|
||||||
|
resolver: AsyncResolver,
|
||||||
|
) -> impl NewService<
|
||||||
|
Request = Connect<T>,
|
||||||
|
Response = Connection<T, TcpStream>,
|
||||||
|
Error = ConnectError,
|
||||||
|
InitError = (),
|
||||||
|
> + Clone {
|
||||||
|
ResolverFactory::new(resolver).and_then(TcpConnectorFactory::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create connector service with default parameters
|
||||||
|
pub fn default_connector<T: Address>(
|
||||||
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
||||||
|
+ Clone {
|
||||||
|
Resolver::default().and_then(TcpConnector::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create connector service factory with default parameters
|
||||||
|
pub fn default_connector_factory<T: Address>() -> impl NewService<
|
||||||
|
Request = Connect<T>,
|
||||||
|
Response = Connection<T, TcpStream>,
|
||||||
|
Error = ConnectError,
|
||||||
|
InitError = (),
|
||||||
|
> + Clone {
|
||||||
|
ResolverFactory::default().and_then(TcpConnectorFactory::new())
|
||||||
|
}
|
186
actix-connect/src/resolver.rs
Normal file
186
actix-connect/src/resolver.rs
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use actix_service::{NewService, Service};
|
||||||
|
use futures::future::{ok, Either, FutureResult};
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
||||||
|
use trust_dns_resolver::{AsyncResolver, Background};
|
||||||
|
|
||||||
|
use crate::connect::{Address, Connect};
|
||||||
|
use crate::error::ConnectError;
|
||||||
|
use crate::get_default_resolver;
|
||||||
|
|
||||||
|
/// DNS Resolver Service factory
|
||||||
|
pub struct ResolverFactory<T> {
|
||||||
|
resolver: Option<AsyncResolver>,
|
||||||
|
_t: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ResolverFactory<T> {
|
||||||
|
/// Create new resolver instance with custom configuration and options.
|
||||||
|
pub fn new(resolver: AsyncResolver) -> Self {
|
||||||
|
ResolverFactory {
|
||||||
|
resolver: Some(resolver),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for ResolverFactory<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
ResolverFactory {
|
||||||
|
resolver: None,
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for ResolverFactory<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
ResolverFactory {
|
||||||
|
resolver: self.resolver.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> NewService for ResolverFactory<T> {
|
||||||
|
type Request = Connect<T>;
|
||||||
|
type Response = Connect<T>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Service = Resolver<T>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
|
ok(Resolver {
|
||||||
|
resolver: self.resolver.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// DNS Resolver Service
|
||||||
|
pub struct Resolver<T> {
|
||||||
|
resolver: Option<AsyncResolver>,
|
||||||
|
_t: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Resolver<T> {
|
||||||
|
/// Create new resolver instance with custom configuration and options.
|
||||||
|
pub fn new(resolver: AsyncResolver) -> Self {
|
||||||
|
Resolver {
|
||||||
|
resolver: Some(resolver),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for Resolver<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Resolver {
|
||||||
|
resolver: None,
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for Resolver<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Resolver {
|
||||||
|
resolver: self.resolver.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Service for Resolver<T> {
|
||||||
|
type Request = Connect<T>;
|
||||||
|
type Response = Connect<T>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Future = Either<ResolverFuture<T>, FutureResult<Connect<T>, Self::Error>>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
||||||
|
if req.addr.is_some() {
|
||||||
|
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 {
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
/// Resolver future
|
||||||
|
pub struct ResolverFuture<T: Address> {
|
||||||
|
req: Option<Connect<T>>,
|
||||||
|
lookup: Background<LookupIpFuture>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> ResolverFuture<T> {
|
||||||
|
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
||||||
|
let lookup = if let Some(host) = req.host().splitn(2, ':').next() {
|
||||||
|
resolver.lookup_ip(host)
|
||||||
|
} else {
|
||||||
|
resolver.lookup_ip(req.host())
|
||||||
|
};
|
||||||
|
|
||||||
|
ResolverFuture {
|
||||||
|
lookup,
|
||||||
|
req: Some(req),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address> Future for ResolverFuture<T> {
|
||||||
|
type Item = Connect<T>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
match self.lookup.poll().map_err(|e| {
|
||||||
|
trace!(
|
||||||
|
"DNS resolver: failed to resolve host {:?} err: {}",
|
||||||
|
self.req.as_ref().unwrap().host(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
e
|
||||||
|
})? {
|
||||||
|
Async::NotReady => Ok(Async::NotReady),
|
||||||
|
Async::Ready(ips) => {
|
||||||
|
let mut req = self.req.take().unwrap();
|
||||||
|
let mut addrs: VecDeque<_> = ips
|
||||||
|
.iter()
|
||||||
|
.map(|ip| SocketAddr::new(ip, req.port()))
|
||||||
|
.collect();
|
||||||
|
trace!(
|
||||||
|
"DNS resolver: host {:?} resolved to {:?}",
|
||||||
|
req.host(),
|
||||||
|
addrs
|
||||||
|
);
|
||||||
|
if addrs.is_empty() {
|
||||||
|
Err(ConnectError::NoRecords)
|
||||||
|
} else if addrs.len() == 1 {
|
||||||
|
req.addr = Some(either::Either::Left(addrs.pop_front().unwrap()));
|
||||||
|
Ok(Async::Ready(req))
|
||||||
|
} else {
|
||||||
|
req.addr = Some(either::Either::Right(addrs));
|
||||||
|
Ok(Async::Ready(req))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
127
actix-connect/src/ssl/openssl.rs
Normal file
127
actix-connect/src/ssl/openssl.rs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
use std::fmt;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
|
use actix_service::{NewService, Service};
|
||||||
|
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
||||||
|
use openssl::ssl::{HandshakeError, SslConnector};
|
||||||
|
use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
|
||||||
|
|
||||||
|
use crate::{Address, Connection};
|
||||||
|
|
||||||
|
/// Openssl connector factory
|
||||||
|
pub struct OpensslConnector<T, U> {
|
||||||
|
connector: SslConnector,
|
||||||
|
_t: PhantomData<(T, U)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> OpensslConnector<T, U> {
|
||||||
|
pub fn new(connector: SslConnector) -> Self {
|
||||||
|
OpensslConnector {
|
||||||
|
connector,
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> OpensslConnector<T, U>
|
||||||
|
where
|
||||||
|
T: Address,
|
||||||
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
||||||
|
{
|
||||||
|
pub fn service(
|
||||||
|
connector: SslConnector,
|
||||||
|
) -> impl Service<
|
||||||
|
Request = Connection<T, U>,
|
||||||
|
Response = Connection<T, SslStream<U>>,
|
||||||
|
Error = HandshakeError<U>,
|
||||||
|
> {
|
||||||
|
OpensslConnectorService {
|
||||||
|
connector: connector,
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> Clone for OpensslConnector<T, U> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
connector: self.connector.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address, U> NewService<()> for OpensslConnector<T, U>
|
||||||
|
where
|
||||||
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
||||||
|
{
|
||||||
|
type Request = Connection<T, U>;
|
||||||
|
type Response = Connection<T, SslStream<U>>;
|
||||||
|
type Error = HandshakeError<U>;
|
||||||
|
type Service = OpensslConnectorService<T, U>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
|
ok(OpensslConnectorService {
|
||||||
|
connector: self.connector.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OpensslConnectorService<T, U> {
|
||||||
|
connector: SslConnector,
|
||||||
|
_t: PhantomData<(T, U)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address, U> Service for OpensslConnectorService<T, U>
|
||||||
|
where
|
||||||
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
||||||
|
{
|
||||||
|
type Request = Connection<T, U>;
|
||||||
|
type Response = Connection<T, SslStream<U>>;
|
||||||
|
type Error = HandshakeError<U>;
|
||||||
|
type Future = ConnectAsyncExt<T, U>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
||||||
|
trace!("SSL Handshake start for: {:?}", stream.host());
|
||||||
|
let (io, stream) = stream.replace(());
|
||||||
|
ConnectAsyncExt {
|
||||||
|
fut: SslConnectorExt::connect_async(&self.connector, stream.host(), io),
|
||||||
|
stream: Some(stream),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConnectAsyncExt<T, U> {
|
||||||
|
fut: ConnectAsync<U>,
|
||||||
|
stream: Option<Connection<T, ()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
|
||||||
|
where
|
||||||
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
||||||
|
{
|
||||||
|
type Item = Connection<T, SslStream<U>>;
|
||||||
|
type Error = HandshakeError<U>;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
match self.fut.poll().map_err(|e| {
|
||||||
|
trace!("SSL Handshake error: {:?}", e);
|
||||||
|
e
|
||||||
|
})? {
|
||||||
|
Async::Ready(stream) => {
|
||||||
|
let s = self.stream.take().unwrap();
|
||||||
|
trace!("SSL Handshake success: {:?}", s.host());
|
||||||
|
Ok(Async::Ready(s.replace(stream).1))
|
||||||
|
}
|
||||||
|
Async::NotReady => Ok(Async::NotReady),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
actix-connect/src/uri.rs
Normal file
37
actix-connect/src/uri.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use http::Uri;
|
||||||
|
|
||||||
|
use crate::Address;
|
||||||
|
|
||||||
|
impl Address for Uri {
|
||||||
|
fn host(&self) -> &str {
|
||||||
|
self.host().unwrap_or("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn port(&self) -> Option<u16> {
|
||||||
|
if let Some(port) = self.port_u16() {
|
||||||
|
Some(port)
|
||||||
|
} else {
|
||||||
|
port(self.scheme_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: load data from file
|
||||||
|
fn port(scheme: Option<&str>) -> Option<u16> {
|
||||||
|
if let Some(scheme) = scheme {
|
||||||
|
match scheme {
|
||||||
|
"http" => Some(80),
|
||||||
|
"https" => Some(443),
|
||||||
|
"ws" => Some(80),
|
||||||
|
"wss" => Some(443),
|
||||||
|
"amqp" => Some(5672),
|
||||||
|
"amqps" => Some(5671),
|
||||||
|
"sb" => Some(5671),
|
||||||
|
"mqtt" => Some(1883),
|
||||||
|
"mqtts" => Some(8883),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
109
actix-connect/tests/test_connect.rs
Normal file
109
actix-connect/tests/test_connect.rs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
use actix_codec::{BytesCodec, Framed};
|
||||||
|
use actix_server_config::Io;
|
||||||
|
use actix_service::{fn_service, NewService, Service};
|
||||||
|
use actix_test_server::TestServer;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::{future::lazy, Future, Sink};
|
||||||
|
use http::{HttpTryFrom, Uri};
|
||||||
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
|
|
||||||
|
use actix_connect::{default_connector, Connect};
|
||||||
|
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
#[test]
|
||||||
|
fn test_string() {
|
||||||
|
let mut srv = TestServer::with(|| {
|
||||||
|
fn_service(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
|
.send(Bytes::from_static(b"test"))
|
||||||
|
.then(|_| Ok::<_, ()>(()))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut conn = default_connector();
|
||||||
|
let addr = format!("localhost:{}", srv.port());
|
||||||
|
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
||||||
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_static_str() {
|
||||||
|
let mut srv = TestServer::with(|| {
|
||||||
|
fn_service(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
|
.send(Bytes::from_static(b"test"))
|
||||||
|
.then(|_| Ok::<_, ()>(()))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let resolver = srv
|
||||||
|
.block_on(lazy(
|
||||||
|
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let mut conn = srv
|
||||||
|
.block_on(lazy(|| {
|
||||||
|
Ok::<_, ()>(actix_connect::new_connector(resolver.clone()))
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let con = srv
|
||||||
|
.block_on(conn.call(Connect::with("10", srv.addr())))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
|
||||||
|
let connect = Connect::new(srv.host().to_owned());
|
||||||
|
let mut conn = srv
|
||||||
|
.block_on(lazy(|| Ok::<_, ()>(actix_connect::new_connector(resolver))))
|
||||||
|
.unwrap();
|
||||||
|
let con = srv.block_on(conn.call(connect));
|
||||||
|
assert!(con.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_service() {
|
||||||
|
let mut srv = TestServer::with(|| {
|
||||||
|
fn_service(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
|
.send(Bytes::from_static(b"test"))
|
||||||
|
.then(|_| Ok::<_, ()>(()))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let resolver = srv
|
||||||
|
.block_on(lazy(|| {
|
||||||
|
Ok::<_, ()>(actix_connect::start_resolver(
|
||||||
|
ResolverConfig::default(),
|
||||||
|
ResolverOpts::default(),
|
||||||
|
))
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
let factory = srv
|
||||||
|
.block_on(lazy(|| {
|
||||||
|
Ok::<_, ()>(actix_connect::new_connector_factory(resolver))
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut conn = srv.block_on(factory.new_service(&())).unwrap();
|
||||||
|
let con = srv
|
||||||
|
.block_on(conn.call(Connect::with("10", srv.addr())))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
#[test]
|
||||||
|
fn test_uri() {
|
||||||
|
let mut srv = TestServer::with(|| {
|
||||||
|
fn_service(|io: Io<tokio_tcp::TcpStream>| {
|
||||||
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
|
.send(Bytes::from_static(b"test"))
|
||||||
|
.then(|_| Ok::<_, ()>(()))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut conn = default_connector();
|
||||||
|
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
||||||
|
let con = srv.run_on(move || conn.call(addr.into())).unwrap();
|
||||||
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
}
|
@@ -1,28 +0,0 @@
|
|||||||
# Changes
|
|
||||||
|
|
||||||
## [0.3.0] - 2019-03-02
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Migrate to actix-service 0.3
|
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2019-02-01
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Migrate to actix-service 0.2
|
|
||||||
|
|
||||||
* Upgrade trust-dns-resolver
|
|
||||||
|
|
||||||
* Use tokio-current-thread instead of direct actix-rt dipendency
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.1] - 2019-01-13
|
|
||||||
|
|
||||||
* Upgrade trust-dns-proto
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0] - 2018-12-09
|
|
||||||
|
|
||||||
* Move server to separate crate
|
|
@@ -1,39 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix-connector"
|
|
||||||
version = "0.3.0"
|
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
|
||||||
description = "Actix Connector - tcp connector service"
|
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
|
||||||
homepage = "https://actix.rs"
|
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
|
||||||
documentation = "https://docs.rs/actix-net/"
|
|
||||||
categories = ["network-programming", "asynchronous"]
|
|
||||||
license = "MIT/Apache-2.0"
|
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
|
||||||
edition = "2018"
|
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
features = ["ssl"]
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "actix_connector"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
|
|
||||||
# openssl
|
|
||||||
ssl = ["openssl", "tokio-openssl"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-service = "0.3.3"
|
|
||||||
actix-codec = "0.1.1"
|
|
||||||
futures = "0.1"
|
|
||||||
tokio-tcp = "0.1"
|
|
||||||
tokio-current-thread = "0.1"
|
|
||||||
trust-dns-resolver = { version="0.11.0-alpha.2", default-features = false }
|
|
||||||
|
|
||||||
# openssl
|
|
||||||
openssl = { version="0.10", optional = true }
|
|
||||||
tokio-openssl = { version="0.3", optional = true }
|
|
@@ -1,386 +0,0 @@
|
|||||||
use std::collections::VecDeque;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::net::{IpAddr, SocketAddr};
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::{fmt, io};
|
|
||||||
|
|
||||||
use actix_service::{fn_factory, NewService, Service};
|
|
||||||
use futures::future::{ok, Either};
|
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
|
||||||
use tokio_tcp::{ConnectFuture, TcpStream};
|
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
||||||
use trust_dns_resolver::system_conf::read_system_conf;
|
|
||||||
|
|
||||||
use super::resolver::{RequestHost, ResolveError, Resolver, ResolverFuture};
|
|
||||||
|
|
||||||
/// Port of the request
|
|
||||||
pub trait RequestPort {
|
|
||||||
fn port(&self) -> u16;
|
|
||||||
}
|
|
||||||
|
|
||||||
// #[derive(Fail, Debug)]
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ConnectorError {
|
|
||||||
/// Failed to resolve the hostname
|
|
||||||
// #[fail(display = "Failed resolving hostname: {}", _0)]
|
|
||||||
Resolver(ResolveError),
|
|
||||||
|
|
||||||
/// No dns records
|
|
||||||
// #[fail(display = "No dns records found for the input")]
|
|
||||||
NoRecords,
|
|
||||||
|
|
||||||
/// Connecting took too long
|
|
||||||
// #[fail(display = "Timeout out while establishing connection")]
|
|
||||||
Timeout,
|
|
||||||
|
|
||||||
/// Invalid input
|
|
||||||
InvalidInput,
|
|
||||||
|
|
||||||
/// Connection io error
|
|
||||||
// #[fail(display = "{}", _0)]
|
|
||||||
IoError(io::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ResolveError> for ConnectorError {
|
|
||||||
fn from(err: ResolveError) -> Self {
|
|
||||||
ConnectorError::Resolver(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> for ConnectorError {
|
|
||||||
fn from(err: io::Error) -> Self {
|
|
||||||
ConnectorError::IoError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connect request
|
|
||||||
#[derive(Eq, PartialEq, Debug, Hash)]
|
|
||||||
pub struct Connect {
|
|
||||||
pub kind: ConnectKind,
|
|
||||||
pub timeout: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug, Hash)]
|
|
||||||
pub enum ConnectKind {
|
|
||||||
Host { host: String, port: u16 },
|
|
||||||
Addr { host: String, addr: SocketAddr },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Connect {
|
|
||||||
/// Create new `Connect` instance.
|
|
||||||
pub fn new<T: AsRef<str>>(host: T, port: u16) -> Connect {
|
|
||||||
Connect {
|
|
||||||
kind: ConnectKind::Host {
|
|
||||||
host: host.as_ref().to_owned(),
|
|
||||||
port,
|
|
||||||
},
|
|
||||||
timeout: Duration::from_secs(1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create `Connect` instance by spliting the string by ':' and convert the second part to u16
|
|
||||||
pub fn with<T: AsRef<str>>(host: T) -> Result<Connect, ConnectorError> {
|
|
||||||
let mut parts_iter = host.as_ref().splitn(2, ':');
|
|
||||||
let host = parts_iter.next().ok_or(ConnectorError::InvalidInput)?;
|
|
||||||
let port_str = parts_iter.next().unwrap_or("");
|
|
||||||
let port = port_str
|
|
||||||
.parse::<u16>()
|
|
||||||
.map_err(|_| ConnectorError::InvalidInput)?;
|
|
||||||
Ok(Connect {
|
|
||||||
kind: ConnectKind::Host {
|
|
||||||
host: host.to_owned(),
|
|
||||||
port,
|
|
||||||
},
|
|
||||||
timeout: Duration::from_secs(1),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create new `Connect` instance from host and address. Connector skips name resolution stage for such connect messages.
|
|
||||||
pub fn with_address<T: Into<String>>(host: T, addr: SocketAddr) -> Connect {
|
|
||||||
Connect {
|
|
||||||
kind: ConnectKind::Addr {
|
|
||||||
addr,
|
|
||||||
host: host.into(),
|
|
||||||
},
|
|
||||||
timeout: Duration::from_secs(1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set connect timeout
|
|
||||||
///
|
|
||||||
/// By default timeout is set to a 1 second.
|
|
||||||
pub fn timeout(mut self, timeout: Duration) -> Connect {
|
|
||||||
self.timeout = timeout;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RequestHost for Connect {
|
|
||||||
fn host(&self) -> &str {
|
|
||||||
match self.kind {
|
|
||||||
ConnectKind::Host { ref host, .. } => host,
|
|
||||||
ConnectKind::Addr { ref host, .. } => host,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RequestPort for Connect {
|
|
||||||
fn port(&self) -> u16 {
|
|
||||||
match self.kind {
|
|
||||||
ConnectKind::Host { port, .. } => port,
|
|
||||||
ConnectKind::Addr { addr, .. } => addr.port(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Connect {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}:{}", self.host(), self.port())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tcp connector
|
|
||||||
pub struct Connector {
|
|
||||||
resolver: Resolver<Connect>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Connector {
|
|
||||||
fn default() -> Self {
|
|
||||||
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
||||||
(cfg, opts)
|
|
||||||
} else {
|
|
||||||
(ResolverConfig::default(), ResolverOpts::default())
|
|
||||||
};
|
|
||||||
|
|
||||||
Connector::new(cfg, opts)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Connector {
|
|
||||||
/// Create new connector with resolver configuration
|
|
||||||
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
||||||
Connector {
|
|
||||||
resolver: Resolver::new(cfg, opts),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create new connector with custom resolver
|
|
||||||
pub fn with_resolver(
|
|
||||||
resolver: Resolver<Connect>,
|
|
||||||
) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError>
|
|
||||||
+ Clone {
|
|
||||||
Connector { resolver }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create new default connector service
|
|
||||||
pub fn new_service_with_config<E>(
|
|
||||||
cfg: ResolverConfig,
|
|
||||||
opts: ResolverOpts,
|
|
||||||
) -> impl NewService<
|
|
||||||
(),
|
|
||||||
Request = Connect,
|
|
||||||
Response = (Connect, TcpStream),
|
|
||||||
Error = ConnectorError,
|
|
||||||
InitError = E,
|
|
||||||
> + Clone {
|
|
||||||
fn_factory(move || ok(Connector::new(cfg.clone(), opts)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for Connector {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Connector {
|
|
||||||
resolver: self.resolver.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Service for Connector {
|
|
||||||
type Request = Connect;
|
|
||||||
type Response = (Connect, TcpStream);
|
|
||||||
type Error = ConnectorError;
|
|
||||||
type Future = Either<ConnectorFuture, ConnectorTcpFuture>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
|
||||||
match req.kind {
|
|
||||||
ConnectKind::Host { .. } => Either::A(ConnectorFuture {
|
|
||||||
fut: self.resolver.call(req),
|
|
||||||
fut2: None,
|
|
||||||
}),
|
|
||||||
ConnectKind::Addr { addr, .. } => {
|
|
||||||
let mut addrs = VecDeque::new();
|
|
||||||
addrs.push_back(addr.ip());
|
|
||||||
Either::B(ConnectorTcpFuture {
|
|
||||||
fut: TcpConnectorResponse::new(req, addrs),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct ConnectorFuture {
|
|
||||||
fut: ResolverFuture<Connect>,
|
|
||||||
fut2: Option<TcpConnectorResponse<Connect>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for ConnectorFuture {
|
|
||||||
type Item = (Connect, TcpStream);
|
|
||||||
type Error = ConnectorError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if let Some(ref mut fut) = self.fut2 {
|
|
||||||
return fut.poll().map_err(ConnectorError::from);
|
|
||||||
}
|
|
||||||
match self.fut.poll().map_err(ConnectorError::from)? {
|
|
||||||
Async::Ready((req, addrs)) => {
|
|
||||||
if addrs.is_empty() {
|
|
||||||
Err(ConnectorError::NoRecords)
|
|
||||||
} else {
|
|
||||||
self.fut2 = Some(TcpConnectorResponse::new(req, addrs));
|
|
||||||
self.poll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct ConnectorTcpFuture {
|
|
||||||
fut: TcpConnectorResponse<Connect>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for ConnectorTcpFuture {
|
|
||||||
type Item = (Connect, TcpStream);
|
|
||||||
type Error = ConnectorError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
self.fut.poll().map_err(ConnectorError::IoError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tcp stream connector service
|
|
||||||
pub struct TcpConnector<T: RequestPort>(PhantomData<T>);
|
|
||||||
|
|
||||||
impl<T: RequestPort> Default for TcpConnector<T> {
|
|
||||||
fn default() -> TcpConnector<T> {
|
|
||||||
TcpConnector(PhantomData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestPort> Service for TcpConnector<T> {
|
|
||||||
type Request = (T, VecDeque<IpAddr>);
|
|
||||||
type Response = (T, TcpStream);
|
|
||||||
type Error = io::Error;
|
|
||||||
type Future = TcpConnectorResponse<T>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, (req, addrs): (T, VecDeque<IpAddr>)) -> Self::Future {
|
|
||||||
TcpConnectorResponse::new(req, addrs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// Tcp stream connector response future
|
|
||||||
pub struct TcpConnectorResponse<T: RequestPort> {
|
|
||||||
port: u16,
|
|
||||||
req: Option<T>,
|
|
||||||
addr: Option<SocketAddr>,
|
|
||||||
addrs: VecDeque<IpAddr>,
|
|
||||||
stream: Option<ConnectFuture>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestPort> TcpConnectorResponse<T> {
|
|
||||||
pub fn new(req: T, addrs: VecDeque<IpAddr>) -> TcpConnectorResponse<T> {
|
|
||||||
TcpConnectorResponse {
|
|
||||||
addrs,
|
|
||||||
port: req.port(),
|
|
||||||
req: Some(req),
|
|
||||||
addr: None,
|
|
||||||
stream: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestPort> Future for TcpConnectorResponse<T> {
|
|
||||||
type Item = (T, TcpStream);
|
|
||||||
type Error = io::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
// connect
|
|
||||||
loop {
|
|
||||||
if let Some(new) = self.stream.as_mut() {
|
|
||||||
match new.poll() {
|
|
||||||
Ok(Async::Ready(sock)) => {
|
|
||||||
return Ok(Async::Ready((self.req.take().unwrap(), sock)));
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
||||||
Err(err) => {
|
|
||||||
if self.addrs.is_empty() {
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to connect
|
|
||||||
let addr = SocketAddr::new(self.addrs.pop_front().unwrap(), self.port);
|
|
||||||
self.stream = Some(TcpStream::connect(&addr));
|
|
||||||
self.addr = Some(addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DefaultConnector(Connector);
|
|
||||||
|
|
||||||
impl Default for DefaultConnector {
|
|
||||||
fn default() -> Self {
|
|
||||||
DefaultConnector(Connector::default())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DefaultConnector {
|
|
||||||
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
||||||
DefaultConnector(Connector::new(cfg, opts))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Service for DefaultConnector {
|
|
||||||
type Request = Connect;
|
|
||||||
type Response = TcpStream;
|
|
||||||
type Error = ConnectorError;
|
|
||||||
type Future = DefaultConnectorFuture;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
self.0.poll_ready()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
|
||||||
DefaultConnectorFuture {
|
|
||||||
fut: self.0.call(req),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct DefaultConnectorFuture {
|
|
||||||
fut: Either<ConnectorFuture, ConnectorTcpFuture>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for DefaultConnectorFuture {
|
|
||||||
type Item = TcpStream;
|
|
||||||
type Error = ConnectorError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
Ok(Async::Ready(try_ready!(self.fut.poll()).1))
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
//! Actix Connector - tcp connector service
|
|
||||||
//!
|
|
||||||
//! ## Package feature
|
|
||||||
//!
|
|
||||||
//! * `tls` - enables ssl support via `native-tls` crate
|
|
||||||
//! * `ssl` - enables ssl support via `openssl` crate
|
|
||||||
//! * `rust-tls` - enables ssl support via `rustls` crate
|
|
||||||
|
|
||||||
mod connector;
|
|
||||||
mod resolver;
|
|
||||||
pub mod ssl;
|
|
||||||
|
|
||||||
pub use self::connector::{
|
|
||||||
Connect, Connector, ConnectorError, DefaultConnector, RequestPort, TcpConnector,
|
|
||||||
};
|
|
||||||
pub use self::resolver::{RequestHost, Resolver};
|
|
@@ -1,129 +0,0 @@
|
|||||||
use std::collections::VecDeque;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::net::IpAddr;
|
|
||||||
|
|
||||||
use actix_service::Service;
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
||||||
pub use trust_dns_resolver::error::ResolveError;
|
|
||||||
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
|
||||||
use trust_dns_resolver::system_conf::read_system_conf;
|
|
||||||
use trust_dns_resolver::{AsyncResolver, Background};
|
|
||||||
|
|
||||||
/// Host name of the request
|
|
||||||
pub trait RequestHost {
|
|
||||||
fn host(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RequestHost for String {
|
|
||||||
fn host(&self) -> &str {
|
|
||||||
self.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Resolver<T = String> {
|
|
||||||
resolver: AsyncResolver,
|
|
||||||
req: PhantomData<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestHost> Default for Resolver<T> {
|
|
||||||
fn default() -> Self {
|
|
||||||
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
||||||
(cfg, opts)
|
|
||||||
} else {
|
|
||||||
(ResolverConfig::default(), ResolverOpts::default())
|
|
||||||
};
|
|
||||||
|
|
||||||
Resolver::new(cfg, opts)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestHost> Resolver<T> {
|
|
||||||
/// Create new resolver instance with custom configuration and options.
|
|
||||||
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
|
||||||
tokio_current_thread::spawn(bg);
|
|
||||||
Resolver {
|
|
||||||
resolver,
|
|
||||||
req: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Change type of resolver request.
|
|
||||||
pub fn into_request<T2: RequestHost>(&self) -> Resolver<T2> {
|
|
||||||
Resolver {
|
|
||||||
resolver: self.resolver.clone(),
|
|
||||||
req: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Clone for Resolver<T> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Resolver {
|
|
||||||
resolver: self.resolver.clone(),
|
|
||||||
req: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestHost> Service for Resolver<T> {
|
|
||||||
type Request = T;
|
|
||||||
type Response = (T, VecDeque<IpAddr>);
|
|
||||||
type Error = ResolveError;
|
|
||||||
type Future = ResolverFuture<T>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: T) -> Self::Future {
|
|
||||||
if let Ok(ip) = req.host().parse() {
|
|
||||||
let mut addrs = VecDeque::new();
|
|
||||||
addrs.push_back(ip);
|
|
||||||
ResolverFuture::new(req, &self.resolver, Some(addrs))
|
|
||||||
} else {
|
|
||||||
ResolverFuture::new(req, &self.resolver, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// Resolver future
|
|
||||||
pub struct ResolverFuture<T> {
|
|
||||||
req: Option<T>,
|
|
||||||
lookup: Option<Background<LookupIpFuture>>,
|
|
||||||
addrs: Option<VecDeque<IpAddr>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestHost> ResolverFuture<T> {
|
|
||||||
pub fn new(addr: T, resolver: &AsyncResolver, addrs: Option<VecDeque<IpAddr>>) -> Self {
|
|
||||||
// we need to do dns resolution
|
|
||||||
let lookup = Some(resolver.lookup_ip(addr.host()));
|
|
||||||
ResolverFuture {
|
|
||||||
lookup,
|
|
||||||
addrs,
|
|
||||||
req: Some(addr),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RequestHost> Future for ResolverFuture<T> {
|
|
||||||
type Item = (T, VecDeque<IpAddr>);
|
|
||||||
type Error = ResolveError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if let Some(addrs) = self.addrs.take() {
|
|
||||||
Ok(Async::Ready((self.req.take().unwrap(), addrs)))
|
|
||||||
} else {
|
|
||||||
match self.lookup.as_mut().unwrap().poll() {
|
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
||||||
Ok(Async::Ready(ips)) => Ok(Async::Ready((
|
|
||||||
self.req.take().unwrap(),
|
|
||||||
ips.iter().collect(),
|
|
||||||
))),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,107 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
|
||||||
use actix_service::{NewService, Service};
|
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
|
||||||
use openssl::ssl::{HandshakeError, SslConnector};
|
|
||||||
use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
|
|
||||||
|
|
||||||
use crate::resolver::RequestHost;
|
|
||||||
|
|
||||||
/// Openssl connector factory
|
|
||||||
pub struct OpensslConnector<R, T, E> {
|
|
||||||
connector: SslConnector,
|
|
||||||
_t: PhantomData<(R, T, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, T, E> OpensslConnector<R, T, E> {
|
|
||||||
pub fn new(connector: SslConnector) -> Self {
|
|
||||||
OpensslConnector {
|
|
||||||
connector,
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
|
|
||||||
pub fn service(
|
|
||||||
connector: SslConnector,
|
|
||||||
) -> impl Service<Request = (R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>>
|
|
||||||
{
|
|
||||||
OpensslConnectorService {
|
|
||||||
connector: connector,
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, T, E> Clone for OpensslConnector<R, T, E> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
connector: self.connector.clone(),
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<()>
|
|
||||||
for OpensslConnector<R, T, E>
|
|
||||||
{
|
|
||||||
type Request = (R, T);
|
|
||||||
type Response = (R, SslStream<T>);
|
|
||||||
type Error = HandshakeError<T>;
|
|
||||||
type Service = OpensslConnectorService<R, T>;
|
|
||||||
type InitError = E;
|
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
|
||||||
ok(OpensslConnectorService {
|
|
||||||
connector: self.connector.clone(),
|
|
||||||
_t: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct OpensslConnectorService<R, T> {
|
|
||||||
connector: SslConnector,
|
|
||||||
_t: PhantomData<(R, T)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> {
|
|
||||||
type Request = (R, T);
|
|
||||||
type Response = (R, SslStream<T>);
|
|
||||||
type Error = HandshakeError<T>;
|
|
||||||
type Future = ConnectAsyncExt<R, T>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, (req, stream): (R, T)) -> Self::Future {
|
|
||||||
ConnectAsyncExt {
|
|
||||||
fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream),
|
|
||||||
req: Some(req),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ConnectAsyncExt<R, T> {
|
|
||||||
req: Option<R>,
|
|
||||||
fut: ConnectAsync<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, T> Future for ConnectAsyncExt<R, T>
|
|
||||||
where
|
|
||||||
R: RequestHost,
|
|
||||||
T: AsyncRead + AsyncWrite,
|
|
||||||
{
|
|
||||||
type Item = (R, SslStream<T>);
|
|
||||||
type Error = HandshakeError<T>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
match self.fut.poll()? {
|
|
||||||
Async::Ready(stream) => Ok(Async::Ready((self.req.take().unwrap(), stream))),
|
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,5 +1,21 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.2] - 2019-03-28
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Moved `blocking` module to `actix-threadpool` crate
|
||||||
|
|
||||||
|
## [0.2.1] - 2019-03-11
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Added `blocking` module
|
||||||
|
|
||||||
|
* Arbiter::exec_fn - execute fn on the arbiter's thread
|
||||||
|
|
||||||
|
* Arbiter::exec - execute fn on the arbiter's thread and wait result
|
||||||
|
|
||||||
## [0.2.0] - 2019-03-06
|
## [0.2.0] - 2019-03-06
|
||||||
|
|
||||||
* `run` method returns `io::Result<()>`
|
* `run` method returns `io::Result<()>`
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-rt"
|
name = "actix-rt"
|
||||||
version = "0.2.0"
|
version = "0.2.2"
|
||||||
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"]
|
||||||
@@ -11,15 +11,14 @@ 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 = "../"
|
workspace = ".."
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_rt"
|
name = "actix_rt"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4"
|
actix-threadpool = "0.1.0"
|
||||||
bytes = "0.4"
|
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
tokio-current-thread = "0.1"
|
tokio-current-thread = "0.1"
|
||||||
tokio-executor = "0.1.5"
|
tokio-executor = "0.1.5"
|
||||||
|
@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
|||||||
use std::{fmt, thread};
|
use std::{fmt, thread};
|
||||||
|
|
||||||
use futures::sync::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
use futures::sync::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
||||||
use futures::sync::oneshot::{channel, Sender};
|
use futures::sync::oneshot::{channel, Canceled, Sender};
|
||||||
use futures::{future, Async, Future, IntoFuture, Poll, Stream};
|
use futures::{future, Async, Future, IntoFuture, Poll, Stream};
|
||||||
use tokio_current_thread::spawn;
|
use tokio_current_thread::spawn;
|
||||||
|
|
||||||
@@ -22,6 +22,7 @@ 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<Future<Item = (), Error = ()> + Send>),
|
||||||
|
ExecuteFn(Box<FnExec>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for ArbiterCommand {
|
impl fmt::Debug for ArbiterCommand {
|
||||||
@@ -29,11 +30,15 @@ impl fmt::Debug for ArbiterCommand {
|
|||||||
match self {
|
match self {
|
||||||
ArbiterCommand::Stop => write!(f, "ArbiterCommand::Stop"),
|
ArbiterCommand::Stop => write!(f, "ArbiterCommand::Stop"),
|
||||||
ArbiterCommand::Execute(_) => write!(f, "ArbiterCommand::Execute"),
|
ArbiterCommand::Execute(_) => write!(f, "ArbiterCommand::Execute"),
|
||||||
|
ArbiterCommand::ExecuteFn(_) => write!(f, "ArbiterCommand::ExecuteFn"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
/// Arbiters provide an asynchronous execution environment for actors, functions
|
||||||
|
/// and futures. When an Arbiter is created, they spawn a new OS thread, and
|
||||||
|
/// host an event loop. Some Arbiter functions execute on the current thread.
|
||||||
pub struct Arbiter(UnboundedSender<ArbiterCommand>);
|
pub struct Arbiter(UnboundedSender<ArbiterCommand>);
|
||||||
|
|
||||||
impl Default for Arbiter {
|
impl Default for Arbiter {
|
||||||
@@ -54,7 +59,8 @@ impl Arbiter {
|
|||||||
arb
|
arb
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns current arbiter's address
|
/// Returns the current thread's arbiter's address. If no Arbiter is present, then this
|
||||||
|
/// function will panic!
|
||||||
pub fn current() -> Arbiter {
|
pub fn current() -> Arbiter {
|
||||||
ADDR.with(|cell| match *cell.borrow() {
|
ADDR.with(|cell| match *cell.borrow() {
|
||||||
Some(ref addr) => addr.clone(),
|
Some(ref addr) => addr.clone(),
|
||||||
@@ -62,7 +68,7 @@ impl Arbiter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stop arbiter
|
/// Stop arbiter from continuing it's event loop.
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
let _ = self.0.unbounded_send(ArbiterCommand::Stop);
|
let _ = self.0.unbounded_send(ArbiterCommand::Stop);
|
||||||
}
|
}
|
||||||
@@ -126,7 +132,9 @@ impl Arbiter {
|
|||||||
RUNNING.with(|cell| cell.set(false));
|
RUNNING.with(|cell| cell.set(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn a future on the current thread.
|
/// Spawn a future on the current thread. This does not create a new Arbiter
|
||||||
|
/// or Arbiter address, it is simply a helper for spawning futures on the current
|
||||||
|
/// thread.
|
||||||
pub fn spawn<F>(future: F)
|
pub fn spawn<F>(future: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
F: Future<Item = (), Error = ()> + 'static,
|
||||||
@@ -140,7 +148,9 @@ impl Arbiter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes a future on the current thread.
|
/// Executes a future on the current thread. This does not create a new Arbiter
|
||||||
|
/// or Arbiter address, it is simply a helper for executing futures on the current
|
||||||
|
/// thread.
|
||||||
pub fn spawn_fn<F, R>(f: F)
|
pub fn spawn_fn<F, R>(f: F)
|
||||||
where
|
where
|
||||||
F: FnOnce() -> R + 'static,
|
F: FnOnce() -> R + 'static,
|
||||||
@@ -149,7 +159,7 @@ impl Arbiter {
|
|||||||
Arbiter::spawn(future::lazy(f))
|
Arbiter::spawn(future::lazy(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a future on the arbiter's thread and spawn.
|
/// Send a future to the Arbiter's thread, and spawn it.
|
||||||
pub fn send<F>(&self, future: F)
|
pub fn send<F>(&self, future: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + Send + 'static,
|
F: Future<Item = (), Error = ()> + Send + 'static,
|
||||||
@@ -158,6 +168,38 @@ impl Arbiter {
|
|||||||
.0
|
.0
|
||||||
.unbounded_send(ArbiterCommand::Execute(Box::new(future)));
|
.unbounded_send(ArbiterCommand::Execute(Box::new(future)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a function to the Arbiter's thread, and execute it. Any result from the function
|
||||||
|
/// is discarded.
|
||||||
|
pub fn exec_fn<F>(&self, f: F)
|
||||||
|
where
|
||||||
|
F: FnOnce() + Send + 'static,
|
||||||
|
{
|
||||||
|
let _ = self
|
||||||
|
.0
|
||||||
|
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
|
||||||
|
let _ = f();
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a function to the Arbiter's thread. This function will be executed asynchronously.
|
||||||
|
/// A future is created, and when resolved will contain the result of the function sent
|
||||||
|
/// to the Arbiters thread.
|
||||||
|
pub fn exec<F, R>(&self, f: F) -> impl Future<Item = R, Error = Canceled>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> R + Send + 'static,
|
||||||
|
R: Send + 'static,
|
||||||
|
{
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let _ = self
|
||||||
|
.0
|
||||||
|
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
|
||||||
|
if !tx.is_canceled() {
|
||||||
|
let _ = tx.send(f());
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
rx
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ArbiterController {
|
struct ArbiterController {
|
||||||
@@ -194,6 +236,9 @@ impl Future for ArbiterController {
|
|||||||
ArbiterCommand::Execute(fut) => {
|
ArbiterCommand::Execute(fut) => {
|
||||||
spawn(fut);
|
spawn(fut);
|
||||||
}
|
}
|
||||||
|
ArbiterCommand::ExecuteFn(f) => {
|
||||||
|
f.call_box();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||||
}
|
}
|
||||||
@@ -257,11 +302,16 @@ impl Future for SystemArbiter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /// Execute function in arbiter's thread
|
pub trait FnExec: Send + 'static {
|
||||||
// impl<I: Send, E: Send> Handler<Execute<I, E>> for SystemArbiter {
|
fn call_box(self: Box<Self>);
|
||||||
// type Result = Result<I, E>;
|
}
|
||||||
|
|
||||||
// fn handle(&mut self, msg: Execute<I, E>, _: &mut Context<Self>) -> Result<I, E> {
|
impl<F> FnExec for F
|
||||||
// msg.exec()
|
where
|
||||||
// }
|
F: FnOnce() + Send + 'static,
|
||||||
// }
|
{
|
||||||
|
#[cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
|
||||||
|
fn call_box(self: Box<Self>) {
|
||||||
|
(*self)()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -86,8 +86,7 @@ impl Builder {
|
|||||||
let (stop_tx, stop) = channel();
|
let (stop_tx, stop) = channel();
|
||||||
let (sys_sender, sys_receiver) = unbounded();
|
let (sys_sender, sys_receiver) = unbounded();
|
||||||
|
|
||||||
let arbiter = Arbiter::new_system();
|
let system = System::construct(sys_sender, Arbiter::new_system(), self.stop_on_panic);
|
||||||
let system = System::construct(sys_sender, arbiter.clone(), self.stop_on_panic);
|
|
||||||
|
|
||||||
// system arbiter
|
// system arbiter
|
||||||
let arb = SystemArbiter::new(stop_tx, sys_receiver);
|
let arb = SystemArbiter::new(stop_tx, sys_receiver);
|
||||||
|
@@ -10,6 +10,9 @@ pub use self::builder::{Builder, SystemRunner};
|
|||||||
pub use self::runtime::Runtime;
|
pub use self::runtime::Runtime;
|
||||||
pub use self::system::System;
|
pub use self::system::System;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use actix_threadpool as blocking;
|
||||||
|
|
||||||
/// Spawns a future on the current arbiter.
|
/// Spawns a future on the current arbiter.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
|
@@ -41,7 +41,7 @@ impl Error for RunError {
|
|||||||
self.inner.description()
|
self.inner.description()
|
||||||
}
|
}
|
||||||
fn cause(&self) -> Option<&Error> {
|
fn cause(&self) -> Option<&Error> {
|
||||||
self.inner.cause()
|
self.inner.source()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,14 +1,18 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use futures::sync::mpsc::UnboundedSender;
|
use futures::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
use crate::arbiter::{Arbiter, SystemCommand};
|
use crate::arbiter::{Arbiter, SystemCommand};
|
||||||
use crate::builder::{Builder, SystemRunner};
|
use crate::builder::{Builder, SystemRunner};
|
||||||
|
|
||||||
|
static SYSTEM_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
/// System is a runtime manager.
|
/// System is a runtime manager.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct System {
|
pub struct System {
|
||||||
|
id: usize,
|
||||||
sys: UnboundedSender<SystemCommand>,
|
sys: UnboundedSender<SystemCommand>,
|
||||||
arbiter: Arbiter,
|
arbiter: Arbiter,
|
||||||
stop_on_panic: bool,
|
stop_on_panic: bool,
|
||||||
@@ -29,6 +33,7 @@ impl System {
|
|||||||
sys,
|
sys,
|
||||||
arbiter,
|
arbiter,
|
||||||
stop_on_panic,
|
stop_on_panic,
|
||||||
|
id: SYSTEM_COUNT.fetch_add(1, Ordering::SeqCst),
|
||||||
};
|
};
|
||||||
System::set_current(sys.clone());
|
System::set_current(sys.clone());
|
||||||
sys
|
sys
|
||||||
@@ -82,6 +87,11 @@ impl System {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// System id
|
||||||
|
pub fn id(&self) -> usize {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
/// Stop the system
|
/// Stop the system
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
self.stop_with_code(0)
|
self.stop_with_code(0)
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
use std::fmt;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -31,3 +32,101 @@ impl ServerConfig {
|
|||||||
self.secure.as_ref().set(true)
|
self.secure.as_ref().set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum Protocol {
|
||||||
|
Unknown,
|
||||||
|
Http10,
|
||||||
|
Http11,
|
||||||
|
Http2,
|
||||||
|
Proto1,
|
||||||
|
Proto2,
|
||||||
|
Proto3,
|
||||||
|
Proto4,
|
||||||
|
Proto5,
|
||||||
|
Proto6,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Io<T, P = ()> {
|
||||||
|
io: T,
|
||||||
|
proto: Protocol,
|
||||||
|
params: P,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Io<T, ()> {
|
||||||
|
pub fn new(io: T) -> Self {
|
||||||
|
Self {
|
||||||
|
io,
|
||||||
|
proto: Protocol::Unknown,
|
||||||
|
params: (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, P> Io<T, P> {
|
||||||
|
/// Reconstruct from a parts.
|
||||||
|
pub fn from_parts(io: T, params: P, proto: Protocol) -> Self {
|
||||||
|
Self { io, params, proto }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deconstruct into a parts.
|
||||||
|
pub fn into_parts(self) -> (T, P, Protocol) {
|
||||||
|
(self.io, self.params, self.proto)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a shared reference to the underlying stream.
|
||||||
|
pub fn get_ref(&self) -> &T {
|
||||||
|
&self.io
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the underlying stream.
|
||||||
|
pub fn get_mut(&mut self) -> &mut T {
|
||||||
|
&mut self.io
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get selected protocol
|
||||||
|
pub fn protocol(&self) -> Protocol {
|
||||||
|
self.proto
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return new Io object with new parameter.
|
||||||
|
pub fn set<U>(self, params: U) -> Io<T, U> {
|
||||||
|
Io {
|
||||||
|
io: self.io,
|
||||||
|
proto: self.proto,
|
||||||
|
params: params,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maps an Io<_, P> to Io<_, U> by applying a function to a contained value.
|
||||||
|
pub fn map<U, F>(self, op: F) -> Io<T, U>
|
||||||
|
where
|
||||||
|
F: FnOnce(P) -> U,
|
||||||
|
{
|
||||||
|
Io {
|
||||||
|
io: self.io,
|
||||||
|
proto: self.proto,
|
||||||
|
params: op(self.params),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, P> std::ops::Deref for Io<T, P> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
&self.io
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, P> std::ops::DerefMut for Io<T, P> {
|
||||||
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
|
&mut self.io
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Debug, P> fmt::Debug for Io<T, P> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Io {{{:?}}}", self.io)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -1,6 +1,26 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.4.0] - 2019-03-xx
|
## [0.4.2] - 2019-03-30
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fix SIGINT force shutdown
|
||||||
|
|
||||||
|
|
||||||
|
## [0.4.1] - 2019-03-14
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* `SystemRuntime::on_start()` - allow to run future before server service initialization
|
||||||
|
|
||||||
|
|
||||||
|
## [0.4.0] - 2019-03-12
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Use `ServerConfig` for service factory
|
||||||
|
|
||||||
|
* Wrap tcp socket to `Io` type
|
||||||
|
|
||||||
* Upgrade actix-service
|
* Upgrade actix-service
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-server"
|
name = "actix-server"
|
||||||
version = "0.3.1"
|
version = "0.4.2"
|
||||||
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"]
|
||||||
@@ -33,9 +33,9 @@ ssl = ["openssl", "tokio-openssl"]
|
|||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.0"
|
actix-rt = "0.2.1"
|
||||||
actix-service = "0.3.3"
|
actix-service = "0.3.4"
|
||||||
actix-server-config = { path="../actix-server-config" }
|
actix-server-config = "0.1.0"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
num_cpus = "1.0"
|
num_cpus = "1.0"
|
||||||
@@ -64,5 +64,6 @@ webpki = { version = "0.19", optional = true }
|
|||||||
webpki-roots = { version = "0.16", optional = true }
|
webpki-roots = { version = "0.16", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "0.6"
|
bytes = "0.4"
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
|
env_logger = "0.6"
|
||||||
|
@@ -12,8 +12,8 @@ use num_cpus;
|
|||||||
use tokio_timer::sleep;
|
use tokio_timer::sleep;
|
||||||
|
|
||||||
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
||||||
|
use crate::config::{ConfiguredService, ServiceConfig};
|
||||||
use crate::server::{Server, ServerCommand};
|
use crate::server::{Server, ServerCommand};
|
||||||
use crate::service_config::{ConfiguredService, ServiceConfig};
|
|
||||||
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
||||||
use crate::signals::{Signal, Signals};
|
use crate::signals::{Signal, Signals};
|
||||||
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
||||||
@@ -23,6 +23,7 @@ use crate::{ssl, Token};
|
|||||||
pub struct ServerBuilder {
|
pub struct ServerBuilder {
|
||||||
threads: usize,
|
threads: usize,
|
||||||
token: Token,
|
token: Token,
|
||||||
|
backlog: i32,
|
||||||
workers: Vec<(usize, WorkerClient)>,
|
workers: Vec<(usize, WorkerClient)>,
|
||||||
services: Vec<Box<InternalServiceFactory>>,
|
services: Vec<Box<InternalServiceFactory>>,
|
||||||
sockets: Vec<(Token, net::TcpListener)>,
|
sockets: Vec<(Token, net::TcpListener)>,
|
||||||
@@ -53,6 +54,7 @@ impl ServerBuilder {
|
|||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
sockets: Vec::new(),
|
sockets: Vec::new(),
|
||||||
accept: AcceptLoop::new(server.clone()),
|
accept: AcceptLoop::new(server.clone()),
|
||||||
|
backlog: 2048,
|
||||||
exit: false,
|
exit: false,
|
||||||
shutdown_timeout: Duration::from_secs(30),
|
shutdown_timeout: Duration::from_secs(30),
|
||||||
no_signals: false,
|
no_signals: false,
|
||||||
@@ -70,6 +72,21 @@ impl ServerBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the maximum number of pending connections.
|
||||||
|
///
|
||||||
|
/// This refers to the number of clients that can be waiting to be served.
|
||||||
|
/// Exceeding this number results in the client getting an error when
|
||||||
|
/// attempting to connect. It should only affect servers under significant
|
||||||
|
/// load.
|
||||||
|
///
|
||||||
|
/// Generally set in the 64-2048 range. Default value is 2048.
|
||||||
|
///
|
||||||
|
/// This method should be called before `bind()` method call.
|
||||||
|
pub fn backlog(mut self, num: i32) -> Self {
|
||||||
|
self.backlog = num;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the maximum per-worker number of concurrent connections.
|
/// Sets the maximum per-worker number of concurrent connections.
|
||||||
///
|
///
|
||||||
/// All socket listeners will stop accepting connections when this limit is
|
/// All socket listeners will stop accepting connections when this limit is
|
||||||
@@ -125,7 +142,7 @@ impl ServerBuilder {
|
|||||||
where
|
where
|
||||||
F: Fn(&mut ServiceConfig) -> io::Result<()>,
|
F: Fn(&mut ServiceConfig) -> io::Result<()>,
|
||||||
{
|
{
|
||||||
let mut cfg = ServiceConfig::new(self.threads);
|
let mut cfg = ServiceConfig::new(self.threads, self.backlog);
|
||||||
|
|
||||||
f(&mut cfg)?;
|
f(&mut cfg)?;
|
||||||
|
|
||||||
@@ -133,7 +150,7 @@ impl ServerBuilder {
|
|||||||
let mut srv = ConfiguredService::new(apply);
|
let mut srv = ConfiguredService::new(apply);
|
||||||
for (name, lst) in cfg.services {
|
for (name, lst) in cfg.services {
|
||||||
let token = self.token.next();
|
let token = self.token.next();
|
||||||
srv.stream(token, name);
|
srv.stream(token, name, lst.local_addr()?);
|
||||||
self.sockets.push((token, lst));
|
self.sockets.push((token, lst));
|
||||||
}
|
}
|
||||||
self.services.push(Box::new(srv));
|
self.services.push(Box::new(srv));
|
||||||
@@ -149,7 +166,7 @@ impl ServerBuilder {
|
|||||||
F: ServiceFactory,
|
F: ServiceFactory,
|
||||||
U: net::ToSocketAddrs,
|
U: net::ToSocketAddrs,
|
||||||
{
|
{
|
||||||
let sockets = bind_addr(addr)?;
|
let sockets = bind_addr(addr, self.backlog)?;
|
||||||
|
|
||||||
for lst in sockets {
|
for lst in sockets {
|
||||||
let token = self.token.next();
|
let token = self.token.next();
|
||||||
@@ -311,7 +328,7 @@ impl ServerBuilder {
|
|||||||
self.accept.send(Command::Stop);
|
self.accept.send(Command::Stop);
|
||||||
|
|
||||||
// stop workers
|
// stop workers
|
||||||
if !self.workers.is_empty() {
|
if !self.workers.is_empty() && graceful {
|
||||||
spawn(
|
spawn(
|
||||||
futures_unordered(
|
futures_unordered(
|
||||||
self.workers
|
self.workers
|
||||||
@@ -393,12 +410,15 @@ impl Future for ServerBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn bind_addr<S: net::ToSocketAddrs>(addr: S) -> io::Result<Vec<net::TcpListener>> {
|
pub(super) fn bind_addr<S: net::ToSocketAddrs>(
|
||||||
|
addr: S,
|
||||||
|
backlog: i32,
|
||||||
|
) -> io::Result<Vec<net::TcpListener>> {
|
||||||
let mut err = None;
|
let mut err = None;
|
||||||
let mut succ = false;
|
let mut succ = false;
|
||||||
let mut sockets = Vec::new();
|
let mut sockets = Vec::new();
|
||||||
for addr in addr.to_socket_addrs()? {
|
for addr in addr.to_socket_addrs()? {
|
||||||
match create_tcp_listener(addr) {
|
match create_tcp_listener(addr, backlog) {
|
||||||
Ok(lst) => {
|
Ok(lst) => {
|
||||||
succ = true;
|
succ = true;
|
||||||
sockets.push(lst);
|
sockets.push(lst);
|
||||||
@@ -421,12 +441,12 @@ pub(super) fn bind_addr<S: net::ToSocketAddrs>(addr: S) -> io::Result<Vec<net::T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_tcp_listener(addr: net::SocketAddr) -> io::Result<net::TcpListener> {
|
fn create_tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result<net::TcpListener> {
|
||||||
let builder = match addr {
|
let builder = match addr {
|
||||||
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,
|
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,
|
||||||
net::SocketAddr::V6(_) => TcpBuilder::new_v6()?,
|
net::SocketAddr::V6(_) => TcpBuilder::new_v6()?,
|
||||||
};
|
};
|
||||||
builder.reuse_address(true)?;
|
builder.reuse_address(true)?;
|
||||||
builder.bind(addr)?;
|
builder.bind(addr)?;
|
||||||
Ok(builder.listen(1024)?)
|
Ok(builder.listen(backlog)?)
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{fmt, io, net};
|
use std::{fmt, io, net};
|
||||||
|
|
||||||
|
use actix_server_config::{Io, ServerConfig};
|
||||||
use actix_service::{IntoNewService, NewService};
|
use actix_service::{IntoNewService, NewService};
|
||||||
use futures::future::{join_all, Future};
|
use futures::future::{join_all, Future};
|
||||||
use log::error;
|
use log::error;
|
||||||
@@ -18,12 +19,14 @@ pub struct ServiceConfig {
|
|||||||
pub(crate) services: Vec<(String, net::TcpListener)>,
|
pub(crate) services: Vec<(String, net::TcpListener)>,
|
||||||
pub(crate) apply: Option<Box<ServiceRuntimeConfiguration>>,
|
pub(crate) apply: Option<Box<ServiceRuntimeConfiguration>>,
|
||||||
pub(crate) threads: usize,
|
pub(crate) threads: usize,
|
||||||
|
pub(crate) backlog: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceConfig {
|
impl ServiceConfig {
|
||||||
pub(super) fn new(threads: usize) -> ServiceConfig {
|
pub(super) fn new(threads: usize, backlog: i32) -> ServiceConfig {
|
||||||
ServiceConfig {
|
ServiceConfig {
|
||||||
threads,
|
threads,
|
||||||
|
backlog,
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
apply: None,
|
apply: None,
|
||||||
}
|
}
|
||||||
@@ -42,7 +45,7 @@ impl ServiceConfig {
|
|||||||
where
|
where
|
||||||
U: net::ToSocketAddrs,
|
U: net::ToSocketAddrs,
|
||||||
{
|
{
|
||||||
let sockets = bind_addr(addr)?;
|
let sockets = bind_addr(addr, self.backlog)?;
|
||||||
|
|
||||||
for lst in sockets {
|
for lst in sockets {
|
||||||
self.listen(name.as_ref(), lst);
|
self.listen(name.as_ref(), lst);
|
||||||
@@ -73,7 +76,7 @@ impl ServiceConfig {
|
|||||||
|
|
||||||
pub(super) struct ConfiguredService {
|
pub(super) struct ConfiguredService {
|
||||||
rt: Box<ServiceRuntimeConfiguration>,
|
rt: Box<ServiceRuntimeConfiguration>,
|
||||||
names: HashMap<Token, String>,
|
names: HashMap<Token, (String, net::SocketAddr)>,
|
||||||
services: HashMap<String, Token>,
|
services: HashMap<String, Token>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,15 +89,15 @@ impl ConfiguredService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn stream(&mut self, token: Token, name: String) {
|
pub(super) fn stream(&mut self, token: Token, name: String, addr: net::SocketAddr) {
|
||||||
self.names.insert(token, name.clone());
|
self.names.insert(token, (name.clone(), addr));
|
||||||
self.services.insert(name, token);
|
self.services.insert(name, token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InternalServiceFactory for ConfiguredService {
|
impl InternalServiceFactory for ConfiguredService {
|
||||||
fn name(&self, token: Token) -> &str {
|
fn name(&self, token: Token) -> &str {
|
||||||
&self.names[&token]
|
&self.names[&token].0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clone_factory(&self) -> Box<InternalServiceFactory> {
|
fn clone_factory(&self) -> Box<InternalServiceFactory> {
|
||||||
@@ -111,15 +114,44 @@ impl InternalServiceFactory for ConfiguredService {
|
|||||||
self.rt.configure(&mut rt);
|
self.rt.configure(&mut rt);
|
||||||
rt.validate();
|
rt.validate();
|
||||||
|
|
||||||
// construct services
|
let services = rt.services;
|
||||||
let mut fut = Vec::new();
|
|
||||||
for (token, ns) in rt.services {
|
|
||||||
fut.push(ns.new_service(&()).map(move |service| (token, service)));
|
|
||||||
}
|
|
||||||
|
|
||||||
Box::new(join_all(fut).map_err(|e| {
|
// on start futures
|
||||||
error!("Can not construct service: {:?}", e);
|
if rt.onstart.is_empty() {
|
||||||
}))
|
// construct services
|
||||||
|
let mut fut = Vec::new();
|
||||||
|
for (token, ns) in services {
|
||||||
|
let config = ServerConfig::new(self.names[&token].1);
|
||||||
|
fut.push(ns.new_service(&config).map(move |service| (token, service)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Box::new(join_all(fut).map_err(|e| {
|
||||||
|
error!("Can not construct service: {:?}", e);
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
let names = self.names.clone();
|
||||||
|
|
||||||
|
// run onstart future and then construct services
|
||||||
|
Box::new(
|
||||||
|
join_all(rt.onstart)
|
||||||
|
.map_err(|e| {
|
||||||
|
error!("Can not construct service: {:?}", e);
|
||||||
|
})
|
||||||
|
.and_then(move |_| {
|
||||||
|
// construct services
|
||||||
|
let mut fut = Vec::new();
|
||||||
|
for (token, ns) in services {
|
||||||
|
let config = ServerConfig::new(names[&token].1);
|
||||||
|
fut.push(
|
||||||
|
ns.new_service(&config).map(move |service| (token, service)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
join_all(fut).map_err(|e| {
|
||||||
|
error!("Can not construct service: {:?}", e);
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,6 +181,7 @@ fn not_configured(_: &mut ServiceRuntime) {
|
|||||||
pub struct ServiceRuntime {
|
pub struct ServiceRuntime {
|
||||||
names: HashMap<String, Token>,
|
names: HashMap<String, Token>,
|
||||||
services: HashMap<Token, BoxedNewService>,
|
services: HashMap<Token, BoxedNewService>,
|
||||||
|
onstart: Vec<Box<Future<Item = (), Error = ()>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceRuntime {
|
impl ServiceRuntime {
|
||||||
@@ -156,6 +189,7 @@ impl ServiceRuntime {
|
|||||||
ServiceRuntime {
|
ServiceRuntime {
|
||||||
names,
|
names,
|
||||||
services: HashMap::new(),
|
services: HashMap::new(),
|
||||||
|
onstart: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,10 +201,14 @@ impl ServiceRuntime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register service.
|
||||||
|
///
|
||||||
|
/// Name of the service must be registered during configuration stage with
|
||||||
|
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
|
||||||
pub fn service<T, F>(&mut self, name: &str, service: F)
|
pub fn service<T, F>(&mut self, name: &str, service: F)
|
||||||
where
|
where
|
||||||
F: IntoNewService<T>,
|
F: IntoNewService<T, ServerConfig>,
|
||||||
T: NewService<Request = TcpStream, Response = ()> + 'static,
|
T: NewService<ServerConfig, Request = Io<TcpStream>> + 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::InitError: fmt::Debug,
|
T::InitError: fmt::Debug,
|
||||||
@@ -187,10 +225,19 @@ impl ServiceRuntime {
|
|||||||
panic!("Unknown service: {:?}", name);
|
panic!("Unknown service: {:?}", name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Execute future before services initialization.
|
||||||
|
pub fn on_start<F>(&mut self, fut: F)
|
||||||
|
where
|
||||||
|
F: Future<Item = (), Error = ()> + 'static,
|
||||||
|
{
|
||||||
|
self.onstart.push(Box::new(fut))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoxedNewService = Box<
|
type BoxedNewService = Box<
|
||||||
NewService<
|
NewService<
|
||||||
|
ServerConfig,
|
||||||
Request = (Option<CounterGuard>, ServerMessage),
|
Request = (Option<CounterGuard>, ServerMessage),
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = (),
|
Error = (),
|
||||||
@@ -204,9 +251,9 @@ struct ServiceFactory<T> {
|
|||||||
inner: T,
|
inner: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> NewService for ServiceFactory<T>
|
impl<T> NewService<ServerConfig> for ServiceFactory<T>
|
||||||
where
|
where
|
||||||
T: NewService<Request = TcpStream, Response = ()>,
|
T: NewService<ServerConfig, Request = Io<TcpStream>>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
@@ -219,8 +266,8 @@ where
|
|||||||
type Service = BoxedServerService;
|
type Service = BoxedServerService;
|
||||||
type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
|
type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
||||||
Box::new(self.inner.new_service(&()).map_err(|_| ()).map(|s| {
|
Box::new(self.inner.new_service(cfg).map_err(|_| ()).map(|s| {
|
||||||
let service: BoxedServerService = Box::new(StreamService::new(s));
|
let service: BoxedServerService = Box::new(StreamService::new(s));
|
||||||
service
|
service
|
||||||
}))
|
}))
|
@@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
mod accept;
|
mod accept;
|
||||||
mod builder;
|
mod builder;
|
||||||
|
mod config;
|
||||||
mod counter;
|
mod counter;
|
||||||
mod server;
|
mod server;
|
||||||
mod service_config;
|
|
||||||
mod services;
|
mod services;
|
||||||
mod signals;
|
mod signals;
|
||||||
pub mod ssl;
|
pub mod ssl;
|
||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
pub use actix_server_config::ServerConfig;
|
pub use actix_server_config::{Io, Protocol, ServerConfig};
|
||||||
|
|
||||||
pub use self::builder::ServerBuilder;
|
pub use self::builder::ServerBuilder;
|
||||||
|
pub use self::config::{ServiceConfig, ServiceRuntime};
|
||||||
pub use self::server::Server;
|
pub use self::server::Server;
|
||||||
pub use self::service_config::{ServiceConfig, ServiceRuntime};
|
|
||||||
pub use self::services::ServiceFactory;
|
pub use self::services::ServiceFactory;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@@ -1,14 +1,14 @@
|
|||||||
use std::net::{SocketAddr, TcpStream};
|
use std::net::{self, SocketAddr};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_rt::spawn;
|
use actix_rt::spawn;
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::{Io, ServerConfig};
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{NewService, Service};
|
||||||
use futures::future::{err, ok, FutureResult};
|
use futures::future::{err, ok, FutureResult};
|
||||||
use futures::{Future, Poll};
|
use futures::{Future, Poll};
|
||||||
use log::error;
|
use log::error;
|
||||||
use tokio_reactor::Handle;
|
use tokio_reactor::Handle;
|
||||||
use tokio_tcp::TcpStream as TokioTcpStream;
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
use super::Token;
|
use super::Token;
|
||||||
use crate::counter::CounterGuard;
|
use crate::counter::CounterGuard;
|
||||||
@@ -16,7 +16,7 @@ use crate::counter::CounterGuard;
|
|||||||
/// Server message
|
/// Server message
|
||||||
pub(crate) enum ServerMessage {
|
pub(crate) enum ServerMessage {
|
||||||
/// New stream
|
/// New stream
|
||||||
Connect(TcpStream),
|
Connect(net::TcpStream),
|
||||||
/// Gracefull shutdown
|
/// Gracefull shutdown
|
||||||
Shutdown(Duration),
|
Shutdown(Duration),
|
||||||
/// Force shutdown
|
/// Force shutdown
|
||||||
@@ -24,7 +24,7 @@ pub(crate) enum ServerMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait ServiceFactory: Send + Clone + 'static {
|
pub trait ServiceFactory: Send + Clone + 'static {
|
||||||
type NewService: NewService<ServerConfig, Request = TokioTcpStream>;
|
type NewService: NewService<ServerConfig, Request = Io<TcpStream>>;
|
||||||
|
|
||||||
fn create(&self) -> Self::NewService;
|
fn create(&self) -> Self::NewService;
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ impl<T> StreamService<T> {
|
|||||||
|
|
||||||
impl<T> Service for StreamService<T>
|
impl<T> Service for StreamService<T>
|
||||||
where
|
where
|
||||||
T: Service<Request = TokioTcpStream>,
|
T: Service<Request = Io<TcpStream>>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
{
|
{
|
||||||
@@ -74,13 +74,12 @@ where
|
|||||||
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
||||||
match req {
|
match req {
|
||||||
ServerMessage::Connect(stream) => {
|
ServerMessage::Connect(stream) => {
|
||||||
let stream =
|
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
|
||||||
TokioTcpStream::from_std(stream, &Handle::default()).map_err(|e| {
|
error!("Can not convert to an async tcp stream: {}", e);
|
||||||
error!("Can not convert to an async tcp stream: {}", e);
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if let Ok(stream) = stream {
|
if let Ok(stream) = stream {
|
||||||
spawn(self.service.call(stream).then(move |res| {
|
spawn(self.service.call(Io::new(stream)).then(move |res| {
|
||||||
drop(guard);
|
drop(guard);
|
||||||
res.map_err(|_| ()).map(|_| ())
|
res.map_err(|_| ()).map(|_| ())
|
||||||
}));
|
}));
|
||||||
@@ -170,7 +169,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
|
|||||||
impl<F, T> ServiceFactory for F
|
impl<F, T> ServiceFactory for F
|
||||||
where
|
where
|
||||||
F: Fn() -> T + Send + Clone + 'static,
|
F: Fn() -> T + Send + Clone + 'static,
|
||||||
T: NewService<ServerConfig, Request = TokioTcpStream>,
|
T: NewService<ServerConfig, Request = Io<TcpStream>>,
|
||||||
{
|
{
|
||||||
type NewService = T;
|
type NewService = T;
|
||||||
|
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use actix_server_config::ServerConfig;
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{NewService, Service};
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
||||||
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
|
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
|
||||||
@@ -9,16 +8,17 @@ use tokio_io::{AsyncRead, AsyncWrite};
|
|||||||
|
|
||||||
use crate::counter::{Counter, CounterGuard};
|
use crate::counter::{Counter, CounterGuard};
|
||||||
use crate::ssl::MAX_CONN_COUNTER;
|
use crate::ssl::MAX_CONN_COUNTER;
|
||||||
|
use crate::{Io, Protocol, ServerConfig};
|
||||||
|
|
||||||
/// Support `SSL` connections via native-tls package
|
/// Support `SSL` connections via native-tls package
|
||||||
///
|
///
|
||||||
/// `tls` feature enables `NativeTlsAcceptor` type
|
/// `tls` feature enables `NativeTlsAcceptor` type
|
||||||
pub struct NativeTlsAcceptor<T> {
|
pub struct NativeTlsAcceptor<T, P = ()> {
|
||||||
acceptor: TlsAcceptor,
|
acceptor: TlsAcceptor,
|
||||||
io: PhantomData<T>,
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NativeTlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> NativeTlsAcceptor<T, P> {
|
||||||
/// Create `NativeTlsAcceptor` instance
|
/// Create `NativeTlsAcceptor` instance
|
||||||
pub fn new(acceptor: TlsAcceptor) -> Self {
|
pub fn new(acceptor: TlsAcceptor) -> Self {
|
||||||
NativeTlsAcceptor {
|
NativeTlsAcceptor {
|
||||||
@@ -28,7 +28,7 @@ impl<T: AsyncRead + AsyncWrite> NativeTlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Clone for NativeTlsAcceptor<T, P> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
acceptor: self.acceptor.clone(),
|
acceptor: self.acceptor.clone(),
|
||||||
@@ -37,11 +37,11 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> NewService<ServerConfig> for NativeTlsAcceptor<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = TlsStream<T>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Service = NativeTlsAcceptorService<T>;
|
type Service = NativeTlsAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
@@ -58,17 +58,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NativeTlsAcceptorService<T> {
|
pub struct NativeTlsAcceptorService<T, P> {
|
||||||
acceptor: TlsAcceptor,
|
acceptor: TlsAcceptor,
|
||||||
io: PhantomData<T>,
|
io: PhantomData<(T, P)>,
|
||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Service for NativeTlsAcceptorService<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = TlsStream<T>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Accept<T>;
|
type Future = Accept<T, P>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
if self.conns.available() {
|
if self.conns.available() {
|
||||||
@@ -78,10 +78,12 @@ impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: T) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
|
let (io, params, _) = req.into_parts();
|
||||||
Accept {
|
Accept {
|
||||||
_guard: self.conns.get(),
|
_guard: self.conns.get(),
|
||||||
inner: Some(self.acceptor.accept(req)),
|
inner: Some(self.acceptor.accept(io)),
|
||||||
|
params: Some(params),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,21 +102,30 @@ pub struct TlsStream<S> {
|
|||||||
|
|
||||||
/// Future returned from `NativeTlsAcceptor::accept` which will resolve
|
/// Future returned from `NativeTlsAcceptor::accept` which will resolve
|
||||||
/// once the accept handshake has finished.
|
/// once the accept handshake has finished.
|
||||||
pub struct Accept<S> {
|
pub struct Accept<S, P> {
|
||||||
inner: Option<Result<native_tls::TlsStream<S>, HandshakeError<S>>>,
|
inner: Option<Result<native_tls::TlsStream<S>, HandshakeError<S>>>,
|
||||||
|
params: Option<P>,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io: AsyncRead + AsyncWrite> Future for Accept<Io> {
|
impl<T: AsyncRead + AsyncWrite, P> Future for Accept<T, P> {
|
||||||
type Item = TlsStream<Io>;
|
type Item = Io<TlsStream<T>, P>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
match self.inner.take().expect("cannot poll MidHandshake twice") {
|
match self.inner.take().expect("cannot poll MidHandshake twice") {
|
||||||
Ok(stream) => Ok(TlsStream { inner: stream }.into()),
|
Ok(stream) => Ok(Async::Ready(Io::from_parts(
|
||||||
|
TlsStream { inner: stream },
|
||||||
|
self.params.take().unwrap(),
|
||||||
|
Protocol::Unknown,
|
||||||
|
))),
|
||||||
Err(HandshakeError::Failure(e)) => Err(e),
|
Err(HandshakeError::Failure(e)) => Err(e),
|
||||||
Err(HandshakeError::WouldBlock(s)) => match s.handshake() {
|
Err(HandshakeError::WouldBlock(s)) => match s.handshake() {
|
||||||
Ok(stream) => Ok(TlsStream { inner: stream }.into()),
|
Ok(stream) => Ok(Async::Ready(Io::from_parts(
|
||||||
|
TlsStream { inner: stream },
|
||||||
|
self.params.take().unwrap(),
|
||||||
|
Protocol::Unknown,
|
||||||
|
))),
|
||||||
Err(HandshakeError::Failure(e)) => Err(e),
|
Err(HandshakeError::Failure(e)) => Err(e),
|
||||||
Err(HandshakeError::WouldBlock(s)) => {
|
Err(HandshakeError::WouldBlock(s)) => {
|
||||||
self.inner = Some(Err(HandshakeError::WouldBlock(s)));
|
self.inner = Some(Err(HandshakeError::WouldBlock(s)));
|
||||||
|
@@ -8,17 +8,17 @@ use tokio_openssl::{AcceptAsync, SslAcceptorExt, SslStream};
|
|||||||
|
|
||||||
use crate::counter::{Counter, CounterGuard};
|
use crate::counter::{Counter, CounterGuard};
|
||||||
use crate::ssl::MAX_CONN_COUNTER;
|
use crate::ssl::MAX_CONN_COUNTER;
|
||||||
use crate::ServerConfig;
|
use crate::{Io, Protocol, ServerConfig};
|
||||||
|
|
||||||
/// Support `SSL` connections via openssl package
|
/// Support `SSL` connections via openssl package
|
||||||
///
|
///
|
||||||
/// `ssl` feature enables `OpensslAcceptor` type
|
/// `ssl` feature enables `OpensslAcceptor` type
|
||||||
pub struct OpensslAcceptor<T> {
|
pub struct OpensslAcceptor<T: AsyncRead + AsyncWrite, P = ()> {
|
||||||
acceptor: SslAcceptor,
|
acceptor: SslAcceptor,
|
||||||
io: PhantomData<T>,
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> OpensslAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> OpensslAcceptor<T, P> {
|
||||||
/// Create default `OpensslAcceptor`
|
/// Create default `OpensslAcceptor`
|
||||||
pub fn new(acceptor: SslAcceptor) -> Self {
|
pub fn new(acceptor: SslAcceptor) -> Self {
|
||||||
OpensslAcceptor {
|
OpensslAcceptor {
|
||||||
@@ -28,7 +28,7 @@ impl<T> OpensslAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Clone for OpensslAcceptor<T, P> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
acceptor: self.acceptor.clone(),
|
acceptor: self.acceptor.clone(),
|
||||||
@@ -37,11 +37,11 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> NewService<ServerConfig> for OpensslAcceptor<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = SslStream<T>;
|
type Response = Io<SslStream<T>, P>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Service = OpensslAcceptorService<T>;
|
type Service = OpensslAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
@@ -58,17 +58,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct OpensslAcceptorService<T> {
|
pub struct OpensslAcceptorService<T, P> {
|
||||||
acceptor: SslAcceptor,
|
acceptor: SslAcceptor,
|
||||||
io: PhantomData<T>,
|
|
||||||
conns: Counter,
|
conns: Counter,
|
||||||
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Service for OpensslAcceptorService<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = SslStream<T>;
|
type Response = Io<SslStream<T>, P>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Future = OpensslAcceptorServiceFut<T>;
|
type Future = OpensslAcceptorServiceFut<T, P>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
if self.conns.available() {
|
if self.conns.available() {
|
||||||
@@ -78,27 +78,52 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: T) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
|
let (io, params, _) = req.into_parts();
|
||||||
OpensslAcceptorServiceFut {
|
OpensslAcceptorServiceFut {
|
||||||
_guard: self.conns.get(),
|
_guard: self.conns.get(),
|
||||||
fut: SslAcceptorExt::accept_async(&self.acceptor, req),
|
fut: SslAcceptorExt::accept_async(&self.acceptor, io),
|
||||||
|
params: Some(params),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct OpensslAcceptorServiceFut<T>
|
pub struct OpensslAcceptorServiceFut<T, P>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
{
|
{
|
||||||
fut: AcceptAsync<T>,
|
fut: AcceptAsync<T>,
|
||||||
|
params: Option<P>,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Future for OpensslAcceptorServiceFut<T, P> {
|
||||||
type Item = SslStream<T>;
|
type Item = Io<SslStream<T>, P>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
self.fut.poll()
|
let io = futures::try_ready!(self.fut.poll());
|
||||||
|
let proto = if let Some(protos) = io.get_ref().ssl().selected_alpn_protocol() {
|
||||||
|
const H2: &[u8] = b"\x02h2";
|
||||||
|
const HTTP10: &[u8] = b"\x08http/1.0";
|
||||||
|
const HTTP11: &[u8] = b"\x08http/1.1";
|
||||||
|
|
||||||
|
if protos.windows(3).any(|window| window == H2) {
|
||||||
|
Protocol::Http2
|
||||||
|
} else if protos.windows(9).any(|window| window == HTTP11) {
|
||||||
|
Protocol::Http11
|
||||||
|
} else if protos.windows(9).any(|window| window == HTTP10) {
|
||||||
|
Protocol::Http10
|
||||||
|
} else {
|
||||||
|
Protocol::Unknown
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Protocol::Unknown
|
||||||
|
};
|
||||||
|
Ok(Async::Ready(Io::from_parts(
|
||||||
|
io,
|
||||||
|
self.params.take().unwrap(),
|
||||||
|
proto,
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,17 +10,17 @@ use tokio_rustls::{Accept, TlsAcceptor, TlsStream};
|
|||||||
|
|
||||||
use crate::counter::{Counter, CounterGuard};
|
use crate::counter::{Counter, CounterGuard};
|
||||||
use crate::ssl::MAX_CONN_COUNTER;
|
use crate::ssl::MAX_CONN_COUNTER;
|
||||||
use crate::ServerConfig as SrvConfig;
|
use crate::{Io, Protocol, ServerConfig as SrvConfig};
|
||||||
|
|
||||||
/// Support `SSL` connections via rustls package
|
/// Support `SSL` connections via rustls package
|
||||||
///
|
///
|
||||||
/// `rust-tls` feature enables `RustlsAcceptor` type
|
/// `rust-tls` feature enables `RustlsAcceptor` type
|
||||||
pub struct RustlsAcceptor<T> {
|
pub struct RustlsAcceptor<T, P = ()> {
|
||||||
config: Arc<ServerConfig>,
|
config: Arc<ServerConfig>,
|
||||||
io: PhantomData<T>,
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> RustlsAcceptor<T, P> {
|
||||||
/// Create `RustlsAcceptor` new service
|
/// Create `RustlsAcceptor` new service
|
||||||
pub fn new(config: ServerConfig) -> Self {
|
pub fn new(config: ServerConfig) -> Self {
|
||||||
RustlsAcceptor {
|
RustlsAcceptor {
|
||||||
@@ -30,7 +30,7 @@ impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Clone for RustlsAcceptor<T> {
|
impl<T, P> Clone for RustlsAcceptor<T, P> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
config: self.config.clone(),
|
config: self.config.clone(),
|
||||||
@@ -39,11 +39,11 @@ impl<T> Clone for RustlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite, P> NewService<SrvConfig> for RustlsAcceptor<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = TlsStream<T, ServerSession>;
|
type Response = Io<TlsStream<T, ServerSession>, P>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Service = RustlsAcceptorService<T>;
|
type Service = RustlsAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
@@ -60,17 +60,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RustlsAcceptorService<T> {
|
pub struct RustlsAcceptorService<T, P> {
|
||||||
acceptor: TlsAcceptor,
|
acceptor: TlsAcceptor,
|
||||||
io: PhantomData<T>,
|
io: PhantomData<(T, P)>,
|
||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Service for RustlsAcceptorService<T, P> {
|
||||||
type Request = T;
|
type Request = Io<T, P>;
|
||||||
type Response = TlsStream<T, ServerSession>;
|
type Response = Io<TlsStream<T, ServerSession>, P>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Future = RustlsAcceptorServiceFut<T>;
|
type Future = RustlsAcceptorServiceFut<T, P>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
if self.conns.available() {
|
if self.conns.available() {
|
||||||
@@ -80,27 +80,35 @@ impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: T) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
|
let (io, params, _) = req.into_parts();
|
||||||
RustlsAcceptorServiceFut {
|
RustlsAcceptorServiceFut {
|
||||||
_guard: self.conns.get(),
|
_guard: self.conns.get(),
|
||||||
fut: self.acceptor.accept(req),
|
fut: self.acceptor.accept(io),
|
||||||
|
params: Some(params),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RustlsAcceptorServiceFut<T>
|
pub struct RustlsAcceptorServiceFut<T, P>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
{
|
{
|
||||||
fut: Accept<T>,
|
fut: Accept<T>,
|
||||||
|
params: Option<P>,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Future for RustlsAcceptorServiceFut<T> {
|
impl<T: AsyncRead + AsyncWrite, P> Future for RustlsAcceptorServiceFut<T, P> {
|
||||||
type Item = TlsStream<T, ServerSession>;
|
type Item = Io<TlsStream<T, ServerSession>, P>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
self.fut.poll()
|
let io = futures::try_ready!(self.fut.poll());
|
||||||
|
Ok(Async::Ready(Io::from_parts(
|
||||||
|
io,
|
||||||
|
self.params.take().unwrap(),
|
||||||
|
Protocol::Unknown,
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,14 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
use std::sync::mpsc;
|
||||||
use std::{net, thread, time};
|
use std::{net, thread, time};
|
||||||
|
|
||||||
use actix_server::{Server, ServerConfig};
|
use actix_codec::{BytesCodec, Framed};
|
||||||
|
use actix_server::{Io, Server, ServerConfig};
|
||||||
use actix_service::{fn_cfg_factory, fn_service, IntoService};
|
use actix_service::{fn_cfg_factory, fn_service, IntoService};
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::{Future, Sink};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
fn unused_addr() -> net::SocketAddr {
|
fn unused_addr() -> net::SocketAddr {
|
||||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
@@ -16,9 +22,11 @@ fn unused_addr() -> net::SocketAddr {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_bind() {
|
fn test_bind() {
|
||||||
let addr = unused_addr();
|
let addr = unused_addr();
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
let h = thread::spawn(move || {
|
||||||
Server::build()
|
let sys = actix_rt::System::new("test");
|
||||||
|
let srv = Server::build()
|
||||||
.bind("test", addr, move || {
|
.bind("test", addr, move || {
|
||||||
fn_cfg_factory(move |cfg: &ServerConfig| {
|
fn_cfg_factory(move |cfg: &ServerConfig| {
|
||||||
assert_eq!(cfg.local_addr(), addr);
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
@@ -26,35 +34,47 @@ fn test_bind() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.run()
|
.start();
|
||||||
|
let _ = tx.send((srv, actix_rt::System::current()));
|
||||||
|
let _ = sys.run();
|
||||||
});
|
});
|
||||||
|
let (_, sys) = rx.recv().unwrap();
|
||||||
|
|
||||||
thread::sleep(time::Duration::from_millis(500));
|
thread::sleep(time::Duration::from_millis(500));
|
||||||
assert!(net::TcpStream::connect(addr).is_ok());
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
let _ = sys.stop();
|
||||||
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bind_no_config() {
|
fn test_bind_no_config() {
|
||||||
let addr = unused_addr();
|
let addr = unused_addr();
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
let h = thread::spawn(move || {
|
||||||
Server::build()
|
let sys = actix_rt::System::new("test");
|
||||||
|
let srv = Server::build()
|
||||||
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
|
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.run()
|
.start();
|
||||||
|
let _ = tx.send((srv, actix_rt::System::current()));
|
||||||
|
let _ = sys.run();
|
||||||
});
|
});
|
||||||
|
let (_, sys) = rx.recv().unwrap();
|
||||||
thread::sleep(time::Duration::from_millis(500));
|
|
||||||
assert!(net::TcpStream::connect(addr).is_ok());
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
let _ = sys.stop();
|
||||||
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_listen() {
|
fn test_listen() {
|
||||||
let addr = unused_addr();
|
let addr = unused_addr();
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
let h = thread::spawn(move || {
|
||||||
|
let sys = actix_rt::System::new("test");
|
||||||
let lst = net::TcpListener::bind(addr).unwrap();
|
let lst = net::TcpListener::bind(addr).unwrap();
|
||||||
Server::build()
|
let srv = Server::build()
|
||||||
.listen("test", lst, move || {
|
.listen("test", lst, move || {
|
||||||
fn_cfg_factory(move |cfg: &ServerConfig| {
|
fn_cfg_factory(move |cfg: &ServerConfig| {
|
||||||
assert_eq!(cfg.local_addr(), addr);
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
@@ -62,9 +82,81 @@ fn test_listen() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.run()
|
.start();
|
||||||
|
let _ = tx.send((srv, actix_rt::System::current()));
|
||||||
|
let _ = sys.run();
|
||||||
});
|
});
|
||||||
|
let (_, sys) = rx.recv().unwrap();
|
||||||
|
|
||||||
thread::sleep(time::Duration::from_millis(500));
|
thread::sleep(time::Duration::from_millis(500));
|
||||||
assert!(net::TcpStream::connect(addr).is_ok());
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
let _ = sys.stop();
|
||||||
|
let _ = h.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_start() {
|
||||||
|
let addr = unused_addr();
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let h = thread::spawn(move || {
|
||||||
|
let sys = actix_rt::System::new("test");
|
||||||
|
let srv = Server::build()
|
||||||
|
.backlog(100)
|
||||||
|
.bind("test", addr, move || {
|
||||||
|
fn_cfg_factory(move |cfg: &ServerConfig| {
|
||||||
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
|
Ok::<_, ()>(
|
||||||
|
(|io: Io<TcpStream>| {
|
||||||
|
Framed::new(io.into_parts().0, BytesCodec)
|
||||||
|
.send(Bytes::from_static(b"test"))
|
||||||
|
.then(|_| Ok::<_, ()>(()))
|
||||||
|
})
|
||||||
|
.into_service(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
let _ = tx.send((srv, actix_rt::System::current()));
|
||||||
|
let _ = sys.run();
|
||||||
|
});
|
||||||
|
let (srv, sys) = rx.recv().unwrap();
|
||||||
|
|
||||||
|
let mut buf = [0u8; 4];
|
||||||
|
let mut conn = net::TcpStream::connect(addr).unwrap();
|
||||||
|
let _ = conn.read_exact(&mut buf);
|
||||||
|
assert_eq!(buf, b"test"[..]);
|
||||||
|
|
||||||
|
// pause
|
||||||
|
let _ = srv.pause();
|
||||||
|
thread::sleep(time::Duration::from_millis(200));
|
||||||
|
let mut conn = net::TcpStream::connect(addr).unwrap();
|
||||||
|
conn.set_read_timeout(Some(time::Duration::from_millis(100)))
|
||||||
|
.unwrap();
|
||||||
|
let res = conn.read_exact(&mut buf);
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
|
// resume
|
||||||
|
let _ = srv.resume();
|
||||||
|
thread::sleep(time::Duration::from_millis(100));
|
||||||
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
assert!(net::TcpStream::connect(addr).is_ok());
|
||||||
|
|
||||||
|
let mut buf = [0u8; 4];
|
||||||
|
let mut conn = net::TcpStream::connect(addr).unwrap();
|
||||||
|
let _ = conn.read_exact(&mut buf);
|
||||||
|
assert_eq!(buf, b"test"[..]);
|
||||||
|
|
||||||
|
// stop
|
||||||
|
let _ = srv.stop(false);
|
||||||
|
thread::sleep(time::Duration::from_millis(100));
|
||||||
|
assert!(net::TcpStream::connect(addr).is_err());
|
||||||
|
|
||||||
|
thread::sleep(time::Duration::from_millis(100));
|
||||||
|
let _ = sys.stop();
|
||||||
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,34 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.3.6] - 2019-04-07
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Poll boxed service call result immediately
|
||||||
|
|
||||||
|
|
||||||
|
## [0.3.5] - 2019-03-29
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add `impl<S: Service> Service for Rc<RefCell<S>>`
|
||||||
|
|
||||||
|
|
||||||
|
## [0.3.4] - 2019-03-12
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add `Transform::from_err()` combinator
|
||||||
|
|
||||||
|
* Add `apply_fn` helper
|
||||||
|
|
||||||
|
* Add `apply_fn_factory` helper
|
||||||
|
|
||||||
|
* Add `apply_transform` helper
|
||||||
|
|
||||||
|
* Add `apply_cfg` helper
|
||||||
|
|
||||||
|
|
||||||
## [0.3.3] - 2019-03-09
|
## [0.3.3] - 2019-03-09
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.3.3"
|
version = "0.3.6"
|
||||||
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"]
|
||||||
@@ -27,4 +27,4 @@ futures = "0.1.24"
|
|||||||
void = "1.0.2"
|
void = "1.0.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.1"
|
actix-rt = "0.2"
|
@@ -1,6 +1,6 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::{IntoNewService, NewService, Service};
|
use super::{IntoNewService, NewService, Service};
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
@@ -48,8 +48,12 @@ where
|
|||||||
type Future = AndThenFuture<A, B>;
|
type Future = AndThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
let not_ready = self.a.poll_ready()?.is_not_ready();
|
||||||
self.b.get_mut().poll_ready()
|
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
} else {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
@@ -107,19 +111,23 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `AndThenNewService` new service combinator
|
/// `AndThenNewService` new service combinator
|
||||||
pub struct AndThenNewService<A, B, C> {
|
pub struct AndThenNewService<A, B, C>
|
||||||
|
where
|
||||||
|
A: NewService<C>,
|
||||||
|
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
|
{
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
_t: PhantomData<C>,
|
_t: PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, C> AndThenNewService<A, B, C> {
|
impl<A, B, C> AndThenNewService<A, B, C>
|
||||||
|
where
|
||||||
|
A: NewService<C>,
|
||||||
|
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
|
{
|
||||||
/// Create new `AndThen` combinator
|
/// Create new `AndThen` combinator
|
||||||
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self
|
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self {
|
||||||
where
|
|
||||||
A: NewService<C>,
|
|
||||||
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
{
|
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
b: f.into_new_service(),
|
b: f.into_new_service(),
|
||||||
@@ -148,8 +156,8 @@ where
|
|||||||
|
|
||||||
impl<A, B, C> Clone for AndThenNewService<A, B, C>
|
impl<A, B, C> Clone for AndThenNewService<A, B, C>
|
||||||
where
|
where
|
||||||
A: Clone,
|
A: NewService<C> + Clone,
|
||||||
B: Clone,
|
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError> + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
use futures::{Async, Future, IntoFuture, Poll};
|
||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
@@ -71,8 +71,12 @@ where
|
|||||||
type Future = AndThenApplyFuture<A, B, F, Out>;
|
type Future = AndThenApplyFuture<A, B, F, Out>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
let not_ready = self.a.poll_ready()?.is_not_ready();
|
||||||
self.b.get_mut().poll_ready()
|
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
} else {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
|
@@ -4,6 +4,34 @@ use futures::{Async, Future, IntoFuture, Poll};
|
|||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
|
|
||||||
|
/// Apply tranform function to a service
|
||||||
|
pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out>
|
||||||
|
where
|
||||||
|
T: Service,
|
||||||
|
F: FnMut(In, &mut T) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
|
U: IntoService<T>,
|
||||||
|
{
|
||||||
|
Apply::new(service.into_service(), f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create fractory for `apply_fn` service.
|
||||||
|
pub fn apply_fn_factory<T, F, In, Out, Cfg, U>(
|
||||||
|
service: U,
|
||||||
|
f: F,
|
||||||
|
) -> ApplyNewService<T, F, In, Out, Cfg>
|
||||||
|
where
|
||||||
|
T: NewService<Cfg>,
|
||||||
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
|
U: IntoNewService<T, Cfg>,
|
||||||
|
{
|
||||||
|
ApplyNewService::new(service.into_new_service(), f)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, F, In, Out>
|
pub struct Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
|
@@ -1,121 +1,70 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use crate::and_then::AndThen;
|
|
||||||
use crate::{IntoNewService, NewService};
|
use crate::{IntoNewService, NewService};
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// Create new ApplyConfig` service factory combinator
|
||||||
pub struct ApplyConfig<F, A, B, C1, C2> {
|
pub fn apply_cfg<F, S, C1, C2, U>(f: F, service: U) -> ApplyConfig<F, S, C1, C2>
|
||||||
a: A,
|
where
|
||||||
b: B,
|
S: NewService<C2>,
|
||||||
|
F: Fn(&C1) -> C2,
|
||||||
|
U: IntoNewService<S, C2>,
|
||||||
|
{
|
||||||
|
ApplyConfig::new(service.into_new_service(), f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `ApplyConfig` service factory combinator
|
||||||
|
pub struct ApplyConfig<F, S, C1, C2> {
|
||||||
|
s: S,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(C1, C2)>,
|
r: PhantomData<(C1, C2)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: NewService<C1>,
|
S: NewService<C2>,
|
||||||
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: Fn(&C1) -> C2,
|
F: Fn(&C1) -> C2,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new ApplyConfig` service factory combinator
|
||||||
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
|
pub fn new<U: IntoNewService<S, C2>>(a: U, f: F) -> Self {
|
||||||
a: A1,
|
|
||||||
b: B1,
|
|
||||||
f: F,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
f,
|
f,
|
||||||
a: a.into_new_service(),
|
s: a.into_new_service(),
|
||||||
b: b.into_new_service(),
|
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> Clone for ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: Clone,
|
S: Clone,
|
||||||
B: Clone,
|
|
||||||
F: Clone,
|
F: Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a: self.a.clone(),
|
s: self.s.clone(),
|
||||||
b: self.b.clone(),
|
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> NewService<C1> for ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: NewService<C1>,
|
S: NewService<C2>,
|
||||||
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: Fn(&C1) -> C2,
|
F: Fn(&C1) -> C2,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = S::Request;
|
||||||
type Response = B::Response;
|
type Response = S::Response;
|
||||||
type Error = A::Error;
|
type Error = S::Error;
|
||||||
type Service = AndThen<A::Service, B::Service>;
|
type Service = S::Service;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = S::InitError;
|
||||||
type Future = ApplyConfigResponse<A, B, C1, C2>;
|
type Future = S::Future;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C1) -> Self::Future {
|
fn new_service(&self, cfg: &C1) -> Self::Future {
|
||||||
let cfg2 = (self.f)(cfg);
|
let cfg2 = (self.f)(cfg);
|
||||||
|
|
||||||
ApplyConfigResponse {
|
self.s.new_service(&cfg2)
|
||||||
a: None,
|
|
||||||
b: None,
|
|
||||||
fut_a: self.a.new_service(cfg),
|
|
||||||
fut_b: self.b.new_service(&cfg2),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ApplyConfigResponse<A, B, C1, C2>
|
|
||||||
where
|
|
||||||
A: NewService<C1>,
|
|
||||||
B: NewService<C2>,
|
|
||||||
{
|
|
||||||
fut_b: B::Future,
|
|
||||||
fut_a: A::Future,
|
|
||||||
a: Option<A::Service>,
|
|
||||||
b: Option<B::Service>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, C1, C2> Future for ApplyConfigResponse<A, B, C1, C2>
|
|
||||||
where
|
|
||||||
A: NewService<C1>,
|
|
||||||
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
{
|
|
||||||
type Item = AndThen<A::Service, B::Service>;
|
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if self.a.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
|
||||||
self.a = Some(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.b.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
|
||||||
self.b = Some(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.a.is_some() && self.b.is_some() {
|
|
||||||
Ok(Async::Ready(AndThen::new(
|
|
||||||
self.a.take().unwrap(),
|
|
||||||
self.b.take().unwrap(),
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,12 +1,14 @@
|
|||||||
|
use futures::future::{err, ok, Either, FutureResult};
|
||||||
|
use futures::{Async, Future, IntoFuture, Poll};
|
||||||
|
|
||||||
use crate::{NewService, Service};
|
use crate::{NewService, Service};
|
||||||
use futures::{Future, IntoFuture, Poll};
|
|
||||||
|
|
||||||
pub type BoxedService<Req, Res, Err> = Box<
|
pub type BoxedService<Req, Res, Err> = Box<
|
||||||
Service<
|
Service<
|
||||||
Request = Req,
|
Request = Req,
|
||||||
Response = Res,
|
Response = Res,
|
||||||
Error = Err,
|
Error = Err,
|
||||||
Future = Box<Future<Item = Res, Error = Err>>,
|
Future = Either<FutureResult<Res, Err>, Box<Future<Item = Res, Error = Err>>>,
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
@@ -125,13 +127,21 @@ where
|
|||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Res;
|
type Response = Res;
|
||||||
type Error = Err;
|
type Error = Err;
|
||||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
type Future = Either<
|
||||||
|
FutureResult<Self::Response, Self::Error>,
|
||||||
|
Box<Future<Item = Self::Response, Error = Self::Error>>,
|
||||||
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.0.poll_ready()
|
self.0.poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
Box::new(self.0.call(req))
|
let mut fut = self.0.call(req);
|
||||||
|
match fut.poll() {
|
||||||
|
Ok(Async::Ready(res)) => Either::A(ok(res)),
|
||||||
|
Err(e) => Either::A(err(e)),
|
||||||
|
Ok(Async::NotReady) => Either::B(Box::new(fut)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,19 @@ use futures::IntoFuture;
|
|||||||
|
|
||||||
use crate::{Apply, IntoTransform, Service, Transform};
|
use crate::{Apply, IntoTransform, Service, Transform};
|
||||||
|
|
||||||
|
/// Use function as transform service
|
||||||
|
pub fn fn_transform<F, S, In, Out, Err>(
|
||||||
|
f: F,
|
||||||
|
) -> impl Transform<S, Request = In, Response = Out::Item, Error = Out::Error, InitError = Err>
|
||||||
|
where
|
||||||
|
S: Service,
|
||||||
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<S::Error>,
|
||||||
|
{
|
||||||
|
FnTransform::new(f)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct FnTransform<F, S, In, Out, Err>
|
pub struct FnTransform<F, S, In, Out, Err>
|
||||||
where
|
where
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -21,21 +22,35 @@ mod map_err;
|
|||||||
mod map_init_err;
|
mod map_init_err;
|
||||||
mod then;
|
mod then;
|
||||||
mod transform;
|
mod transform;
|
||||||
mod transform_map_init_err;
|
mod transform_err;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.3.4", note = "please use `apply_fn` instead")]
|
||||||
|
pub use self::apply::Apply;
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.3.4", note = "please use `apply_fn_factory` instead")]
|
||||||
|
pub use self::apply::ApplyNewService;
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.3.4", note = "please use `fn_transform` instead")]
|
||||||
|
pub use self::fn_transform::FnTransform;
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.3.4", note = "please use `apply_transform` instead")]
|
||||||
|
pub use self::transform::ApplyTransform;
|
||||||
|
|
||||||
pub use self::and_then::{AndThen, AndThenNewService};
|
pub use self::and_then::{AndThen, AndThenNewService};
|
||||||
use self::and_then_apply::AndThenTransform;
|
use self::and_then_apply::AndThenTransform;
|
||||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
||||||
pub use self::apply_cfg::ApplyConfig;
|
pub use self::apply_cfg::apply_cfg;
|
||||||
|
use self::apply_cfg::ApplyConfig;
|
||||||
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
||||||
pub use self::fn_transform::FnTransform;
|
pub use self::fn_transform::fn_transform;
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
pub use self::map::{Map, MapNewService};
|
pub use self::map::{Map, MapNewService};
|
||||||
pub use self::map_err::{MapErr, MapErrNewService};
|
pub use self::map_err::{MapErr, MapErrNewService};
|
||||||
pub use self::map_init_err::MapInitErr;
|
pub use self::map_init_err::MapInitErr;
|
||||||
pub use self::then::{Then, ThenNewService};
|
pub use self::then::{Then, ThenNewService};
|
||||||
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
|
pub use self::transform::{apply_transform, IntoTransform, Transform};
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
pub trait Service {
|
pub trait Service {
|
||||||
@@ -244,19 +259,23 @@ pub trait NewService<Config = ()> {
|
|||||||
|
|
||||||
/// Map this service's config type to a different config,
|
/// Map this service's config type to a different config,
|
||||||
/// and use for nested service
|
/// and use for nested service
|
||||||
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
|
fn apply_cfg<F, C, S, U>(
|
||||||
|
self,
|
||||||
|
service: U,
|
||||||
|
f: F,
|
||||||
|
) -> AndThenNewService<Self, ApplyConfig<F, S, Config, C>, Config>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: Fn(&Config) -> C,
|
F: Fn(&Config) -> C,
|
||||||
B1: IntoNewService<B, C>,
|
U: IntoNewService<S, C>,
|
||||||
B: NewService<
|
S: NewService<
|
||||||
C,
|
C,
|
||||||
Request = Self::Response,
|
Request = Self::Response,
|
||||||
Error = Self::Error,
|
Error = Self::Error,
|
||||||
InitError = Self::InitError,
|
InitError = Self::InitError,
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
ApplyConfig::new(self, service, f)
|
self.and_then(ApplyConfig::new(service, f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Call another service after call to this one has resolved successfully.
|
/// Call another service after call to this one has resolved successfully.
|
||||||
@@ -322,7 +341,7 @@ pub trait NewService<Config = ()> {
|
|||||||
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E, Config>
|
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E, Config>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: Fn(Self::Error) -> E,
|
F: Fn(Self::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
MapErrNewService::new(self, f)
|
MapErrNewService::new(self, f)
|
||||||
}
|
}
|
||||||
@@ -373,6 +392,24 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S> Service for Rc<RefCell<S>>
|
||||||
|
where
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
type Request = S::Request;
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = S::Future;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
||||||
|
self.borrow_mut().poll_ready()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
|
self.borrow_mut().call(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S, C> NewService<C> for Rc<S>
|
impl<S, C> NewService<C> for Rc<S>
|
||||||
where
|
where
|
||||||
S: NewService<C>,
|
S: NewService<C>,
|
||||||
|
@@ -98,19 +98,23 @@ where
|
|||||||
/// service's error.
|
/// service's error.
|
||||||
///
|
///
|
||||||
/// This is created by the `NewServiceExt::map_err` method.
|
/// This is created by the `NewServiceExt::map_err` method.
|
||||||
pub struct MapErrNewService<A, F, E, C> {
|
pub struct MapErrNewService<A, F, E, C>
|
||||||
|
where
|
||||||
|
A: NewService<C>,
|
||||||
|
F: Fn(A::Error) -> E + Clone,
|
||||||
|
{
|
||||||
a: A,
|
a: A,
|
||||||
f: F,
|
f: F,
|
||||||
e: PhantomData<(E, C)>,
|
e: PhantomData<(E, C)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, C> MapErrNewService<A, F, E, C> {
|
impl<A, F, E, C> MapErrNewService<A, F, E, C>
|
||||||
|
where
|
||||||
|
A: NewService<C>,
|
||||||
|
F: Fn(A::Error) -> E + Clone,
|
||||||
|
{
|
||||||
/// Create new `MapErr` new service instance
|
/// Create new `MapErr` new service instance
|
||||||
pub fn new(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self {
|
||||||
where
|
|
||||||
A: NewService<C>,
|
|
||||||
F: Fn(A::Error) -> E,
|
|
||||||
{
|
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
f,
|
f,
|
||||||
@@ -121,8 +125,8 @@ impl<A, F, E, C> MapErrNewService<A, F, E, C> {
|
|||||||
|
|
||||||
impl<A, F, E, C> Clone for MapErrNewService<A, F, E, C>
|
impl<A, F, E, C> Clone for MapErrNewService<A, F, E, C>
|
||||||
where
|
where
|
||||||
A: Clone,
|
A: NewService<C> + Clone,
|
||||||
F: Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::{IntoNewService, NewService, Service};
|
use super::{IntoNewService, NewService, Service};
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
@@ -48,8 +48,12 @@ where
|
|||||||
type Future = ThenFuture<A, B>;
|
type Future = ThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
let not_ready = self.a.poll_ready()?.is_not_ready();
|
||||||
self.b.get_mut().poll_ready()
|
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
} else {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
|
@@ -3,12 +3,19 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
use futures::{Async, Future, IntoFuture, Poll};
|
||||||
|
|
||||||
use crate::transform_map_init_err::TransformMapInitErr;
|
use crate::transform_err::{TransformFromErr, TransformMapInitErr};
|
||||||
use crate::{NewService, Service};
|
use crate::{IntoNewService, NewService, Service};
|
||||||
|
|
||||||
/// `Transform` service factory.
|
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
||||||
|
/// is often implemented for middleware, defining how to manufacture a
|
||||||
|
/// middleware Service. A Service that is manufactured by the factory takes
|
||||||
|
/// the Service that follows it during execution as a parameter, assuming
|
||||||
|
/// ownership of the next Service. A Service can be a variety of types, such
|
||||||
|
/// as (but not limited to) another middleware Service, an extractor Service,
|
||||||
|
/// other helper Services, or the request handler endpoint Service.
|
||||||
|
///
|
||||||
|
/// A Service is created by the factory during server initialization.
|
||||||
///
|
///
|
||||||
/// Transform factory creates service that wraps other services.
|
|
||||||
/// `Config` is a service factory configuration type.
|
/// `Config` is a service factory configuration type.
|
||||||
pub trait Transform<S> {
|
pub trait Transform<S> {
|
||||||
/// Requests handled by the service.
|
/// Requests handled by the service.
|
||||||
@@ -33,10 +40,10 @@ pub trait Transform<S> {
|
|||||||
/// The future response value.
|
/// The future response value.
|
||||||
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
||||||
|
|
||||||
/// Create and return a new service value asynchronously.
|
/// Creates and returns a new Service component, asynchronously
|
||||||
fn new_transform(&self, service: S) -> Self::Future;
|
fn new_transform(&self, service: S) -> Self::Future;
|
||||||
|
|
||||||
/// Map this service's factory init error to a different error,
|
/// Map this service's factory error to a different error,
|
||||||
/// returning a new transform service factory.
|
/// returning a new transform service factory.
|
||||||
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
|
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
|
||||||
where
|
where
|
||||||
@@ -45,6 +52,32 @@ pub trait Transform<S> {
|
|||||||
{
|
{
|
||||||
TransformMapInitErr::new(self, f)
|
TransformMapInitErr::new(self, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map this service's init error to any error implementing `From` for
|
||||||
|
/// this service`s `Error`.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving transform and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
fn from_err<E>(self) -> TransformFromErr<Self, S, E>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
E: From<Self::InitError>,
|
||||||
|
{
|
||||||
|
TransformFromErr::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
// /// Map this service's init error to service's init error
|
||||||
|
// /// if it is implementing `Into` to this service`s `InitError`.
|
||||||
|
// ///
|
||||||
|
// /// Note that this function consumes the receiving transform and returns a
|
||||||
|
// /// wrapped version of it.
|
||||||
|
// fn into_err<E>(self) -> TransformIntoErr<Self, S>
|
||||||
|
// where
|
||||||
|
// Self: Sized,
|
||||||
|
// Self::InitError: From<Self::InitError>,
|
||||||
|
// {
|
||||||
|
// TransformFromErr::new(self)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> Transform<S> for Rc<T>
|
impl<T, S> Transform<S> for Rc<T>
|
||||||
@@ -97,33 +130,65 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Apply` transform new service
|
/// Apply transform to service factory. Function returns
|
||||||
#[derive(Clone)]
|
/// services factory that in initialization creates
|
||||||
pub struct ApplyTransform<T, A, C> {
|
/// service and applies transform to this service.
|
||||||
a: A,
|
pub fn apply_transform<T, S, C, F, U>(
|
||||||
|
t: F,
|
||||||
|
service: U,
|
||||||
|
) -> impl NewService<
|
||||||
|
C,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = T::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
Service = T::Transform,
|
||||||
|
InitError = S::InitError,
|
||||||
|
> + Clone
|
||||||
|
where
|
||||||
|
S: NewService<C>,
|
||||||
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
|
F: IntoTransform<T, S::Service>,
|
||||||
|
U: IntoNewService<S, C>,
|
||||||
|
{
|
||||||
|
ApplyTransform::new(t.into_transform(), service.into_new_service())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `Apply` transform to new service
|
||||||
|
pub struct ApplyTransform<T, S, C> {
|
||||||
|
s: Rc<S>,
|
||||||
t: Rc<T>,
|
t: Rc<T>,
|
||||||
_t: std::marker::PhantomData<C>,
|
_t: std::marker::PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, C> ApplyTransform<T, A, C>
|
impl<T, S, C> ApplyTransform<T, S, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
S: NewService<C>,
|
||||||
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyTransform` new service instance
|
||||||
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
|
pub fn new<F: IntoTransform<T, S::Service>>(t: F, service: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a,
|
s: Rc::new(service),
|
||||||
t: Rc::new(t.into_transform()),
|
t: Rc::new(t.into_transform()),
|
||||||
_t: std::marker::PhantomData,
|
_t: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
|
impl<T, S, C> Clone for ApplyTransform<T, S, C> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
ApplyTransform {
|
||||||
|
s: self.s.clone(),
|
||||||
|
t: self.t.clone(),
|
||||||
|
_t: std::marker::PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, C> NewService<C> for ApplyTransform<T, S, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
S: NewService<C>,
|
||||||
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
@@ -131,31 +196,31 @@ where
|
|||||||
|
|
||||||
type Service = T::Transform;
|
type Service = T::Transform;
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyTransformFuture<T, A, C>;
|
type Future = ApplyTransformFuture<T, S, C>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
ApplyTransformFuture {
|
ApplyTransformFuture {
|
||||||
t_cell: self.t.clone(),
|
t_cell: self.t.clone(),
|
||||||
fut_a: self.a.new_service(cfg).into_future(),
|
fut_a: self.s.new_service(cfg).into_future(),
|
||||||
fut_t: None,
|
fut_t: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyTransformFuture<T, A, C>
|
pub struct ApplyTransformFuture<T, S, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
S: NewService<C>,
|
||||||
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
fut_a: A::Future,
|
fut_a: S::Future,
|
||||||
fut_t: Option<<T::Future as IntoFuture>::Future>,
|
fut_t: Option<<T::Future as IntoFuture>::Future>,
|
||||||
t_cell: Rc<T>,
|
t_cell: Rc<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
|
impl<T, S, C> Future for ApplyTransformFuture<T, S, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
S: NewService<C>,
|
||||||
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
type Item = T::Transform;
|
type Item = T::Transform;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
|
162
actix-service/src/transform_err.rs
Normal file
162
actix-service/src/transform_err.rs
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::{Future, Poll};
|
||||||
|
|
||||||
|
use super::Transform;
|
||||||
|
|
||||||
|
/// Transform for the `map_err` combinator, changing the type of a new
|
||||||
|
/// transform's init error.
|
||||||
|
///
|
||||||
|
/// This is created by the `Transform::map_err` method.
|
||||||
|
pub struct TransformMapInitErr<T, S, F, E> {
|
||||||
|
t: T,
|
||||||
|
f: F,
|
||||||
|
e: PhantomData<(S, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
|
||||||
|
/// Create new `TransformMapErr` new transform instance
|
||||||
|
pub fn new(t: T, f: F) -> Self
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
t,
|
||||||
|
f,
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
t: self.t.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Transform = T::Transform;
|
||||||
|
|
||||||
|
type InitError = E;
|
||||||
|
type Future = TransformMapInitErrFuture<T, S, F, E>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
TransformMapInitErrFuture {
|
||||||
|
fut: self.t.new_transform(service),
|
||||||
|
f: self.f.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TransformMapInitErrFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E,
|
||||||
|
{
|
||||||
|
fut: T::Future,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
type Item = T::Transform;
|
||||||
|
type Error = E;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
self.fut.poll().map_err(&self.f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transform for the `from_err` combinator, changing the type of a new
|
||||||
|
/// transform's init error.
|
||||||
|
///
|
||||||
|
/// This is created by the `Transform::from_err` method.
|
||||||
|
pub struct TransformFromErr<T, S, E> {
|
||||||
|
t: T,
|
||||||
|
e: PhantomData<(S, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, E> TransformFromErr<T, S, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
E: From<T::InitError>,
|
||||||
|
{
|
||||||
|
/// Create new `TransformFromErr` new transform instance
|
||||||
|
pub fn new(t: T) -> Self {
|
||||||
|
Self { t, e: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, E> Clone for TransformFromErr<T, S, E>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
t: self.t.clone(),
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, E> Transform<S> for TransformFromErr<T, S, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
E: From<T::InitError>,
|
||||||
|
{
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Transform = T::Transform;
|
||||||
|
|
||||||
|
type InitError = E;
|
||||||
|
type Future = TransformFromErrFuture<T, S, E>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
TransformFromErrFuture {
|
||||||
|
fut: self.t.new_transform(service),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TransformFromErrFuture<T, S, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
E: From<T::InitError>,
|
||||||
|
{
|
||||||
|
fut: T::Future,
|
||||||
|
_t: PhantomData<E>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, E> Future for TransformFromErrFuture<T, S, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
E: From<T::InitError>,
|
||||||
|
{
|
||||||
|
type Item = T::Transform;
|
||||||
|
type Error = E;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
self.fut.poll().map_err(E::from)
|
||||||
|
}
|
||||||
|
}
|
@@ -1,94 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use futures::{Future, Poll};
|
|
||||||
|
|
||||||
use super::Transform;
|
|
||||||
|
|
||||||
/// NewTransform for the `map_init_err` combinator, changing the type of a new
|
|
||||||
/// transform's error.
|
|
||||||
///
|
|
||||||
/// This is created by the `NewTransform::map_init_err` method.
|
|
||||||
pub struct TransformMapInitErr<T, S, F, E> {
|
|
||||||
t: T,
|
|
||||||
f: F,
|
|
||||||
e: PhantomData<(S, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
|
|
||||||
/// Create new `MapInitErr` new transform instance
|
|
||||||
pub fn new(t: T, f: F) -> Self
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
F: Fn(T::InitError) -> E,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
t,
|
|
||||||
f,
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
|
|
||||||
where
|
|
||||||
T: Clone,
|
|
||||||
F: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
t: self.t.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
F: Fn(T::InitError) -> E + Clone,
|
|
||||||
{
|
|
||||||
type Request = T::Request;
|
|
||||||
type Response = T::Response;
|
|
||||||
type Error = T::Error;
|
|
||||||
type Transform = T::Transform;
|
|
||||||
|
|
||||||
type InitError = E;
|
|
||||||
type Future = TransformMapInitErrFuture<T, S, F, E>;
|
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
|
||||||
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TransformMapInitErrFuture<T, S, F, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
F: Fn(T::InitError) -> E,
|
|
||||||
{
|
|
||||||
fut: T::Future,
|
|
||||||
f: F,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
F: Fn(T::InitError) -> E,
|
|
||||||
{
|
|
||||||
fn new(fut: T::Future, f: F) -> Self {
|
|
||||||
TransformMapInitErrFuture { f, fut }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
F: Fn(T::InitError) -> E + Clone,
|
|
||||||
{
|
|
||||||
type Item = T::Transform;
|
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
self.fut.poll().map_err(&self.f)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-test-server"
|
name = "actix-test-server"
|
||||||
version = "0.1.0"
|
version = "0.2.1"
|
||||||
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"]
|
||||||
@@ -33,9 +33,9 @@ ssl = ["openssl", "actix-server/ssl"]
|
|||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.0"
|
actix-rt = "0.2.1"
|
||||||
# actix-server = "0.3.3"
|
actix-server = "0.4.0"
|
||||||
actix-server = { path="../actix-server" }
|
actix-server-config = "0.1.0"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
@@ -56,4 +56,4 @@ webpki = { version = "0.19", optional = true }
|
|||||||
webpki-roots = { version = "0.16", optional = true }
|
webpki-roots = { version = "0.16", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-service = "0.3.3"
|
actix-service = "0.3.4"
|
||||||
|
@@ -4,8 +4,9 @@ use std::{net, thread};
|
|||||||
|
|
||||||
use actix_rt::{Runtime, System};
|
use actix_rt::{Runtime, System};
|
||||||
use actix_server::{Server, StreamServiceFactory};
|
use actix_server::{Server, StreamServiceFactory};
|
||||||
|
pub use actix_server_config::{Io, ServerConfig};
|
||||||
|
|
||||||
use futures::Future;
|
use futures::future::{lazy, Future, IntoFuture};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
use tokio_reactor::Handle;
|
use tokio_reactor::Handle;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_tcp::TcpStream;
|
||||||
@@ -98,6 +99,15 @@ impl TestServerRuntime {
|
|||||||
self.rt.block_on(fut)
|
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
|
/// Spawn future to the current runtime
|
||||||
pub fn spawn<F>(&mut self, fut: F)
|
pub fn spawn<F>(&mut self, fut: F)
|
||||||
where
|
where
|
||||||
|
5
actix-threadpool/CHANGES.md
Normal file
5
actix-threadpool/CHANGES.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.0] - 2019-03-28
|
||||||
|
|
||||||
|
* Move threadpool to separate crate
|
27
actix-threadpool/Cargo.toml
Normal file
27
actix-threadpool/Cargo.toml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-threadpool"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
description = "Actix thread pool for sync code"
|
||||||
|
keywords = ["actix", "network", "framework", "async", "futures"]
|
||||||
|
homepage = "https://actix.rs"
|
||||||
|
repository = "https://github.com/actix/actix-net.git"
|
||||||
|
documentation = "https://docs.rs/actix-threadpool/"
|
||||||
|
categories = ["network-programming", "asynchronous"]
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "actix_threadpool"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
derive_more = "0.14"
|
||||||
|
futures = "0.1.25"
|
||||||
|
parking_lot = "0.7"
|
||||||
|
lazy_static = "1.2"
|
||||||
|
log = "0.4"
|
||||||
|
num_cpus = "1.10"
|
||||||
|
threadpool = "1.7"
|
88
actix-threadpool/src/lib.rs
Normal file
88
actix-threadpool/src/lib.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
//! Thread pool for blocking operations
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use derive_more::Display;
|
||||||
|
use futures::sync::oneshot;
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use threadpool::ThreadPool;
|
||||||
|
|
||||||
|
/// Env variable for default cpu pool size
|
||||||
|
const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL";
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
|
||||||
|
let default = match std::env::var(ENV_CPU_POOL_VAR) {
|
||||||
|
Ok(val) => {
|
||||||
|
if let Ok(val) = val.parse() {
|
||||||
|
val
|
||||||
|
} else {
|
||||||
|
log::error!("Can not parse ACTIX_THREADPOOL value");
|
||||||
|
num_cpus::get() * 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => num_cpus::get() * 5,
|
||||||
|
};
|
||||||
|
Mutex::new(
|
||||||
|
threadpool::Builder::new()
|
||||||
|
.thread_name("actix-web".to_owned())
|
||||||
|
.num_threads(default)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static POOL: ThreadPool = {
|
||||||
|
DEFAULT_POOL.lock().clone()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Blocking operation execution error
|
||||||
|
#[derive(Debug, Display)]
|
||||||
|
pub enum BlockingError<E: fmt::Debug> {
|
||||||
|
#[display(fmt = "{:?}", _0)]
|
||||||
|
Error(E),
|
||||||
|
#[display(fmt = "Thread pool is gone")]
|
||||||
|
Canceled,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute blocking function on a thread pool, returns future that resolves
|
||||||
|
/// to result of the function execution.
|
||||||
|
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Result<I, E> + Send + 'static,
|
||||||
|
I: Send + 'static,
|
||||||
|
E: Send + fmt::Debug + 'static,
|
||||||
|
{
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
POOL.with(|pool| {
|
||||||
|
pool.execute(move || {
|
||||||
|
if !tx.is_canceled() {
|
||||||
|
let _ = tx.send(f());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
CpuFuture { rx }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Blocking operation completion future. It resolves with results
|
||||||
|
/// of blocking function execution.
|
||||||
|
pub struct CpuFuture<I, E> {
|
||||||
|
rx: oneshot::Receiver<Result<I, E>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I, E: fmt::Debug> Future for CpuFuture<I, E> {
|
||||||
|
type Item = I;
|
||||||
|
type Error = BlockingError<E>;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
let res = futures::try_ready!(self.rx.poll().map_err(|_| BlockingError::Canceled));
|
||||||
|
match res {
|
||||||
|
Ok(val) => Ok(Async::Ready(val)),
|
||||||
|
Err(err) => Err(BlockingError::Error(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,30 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
|
||||||
|
## [0.3.5] - 2019-04-04
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Allow to send messages to `FramedTransport` via mpsc channel.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Remove 'static constraint from Clonable service
|
||||||
|
|
||||||
|
|
||||||
|
## [0.3.4] - 2019-03-12
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* `TimeoutService`, `InOrderService`, `InFlightService` accepts generic IntoService services.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fix `InFlightService::poll_ready()` nested service readiness check
|
||||||
|
|
||||||
|
* Fix `InOrderService::poll_ready()` nested service readiness check
|
||||||
|
|
||||||
|
|
||||||
## [0.3.3] - 2019-03-09
|
## [0.3.3] - 2019-03-09
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-utils"
|
name = "actix-utils"
|
||||||
version = "0.3.3"
|
version = "0.3.5"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix utils - various actix net related services"
|
description = "Actix utils - various actix net related services"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@@ -27,4 +27,4 @@ tokio-current-thread = "0.1.4"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2"
|
actix-rt = "0.2.1"
|
||||||
|
@@ -7,12 +7,12 @@ use futures::Poll;
|
|||||||
use super::cell::Cell;
|
use super::cell::Cell;
|
||||||
|
|
||||||
/// Service that allows to turn non-clone service to a service with `Clone` impl
|
/// Service that allows to turn non-clone service to a service with `Clone` impl
|
||||||
pub struct CloneableService<T: 'static> {
|
pub struct CloneableService<T> {
|
||||||
service: Cell<T>,
|
service: Cell<T>,
|
||||||
_t: PhantomData<Rc<()>>,
|
_t: PhantomData<Rc<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> CloneableService<T> {
|
impl<T> CloneableService<T> {
|
||||||
pub fn new(service: T) -> Self
|
pub fn new(service: T) -> Self
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
@@ -24,7 +24,7 @@ impl<T: 'static> CloneableService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> Clone for CloneableService<T> {
|
impl<T> Clone for CloneableService<T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: self.service.clone(),
|
service: self.service.clone(),
|
||||||
@@ -35,7 +35,7 @@ impl<T: 'static> Clone for CloneableService<T> {
|
|||||||
|
|
||||||
impl<T> Service for CloneableService<T>
|
impl<T> Service for CloneableService<T>
|
||||||
where
|
where
|
||||||
T: Service + 'static,
|
T: Service,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
|
@@ -7,6 +7,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
|||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::task::AtomicTask;
|
use futures::task::AtomicTask;
|
||||||
|
use futures::unsync::mpsc;
|
||||||
use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
|
use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
@@ -178,6 +179,11 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum FramedMessage<T> {
|
||||||
|
Message(T),
|
||||||
|
Close,
|
||||||
|
}
|
||||||
|
|
||||||
/// FramedTransport - is a future that reads frames from Framed object
|
/// FramedTransport - is a future that reads frames from Framed object
|
||||||
/// and pass then to the service.
|
/// and pass then to the service.
|
||||||
pub struct FramedTransport<S, T, U>
|
pub struct FramedTransport<S, T, U>
|
||||||
@@ -193,6 +199,7 @@ where
|
|||||||
service: S,
|
service: S,
|
||||||
state: TransportState<S, U>,
|
state: TransportState<S, U>,
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
|
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,6 +207,7 @@ enum TransportState<S: Service, U: Encoder + Decoder> {
|
|||||||
Processing,
|
Processing,
|
||||||
Error(FramedTransportError<S::Error, U>),
|
Error(FramedTransportError<S::Error, U>),
|
||||||
FramedError(FramedTransportError<S::Error, U>),
|
FramedError(FramedTransportError<S::Error, U>),
|
||||||
|
FlushAndStop,
|
||||||
Stopping,
|
Stopping,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +229,7 @@ where
|
|||||||
fn poll_read(&mut self) -> bool {
|
fn poll_read(&mut self) -> bool {
|
||||||
loop {
|
loop {
|
||||||
match self.service.poll_ready() {
|
match self.service.poll_ready() {
|
||||||
Ok(Async::Ready(_)) => loop {
|
Ok(Async::Ready(_)) => {
|
||||||
let item = match self.framed.poll() {
|
let item = match self.framed.poll() {
|
||||||
Ok(Async::Ready(Some(el))) => el,
|
Ok(Async::Ready(Some(el))) => el,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -244,7 +252,7 @@ where
|
|||||||
inner.task.notify();
|
inner.task.notify();
|
||||||
Ok(())
|
Ok(())
|
||||||
}));
|
}));
|
||||||
},
|
}
|
||||||
Ok(Async::NotReady) => return false,
|
Ok(Async::NotReady) => return false,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.state = TransportState::Error(FramedTransportError::Service(err));
|
self.state = TransportState::Error(FramedTransportError::Service(err));
|
||||||
@@ -257,10 +265,12 @@ where
|
|||||||
/// write to framed object
|
/// write to framed object
|
||||||
fn poll_write(&mut self) -> bool {
|
fn poll_write(&mut self) -> bool {
|
||||||
let inner = self.inner.get_mut();
|
let inner = self.inner.get_mut();
|
||||||
|
let mut rx_done = self.rx.is_none();
|
||||||
|
let mut buf_empty = inner.buf.is_empty();
|
||||||
loop {
|
loop {
|
||||||
while !self.framed.is_write_buf_full() {
|
while !self.framed.is_write_buf_full() {
|
||||||
if let Some(msg) = inner.buf.pop_front() {
|
if !buf_empty {
|
||||||
match msg {
|
match inner.buf.pop_front().unwrap() {
|
||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
if let Err(err) = self.framed.force_send(msg) {
|
if let Err(err) = self.framed.force_send(msg) {
|
||||||
self.state = TransportState::FramedError(
|
self.state = TransportState::FramedError(
|
||||||
@@ -268,6 +278,7 @@ where
|
|||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
buf_empty = inner.buf.is_empty();
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.state =
|
self.state =
|
||||||
@@ -275,7 +286,33 @@ where
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if !rx_done && self.rx.is_some() {
|
||||||
|
match self.rx.as_mut().unwrap().poll() {
|
||||||
|
Ok(Async::Ready(Some(FramedMessage::Message(msg)))) => {
|
||||||
|
if let Err(err) = self.framed.force_send(msg) {
|
||||||
|
self.state = TransportState::FramedError(
|
||||||
|
FramedTransportError::Encoder(err),
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Async::Ready(Some(FramedMessage::Close))) => {
|
||||||
|
self.state = TransportState::FlushAndStop;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Ok(Async::Ready(None)) => {
|
||||||
|
let _ = self.rx.take();
|
||||||
|
}
|
||||||
|
Ok(Async::NotReady) => rx_done = true,
|
||||||
|
Err(_e) => {
|
||||||
|
let _ = self.rx.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rx_done && buf_empty {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,6 +350,7 @@ where
|
|||||||
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
|
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
|
||||||
FramedTransport {
|
FramedTransport {
|
||||||
framed,
|
framed,
|
||||||
|
rx: None,
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
state: TransportState::Processing,
|
state: TransportState::Processing,
|
||||||
inner: Cell::new(FramedTransportInner {
|
inner: Cell::new(FramedTransportInner {
|
||||||
@@ -322,6 +360,15 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get Sender
|
||||||
|
pub fn set_receiver(
|
||||||
|
mut self,
|
||||||
|
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
||||||
|
) -> Self {
|
||||||
|
self.rx = Some(rx);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Get reference to a service wrapped by `FramedTransport` instance.
|
/// Get reference to a service wrapped by `FramedTransport` instance.
|
||||||
pub fn get_ref(&self) -> &S {
|
pub fn get_ref(&self) -> &S {
|
||||||
&self.service
|
&self.service
|
||||||
@@ -378,6 +425,20 @@ where
|
|||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TransportState::FlushAndStop => {
|
||||||
|
if !self.framed.is_write_buf_empty() {
|
||||||
|
match self.framed.poll_complete() {
|
||||||
|
Err(err) => {
|
||||||
|
debug!("Error sending data: {:?}", err);
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||||
|
Ok(Async::Ready(_)) => Ok(Async::Ready(())),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
TransportState::FramedError(err) => Err(err),
|
TransportState::FramedError(err) => Err(err),
|
||||||
TransportState::Stopping => Ok(Async::Ready(())),
|
TransportState::Stopping => Ok(Async::Ready(())),
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
use actix_service::{Service, Transform, Void};
|
use actix_service::{IntoService, Service, Transform, Void};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
@@ -42,11 +42,17 @@ pub struct InFlightService<S> {
|
|||||||
service: S,
|
service: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> InFlightService<S> {
|
impl<S> InFlightService<S>
|
||||||
pub fn new(max: usize, service: S) -> Self {
|
where
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
pub fn new<U>(max: usize, service: U) -> Self
|
||||||
|
where
|
||||||
|
U: IntoService<S>,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
service,
|
|
||||||
count: Counter::new(max),
|
count: Counter::new(max),
|
||||||
|
service: service.into_service(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,9 +67,9 @@ where
|
|||||||
type Future = InFlightServiceResponse<T>;
|
type Future = InFlightServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.service.poll_ready()?;
|
if let Async::NotReady = self.service.poll_ready()? {
|
||||||
|
Ok(Async::NotReady)
|
||||||
if !self.count.available() {
|
} else if !self.count.available() {
|
||||||
log::trace!("InFlight limit exceeded");
|
log::trace!("InFlight limit exceeded");
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
} else {
|
} else {
|
||||||
|
@@ -3,7 +3,7 @@ use std::fmt;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_service::{Service, Transform, Void};
|
use actix_service::{IntoService, Service, Transform, Void};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::task::AtomicTask;
|
use futures::task::AtomicTask;
|
||||||
use futures::unsync::oneshot;
|
use futures::unsync::oneshot;
|
||||||
@@ -112,9 +112,12 @@ where
|
|||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
{
|
{
|
||||||
pub fn new(service: S) -> Self {
|
pub fn new<U>(service: U) -> Self
|
||||||
|
where
|
||||||
|
U: IntoService<S>,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
service,
|
service: service.into_service(),
|
||||||
acks: VecDeque::new(),
|
acks: VecDeque::new(),
|
||||||
task: Rc::new(AtomicTask::new()),
|
task: Rc::new(AtomicTask::new()),
|
||||||
}
|
}
|
||||||
@@ -137,9 +140,6 @@ where
|
|||||||
// poll_ready could be called from different task
|
// poll_ready could be called from different task
|
||||||
self.task.register();
|
self.task.register();
|
||||||
|
|
||||||
// check nested service
|
|
||||||
self.service.poll_ready().map_err(InOrderError::Service)?;
|
|
||||||
|
|
||||||
// check acks
|
// check acks
|
||||||
while !self.acks.is_empty() {
|
while !self.acks.is_empty() {
|
||||||
let rec = self.acks.front_mut().unwrap();
|
let rec = self.acks.front_mut().unwrap();
|
||||||
@@ -153,7 +153,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Async::Ready(()))
|
// check nested service
|
||||||
|
if let Async::NotReady = self.service.poll_ready().map_err(InOrderError::Service)? {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
} else {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: S::Request) -> Self::Future {
|
fn call(&mut self, request: S::Request) -> Self::Future {
|
||||||
@@ -240,7 +245,7 @@ mod tests {
|
|||||||
let (tx3, rx3) = oneshot::channel();
|
let (tx3, rx3) = oneshot::channel();
|
||||||
let (tx_stop, rx_stop) = oneshot::channel();
|
let (tx_stop, rx_stop) = oneshot::channel();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
let h = std::thread::spawn(move || {
|
||||||
let rx1 = rx1;
|
let rx1 = rx1;
|
||||||
let rx2 = rx2;
|
let rx2 = rx2;
|
||||||
let rx3 = rx3;
|
let rx3 = rx3;
|
||||||
@@ -258,6 +263,7 @@ mod tests {
|
|||||||
.and_then(move |res: Vec<_>| {
|
.and_then(move |res: Vec<_>| {
|
||||||
assert_eq!(res, vec![1, 2, 3]);
|
assert_eq!(res, vec![1, 2, 3]);
|
||||||
let _ = tx_stop.send(());
|
let _ = tx_stop.send(());
|
||||||
|
actix_rt::System::current().stop();
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
@@ -269,5 +275,6 @@ mod tests {
|
|||||||
let _ = tx1.send(1);
|
let _ = tx1.send(1);
|
||||||
|
|
||||||
let _ = rx_stop.wait();
|
let _ = rx_stop.wait();
|
||||||
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ use std::fmt;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_service::{Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
use tokio_timer::{clock, Delay};
|
use tokio_timer::{clock, Delay};
|
||||||
@@ -106,9 +106,18 @@ pub struct TimeoutService<S> {
|
|||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> TimeoutService<S> {
|
impl<S> TimeoutService<S>
|
||||||
pub fn new(timeout: Duration, service: S) -> Self {
|
where
|
||||||
TimeoutService { service, timeout }
|
S: Service,
|
||||||
|
{
|
||||||
|
pub fn new<U>(timeout: Duration, service: U) -> Self
|
||||||
|
where
|
||||||
|
U: IntoService<S>,
|
||||||
|
{
|
||||||
|
TimeoutService {
|
||||||
|
timeout,
|
||||||
|
service: service.into_service(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,7 +9,7 @@ use std::{env, fmt, io};
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use actix_server::Server;
|
use actix_server::{Io, Server};
|
||||||
use actix_service::{fn_service, NewService};
|
use actix_service::{fn_service, NewService};
|
||||||
use futures::{future, Future};
|
use futures::{future, Future};
|
||||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
@@ -54,8 +54,8 @@ fn main() -> io::Result<()> {
|
|||||||
let acceptor = acceptor.clone();
|
let acceptor = acceptor.clone();
|
||||||
|
|
||||||
// service for converting incoming TcpStream to a SslStream<TcpStream>
|
// service for converting incoming TcpStream to a SslStream<TcpStream>
|
||||||
fn_service(move |stream: tokio_tcp::TcpStream| {
|
fn_service(move |stream: Io<tokio_tcp::TcpStream>| {
|
||||||
SslAcceptorExt::accept_async(&acceptor, stream)
|
SslAcceptorExt::accept_async(&acceptor, stream.into_parts().0)
|
||||||
.map_err(|e| println!("Openssl error: {}", e))
|
.map_err(|e| println!("Openssl error: {}", e))
|
||||||
})
|
})
|
||||||
// .and_then() combinator uses other service to convert incoming `Request` to a
|
// .and_then() combinator uses other service to convert incoming `Request` to a
|
||||||
|
@@ -1,5 +1,15 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.2] - 2019-04-07
|
||||||
|
|
||||||
|
* Export `Quoter` type
|
||||||
|
|
||||||
|
* Allow to reset `Path` instance
|
||||||
|
|
||||||
|
## [0.1.1] - 2019-04-03
|
||||||
|
|
||||||
|
* Get dynamic segment by name instead of iterator.
|
||||||
|
|
||||||
## [0.1.0] - 2019-03-09
|
## [0.1.0] - 2019-03-09
|
||||||
|
|
||||||
* Initial release
|
* Initial release
|
||||||
|
@@ -1,16 +1,16 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-router"
|
name = "actix-router"
|
||||||
version = "0.1.0"
|
version = "0.1.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Path router"
|
description = "Path router"
|
||||||
keywords = ["actix"]
|
keywords = ["actix"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
repository = "https://github.com/actix/actix-net.git"
|
||||||
documentation = "https://actix.rs/api/actix-net/stable/actix_router/"
|
documentation = "https://docs.rs/actix-router/"
|
||||||
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 = "../"
|
workspace = ".."
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_router"
|
name = "actix_router"
|
||||||
@@ -23,7 +23,8 @@ default = ["http"]
|
|||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
regex = "1.0"
|
regex = "1.0"
|
||||||
serde = "1.0.80"
|
serde = "1.0.80"
|
||||||
string = "0.1.3"
|
string = "0.2.0"
|
||||||
|
log = "0.4"
|
||||||
http = { version="0.1.14", optional=true }
|
http = { version="0.1.14", optional=true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
@@ -39,7 +39,7 @@ impl<T: AsRef<[u8]>> ResourcePath for string::String<T> {
|
|||||||
mod url;
|
mod url;
|
||||||
|
|
||||||
#[cfg(feature = "http")]
|
#[cfg(feature = "http")]
|
||||||
pub use self::url::Url;
|
pub use self::url::{Quoter, Url};
|
||||||
|
|
||||||
#[cfg(feature = "http")]
|
#[cfg(feature = "http")]
|
||||||
mod http_support {
|
mod http_support {
|
||||||
|
@@ -51,16 +51,19 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Get reference to inner path instance
|
/// Get reference to inner path instance
|
||||||
pub fn get_ref(&self) -> &T {
|
pub fn get_ref(&self) -> &T {
|
||||||
&self.path
|
&self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Get mutable reference to inner path instance
|
/// Get mutable reference to inner path instance
|
||||||
pub fn get_mut(&mut self) -> &mut T {
|
pub fn get_mut(&mut self) -> &mut T {
|
||||||
&mut self.path
|
&mut self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Path
|
/// Path
|
||||||
pub fn path(&self) -> &str {
|
pub fn path(&self) -> &str {
|
||||||
let skip = self.skip as usize;
|
let skip = self.skip as usize;
|
||||||
@@ -72,13 +75,22 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reset inner path
|
#[inline]
|
||||||
|
/// Set new path
|
||||||
pub fn set(&mut self, path: T) {
|
pub fn set(&mut self, path: T) {
|
||||||
self.skip = 0;
|
self.skip = 0;
|
||||||
self.path = path;
|
self.path = path;
|
||||||
self.segments.clear();
|
self.segments.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Reset state
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
self.skip = 0;
|
||||||
|
self.segments.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 = self.skip + n;
|
||||||
@@ -99,11 +111,13 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
.push((Rc::new(name.to_string()), PathItem::Static(value)));
|
.push((Rc::new(name.to_string()), PathItem::Static(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Check if there are any matched patterns
|
/// Check if there are any matched patterns
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.segments.is_empty()
|
self.segments.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Check number of extracted parameters
|
/// Check number of extracted parameters
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.segments.len()
|
self.segments.len()
|
||||||
|
@@ -118,6 +118,7 @@ impl ResourceDef {
|
|||||||
&self.pattern
|
&self.pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
/// Check if path matchs this pattern?
|
/// Check if path matchs this pattern?
|
||||||
pub fn is_match(&self, path: &str) -> bool {
|
pub fn is_match(&self, path: &str) -> bool {
|
||||||
match self.tp {
|
match self.tp {
|
||||||
@@ -196,18 +197,17 @@ impl ResourceDef {
|
|||||||
[PathItem::Static(""); MAX_DYNAMIC_SEGMENTS];
|
[PathItem::Static(""); MAX_DYNAMIC_SEGMENTS];
|
||||||
|
|
||||||
if let Some(captures) = re.captures(path.path()) {
|
if let Some(captures) = re.captures(path.path()) {
|
||||||
let mut passed = false;
|
for (no, name) in names.iter().enumerate() {
|
||||||
|
if let Some(m) = captures.name(&name) {
|
||||||
for capture in captures.iter() {
|
|
||||||
if let Some(ref m) = capture {
|
|
||||||
if !passed {
|
|
||||||
passed = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
segments[idx] = PathItem::Segment(m.start() as u16, m.end() as u16);
|
|
||||||
idx += 1;
|
idx += 1;
|
||||||
pos = m.end();
|
pos = m.end();
|
||||||
|
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
|
||||||
|
} else {
|
||||||
|
log::error!(
|
||||||
|
"Dynamic path match but not all segments found: {}",
|
||||||
|
name
|
||||||
|
);
|
||||||
|
false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@@ -1,5 +1,3 @@
|
|||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use crate::ResourcePath;
|
use crate::ResourcePath;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -39,7 +37,7 @@ thread_local! {
|
|||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
pub struct Url {
|
pub struct Url {
|
||||||
uri: http::Uri,
|
uri: http::Uri,
|
||||||
path: Option<Rc<String>>,
|
path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Url {
|
impl Url {
|
||||||
@@ -49,6 +47,13 @@ impl Url {
|
|||||||
Url { uri, path }
|
Url { uri, path }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_quoter(uri: http::Uri, quoter: &Quoter) -> Url {
|
||||||
|
Url {
|
||||||
|
path: quoter.requote(uri.path().as_bytes()),
|
||||||
|
uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uri(&self) -> &http::Uri {
|
pub fn uri(&self) -> &http::Uri {
|
||||||
&self.uri
|
&self.uri
|
||||||
}
|
}
|
||||||
@@ -61,19 +66,27 @@ impl Url {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn update(&mut self, uri: &http::Uri) {
|
pub fn update(&mut self, uri: &http::Uri) {
|
||||||
self.uri = uri.clone();
|
self.uri = uri.clone();
|
||||||
self.path = DEFAULT_QUOTER.with(|q| q.requote(uri.path().as_bytes()));
|
self.path = DEFAULT_QUOTER.with(|q| q.requote(uri.path().as_bytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn update_with_quoter(&mut self, uri: &http::Uri, quoter: &Quoter) {
|
||||||
|
self.uri = uri.clone();
|
||||||
|
self.path = quoter.requote(uri.path().as_bytes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResourcePath for Url {
|
impl ResourcePath for Url {
|
||||||
|
#[inline]
|
||||||
fn path(&self) -> &str {
|
fn path(&self) -> &str {
|
||||||
self.path()
|
self.path()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Quoter {
|
pub struct Quoter {
|
||||||
safe_table: [u8; 16],
|
safe_table: [u8; 16],
|
||||||
protected_table: [u8; 16],
|
protected_table: [u8; 16],
|
||||||
}
|
}
|
||||||
@@ -108,7 +121,7 @@ impl Quoter {
|
|||||||
q
|
q
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn requote(&self, val: &[u8]) -> Option<Rc<String>> {
|
pub fn requote(&self, val: &[u8]) -> Option<String> {
|
||||||
let mut has_pct = 0;
|
let mut has_pct = 0;
|
||||||
let mut pct = [b'%', 0, 0];
|
let mut pct = [b'%', 0, 0];
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
@@ -160,7 +173,7 @@ impl Quoter {
|
|||||||
if let Some(data) = cloned {
|
if let Some(data) = cloned {
|
||||||
// Unsafe: we get data from http::Uri, which does utf-8 checks already
|
// Unsafe: we get data from http::Uri, which does utf-8 checks already
|
||||||
// this code only decodes valid pct encoded values
|
// this code only decodes valid pct encoded values
|
||||||
Some(Rc::new(unsafe { String::from_utf8_unchecked(data) }))
|
Some(unsafe { String::from_utf8_unchecked(data) })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user