1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-19 14:14:41 +01:00
actix-web/actix-http/src/builder.rs

279 lines
9.1 KiB
Rust
Raw Normal View History

2019-03-09 10:39:06 -08:00
use std::marker::PhantomData;
2019-06-28 14:34:26 +06:00
use std::rc::Rc;
2019-12-02 17:33:11 +06:00
use std::{fmt, net};
2019-03-09 10:39:06 -08:00
2019-04-08 14:51:16 -07:00
use actix_codec::Framed;
use actix_service::{IntoServiceFactory, Service, ServiceFactory};
2019-03-09 10:39:06 -08:00
use crate::body::MessageBody;
use crate::config::{KeepAlive, ServiceConfig};
2019-04-05 16:46:44 -07:00
use crate::error::Error;
2019-04-08 14:51:16 -07:00
use crate::h1::{Codec, ExpectHandler, H1Service, UpgradeHandler};
2019-04-05 16:46:44 -07:00
use crate::h2::H2Service;
2019-06-28 14:34:26 +06:00
use crate::helpers::{Data, DataFactory};
2019-03-09 10:39:06 -08:00
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpService;
use crate::{ConnectCallback, Extensions};
2019-03-09 10:39:06 -08:00
/// A HTTP service builder
2019-03-09 10:39:06 -08:00
///
/// This type can be used to construct an instance of [`HttpService`] through a
2019-03-09 10:39:06 -08:00
/// builder-like pattern.
2019-04-08 14:51:16 -07:00
pub struct HttpServiceBuilder<T, S, X = ExpectHandler, U = UpgradeHandler<T>> {
2019-03-09 10:39:06 -08:00
keep_alive: KeepAlive,
client_timeout: u64,
client_disconnect: u64,
2019-12-02 17:33:11 +06:00
secure: bool,
local_addr: Option<net::SocketAddr>,
2019-04-05 16:46:44 -07:00
expect: X,
2019-04-08 14:51:16 -07:00
upgrade: Option<U>,
// DEPRECATED: in favor of on_connect_ext
2019-07-17 15:48:37 +06:00
on_connect: Option<Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2019-03-09 10:39:06 -08:00
_t: PhantomData<(T, S)>,
}
2019-04-08 14:51:16 -07:00
impl<T, S> HttpServiceBuilder<T, S, ExpectHandler, UpgradeHandler<T>>
2019-03-09 10:39:06 -08:00
where
2019-12-02 17:33:11 +06:00
S: ServiceFactory<Config = (), Request = Request>,
2019-11-19 18:54:19 +06:00
S::Error: Into<Error> + 'static,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<S::Service as Service>::Future: 'static,
2019-03-09 10:39:06 -08:00
{
/// Create instance of `ServiceConfigBuilder`
2019-04-05 16:46:44 -07:00
pub fn new() -> Self {
2019-03-09 10:39:06 -08:00
HttpServiceBuilder {
keep_alive: KeepAlive::Timeout(5),
client_timeout: 5000,
client_disconnect: 0,
2019-12-02 17:33:11 +06:00
secure: false,
local_addr: None,
2019-04-05 16:46:44 -07:00
expect: ExpectHandler,
2019-04-08 14:51:16 -07:00
upgrade: None,
2019-06-28 14:34:26 +06:00
on_connect: None,
on_connect_ext: None,
2019-03-09 10:39:06 -08:00
_t: PhantomData,
}
}
2019-04-05 16:46:44 -07:00
}
2019-03-09 10:39:06 -08:00
2019-04-08 14:51:16 -07:00
impl<T, S, X, U> HttpServiceBuilder<T, S, X, U>
2019-04-05 16:46:44 -07:00
where
2019-12-02 17:33:11 +06:00
S: ServiceFactory<Config = (), Request = Request>,
2019-11-19 18:54:19 +06:00
S::Error: Into<Error> + 'static,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<S::Service as Service>::Future: 'static,
2019-12-02 17:33:11 +06:00
X: ServiceFactory<Config = (), Request = Request, Response = Request>,
2019-04-05 16:46:44 -07:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<X::Service as Service>::Future: 'static,
2019-12-02 17:33:11 +06:00
U: ServiceFactory<Config = (), Request = (Request, Framed<T, Codec>), Response = ()>,
2019-04-08 14:51:16 -07:00
U::Error: fmt::Display,
U::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<U::Service as Service>::Future: 'static,
2019-04-05 16:46:44 -07:00
{
2019-03-09 10:39:06 -08:00
/// Set server keep-alive setting.
///
/// By default keep alive is set to a 5 seconds.
2019-04-08 14:51:16 -07:00
pub fn keep_alive<W: Into<KeepAlive>>(mut self, val: W) -> Self {
2019-03-09 10:39:06 -08:00
self.keep_alive = val.into();
self
}
2019-12-02 17:33:11 +06:00
/// Set connection secure state
pub fn secure(mut self) -> Self {
self.secure = true;
self
}
/// Set the local address that this service is bound to.
pub fn local_addr(mut self, addr: net::SocketAddr) -> Self {
self.local_addr = Some(addr);
self
}
2019-03-09 10:39:06 -08:00
/// Set server client timeout in milliseconds for first request.
///
/// Defines a timeout for reading client request header. If a client does not transmit
/// the entire set headers within this time, the request is terminated with
/// the 408 (Request Time-out) error.
///
/// To disable timeout set value to 0.
///
/// By default client timeout is set to 5000 milliseconds.
pub fn client_timeout(mut self, val: u64) -> Self {
self.client_timeout = val;
self
}
/// Set server connection disconnect timeout in milliseconds.
///
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
/// within this time, the request get dropped. This timeout affects secure connections.
///
/// To disable timeout set value to 0.
///
2019-03-09 13:38:56 -08:00
/// By default disconnect timeout is set to 0.
2019-03-09 10:39:06 -08:00
pub fn client_disconnect(mut self, val: u64) -> Self {
self.client_disconnect = val;
self
}
/// Provide service for `EXPECT: 100-Continue` support.
///
/// Service get called with request that contains `EXPECT` header.
/// Service must return request in case of success, in that case
/// request will be forwarded to main service.
2019-04-08 14:51:16 -07:00
pub fn expect<F, X1>(self, expect: F) -> HttpServiceBuilder<T, S, X1, U>
where
F: IntoServiceFactory<X1>,
2019-12-02 17:33:11 +06:00
X1: ServiceFactory<Config = (), Request = Request, Response = Request>,
2019-04-08 14:51:16 -07:00
X1::Error: Into<Error>,
X1::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<X1::Service as Service>::Future: 'static,
{
HttpServiceBuilder {
keep_alive: self.keep_alive,
client_timeout: self.client_timeout,
client_disconnect: self.client_disconnect,
2019-12-02 17:33:11 +06:00
secure: self.secure,
local_addr: self.local_addr,
expect: expect.into_factory(),
2019-04-08 14:51:16 -07:00
upgrade: self.upgrade,
2019-06-28 14:34:26 +06:00
on_connect: self.on_connect,
on_connect_ext: self.on_connect_ext,
_t: PhantomData,
}
}
/// Provide service for custom `Connection: UPGRADE` support.
///
/// If service is provided then normal requests handling get halted
/// and this service get called with original request and framed object.
pub fn upgrade<F, U1>(self, upgrade: F) -> HttpServiceBuilder<T, S, X, U1>
where
F: IntoServiceFactory<U1>,
U1: ServiceFactory<
2019-12-02 17:33:11 +06:00
Config = (),
2019-05-12 08:34:51 -07:00
Request = (Request, Framed<T, Codec>),
Response = (),
>,
U1::Error: fmt::Display,
U1::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
<U1::Service as Service>::Future: 'static,
{
HttpServiceBuilder {
keep_alive: self.keep_alive,
client_timeout: self.client_timeout,
client_disconnect: self.client_disconnect,
2019-12-02 17:33:11 +06:00
secure: self.secure,
local_addr: self.local_addr,
expect: self.expect,
upgrade: Some(upgrade.into_factory()),
2019-06-28 14:34:26 +06:00
on_connect: self.on_connect,
on_connect_ext: self.on_connect_ext,
_t: PhantomData,
}
}
2019-06-28 14:34:26 +06:00
/// Set on-connect callback.
///
/// Called once per connection. Return value of the call is stored in request extensions.
///
/// *SOFT DEPRECATED*: Prefer the `on_connect_ext` style callback.
2019-06-28 14:34:26 +06:00
pub fn on_connect<F, I>(mut self, f: F) -> Self
where
F: Fn(&T) -> I + 'static,
I: Clone + 'static,
{
self.on_connect = Some(Rc::new(move |io| Box::new(Data(f(io)))));
self
}
/// Sets the callback to be run on connection establishment.
///
/// Has mutable access to a data container that will be merged into request extensions.
/// This enables transport layer data (like client certificates) to be accessed in middleware
/// and handlers.
pub fn on_connect_ext<F>(mut self, f: F) -> Self
where
F: Fn(&T, &mut Extensions) + 'static,
{
self.on_connect_ext = Some(Rc::new(f));
self
}
/// Finish service configuration and create a HTTP Service for HTTP/1 protocol.
2019-12-02 17:33:11 +06:00
pub fn h1<F, B>(self, service: F) -> H1Service<T, S, B, X, U>
2019-03-09 10:39:06 -08:00
where
2019-11-20 23:33:22 +06:00
B: MessageBody,
F: IntoServiceFactory<S>,
2019-11-20 23:33:22 +06:00
S::Error: Into<Error>,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2019-11-20 23:33:22 +06:00
S::Response: Into<Response<B>>,
2019-03-09 10:39:06 -08:00
{
let cfg = ServiceConfig::new(
self.keep_alive,
self.client_timeout,
self.client_disconnect,
2019-12-02 17:33:11 +06:00
self.secure,
self.local_addr,
2019-03-09 10:39:06 -08:00
);
H1Service::with_config(cfg, service.into_factory())
2019-04-08 14:51:16 -07:00
.expect(self.expect)
.upgrade(self.upgrade)
2019-06-28 14:34:26 +06:00
.on_connect(self.on_connect)
.on_connect_ext(self.on_connect_ext)
2019-03-09 10:39:06 -08:00
}
/// Finish service configuration and create a HTTP service for HTTP/2 protocol.
2019-12-02 17:33:11 +06:00
pub fn h2<F, B>(self, service: F) -> H2Service<T, S, B>
2019-03-09 10:39:06 -08:00
where
B: MessageBody + 'static,
F: IntoServiceFactory<S>,
2019-11-19 18:54:19 +06:00
S::Error: Into<Error> + 'static,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service>::Future: 'static,
2019-03-09 10:39:06 -08:00
{
let cfg = ServiceConfig::new(
self.keep_alive,
self.client_timeout,
self.client_disconnect,
2019-12-02 17:33:11 +06:00
self.secure,
self.local_addr,
2019-03-09 10:39:06 -08:00
);
H2Service::with_config(cfg, service.into_factory())
.on_connect(self.on_connect)
.on_connect_ext(self.on_connect_ext)
2019-03-09 10:39:06 -08:00
}
/// Finish service configuration and create `HttpService` instance.
2019-12-02 17:33:11 +06:00
pub fn finish<F, B>(self, service: F) -> HttpService<T, S, B, X, U>
2019-03-09 10:39:06 -08:00
where
B: MessageBody + 'static,
F: IntoServiceFactory<S>,
2019-11-19 18:54:19 +06:00
S::Error: Into<Error> + 'static,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2019-11-19 18:54:19 +06:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service>::Future: 'static,
2019-03-09 10:39:06 -08:00
{
let cfg = ServiceConfig::new(
self.keep_alive,
self.client_timeout,
self.client_disconnect,
2019-12-02 17:33:11 +06:00
self.secure,
self.local_addr,
2019-03-09 10:39:06 -08:00
);
HttpService::with_config(cfg, service.into_factory())
2019-04-08 14:51:16 -07:00
.expect(self.expect)
.upgrade(self.upgrade)
2019-06-28 14:34:26 +06:00
.on_connect(self.on_connect)
.on_connect_ext(self.on_connect_ext)
2019-03-09 10:39:06 -08:00
}
}