1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-08-13 21:18:21 +02:00

Compare commits

..

29 Commits

Author SHA1 Message Date
Nikolay Kim
86f57e5a4a prepare actix-service release 2019-03-09 14:10:02 -08:00
Nikolay Kim
43ad18ccb1 remove get_decoded 2019-03-09 13:58:07 -08:00
Nikolay Kim
34995a8ccf revert re-quoting change 2019-03-09 13:56:09 -08:00
Nikolay Kim
0ff300c40f export ApplyConfig 2019-03-09 09:05:51 -08:00
Nikolay Kim
629ef23371 add .apply_cfg new service combinator 2019-03-09 09:02:23 -08:00
Nikolay Kim
d2b96ff877 add ServerConfig to server services 2019-03-09 07:31:22 -08:00
Nikolay Kim
ac62e2dbf9 revert generic request in actix-utils 2019-03-09 07:27:35 -08:00
Nikolay Kim
6bbbdba921 revert generic Request change 2019-03-09 06:36:23 -08:00
Nikolay Kim
2099629fe3 cleanup ssl services 2019-03-08 22:41:30 -08:00
Nikolay Kim
a4d4770462 remove server config 2019-03-08 22:38:39 -08:00
Nikolay Kim
70ead175b9 IntoService for fn_cfg_factory 2019-03-08 20:51:50 -08:00
Nikolay Kim
49867b5e9d fix IntoService 2019-03-08 20:50:29 -08:00
Nikolay Kim
0f064c43e9 remove uneeded code 2019-03-08 20:10:47 -08:00
Nikolay Kim
7db29544f9 add ServerConfig param for server service 2019-03-08 19:43:13 -08:00
Nikolay Kim
4850cf41ff rename module 2019-03-08 16:26:30 -08:00
Nikolay Kim
046142ffbc added is_prefix_match and resource_path 2019-03-08 15:34:40 -08:00
Nikolay Kim
49e6dbcda2 remove unused ResourceMap 2019-03-08 12:33:44 -08:00
Nikolay Kim
877614a494 add params decoding 2019-03-08 04:43:51 -08:00
Nikolay Kim
ac0e8b9e53 re-enable examples 2019-03-06 23:33:35 -08:00
Nikolay Kim
b407c65f4c add FramedParts::with_read_buf method 2019-03-06 22:53:55 -08:00
Nikolay Kim
51bd7d2721 update actix-rt 2019-03-06 10:39:53 -08:00
Nikolay Kim
c03d869694 return io::Result from run method, remove Handle 2019-03-06 10:24:58 -08:00
Nikolay Kim
25f1eae51f add ResourceDef::root_prefix, insert slash to the beggining of the pattern 2019-03-05 21:03:53 -08:00
Nikolay Kim
1153715149 change generics order for Transform trait 2019-03-05 09:49:08 -08:00
Nikolay Kim
aa2967c653 fix feature gated code 2019-03-05 07:41:41 -08:00
Nikolay Kim
dfbb77f98d make service Request type generic 2019-03-05 07:35:26 -08:00
Nikolay Kim
e8a49801eb revert IntoFuture change 2019-03-04 21:37:06 -08:00
Nikolay Kim
03f2046a42 add ApplyTransform new service 2019-03-04 21:25:50 -08:00
Nikolay Kim
2e18ca805c use IntoFuture 2019-03-04 20:40:38 -08:00
50 changed files with 892 additions and 538 deletions

View File

@@ -1,3 +1,18 @@
[package]
name = "actix-net"
version = "0.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix net - framework for the compisible network services for Rust"
readme = "README.md"
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]
members = [
"actix-codec",
@@ -5,7 +20,19 @@ members = [
"actix-rt",
"actix-service",
"actix-server",
"actix-server-config",
"actix-test-server",
"actix-utils",
"router",
]
[dev-dependencies]
actix-service = { path="actix-service" }
actix-codec = "0.1.1"
actix-rt = "0.2.0"
actix-server = { path="actix-server", features=["ssl"] }
env_logger = "0.6"
futures = "0.1.25"
openssl = "0.10"
tokio-tcp = "0.1"
tokio-openssl = "0.3"

View File

@@ -1,5 +1,10 @@
# Changes
## [0.1.0] - 2019-03-06
* Added `FramedParts::with_read_buffer()` method.
## [0.1.0] - 2018-12-09
* Move codec to separate crate

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-codec"
version = "0.1.0"
version = "0.1.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Utilities for encoding and decoding frames"
keywords = ["network", "framework", "async", "futures"]

View File

@@ -349,4 +349,17 @@ impl<T, U> FramedParts<T, U> {
_priv: (),
}
}
/// Create a new `FramedParts` with read buffer
pub fn with_read_buf(io: T, codec: U, read_buf: BytesMut) -> FramedParts<T, U> {
FramedParts {
io,
codec,
read_buf,
write_buf: BytesMut::new(),
write_buf_lw: LW,
write_buf_hw: HW,
_priv: (),
}
}
}

View File

@@ -27,7 +27,8 @@ default = []
ssl = ["openssl", "tokio-openssl"]
[dependencies]
actix-service = "0.3.0"
#actix-service = "0.3.0"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0"
futures = "0.1"
tokio-tcp = "0.1"

View File

@@ -1,5 +1,11 @@
# Changes
## [0.2.0] - 2019-03-06
* `run` method returns `io::Result<()>`
* Removed `Handle`
## [0.1.0] - 2018-12-09
* Initial release

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-rt"
version = "0.1.0"
version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix runtime"
keywords = ["network", "framework", "async", "futures"]
@@ -20,7 +20,7 @@ path = "src/lib.rs"
[dependencies]
log = "0.4"
bytes = "0.4"
futures = "0.1.24"
futures = "0.1.25"
tokio-current-thread = "0.1"
tokio-executor = "0.1.5"
tokio-reactor = "0.1.7"

View File

