1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/actix-framed/src/app.rs

218 lines
6.0 KiB
Rust
Raw Normal View History

2019-04-11 00:06:27 +02:00
use std::rc::Rc;
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::h1::{Codec, SendResponse};
use actix_http::{Error, Request, Response};
use actix_router::{Path, Router, Url};
2019-05-12 17:34:51 +02:00
use actix_server_config::ServerConfig;
2019-04-11 00:06:27 +02:00
use actix_service::{IntoNewService, NewService, Service};
use futures::{Async, Future, Poll};
use crate::helpers::{BoxedHttpNewService, BoxedHttpService, HttpNewService};
use crate::request::FramedRequest;
use crate::state::State;
type BoxedResponse = Box<dyn Future<Item = (), Error = Error>>;
2019-04-11 00:06:27 +02:00
pub trait HttpServiceFactory {
type Factory: NewService;
fn path(&self) -> &str;
fn create(self) -> Self::Factory;
}
/// Application builder
2019-04-11 03:08:28 +02:00
pub struct FramedApp<T, S = ()> {
2019-04-11 00:06:27 +02:00
state: State<S>,
services: Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>,
}
2019-04-11 03:08:28 +02:00
impl<T: 'static> FramedApp<T, ()> {
2019-04-11 00:06:27 +02:00
pub fn new() -> Self {
2019-04-11 03:08:28 +02:00
FramedApp {
2019-04-11 00:06:27 +02:00
state: State::new(()),
services: Vec::new(),
}
}
}
2019-04-11 03:08:28 +02:00
impl<T: 'static, S: 'static> FramedApp<T, S> {
pub fn with(state: S) -> FramedApp<T, S> {
FramedApp {
2019-04-11 00:06:27 +02:00
services: Vec::new(),
state: State::new(state),
}
}
pub fn service<U>(mut self, factory: U) -> Self
where
U: HttpServiceFactory,
U::Factory: NewService<
2019-05-12 17:34:51 +02:00
Config = (),
2019-04-11 00:06:27 +02:00
Request = FramedRequest<T, S>,
Response = (),
Error = Error,
InitError = (),
> + 'static,
<U::Factory as NewService>::Future: 'static,
<U::Factory as NewService>::Service: Service<
Request = FramedRequest<T, S>,
Response = (),
Error = Error,
Future = Box<dyn Future<Item = (), Error = Error>>,
2019-04-11 00:06:27 +02:00
>,
{
let path = factory.path().to_string();
self.services
.push((path, Box::new(HttpNewService::new(factory.create()))));
self
}
}
2019-04-11 03:08:28 +02:00
impl<T, S> IntoNewService<FramedAppFactory<T, S>> for FramedApp<T, S>
2019-04-11 00:06:27 +02:00
where
T: AsyncRead + AsyncWrite + 'static,
S: 'static,
{
2019-04-11 03:08:28 +02:00
fn into_new_service(self) -> FramedAppFactory<T, S> {
FramedAppFactory {
2019-04-11 00:06:27 +02:00
state: self.state,
services: Rc::new(self.services),
}
}
}
#[derive(Clone)]
2019-04-11 03:08:28 +02:00
pub struct FramedAppFactory<T, S> {
2019-04-11 00:06:27 +02:00
state: State<S>,
services: Rc<Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>>,
}
2019-05-12 17:34:51 +02:00
impl<T, S> NewService for FramedAppFactory<T, S>
2019-04-11 00:06:27 +02:00
where
T: AsyncRead + AsyncWrite + 'static,
S: 'static,
{
2019-05-12 17:34:51 +02:00
type Config = ServerConfig;
2019-04-11 00:06:27 +02:00
type Request = (Request, Framed<T, Codec>);
type Response = ();
type Error = Error;
type InitError = ();
2019-07-20 06:50:36 +02:00
type Service = FramedAppService<T, S>;
2019-04-11 00:06:27 +02:00
type Future = CreateService<T, S>;
2019-05-12 17:34:51 +02:00
fn new_service(&self, _: &ServerConfig) -> Self::Future {
2019-04-11 00:06:27 +02:00
CreateService {
fut: self
.services
.iter()
.map(|(path, service)| {
CreateServiceItem::Future(
Some(path.clone()),
service.new_service(&()),
)
})
.collect(),
state: self.state.clone(),
}
}
}
#[doc(hidden)]
pub struct CreateService<T, S> {
fut: Vec<CreateServiceItem<T, S>>,
state: State<S>,
}
enum CreateServiceItem<T, S> {
Future(
Option<String>,
Box<dyn Future<Item = BoxedHttpService<FramedRequest<T, S>>, Error = ()>>,
2019-04-11 00:06:27 +02:00
),
Service(String, BoxedHttpService<FramedRequest<T, S>>),
}
impl<S: 'static, T: 'static> Future for CreateService<T, S>
where
T: AsyncRead + AsyncWrite,
{
2019-07-20 06:50:36 +02:00
type Item = FramedAppService<T, S>;
2019-04-11 00:06:27 +02:00
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut done = true;
// poll http services
for item in &mut self.fut {
let res = match item {
CreateServiceItem::Future(ref mut path, ref mut fut) => {
match fut.poll()? {
Async::Ready(service) => Some((path.take().unwrap(), service)),
Async::NotReady => {
done = false;
None
}
}
}
CreateServiceItem::Service(_, _) => continue,
};
if let Some((path, service)) = res {
*item = CreateServiceItem::Service(path, service);
}
}
if done {
let router = self
.fut
.drain(..)
.fold(Router::build(), |mut router, item| {
match item {
CreateServiceItem::Service(path, service) => {
router.path(&path, service);
}
CreateServiceItem::Future(_, _) => unreachable!(),
}
router
});
2019-07-20 06:50:36 +02:00
Ok(Async::Ready(FramedAppService {
2019-04-11 00:06:27 +02:00
router: router.finish(),
state: self.state.clone(),
2019-07-20 06:50:36 +02:00
}))
2019-04-11 00:06:27 +02:00
} else {
Ok(Async::NotReady)
}
}
}
2019-04-11 03:08:28 +02:00
pub struct FramedAppService<T, S> {
2019-04-11 00:06:27 +02:00
state: State<S>,
router: Router<BoxedHttpService<FramedRequest<T, S>>>,
}
2019-04-11 03:08:28 +02:00
impl<S: 'static, T: 'static> Service for FramedAppService<T, S>
2019-04-11 00:06:27 +02:00
where
T: AsyncRead + AsyncWrite,
{
type Request = (Request, Framed<T, Codec>);
type Response = ();
type Error = Error;
type Future = BoxedResponse;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, (req, framed): (Request, Framed<T, Codec>)) -> Self::Future {
let mut path = Path::new(Url::new(req.uri().clone()));
if let Some((srv, _info)) = self.router.recognize_mut(&mut path) {
2019-04-11 04:57:34 +02:00
return srv.call(FramedRequest::new(req, framed, path, self.state.clone()));
2019-04-11 00:06:27 +02:00
}
Box::new(
SendResponse::new(framed, Response::NotFound().finish()).then(|_| Ok(())),
)
}
}