mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-12 03:01:35 +02:00
Compare commits
10 Commits
connect-v0
...
router-v0.
Author | SHA1 | Date | |
---|---|---|---|
|
059e2ad042 | ||
|
fdf2a6f422 | ||
|
fc2631c852 | ||
|
d51b210ae7 | ||
|
0a6cded975 | ||
|
14e3933d8b | ||
|
837504c10f | ||
|
c7676df697 | ||
|
ecf7a11a20 | ||
|
686958fe0c |
@@ -29,10 +29,10 @@ members = [
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
actix-service = "0.3.3"
|
||||
actix-service = "0.4.0"
|
||||
actix-codec = "0.1.1"
|
||||
actix-rt = "0.2.0"
|
||||
actix-server = { path="actix-server", features=["ssl"] }
|
||||
actix-server = { version="0.5.0", features=["ssl"] }
|
||||
env_logger = "0.6"
|
||||
futures = "0.1.25"
|
||||
openssl = "0.10"
|
||||
|
@@ -1,4 +1,60 @@
|
||||
//! Utilities to ease interoperability with services based on the `tower-service` crate.
|
||||
//! Utilities to provide interoperability between services based on the
|
||||
//! `actix-service` and `tower-service` crates.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! In the following example, we take a `RandomService`—which will always
|
||||
//! return 4—and wraps it with a middleware that will always add 1 to the
|
||||
//! result. This pattern can be further used to wrap services from either
|
||||
//! `tower-service` or `actix-service` with middleware provided by the other.
|
||||
//!
|
||||
//! ```
|
||||
//! use actix_tower::ActixServiceExt;
|
||||
//! # use futures::{Async, Future};
|
||||
//! use actix_service::Service;
|
||||
//!
|
||||
//! struct RandomService;
|
||||
//! impl Service for RandomService {
|
||||
//! // …
|
||||
//! # type Request = ();
|
||||
//! # type Response = u32;
|
||||
//! # type Error = ();
|
||||
//! # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
//! #
|
||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
//! # Ok(Async::Ready(()))
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
//! # futures::finished(4)
|
||||
//! # }
|
||||
//! }
|
||||
//!
|
||||
//! struct AddOneMiddleware<S>(S);
|
||||
//! impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
||||
//! where
|
||||
//! S: tower_service::Service<R, Response = u32>,
|
||||
//! S::Future: 'static,
|
||||
//! {
|
||||
//! /// …
|
||||
//! # type Response = u32;
|
||||
//! # type Error = S::Error;
|
||||
//! # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
//! #
|
||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
//! # self.0.poll_ready()
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn call(&mut self, req: R) -> Self::Future {
|
||||
//! # let fut = self.0.call(req).map(|x| x + 1);
|
||||
//! # Box::new(fut)
|
||||
//! # }
|
||||
//! }
|
||||
//!
|
||||
//! let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
||||
//! assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
//! assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
//! ```
|
||||
|
||||
use actix_service::Service as ActixService;
|
||||
use std::marker::PhantomData;
|
||||
@@ -6,40 +62,123 @@ use tower_service::Service as TowerService;
|
||||
|
||||
/// Compatibility wrapper associating a `tower_service::Service` with a particular
|
||||
/// `Request` type, so that it can be used as an `actix_service::Service`.
|
||||
pub struct TowerCompat<S, R> {
|
||||
///
|
||||
/// Generally created through convenience methods on the `TowerServiceExt<R>` trait.
|
||||
pub struct ActixCompat<S, R> {
|
||||
inner: S,
|
||||
_phantom: PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<S, R> TowerCompat<S, R> {
|
||||
impl<S, R> ActixCompat<S, R> {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
pub fn new(inner: S) -> Self {
|
||||
TowerCompat {
|
||||
ActixCompat {
|
||||
inner,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for wrapping `tower_service::Service` instances for use as
|
||||
/// Extension trait for wrapping a `tower_service::Service` instance for use as
|
||||
/// an `actix_service::Service`.
|
||||
pub trait TowerServiceExt {
|
||||
pub trait TowerServiceExt<R> : TowerService<R> + Sized {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
fn compat<R>(self) -> TowerCompat<Self, R>
|
||||
where
|
||||
Self: TowerService<R> + Sized;
|
||||
}
|
||||
///
|
||||
/// ```
|
||||
/// use actix_service::Service;
|
||||
/// use actix_tower::TowerServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl<R> tower_service::Service<R> for RandomService {
|
||||
/// // …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.into_actix_service();
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
/// ```
|
||||
fn into_actix_service(self) -> ActixCompat<Self, R> {
|
||||
ActixCompat::new(self)
|
||||
}
|
||||
|
||||
impl<S> TowerServiceExt for S {
|
||||
fn compat<R>(self) -> TowerCompat<Self, R>
|
||||
/// Takes a function that, when provided with an `actix_service::Service` wraps it
|
||||
/// and returns a new service. Useful for wrapping a `tower_service::Service` with
|
||||
/// middleware built for `actix_service`.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::TowerServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use tower_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl<R> Service<R> for RandomService {
|
||||
/// // …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// struct AddOneTransform<S>(S);
|
||||
/// impl<S> actix_service::Service for AddOneTransform<S>
|
||||
/// where
|
||||
/// S: actix_service::Service<Response = u32>,
|
||||
/// S::Future: 'static,
|
||||
/// {
|
||||
/// /// …
|
||||
/// # type Request = S::Request;
|
||||
/// # type Response = u32;
|
||||
/// # type Error = S::Error;
|
||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # self.0.poll_ready()
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
||||
/// # Box::new(fut)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.wrap_with_actix_middleware(AddOneTransform);
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
/// ```
|
||||
fn wrap_with_actix_middleware<F, U>(self, f: F) -> TowerCompat<U>
|
||||
where
|
||||
Self: TowerService<R> + Sized
|
||||
F: FnOnce(ActixCompat<Self, R>) -> U,
|
||||
U: ActixService<Request = R>
|
||||
{
|
||||
TowerCompat::new(self)
|
||||
f(self.into_actix_service()).into_tower_service()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, R> ActixService for TowerCompat<S, R>
|
||||
impl<S, R> TowerServiceExt<R> for S
|
||||
where
|
||||
S: TowerService<R> + Sized
|
||||
{}
|
||||
|
||||
impl<S, R> ActixService for ActixCompat<S, R>
|
||||
where
|
||||
S: TowerService<R>,
|
||||
{
|
||||
@@ -57,135 +196,392 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Compatibility wrapper associating an `actix_service::Service` with a particular
|
||||
/// `Request` type, so that it can be used as a `tower_service::Service`.
|
||||
///
|
||||
/// Generally created through convenience methods on the `ActixServiceExt<R>` trait.
|
||||
pub struct TowerCompat<S> {
|
||||
inner: S,
|
||||
}
|
||||
|
||||
impl<S> TowerCompat<S> {
|
||||
/// Wraps an `actix_service::Service` in a compatibility wrapper.
|
||||
pub fn new(inner: S) -> Self {
|
||||
TowerCompat {
|
||||
inner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for wrapping an `actix_service::Service` instance for use as
|
||||
/// a `tower_service::Service`.
|
||||
pub trait ActixServiceExt: ActixService + Sized {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::ActixServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use tower_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl actix_service::Service for RandomService {
|
||||
/// // …
|
||||
/// # type Request = ();
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.into_tower_service();
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
/// ```
|
||||
fn into_tower_service(self) -> TowerCompat<Self> {
|
||||
TowerCompat::new(self)
|
||||
}
|
||||
|
||||
/// Takes a function that, when provided with a `tower_service::Service` wraps it
|
||||
/// and returns a new service. Useful for wrapping an `actix_service::Service` with
|
||||
/// middleware built for `tower_service`.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::ActixServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use actix_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl Service for RandomService {
|
||||
/// // …
|
||||
/// # type Request = ();
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// struct AddOneMiddleware<S>(S);
|
||||
/// impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
||||
/// where
|
||||
/// S: tower_service::Service<R, Response = u32>,
|
||||
/// S::Future: 'static,
|
||||
/// {
|
||||
/// /// …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = S::Error;
|
||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # self.0.poll_ready()
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, req: R) -> Self::Future {
|
||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
||||
/// # Box::new(fut)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
/// ```
|
||||
fn wrap_with_tower_middleware<F, U>(self, f: F) -> ActixCompat<U, Self::Request>
|
||||
where
|
||||
F: FnOnce(TowerCompat<Self>) -> U,
|
||||
U: TowerService<Self::Request>
|
||||
{
|
||||
f(self.into_tower_service()).into_actix_service()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ActixServiceExt for S
|
||||
where
|
||||
S: ActixService + Sized
|
||||
{}
|
||||
|
||||
impl<S> TowerService<S::Request> for TowerCompat<S>
|
||||
where
|
||||
S: ActixService,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
ActixService::poll_ready(&mut self.inner)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: S::Request) -> Self::Future {
|
||||
ActixService::call(&mut self.inner, req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::TowerServiceExt;
|
||||
use actix_service::{Service as ActixService, ServiceExt, Transform};
|
||||
use futures::{future::FutureResult, Async, Poll, Future};
|
||||
use tower_service::Service as TowerService;
|
||||
mod tower_service_into_actix_service {
|
||||
use crate::TowerServiceExt;
|
||||
use actix_service::{Service as ActixService, ServiceExt, Transform};
|
||||
use futures::{future::FutureResult, Async, Poll, Future};
|
||||
use tower_service::Service as TowerService;
|
||||
|
||||
struct RandomService;
|
||||
impl<R> TowerService<R> for RandomService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
#[test]
|
||||
fn random_service_returns_4() {
|
||||
let mut s = RandomService.into_actix_service();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, _req: R) -> Self::Future {
|
||||
futures::finished(4)
|
||||
#[test]
|
||||
fn random_service_can_combine() {
|
||||
let mut s = RandomService.into_actix_service().map(|x| x + 1);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_can_use_actix_middleware() {
|
||||
let mut s = RandomService.wrap_with_actix_middleware(DoMathTransform);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_and_add_service_chained() {
|
||||
let s1 = RandomService.into_actix_service();
|
||||
let s2 = AddOneService.into_actix_service();
|
||||
let s3 = AddOneService.into_actix_service();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(6)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
||||
let s1 = RandomService.into_actix_service();
|
||||
let s2 = AddOneService.into_actix_service();
|
||||
let s3 = AddOneService.into_actix_service();
|
||||
let s4 = RandomService.into_actix_service();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3).and_then(s4);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_can_be_transformed_to_do_math() {
|
||||
let transform = DoMath;
|
||||
|
||||
let mut s = transform.new_transform(RandomService.into_actix_service()).wait().unwrap();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct RandomService;
|
||||
impl<R> TowerService<R> for RandomService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _req: R) -> Self::Future {
|
||||
futures::finished(4)
|
||||
}
|
||||
}
|
||||
|
||||
struct AddOneService;
|
||||
impl TowerService<u32> for AddOneService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: u32) -> Self::Future {
|
||||
futures::finished(req + 1)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMathTransform<S>(S);
|
||||
impl<S> ActixService for DoMathTransform<S>
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.0.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
let fut = self.0.call(req).map(|x| x * 17);
|
||||
Box::new(fut)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMath;
|
||||
impl<S> Transform<S> for DoMath
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Transform = DoMathTransform<S>;
|
||||
type InitError = ();
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
futures::finished(DoMathTransform(service))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_returns_4() {
|
||||
let mut s = RandomService.compat();
|
||||
mod actix_service_into_tower_service {
|
||||
use crate::{ActixServiceExt, TowerServiceExt};
|
||||
use actix_service::{Service as ActixService, ServiceExt};
|
||||
use futures::{future::FutureResult, Async, Poll, Future};
|
||||
use tower_service::Service as TowerService;
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
#[test]
|
||||
fn random_service_returns_4() {
|
||||
let mut s = RandomService.into_tower_service();
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_can_combine() {
|
||||
let mut s = RandomService.compat().map(|x| x + 1);
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct AddOneService;
|
||||
impl TowerService<u32> for AddOneService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, req: u32) -> Self::Future {
|
||||
futures::finished(req + 1)
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn random_service_can_use_tower_middleware() {
|
||||
let mut s = AddOneService::wrap(RandomService.into_tower_service()).into_actix_service();
|
||||
|
||||
#[test]
|
||||
fn tower_services_as_actix_services_chained() {
|
||||
let s1 = RandomService.compat();
|
||||
let s2 = AddOneService.compat();
|
||||
let s3 = AddOneService.compat();
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(6)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_services_as_actix_services_chained_2() {
|
||||
let s1 = RandomService.compat();
|
||||
let s2 = AddOneService.compat();
|
||||
let s3 = AddOneService.compat();
|
||||
let s4 = RandomService.compat();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3).and_then(s4);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct DoMathTransform<S>(S);
|
||||
impl<S> ActixService for DoMathTransform<S>
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.0.poll_ready()
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
let fut = self.0.call(req).map(|x| x * 17);
|
||||
Box::new(fut)
|
||||
#[test]
|
||||
fn do_math_service_can_use_tower_middleware() {
|
||||
let mut s = AddOneService::wrap(DoMathService.into_tower_service()).into_actix_service();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(188)), s.call(11).poll());
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMath;
|
||||
impl<S> Transform<S> for DoMath
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Transform = DoMathTransform<S>;
|
||||
type InitError = ();
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
#[test]
|
||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
||||
let s1 = RandomService.wrap_with_tower_middleware(AddOneService::wrap);
|
||||
let s2 = DoMathService.wrap_with_tower_middleware(AddOneService::wrap);
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
futures::finished(DoMathTransform(service))
|
||||
let mut s = s1.and_then(s2);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(86)), s.call(()).poll());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_can_be_transformed() {
|
||||
let transform = DoMath;
|
||||
struct RandomService;
|
||||
impl ActixService for RandomService {
|
||||
type Request = ();
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
let mut s = transform.new_transform(RandomService.compat()).wait().unwrap();
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
futures::finished(4)
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
struct AddOneService<S> {
|
||||
inner: S
|
||||
}
|
||||
|
||||
impl<S> AddOneService<S> {
|
||||
fn wrap(inner: S) -> Self {
|
||||
AddOneService {
|
||||
inner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, R> TowerService<R> for AddOneService<S>
|
||||
where
|
||||
S: TowerService<R, Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.inner.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: R) -> Self::Future {
|
||||
let fut = self.inner.call(req)
|
||||
.map(|x| x + 1);
|
||||
|
||||
Box::new(fut)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMathService;
|
||||
impl ActixService for DoMathService {
|
||||
type Request = u32;
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
futures::finished(req * 17)
|
||||
}
|
||||
}}
|
||||
}
|
@@ -1,5 +1,12 @@
|
||||
# Changes
|
||||
|
||||
##[0.4.1] - 2019-05-15
|
||||
|
||||
### Changed
|
||||
|
||||
* Change `Either` constructor
|
||||
|
||||
|
||||
## [0.4.0] - 2019-05-11
|
||||
|
||||
### Changed
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-utils"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix utils - various actix net related services"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
@@ -57,25 +57,21 @@ pub struct Either<A, B> {
|
||||
}
|
||||
|
||||
impl<A, B> Either<A, B> {
|
||||
pub fn new_a<F>(srv: F) -> Either<A, ()>
|
||||
pub fn new<F1, F2>(srv_a: F1, srv_b: F2) -> Either<A, B>
|
||||
where
|
||||
A: NewService,
|
||||
F: IntoNewService<A>,
|
||||
B: NewService<
|
||||
Config = A::Config,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
InitError = A::InitError,
|
||||
>,
|
||||
F1: IntoNewService<A>,
|
||||
F2: IntoNewService<B>,
|
||||
{
|
||||
Either {
|
||||
left: srv.into_new_service(),
|
||||
right: (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_b<F>(srv: F) -> Either<(), B>
|
||||
where
|
||||
B: NewService,
|
||||
F: IntoNewService<B>,
|
||||
{
|
||||
Either {
|
||||
left: (),
|
||||
right: srv.into_new_service(),
|
||||
left: srv_a.into_new_service(),
|
||||
right: srv_b.into_new_service(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,9 @@
|
||||
# Changes
|
||||
|
||||
## [0.1.4] - 2019-05-15
|
||||
|
||||
* Fix checked resource match
|
||||
|
||||
## [0.1.3] - 2019-04-22
|
||||
|
||||
* Added support for `remainder match` (i.e "/path/{tail}*")
|
||||
|
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "actix-router"
|
||||
version = "0.1.3"
|
||||
version = "0.1.4"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Path table router"
|
||||
description = "Path router"
|
||||
keywords = ["actix"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-net.git"
|
||||
|
@@ -5,7 +5,7 @@ use std::rc::Rc;
|
||||
use regex::{escape, Regex};
|
||||
|
||||
use crate::path::{Path, PathItem};
|
||||
use crate::ResourcePath;
|
||||
use crate::{Resource, ResourcePath};
|
||||
|
||||
const MAX_DYNAMIC_SEGMENTS: usize = 16;
|
||||
|
||||
@@ -240,6 +240,90 @@ impl ResourceDef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Is the given path and parameters a match against this pattern?
|
||||
pub fn match_path_checked<R, T, F, U>(
|
||||
&self,
|
||||
res: &mut R,
|
||||
check: &F,
|
||||
user_data: &Option<U>,
|
||||
) -> bool
|
||||
where
|
||||
T: ResourcePath,
|
||||
R: Resource<T>,
|
||||
F: Fn(&R, &Option<U>) -> bool,
|
||||
{
|
||||
match self.tp {
|
||||
PatternType::Static(ref s) => {
|
||||
if s == res.resource_path().path() && check(res, user_data) {
|
||||
let path = res.resource_path();
|
||||
path.skip(path.len() as u16);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
PatternType::Dynamic(ref re, ref names, len) => {
|
||||
let mut idx = 0;
|
||||
let mut pos = 0;
|
||||
let mut segments: [PathItem; MAX_DYNAMIC_SEGMENTS] =
|
||||
[PathItem::Static(""); MAX_DYNAMIC_SEGMENTS];
|
||||
|
||||
if let Some(captures) = re.captures(res.resource_path().path()) {
|
||||
for (no, name) in names.iter().enumerate() {
|
||||
if let Some(m) = captures.name(&name) {
|
||||
idx += 1;
|
||||
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 {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !check(res, user_data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let path = res.resource_path();
|
||||
for idx in 0..idx {
|
||||
path.add(names[idx].clone(), segments[idx]);
|
||||
}
|
||||
path.skip((pos + len) as u16);
|
||||
true
|
||||
}
|
||||
PatternType::Prefix(ref s) => {
|
||||
let len = {
|
||||
let rpath = res.resource_path().path();
|
||||
if s == rpath {
|
||||
s.len()
|
||||
} else if rpath.starts_with(s)
|
||||
&& (s.ends_with('/') || rpath.split_at(s.len()).1.starts_with('/'))
|
||||
{
|
||||
if s.ends_with('/') {
|
||||
s.len() - 1
|
||||
} else {
|
||||
s.len()
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if !check(res, user_data) {
|
||||
return false;
|
||||
}
|
||||
let path = res.resource_path();
|
||||
path.skip(min(path.path().len(), len) as u16);
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build resource path from elements. Returns `true` on success.
|
||||
pub fn resource_path<U, I>(&self, path: &mut String, elements: &mut U) -> bool
|
||||
where
|
||||
|
@@ -19,26 +19,26 @@ impl<T, U> Router<T, U> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recognize<R, P>(&self, path: &mut R) -> Option<(&T, ResourceId)>
|
||||
pub fn recognize<R, P>(&self, reosurce: &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()) {
|
||||
if item.0.match_path(reosurce.resource_path()) {
|
||||
return Some((&item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn recognize_mut<R, P>(&mut self, res: &mut R) -> Option<(&mut T, ResourceId)>
|
||||
pub fn recognize_mut<R, P>(&mut self, reosurce: &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()) {
|
||||
if item.0.match_path(reosurce.resource_path()) {
|
||||
return Some((&mut item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ impl<T, U> Router<T, U> {
|
||||
|
||||
pub fn recognize_mut_checked<R, P, F>(
|
||||
&mut self,
|
||||
res: &mut R,
|
||||
resource: &mut R,
|
||||
check: F,
|
||||
) -> Option<(&mut T, ResourceId)>
|
||||
where
|
||||
@@ -55,8 +55,11 @@ impl<T, U> Router<T, U> {
|
||||
R: Resource<P>,
|
||||
P: ResourcePath,
|
||||
{
|
||||
println!("router ==== {:?}", self.0.len());
|
||||
for item in self.0.iter_mut() {
|
||||
if item.0.match_path(res.resource_path()) && check(res, &item.2) {
|
||||
println!("1");
|
||||
if item.0.match_path_checked(resource, &check, &item.2) {
|
||||
println!("lll");
|
||||
return Some((&mut item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user