@@ -72,7 +72,7 @@ impl Builder {
/// This function will start tokio runtime and will finish once the
/// `System::stop()` message get called.
/// Function `f` get called within tokio runtime context.
pub fn run<F>(self, f: F) -> i32
pub fn run<F>(self, f: F) -> io::Result<()>
where
F: FnOnce() + 'static,
{
@@ -140,7 +140,7 @@ pub struct SystemRunner {
impl SystemRunner {
/// This function will start event loop and will finish once the
/// `System::stop()` function is called.
pub fn run(self) -> i32 {
pub fn run(self) -> io::Result<()> {
let SystemRunner { mut rt, stop, .. } = self;
// run loop
@@ -148,12 +148,21 @@ impl SystemRunner {
Arbiter::run_system();
Ok::<_, ()>(())
}));
let code = match rt.block_on(stop) {
Ok(code) => code,
Err(_) => 1,
let result = match rt.block_on(stop) {
Ok(code) => {
if code != 0 {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Non-zero exit code: {}", code),
))
} else {
Ok(())
}
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
};
Arbiter::stop_system();
code
result
}
/// Execute a future and wait for result.

View File

@@ -7,7 +7,7 @@ mod system;
pub use self::arbiter::Arbiter;
pub use self::builder::{Builder, SystemRunner};
pub use self::runtime::{Handle, Runtime};
pub use self::runtime::Runtime;
pub use self::system::System;
/// Spawns a future on the current arbiter.

View File

@@ -1,9 +1,7 @@
use std::error::Error;
use std::fmt;
use std::io;
use std::{fmt, io};
use futures::{future, Future};
use tokio_current_thread::Handle as ExecutorHandle;
use futures::Future;
use tokio_current_thread::{self as current_thread, CurrentThread};
use tokio_executor;
use tokio_reactor::{self, Reactor};
@@ -26,58 +24,6 @@ pub struct Runtime {
executor: CurrentThread<Timer<Reactor>>,
}
/// Handle to spawn a future on the corresponding `CurrentThread` runtime instance
#[derive(Debug, Clone)]
pub struct Handle(ExecutorHandle);
impl Handle {
/// Spawn a future onto the `CurrentThread` runtime instance corresponding to this handle
///
/// # Panics
///
/// This function panics if the spawn fails. Failure occurs if the `CurrentThread`
/// instance of the `Handle` does not exist anymore.
pub fn spawn<F>(&self, future: F) -> Result<(), tokio_executor::SpawnError>
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
self.0.spawn(future)
}
/// Provides a best effort **hint** to whether or not `spawn` will succeed.
///
/// This function may return both false positives **and** false negatives.
/// If `status` returns `Ok`, then a call to `spawn` will *probably*
/// succeed, but may fail. If `status` returns `Err`, a call to `spawn` will
/// *probably* fail, but may succeed.
///
/// This allows a caller to avoid creating the task if the call to `spawn`
/// has a high likelihood of failing.
pub fn status(&self) -> Result<(), tokio_executor::SpawnError> {
self.0.status()
}
}
impl<T> future::Executor<T> for Handle
where
T: Future<Item = (), Error = ()> + Send + 'static,
{
fn execute(&self, future: T) -> Result<(), future::ExecuteError<T>> {
if let Err(e) = self.status() {
let kind = if e.is_at_capacity() {
future::ExecuteErrorKind::NoCapacity
} else {
future::ExecuteErrorKind::Shutdown
};
return Err(future::ExecuteError::new(kind, future));
}
let _ = self.spawn(future);
Ok(())
}
}
/// Error returned by the `run` function.
#[derive(Debug)]
pub struct RunError {
@@ -120,14 +66,6 @@ impl Runtime {
}
}
/// Get a new handle to spawn futures on the single-threaded Tokio runtime
///
/// Different to the runtime itself, the handle can be sent to different
/// threads.
pub fn handle(&self) -> Handle {
Handle(self.executor.handle().clone())
}
/// Spawn a future onto the single-threaded Tokio runtime.
///
/// See [module level][mod] documentation for more details.

View File

@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::io;
use futures::sync::mpsc::UnboundedSender;
@@ -109,7 +110,7 @@ impl System {
/// This function will start tokio runtime and will finish once the
/// `System::stop()` message get called.
/// Function `f` get called within tokio runtime context.
pub fn run<F>(f: F) -> i32
pub fn run<F>(f: F) -> io::Result<()>
where
F: FnOnce() + 'static,
{

View File

@@ -0,0 +1,18 @@
[package]
name = "actix-server-config"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server config utils"
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
license = "MIT/Apache-2.0"
edition = "2018"
workspace = ".."
[lib]
name = "actix_server_config"
path = "src/lib.rs"
[dependencies]
actix-service = { path="../actix-service" }
futures = "0.1.25"

View File

@@ -0,0 +1,33 @@
use std::cell::Cell;
use std::net::SocketAddr;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct ServerConfig {
addr: SocketAddr,
secure: Rc<Cell<bool>>,
}
impl ServerConfig {
pub fn new(addr: SocketAddr) -> Self {
ServerConfig {
addr,
secure: Rc::new(Cell::new(false)),
}
}
/// Returns the address of the local half of this TCP server socket
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
/// Returns true if connection is secure (tls enabled)
pub fn secure(&self) -> bool {
self.secure.as_ref().get()
}
/// Set secure flag
pub fn set_secure(&self) {
self.secure.as_ref().set(true)
}
}

View File

@@ -1,6 +1,11 @@
# Changes
## [0.3.1] - 2019-03-xx
## [0.4.0] - 2019-03-xx
* Upgrade actix-service
## [0.3.1] - 2019-03-04
### Added

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-server"
version = "0.3.0"
version = "0.3.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server - General purpose tcp server"
keywords = ["network", "framework", "async", "futures"]
@@ -33,8 +33,10 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-service = "0.3.0"
actix-rt = "0.1.0"
actix-rt = "0.2.0"
#actix-service = "0.3.2"
actix-service = { path="../actix-service" }
actix-server-config = { path="../actix-server-config" }
log = "0.4"
num_cpus = "1.0"

View File

@@ -12,8 +12,8 @@ use num_cpus;
use tokio_timer::sleep;
use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand};
use crate::service_config::{ConfiguredService, ServiceConfig};
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
@@ -151,14 +151,14 @@ impl ServerBuilder {
{
let sockets = bind_addr(addr)?;
for lst in sockets {
let token = self.token.next();
self.services.push(StreamNewService::create(
name.as_ref().to_string(),
token,
factory,
factory.clone(),
lst.local_addr()?,
));
for lst in sockets {
self.sockets.push((token, lst));
}
Ok(self)
@@ -170,7 +170,7 @@ impl ServerBuilder {
name: N,
lst: net::TcpListener,
factory: F,
) -> Self
) -> io::Result<Self>
where
F: ServiceFactory,
{
@@ -179,9 +179,10 @@ impl ServerBuilder {
name.as_ref().to_string(),
token,
factory,
lst.local_addr()?,
));
self.sockets.push((token, lst));
self
Ok(self)
}
/// Spawn new thread and start listening for incoming connections.
@@ -194,19 +195,18 @@ impl ServerBuilder {
/// ```rust,ignore
/// use actix_web::*;
///
/// fn main() {
/// fn main() -> std::io::Result<()> {
/// Server::new().
/// .service(
/// HttpServer::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok())))
/// HttpServer::new(|| App::new().service(web::service("/").to(|| HttpResponse::Ok())))
/// .bind("127.0.0.1:0")
/// .expect("Can not bind to 127.0.0.1:0"))
/// .run();
/// .run()
/// }
/// ```
pub fn run(self) {
pub fn run(self) -> io::Result<()> {
let sys = System::new("http-server");
self.start();
sys.run();
sys.run()
}
/// Starts processing incoming connections and return server controller.

View File

@@ -2,17 +2,19 @@
mod accept;
mod builder;
mod config;
mod counter;
mod server;
mod service_config;
mod services;
mod signals;
pub mod ssl;
mod worker;
pub use actix_server_config::ServerConfig;
pub use self::builder::ServerBuilder;
pub use self::config::{ServiceConfig, ServiceRuntime};
pub use self::server::Server;
pub use self::service_config::{ServiceConfig, ServiceRuntime};
pub use self::services::ServiceFactory;
#[doc(hidden)]

View File

@@ -1,13 +1,14 @@
use std::net;
use std::net::{SocketAddr, TcpStream};
use std::time::Duration;
use actix_rt::spawn;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service};
use futures::future::{err, ok, FutureResult};
use futures::{Future, Poll};
use log::error;
use tokio_reactor::Handle;
use tokio_tcp::TcpStream;
use tokio_tcp::TcpStream as TokioTcpStream;
use super::Token;
use crate::counter::CounterGuard;
@@ -15,7 +16,7 @@ use crate::counter::CounterGuard;
/// Server message
pub(crate) enum ServerMessage {
/// New stream
Connect(net::TcpStream),
Connect(TcpStream),
/// Gracefull shutdown
Shutdown(Duration),
/// Force shutdown
@@ -23,7 +24,7 @@ pub(crate) enum ServerMessage {
}
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = TcpStream>;
type NewService: NewService<ServerConfig, Request = TokioTcpStream>;
fn create(&self) -> Self::NewService;
}
@@ -57,7 +58,7 @@ impl<T> StreamService<T> {
impl<T> Service for StreamService<T>
where
T: Service<Request = TcpStream>,
T: Service<Request = TokioTcpStream>,
T::Future: 'static,
T::Error: 'static,
{
@@ -73,7 +74,8 @@ where
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
match req {
ServerMessage::Connect(stream) => {
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
let stream =
TokioTcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
@@ -96,14 +98,25 @@ pub(crate) struct StreamNewService<F: ServiceFactory> {
name: String,
inner: F,
token: Token,
addr: SocketAddr,
}
impl<F> StreamNewService<F>
where
F: ServiceFactory,
{
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
Box::new(Self { name, token, inner })
pub(crate) fn create(
name: String,
token: Token,
inner: F,
addr: SocketAddr,
) -> Box<InternalServiceFactory> {
Box::new(Self {
name,
token,
inner,
addr,
})
}
}
@@ -120,15 +133,17 @@ where
name: self.name.clone(),
inner: self.inner.clone(),
token: self.token,
addr: self.addr,
})
}
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
let token = self.token;
let config = ServerConfig::new(self.addr);
Box::new(
self.inner
.create()
.new_service(&())
.new_service(&config)
.map_err(|_| ())
.map(move |inner| {
let service: BoxedServerService = Box::new(StreamService::new(inner));
@@ -155,7 +170,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream>,
T: NewService<ServerConfig, Request = TokioTcpStream>,
{
type NewService = T;

View File

@@ -1,13 +1,14 @@
use std::io;
use std::marker::PhantomData;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service};
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
use tokio_io::{AsyncRead, AsyncWrite};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
/// Support `SSL` connections via native-tls package
///
@@ -36,7 +37,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T>;
type Error = Error;
@@ -44,7 +45,9 @@ impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(NativeTlsAcceptorService {
acceptor: self.acceptor.clone(),

View File

@@ -6,8 +6,9 @@ use openssl::ssl::{HandshakeError, SslAcceptor};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_openssl::{AcceptAsync, SslAcceptorExt, SslStream};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig;
/// Support `SSL` connections via openssl package
///
@@ -36,7 +37,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T> {
type Request = T;
type Response = SslStream<T>;
type Error = HandshakeError<T>;
@@ -44,7 +45,9 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(),

View File

@@ -8,8 +8,9 @@ use rustls::{ServerConfig, ServerSession};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_rustls::{Accept, TlsAcceptor, TlsStream};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig as SrvConfig;
/// Support `SSL` connections via rustls package
///
@@ -38,7 +39,7 @@ impl<T> Clone for RustlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>;
type Error = io::Error;
@@ -46,7 +47,9 @@ impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> {
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(RustlsAcceptorService {
acceptor: self.config.clone().into(),

View File

@@ -0,0 +1,70 @@
use std::{net, thread, time};
use actix_server::{Server, ServerConfig};
use actix_service::{fn_cfg_factory, fn_service, IntoService};
use net2::TcpBuilder;
fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = TcpBuilder::new_v4().unwrap();
socket.bind(&addr).unwrap();
socket.reuse_address(true).unwrap();
let tcp = socket.to_tcp_listener().unwrap();
tcp.local_addr().unwrap()
}
#[test]
fn test_bind() {
let addr = unused_addr();
thread::spawn(move || {
Server::build()
.bind("test", addr, move || {
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}
#[test]
fn test_bind_no_config() {
let addr = unused_addr();
thread::spawn(move || {
Server::build()
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}
#[test]
fn test_listen() {
let addr = unused_addr();
thread::spawn(move || {
let lst = net::TcpListener::bind(addr).unwrap();
Server::build()
.listen("test", lst, move || {
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}

View File

@@ -1,5 +1,19 @@
# Changes
## [0.3.3] - 2019-03-09
### Added
* Add `ApplyTransform` new service for transform and new service.
* Add `NewService::apply_cfg()` combinator, allows to use
nested `NewService` with different config parameter.
### Changed
* Revert IntoFuture change
## [0.3.2] - 2019-03-04
### Changed

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-service"
version = "0.3.2"
version = "0.3.3"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service"
keywords = ["network", "framework", "async", "futures"]
@@ -25,3 +25,6 @@ path = "src/lib.rs"
[dependencies]
futures = "0.1.24"
void = "1.0.2"
[dev-dependencies]
actix-rt = "0.1"

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, IntoFuture, Poll};
use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
use crate::cell::Cell;
@@ -142,10 +142,7 @@ where
type Future = AndThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
AndThenNewServiceFuture::new(
self.a.new_service(cfg).into_future(),
self.b.new_service(cfg).into_future(),
)
AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
}
}
@@ -168,8 +165,8 @@ where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
{
fut_b: <B::Future as IntoFuture>::Future,
fut_a: <A::Future as IntoFuture>::Future,
fut_b: B::Future,
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
}
@@ -179,10 +176,7 @@ where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
{
fn new(
fut_a: <A::Future as IntoFuture>::Future,
fut_b: <B::Future as IntoFuture>::Future,
) -> Self {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture {
fut_a,
fut_b,

View File

@@ -1,6 +1,6 @@
use std::rc::Rc;
use futures::{Async, Future, IntoFuture, Poll};
use futures::{Async, Future, Poll};
use crate::and_then::AndThen;
use crate::from_err::FromErr;
@@ -67,8 +67,8 @@ where
a: None,
t: None,
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_b: self.b.new_service(cfg).into_future(),
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg),
fut_t: None,
}
}
@@ -81,9 +81,9 @@ where
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
fut_a: <A::Future as IntoFuture>::Future,
fut_b: <B::Future as IntoFuture>::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
fut_a: A::Future,
fut_b: B::Future,
fut_t: Option<T::Future>,
a: Option<A::Service>,
t: Option<T::Transform>,
t_cell: Rc<T>,
@@ -102,7 +102,7 @@ where
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_b.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
self.fut_t = Some(self.t_cell.new_transform(service));
}
}

View File

@@ -209,8 +209,8 @@ where
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
fut_b: <B::Future as IntoFuture>::Future,
fut_a: <A::Future as IntoFuture>::Future,
fut_b: B::Future,
fut_a: A::Future,
f: Cell<F>,
a: Option<A::Service>,
b: Option<B::Service>,

View File

@@ -124,7 +124,7 @@ where
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg).into_future(), self.f.clone())
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
}
}
@@ -134,7 +134,7 @@ where
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fut: <T::Future as IntoFuture>::Future,
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
}
@@ -145,7 +145,7 @@ where
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
fn new(fut: T::Future, f: F) -> Self {
ApplyNewServiceFuture {
f: Some(f),
fut,

View File

@@ -0,0 +1,163 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use crate::and_then::AndThen;
use crate::{IntoNewService, NewService};
/// `ApplyNewService` new service combinator
pub struct ApplyConfig<F, A, B, C1, C2> {
a: A,
b: B,
f: F,
r: PhantomData<(C1, C2)>,
}
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
F: Fn(&C1) -> C2,
{
/// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
a: A1,
b: B1,
f: F,
) -> Self {
Self {
f,
a: a.into_new_service(),
b: b.into_new_service(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
where
A: Clone,
B: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
f: self.f.clone(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
F: Fn(&C1) -> C2,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError;
type Future = ApplyConfigResponse<A, B, C1, C2>;
fn new_service(&self, cfg: &C1) -> Self::Future {
let cfg2 = (self.f)(cfg);
ApplyConfigResponse {
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)
}
}
}
#[cfg(test)]
mod tests {
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use crate::{fn_cfg_factory, NewService, Service};
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, _: ()) -> Self::Future {
ok(())
}
}
#[test]
fn test_new_service() {
let new_srv = fn_cfg_factory(|_: &usize| Ok::<_, ()>(Srv)).apply_cfg(
fn_cfg_factory(|s: &String| {
assert_eq!(s, "test");
Ok::<_, ()>(Srv)
}),
|cfg: &usize| {
assert_eq!(*cfg, 1);
"test".to_string()
},
);
if let Async::Ready(mut srv) = new_srv.new_service(&1).poll().unwrap() {
assert!(srv.poll_ready().is_ok());
}
}
}

View File

@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use futures::future::{ok, Future, FutureResult};
use futures::{try_ready, Async, IntoFuture, Poll};
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
@@ -28,7 +28,8 @@ where
pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
FnNewServiceConfig::new(f)
@@ -218,7 +219,8 @@ where
pub struct FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
f: F,
@@ -228,7 +230,8 @@ where
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
pub fn new(f: F) -> Self {
@@ -239,7 +242,8 @@ where
impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
type Request = S::Request;
@@ -248,17 +252,45 @@ where
type Service = S;
type InitError = E;
type Future = R::Future;
type Future = FnNewServiceConfigFut<R, S, E>;
fn new_service(&self, cfg: &C) -> Self::Future {
(self.f)(cfg).into_future()
FnNewServiceConfigFut {
fut: (self.f)(cfg).into_future(),
_t: PhantomData,
}
}
}
pub struct FnNewServiceConfigFut<R, S, E>
where
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fut: R::Future,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
where
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
type Item = S;
type Error = R::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
}
}
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fn clone(&self) -> Self {
@@ -269,7 +301,8 @@ where
impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> {

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{Async, Future, IntoFuture, Poll};
use futures::{Async, Future, Poll};
use super::{NewService, Service};
@@ -124,7 +124,7 @@ where
fn new_service(&self, cfg: &C) -> Self::Future {
FromErrNewServiceFuture {
fut: self.a.new_service(cfg).into_future(),
fut: self.a.new_service(cfg),
e: PhantomData,
}
}
@@ -135,7 +135,7 @@ where
A: NewService<C>,
E: From<A::Error>,
{
fut: <A::Future as IntoFuture>::Future,
fut: A::Future,
e: PhantomData<E>,
}

View File

@@ -9,6 +9,7 @@ mod and_then;
mod and_then_apply;
mod and_then_apply_fn;
mod apply;
mod apply_cfg;
pub mod blank;
pub mod boxed;
mod cell;
@@ -23,9 +24,10 @@ mod transform;
mod transform_map_init_err;
pub use self::and_then::{AndThen, AndThenNewService};
pub use self::and_then_apply::AndThenTransform;
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::apply_cfg::ApplyConfig;
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::FnTransform;
pub use self::from_err::{FromErr, FromErrNewService};
@@ -33,7 +35,7 @@ pub use self::map::{Map, MapNewService};
pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{IntoTransform, Transform};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
@@ -199,12 +201,12 @@ pub trait NewService<Config = ()> {
type InitError;
/// The future of the `Service` instance.
type Future: IntoFuture<Item = Self::Service, Error = Self::InitError>;
type Future: Future<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_service(&self, cfg: &Config) -> Self::Future;
/// Apply function to specified service and use it as a next service in
/// Apply transform service to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(
self,
@@ -240,6 +242,23 @@ pub trait NewService<Config = ()> {
AndThenApplyNewService::new(self, service, f)
}
/// Map this service's config type to a different config,
/// and use for nested service
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
where
Self: Sized,
F: Fn(&Config) -> C,
B1: IntoNewService<B, C>,
B: NewService<
C,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
ApplyConfig::new(self, service, f)
}
/// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
where

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{Async, Future, IntoFuture, Poll};
use futures::{Async, Future, Poll};
use super::{NewService, Service};
@@ -146,7 +146,7 @@ where
type Future = MapNewServiceFuture<A, F, Res, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
@@ -155,7 +155,7 @@ where
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
fut: <A::Future as IntoFuture>::Future,
fut: A::Future,
f: Option<F>,
}
@@ -164,7 +164,7 @@ where
A: NewService<Cfg>,
F: FnMut(A::Response) -> Res,
{
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
fn new(fut: A::Future, f: F) -> Self {
MapNewServiceFuture { f: Some(f), fut }
}
}

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{Async, Future, IntoFuture, Poll};
use futures::{Async, Future, Poll};
use super::{NewService, Service};
@@ -147,7 +147,7 @@ where
type Future = MapErrNewServiceFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
@@ -156,7 +156,7 @@ where
A: NewService<C>,
F: Fn(A::Error) -> E,
{
fut: <A::Future as IntoFuture>::Future,
fut: A::Future,
f: F,
}
@@ -165,7 +165,7 @@ where
A: NewService<C>,
F: Fn(A::Error) -> E,
{
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
fn new(fut: A::Future, f: F) -> Self {
MapErrNewServiceFuture { f, fut }
}
}

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{Future, IntoFuture, Poll};
use futures::{Future, Poll};
use super::NewService;
@@ -54,7 +54,7 @@ where
type Future = MapInitErrFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
@@ -64,7 +64,7 @@ where
F: Fn(A::InitError) -> E,
{
f: F,
fut: <A::Future as IntoFuture>::Future,
fut: A::Future,
}
impl<A, F, E, C> MapInitErrFuture<A, F, E, C>
@@ -72,7 +72,7 @@ where
A: NewService<C>,
F: Fn(A::InitError) -> E,
{
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
fn new(fut: A::Future, f: F) -> Self {
MapInitErrFuture { f, fut }
}
}

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, IntoFuture, Poll};
use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
use crate::cell::Cell;
@@ -157,10 +157,7 @@ where
type Future = ThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
ThenNewServiceFuture::new(
self.a.new_service(cfg).into_future(),
self.b.new_service(cfg).into_future(),
)
ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
}
}
@@ -188,8 +185,8 @@ where
InitError = A::InitError,
>,
{
fut_b: <B::Future as IntoFuture>::Future,
fut_a: <A::Future as IntoFuture>::Future,
fut_b: B::Future,
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
}
@@ -204,10 +201,7 @@ where
InitError = A::InitError,
>,
{
fn new(
fut_a: <A::Future as IntoFuture>::Future,
fut_b: <B::Future as IntoFuture>::Future,
) -> Self {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
ThenNewServiceFuture {
fut_a,
fut_b,

View File

@@ -1,10 +1,10 @@
use std::rc::Rc;
use std::sync::Arc;
use futures::IntoFuture;
use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_init_err::TransformMapInitErr;
use crate::Service;
use crate::{NewService, Service};
/// `Transform` service factory.
///
@@ -31,7 +31,7 @@ pub trait Transform<S> {
type InitError;
/// The future response value.
type Future: IntoFuture<Item = Self::Transform, Error = Self::InitError>;
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_transform(&self, service: S) -> Self::Future;
@@ -96,3 +96,80 @@ where
self
}
}
/// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, A, C> {
a: A,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, A, C> ApplyTransform<T, A, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
Self {
a,
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, A, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_t: None,
}
}
}
pub struct ApplyTransformFuture<T, A, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
fut_a: A::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
t_cell: Rc<T>,
}
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Item = T::Transform;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_a.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
}
}
if let Some(ref mut fut) = self.fut_t {
fut.poll()
} else {
Ok(Async::NotReady)
}
}
}

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{Future, IntoFuture, Poll};
use futures::{Future, Poll};
use super::Transform;
@@ -57,10 +57,7 @@ where
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(
self.t.new_transform(service).into_future(),
self.f.clone(),
)
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
}
}
@@ -69,7 +66,7 @@ where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: <T::Future as IntoFuture>::Future,
fut: T::Future,
f: F,
}
@@ -78,7 +75,7 @@ where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
fn new(fut: T::Future, f: F) -> Self {
TransformMapInitErrFuture { f, fut }
}
}

View File

@@ -34,7 +34,8 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-rt = "0.1.0"
actix-server = "0.3.0"
#actix-server = "0.3.0"
actix-server = { path="../actix-server" }
log = "0.4"
net2 = "0.2"

View File

@@ -1,5 +1,10 @@
# Changes
## [0.4.0] - 2019-03-xx
* Upgrade actix-service
## [0.3.2] - 2019-03-04
### Changed

View File

@@ -18,7 +18,8 @@ name = "actix_utils"
path = "src/lib.rs"
[dependencies]
actix-service = "0.3.2"
#actix-service = "0.3.2"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0"
bytes = "0.4"
futures = "0.1.24"

View File

@@ -102,8 +102,8 @@ where
fn new_service(&self, cfg: &C) -> Self::Future {
match self {
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg).into_future()),
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg).into_future()),
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
}
}
}

View File

@@ -120,7 +120,7 @@ where
fn call(&mut self, req: Framed<T, U>) -> Self::Future {
FramedServiceResponseFuture {
fut: self.factory.new_service(&self.config).into_future(),
fut: self.factory.new_service(&self.config),
framed: Some(req),
}
}
@@ -408,7 +408,7 @@ where
}
}
impl<T, U, F> NewService<()> for IntoFramed<T, U, F>
impl<T, C, U, F> NewService<C> for IntoFramed<T, U, F>
where
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
@@ -421,7 +421,7 @@ where
type Service = IntoFramedService<T, U, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, _: &C) -> Self::Future {
ok(IntoFramedService {
factory: self.factory.clone(),
_t: PhantomData,

View File

@@ -4,7 +4,7 @@ use std::rc::Rc;
use actix_service::{IntoNewService, IntoService, NewService, Service};
use futures::future::{ok, Future, FutureResult};
use futures::unsync::mpsc;
use futures::{Async, IntoFuture, Poll, Stream};
use futures::{Async, Poll, Stream};
type Request<T> = Result<<T as IntoStream>::Item, <T as IntoStream>::Error>;
@@ -113,7 +113,6 @@ where
Box::new(
self.factory
.new_service(&self.config)
.into_future()
.and_then(move |srv| StreamDispatcher::new(req, srv)),
)
}
@@ -233,7 +232,7 @@ impl<T> Clone for TakeItem<T> {
}
}
impl<T: Stream> NewService<()> for TakeItem<T> {
impl<T: Stream, C> NewService<C> for TakeItem<T> {
type Request = T;
type Response = (Option<T::Item>, T);
type Error = T::Error;
@@ -241,7 +240,7 @@ impl<T: Stream> NewService<()> for TakeItem<T> {
type Service = TakeItemService<T>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, _: &C) -> Self::Future {
ok(TakeItemService { _t: PhantomData })
}
}

View File

@@ -5,12 +5,12 @@ use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use std::{env, fmt};
use std::{env, fmt, io};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::System;
use actix_server::Server;
use actix_service::{IntoNewService, NewService};
use actix_service::{fn_service, NewService};
use futures::{future, Future};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use tokio_openssl::SslAcceptorExt;
@@ -23,7 +23,7 @@ fn logger<T: AsyncRead + AsyncWrite + fmt::Debug>(
future::ok(stream)
}
fn main() {
fn main() -> io::Result<()> {
env::set_var("RUST_LOG", "actix_net=trace");
env_logger::init();
@@ -54,16 +54,14 @@ fn main() {
let acceptor = acceptor.clone();
// service for converting incoming TcpStream to a SslStream<TcpStream>
(move |stream| {
fn_service(move |stream: tokio_tcp::TcpStream| {
SslAcceptorExt::accept_async(&acceptor, stream)
.map_err(|e| println!("Openssl error: {}", e))
})
// convert closure to a `NewService`
.into_new_service()
// .and_then() combinator uses other service to convert incoming `Request` to a
// `Response` and then uses that response as an input for next
// service. in this case, on success we use `logger` service
.and_then(logger)
.and_then(fn_service(logger))
// Next service counts number of connections
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
@@ -75,5 +73,5 @@ fn main() {
.unwrap()
.start();
sys.run();
sys.run()
}

View File

@@ -1,13 +1,13 @@
use std::io;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::System;
use actix_server::{ssl, Server};
use actix_service::NewService;
use futures::{future, Future};
use futures::future;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
#[derive(Debug)]
@@ -15,16 +15,7 @@ struct ServiceState {
num: Arc<AtomicUsize>,
}
fn service<T: AsyncRead + AsyncWrite>(
st: &mut ServiceState,
_: T,
) -> impl Future<Item = (), Error = ()> {
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
}
fn main() {
fn main() -> io::Result<()> {
let sys = System::new("test");
// load ssl keys
@@ -53,9 +44,8 @@ fn main() {
println!("got ssl connection {:?}", num);
future::ok(())
})
})
.unwrap()
})?
.start();
sys.run();
sys.run()
}

View File

@@ -208,3 +208,32 @@ impl<T: ResourcePath> Resource<T> for Path<T> {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "http")]
#[test]
fn test_get_param_by_name() {
use crate::Url;
use http::{HttpTryFrom, Uri};
let mut params = Path::new(Url::new(Uri::try_from("/").unwrap()));
params.add_static("item1", "path");
params.add_static("item2", "http%3A%2F%2Flocalhost%3A80%2Ffoo");
assert_eq!(params.get("item0"), None);
assert_eq!(params.get_decoded("item0"), None);
assert_eq!(params.get("item1"), Some("path"));
assert_eq!(params.get_decoded("item1").unwrap().to_owned(), "path");
assert_eq!(
params.get("item2"),
Some("http%3A%2F%2Flocalhost%3A80%2Ffoo")
);
assert_eq!(
params.get_decoded("item2").unwrap().to_owned(),
"http://localhost:80/foo"
);
}
}

View File

@@ -14,26 +14,13 @@ const MAX_DYNAMIC_SEGMENTS: usize = 16;
/// Resource definition can contain only 16 dynamic segments
#[derive(Clone, Debug)]
pub struct ResourceDef {
id: u16,
tp: PatternType,
rtp: ResourceType,
name: String,
pattern: String,
elements: Vec<PatternElement>,
}
#[derive(Debug, Copy, Clone, PartialEq)]
/// Resource type
pub enum ResourceType {
/// Normal resource
Normal,
/// Resource for application default handler
Default,
/// External resource
External,
/// Unknown resource type
Unset,
}
#[derive(Debug, Clone, PartialEq)]
enum PatternElement {
Str(String),
@@ -64,13 +51,25 @@ impl ResourceDef {
ResourceDef::with_prefix(path, true)
}
/// Construct external resource def
/// Parse path pattern and create new `Pattern` instance.
/// Inserts `/` to begging of the pattern.
///
/// Panics if path pattern is malformed.
pub fn external(path: &str) -> Self {
let mut resource = ResourceDef::with_prefix(path, false);
resource.rtp = ResourceType::External;
resource
///
/// Use `prefix` type instead of `static`.
///
/// Panics if path regex pattern is wrong.
pub fn root_prefix(path: &str) -> Self {
ResourceDef::with_prefix(&insert_slash(path), true)
}
/// Resource id
pub fn id(&self) -> u16 {
self.id
}
/// Set resource id
pub fn set_id(&mut self, id: u16) {
self.id = id;
}
/// Parse path pattern and create new `Pattern` instance with custom prefix
@@ -98,12 +97,22 @@ impl ResourceDef {
ResourceDef {
tp,
elements,
id: 0,
name: String::new(),
rtp: ResourceType::Normal,
pattern: path.to_owned(),
}
}
/// Resource pattern name
pub fn name(&self) -> &str {
&self.name
}
/// Mutable reference to a name of a resource definition.
pub fn name_mut(&mut self) -> &mut String {
&mut self.name
}
/// Path pattern of the resource
pub fn pattern(&self) -> &str {
&self.pattern
@@ -118,6 +127,57 @@ impl ResourceDef {
}
}
/// Is prefix path a match against this resource?
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
let plen = path.len();
let path = if path.is_empty() { "/" } else { path };
match self.tp {
PatternType::Static(ref s) => {
if s == path {
Some(plen)
} else {
None
}
}
PatternType::Dynamic(ref re, _, len) => {
if let Some(captures) = re.captures(path) {
let mut pos = 0;
let mut passed = false;
for capture in captures.iter() {
if let Some(ref m) = capture {
if !passed {
passed = true;
continue;
}
pos = m.end();
}
}
Some(pos + len)
} else {
None
}
}
PatternType::Prefix(ref s) => {
let len = if path == s {
s.len()
} else if path.starts_with(s)
&& (s.ends_with('/') || path.split_at(s.len()).1.starts_with('/'))
{
if s.ends_with('/') {
s.len() - 1
} else {
s.len()
}
} else {
return None;
};
Some(min(plen, len))
}
}
}
/// Is the given path and parameters a match against this pattern?
pub fn match_path<T: ResourcePath>(&self, path: &mut Path<T>) -> bool {
match self.tp {
@@ -180,34 +240,32 @@ impl ResourceDef {
}
}
// /// Build resource path.
// pub fn resource_path<U, I>(
// &self, path: &mut String, elements: &mut U,
// ) -> Result<(), UrlGenerationError>
// where
// U: Iterator<Item = I>,
// I: AsRef<str>,
// {
// match self.tp {
// PatternType::Prefix(ref p) => path.push_str(p),
// PatternType::Static(ref p) => path.push_str(p),
// PatternType::Dynamic(..) => {
// for el in &self.elements {
// match *el {
// PatternElement::Str(ref s) => path.push_str(s),
// PatternElement::Var(_) => {
// if let Some(val) = elements.next() {
// path.push_str(val.as_ref())
// } else {
// return Err(UrlGenerationError::NotEnoughElements);
// }
// }
// }
// }
// }
// };
// Ok(())
// }
/// Build resource path from elements. Returns `true` on success.
pub fn resource_path<U, I>(&self, path: &mut String, elements: &mut U) -> bool
where
U: Iterator<Item = I>,
I: AsRef<str>,
{
match self.tp {
PatternType::Prefix(ref p) => path.push_str(p),
PatternType::Static(ref p) => path.push_str(p),
PatternType::Dynamic(..) => {
for el in &self.elements {
match *el {
PatternElement::Str(ref s) => path.push_str(s),
PatternElement::Var(_) => {
if let Some(val) = elements.next() {
path.push_str(val.as_ref())
} else {
return false;
}
}
}
}
}
};
true
}
fn parse_param(pattern: &str) -> (PatternElement, String, &str) {
const DEFAULT_PATTERN: &str = "[^/]+";
@@ -313,6 +371,14 @@ impl From<String> for ResourceDef {
}
}
pub(crate) fn insert_slash(path: &str) -> String {
let mut path = path.to_owned();
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
path
}
#[cfg(test)]
mod tests {
use super::*;
@@ -330,6 +396,11 @@ mod tests {
assert!(!re.is_match("/name/"));
assert!(!re.is_match("/name~"));
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name/"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
let re = ResourceDef::new("/name/");
assert!(re.is_match("/name/"));
assert!(!re.is_match("/name"));
@@ -404,10 +475,21 @@ mod tests {
assert!(re.is_match("/name1"));
assert!(re.is_match("/name~"));
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name/"), Some(5));
assert_eq!(re.is_prefix_match("/name/test/test"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
let re = ResourceDef::prefix("/name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
let re = ResourceDef::root_prefix("name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
}
#[test]
@@ -417,6 +499,10 @@ mod tests {
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
assert_eq!(re.is_prefix_match("/name/"), Some(6));
assert_eq!(re.is_prefix_match("/name/gs"), Some(6));
assert_eq!(re.is_prefix_match("/name"), None);
let mut path = Path::new("/test2/");
assert!(re.match_path(&mut path));
assert_eq!(&path["name"], "test2");

View File

@@ -1,170 +1,97 @@
use std::collections::HashMap;
use std::rc::Rc;
use crate::resource::ResourceDef;
use crate::{Resource, ResourcePath};
use crate::{Resource, ResourceDef, ResourcePath};
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum ResourceId {
Default,
Normal(u16),
}
pub struct ResourceId(pub u16);
/// Information about current resource
#[derive(Clone, Debug)]
pub struct ResourceInfo {
rmap: Rc<ResourceMap>,
resource: ResourceId,
}
#[derive(Default, Debug)]
pub(crate) struct ResourceMap {
root: Option<ResourceDef>,
named: HashMap<String, ResourceDef>,
patterns: Vec<ResourceDef>,
}
/// Resource router.
pub struct Router<T, U = ()> {
rmap: Rc<ResourceMap>,
named: HashMap<String, ResourceDef>,
resources: Vec<(T, Option<U>)>,
}
pub struct Router<T, U = ()>(Vec<(ResourceDef, T, Option<U>)>);
impl<T, U> Router<T, U> {
pub fn build() -> RouterBuilder<T, U> {
RouterBuilder {
rmap: ResourceMap::default(),
named: HashMap::new(),
resources: Vec::new(),
}
}
pub fn recognize<R: Resource<P>, P: ResourcePath>(
&self,
res: &mut R,
) -> Option<(&T, ResourceInfo)> {
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&self.resources[idx].0, info));
pub fn recognize<R, P>(&self, path: &mut R) -> Option<(&T, ResourceId)>
where
R: Resource<P>,
P: ResourcePath,
{
for item in self.0.iter() {
if item.0.match_path(path.resource_path()) {
return Some((&item.1, ResourceId(item.0.id())));
}
}
None
}
pub fn recognize_mut<R: Resource<P>, P: ResourcePath>(
&mut self,
res: &mut R,
) -> Option<(&mut T, ResourceInfo)> {
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&mut self.resources[idx].0, info));
pub fn recognize_mut<R, P>(&mut self, res: &mut R) -> Option<(&mut T, ResourceId)>
where
R: Resource<P>,
P: ResourcePath,
{
for item in self.0.iter_mut() {
if item.0.match_path(res.resource_path()) {
return Some((&mut item.1, ResourceId(item.0.id())));
}
}
None
}
pub fn recognize_mut_checked<R: Resource<P>, P: ResourcePath, F>(
pub fn recognize_mut_checked<R, P, F>(
&mut self,
res: &mut R,
check: F,
) -> Option<(&mut T, ResourceInfo)>
) -> Option<(&mut T, ResourceId)>
where
F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>,
P: ResourcePath,
{
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) && check(res, &self.resources[idx].1) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&mut self.resources[idx].0, info));
for item in self.0.iter_mut() {
if item.0.match_path(res.resource_path()) && check(res, &item.2) {
return Some((&mut item.1, ResourceId(item.0.id())));
}
}
None
}
}
impl<'a, T, U> IntoIterator for &'a Router<T, U> {
type Item = &'a (T, Option<U>);
type IntoIter = std::slice::Iter<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter()
}
}
impl<'a, T, U> IntoIterator for &'a mut Router<T, U> {
type Item = &'a mut (T, Option<U>);
type IntoIter = std::slice::IterMut<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter_mut()
}
}
impl ResourceMap {
fn register(&mut self, pattern: ResourceDef) {
self.patterns.push(pattern);
}
fn register_named(&mut self, name: String, pattern: ResourceDef) {
self.patterns.push(pattern.clone());
self.named.insert(name, pattern);
}
fn has_resource(&self, path: &str) -> bool {
unimplemented!()
}
}
pub struct RouterBuilder<T, U = ()> {
rmap: ResourceMap,
named: HashMap<String, ResourceDef>,
resources: Vec<(T, Option<U>)>,
resources: Vec<(ResourceDef, T, Option<U>)>,
}
impl<T, U> RouterBuilder<T, U> {
/// Register resource for specified path.
pub fn path(&mut self, path: &str, resource: T) {
self.rmap.register(ResourceDef::new(path));
self.resources.push((resource, None));
pub fn path(&mut self, path: &str, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources
.push((ResourceDef::new(path), resource, None));
self.resources.last_mut().unwrap()
}
/// Register resource for specified path prefix.
pub fn prefix(&mut self, prefix: &str, resource: T) {
self.rmap.register(ResourceDef::prefix(prefix));
self.resources.push((resource, None));
pub fn prefix(&mut self, prefix: &str, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources
.push((ResourceDef::prefix(prefix), resource, None));
self.resources.last_mut().unwrap()
}
/// Register resource for ResourceDef
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) {
self.rmap.register(rdef);
self.resources.push((resource, None));
}
/// Method attachs user data to lastly added resource.
///
/// This panics if no resources were added.
pub fn set_user_data(&mut self, userdata: Option<U>) {
self.resources.last_mut().unwrap().1 = userdata;
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources.push((rdef, resource, None));
self.resources.last_mut().unwrap()
}
/// Finish configuration and create router instance.
pub fn finish(self) -> Router<T, U> {
Router {
rmap: Rc::new(self.rmap),
named: self.named,
resources: self.resources,
}
Router(self.resources)
}
}
@@ -176,14 +103,14 @@ mod tests {
#[test]
fn test_recognizer_1() {
let mut router = Router::<usize>::build();
router.path("/name", 10);
router.path("/name/{val}", 11);
router.path("/name/{val}/index.html", 12);
router.path("/file/{file}.{ext}", 13);
router.path("/v{val}/{val2}/index.html", 14);
router.path("/v/{tail:.*}", 15);
router.path("/test2/{test}.html", 16);
router.path("/{test}/index.html", 17);
router.path("/name", 10).0.set_id(0);
router.path("/name/{val}", 11).0.set_id(1);
router.path("/name/{val}/index.html", 12).0.set_id(2);
router.path("/file/{file}.{ext}", 13).0.set_id(3);
router.path("/v{val}/{val2}/index.html", 14).0.set_id(4);
router.path("/v/{tail:.*}", 15).0.set_id(5);
router.path("/test2/{test}.html", 16).0.set_id(6);
router.path("/{test}/index.html", 17).0.set_id(7);
let mut router = router.finish();
let mut path = Path::new("/unknown");
@@ -192,52 +119,52 @@ mod tests {
let mut path = Path::new("/name");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 10);
assert_eq!(info.resource, ResourceId::Normal(0));
assert_eq!(info, ResourceId(0));
assert!(path.is_empty());
let mut path = Path::new("/name/value");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 11);
assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(info, ResourceId(1));
assert_eq!(path.get("val").unwrap(), "value");
assert_eq!(&path["val"], "value");
let mut path = Path::new("/name/value2/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 12);
assert_eq!(info.resource, ResourceId::Normal(2));
assert_eq!(info, ResourceId(2));
assert_eq!(path.get("val").unwrap(), "value2");
let mut path = Path::new("/file/file.gz");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 13);
assert_eq!(info.resource, ResourceId::Normal(3));
assert_eq!(info, ResourceId(3));
assert_eq!(path.get("file").unwrap(), "file");
assert_eq!(path.get("ext").unwrap(), "gz");
let mut path = Path::new("/vtest/ttt/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 14);
assert_eq!(info.resource, ResourceId::Normal(4));
assert_eq!(info, ResourceId(4));
assert_eq!(path.get("val").unwrap(), "test");
assert_eq!(path.get("val2").unwrap(), "ttt");
let mut path = Path::new("/v/blah-blah/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 15);
assert_eq!(info.resource, ResourceId::Normal(5));
assert_eq!(info, ResourceId(5));
assert_eq!(path.get("tail").unwrap(), "blah-blah/index.html");
let mut path = Path::new("/test2/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 16);
assert_eq!(info.resource, ResourceId::Normal(6));
assert_eq!(info, ResourceId(6));
assert_eq!(path.get("test").unwrap(), "index");
let mut path = Path::new("/bbb/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 17);
assert_eq!(info.resource, ResourceId::Normal(7));
assert_eq!(info, ResourceId(7));
assert_eq!(path.get("test").unwrap(), "bbb");
}
@@ -260,8 +187,8 @@ mod tests {
#[test]
fn test_recognizer_with_prefix() {
let mut router = Router::<usize>::build();
router.path("/name", 10);
router.path("/name/{val}", 11);
router.path("/name", 10).0.set_id(0);
router.path("/name/{val}", 11).0.set_id(1);
let mut router = router.finish();
let mut path = Path::new("/name");
@@ -275,9 +202,9 @@ mod tests {
let mut path = Path::new("/test/name/value");
path.skip(5);
let (h, info) = router.recognize_mut(&mut path).unwrap();
let (h, id) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 11);
assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(id, ResourceId(1));
assert_eq!(path.get("val").unwrap(), "value");
assert_eq!(&path["val"], "value");
@@ -306,134 +233,4 @@ mod tests {
assert_eq!(*h, 11);
assert_eq!(&path["val"], "ttt");
}
// #[test]
// fn test_request_resource() {
// let mut router = Router::<()>::default();
// let mut resource = Resource::new(ResourcePattern::new("/index.json"));
// resource.name("r1");
// router.register_resource(resource);
// let mut resource = Resource::new(ResourcePattern::new("/test.json"));
// resource.name("r2");
// router.register_resource(resource);
// let req = TestRequest::with_uri("/index.json").finish();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(0));
// assert_eq!(info.name(), "r1");
// let req = TestRequest::with_uri("/test.json").finish();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(1));
// assert_eq!(info.name(), "r2");
// }
// #[test]
// fn test_has_resource() {
// let mut router = Router::<()>::default();
// let scope = Scope::new("/test").resource("/name", |_| "done");
// router.register_scope(scope);
// {
// let info = router.default_route_info();
// assert!(!info.has_resource("/test"));
// assert!(info.has_resource("/test/name"));
// }
// let scope = Scope::new("/test2").nested("/test10", |s| s.resource("/name", |_| "done"));
// router.register_scope(scope);
// let info = router.default_route_info();
// assert!(info.has_resource("/test2/test10/name"));
// }
// #[test]
// fn test_url_for() {
// let mut router = Router::<()>::new(ResourcePattern::prefix(""));
// let mut resource = Resource::new(ResourcePattern::new("/tttt"));
// resource.name("r0");
// router.register_resource(resource);
// let scope = Scope::new("/test").resource("/name", |r| {
// r.name("r1");
// });
// router.register_scope(scope);
// let scope =
// Scope::new("/test2").nested("/test10", |s| s.resource("/name", |r| r.name("r2")));
// router.register_scope(scope);
// router.finish();
// let req = TestRequest::with_uri("/test").request();
// {
// let info = router.default_route_info();
// let res = info
// .url_for(&req, "r0", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/tttt");
// let res = info
// .url_for(&req, "r1", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test/name");
// let res = info
// .url_for(&req, "r2", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test2/test10/name");
// }
// let req = TestRequest::with_uri("/test/name").request();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(1));
// let res = info
// .url_for(&req, "r0", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/tttt");
// let res = info
// .url_for(&req, "r1", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test/name");
// let res = info
// .url_for(&req, "r2", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test2/test10/name");
// }
// #[test]
// fn test_url_for_dynamic() {
// let mut router = Router::<()>::new(ResourcePattern::prefix(""));
// let mut resource = Resource::new(ResourcePattern::new("/{name}/test/index.{ext}"));
// resource.name("r0");
// router.register_resource(resource);
// let scope = Scope::new("/{name1}").nested("/{name2}", |s| {
// s.resource("/{name3}/test/index.{ext}", |r| r.name("r2"))
// });
// router.register_scope(scope);
// router.finish();
// let req = TestRequest::with_uri("/test").request();
// {
// let info = router.default_route_info();
// let res = info.url_for(&req, "r0", vec!["sec1", "html"]).unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/sec1/test/index.html");
// let res = info
// .url_for(&req, "r2", vec!["sec1", "sec2", "sec3", "html"])
// .unwrap();
// assert_eq!(
// res.as_str(),
// "http://localhost:8080/sec1/sec2/sec3/test/index.html"
// );
// }
// }
}