Struct actix_http::HttpService
source · pub struct HttpService<T, S, B, X = ExpectHandler, U = UpgradeHandler> { /* private fields */ }
Expand description
A [ServiceFactory
] for HTTP/1.1 and HTTP/2 connections.
Use build
to begin constructing service. Also see HttpServiceBuilder
.
§Automatic HTTP Version Selection
There are two ways to select the HTTP version of an incoming connection:
- One is to rely on the ALPN information that is provided when using TLS (HTTPS); both versions
are supported automatically when using either of the
.rustls()
or.openssl()
finalizing methods. - The other is to read the first few bytes of the TCP stream. This is the only viable approach
for supporting H2C, which allows the HTTP/2 protocol to work over plaintext connections. Use
the
.tcp_auto_h2c()
finalizing method to enable this behavior.
§Examples
use actix_http::{HttpService, Request, Response, StatusCode};
// this service would constructed in an actix_server::Server
HttpService::build()
// the builder finalizing method, other finalizers would not return an `HttpService`
.finish(|_req: Request| async move {
Ok::<_, Infallible>(
Response::build(StatusCode::OK).body("Hello!")
)
})
// the service finalizing method method
// you can use `.tcp_auto_h2c()`, `.rustls()`, or `.openssl()` instead of `.tcp()`
.tcp();
Implementations§
source§impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn openssl(
self,
acceptor: SslAcceptor
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<SslError, DispatchError>, InitError = ()>
Available on crate feature openssl
only.
pub fn openssl( self, acceptor: SslAcceptor ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<SslError, DispatchError>, InitError = ()>
openssl
only.Create OpenSSL based service.
sourcepub fn openssl_with_config(
self,
acceptor: SslAcceptor,
tls_acceptor_config: TlsAcceptorConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<SslError, DispatchError>, InitError = ()>
Available on crate feature openssl
only.
pub fn openssl_with_config( self, acceptor: SslAcceptor, tls_acceptor_config: TlsAcceptorConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<SslError, DispatchError>, InitError = ()>
openssl
only.Create OpenSSL based service with custom TLS acceptor configuration.
source§impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn rustls(
self,
config: ServerConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_20
only.
pub fn rustls( self, config: ServerConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_20
only.Create Rustls v0.20 based service.
sourcepub fn rustls_with_config(
self,
config: ServerConfig,
tls_acceptor_config: TlsAcceptorConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_20
only.
pub fn rustls_with_config( self, config: ServerConfig, tls_acceptor_config: TlsAcceptorConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_20
only.Create Rustls v0.20 based service with custom TLS acceptor configuration.
source§impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn rustls_021(
self,
config: ServerConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_21
only.
pub fn rustls_021( self, config: ServerConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_21
only.Create Rustls v0.21 based service.
sourcepub fn rustls_021_with_config(
self,
config: ServerConfig,
tls_acceptor_config: TlsAcceptorConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_21
only.
pub fn rustls_021_with_config( self, config: ServerConfig, tls_acceptor_config: TlsAcceptorConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_21
only.Create Rustls v0.21 based service with custom TLS acceptor configuration.
source§impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn rustls_0_22(
self,
config: ServerConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_22
only.
pub fn rustls_0_22( self, config: ServerConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_22
only.Create Rustls v0.22 based service.
sourcepub fn rustls_0_22_with_config(
self,
config: ServerConfig,
tls_acceptor_config: TlsAcceptorConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_22
only.
pub fn rustls_0_22_with_config( self, config: ServerConfig, tls_acceptor_config: TlsAcceptorConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_22
only.Create Rustls v0.22 based service with custom TLS acceptor configuration.
source§impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TlsStream<TcpStream>, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn rustls_0_23(
self,
config: ServerConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_23
only.
pub fn rustls_0_23( self, config: ServerConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_23
only.Create Rustls v0.23 based service.
sourcepub fn rustls_0_23_with_config(
self,
config: ServerConfig,
tls_acceptor_config: TlsAcceptorConfig
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
Available on crate feature rustls-0_23
only.
pub fn rustls_0_23_with_config( self, config: ServerConfig, tls_acceptor_config: TlsAcceptorConfig ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = TlsError<Error, DispatchError>, InitError = ()>
rustls-0_23
only.Create Rustls v0.23 based service with custom TLS acceptor configuration.
source§impl<T, S, B> HttpService<T, S, B>
impl<T, S, B> HttpService<T, S, B>
sourcepub fn build() -> HttpServiceBuilder<T, S>
pub fn build() -> HttpServiceBuilder<T, S>
Constructs builder for HttpService
instance.
source§impl<T, S, B> HttpService<T, S, B>
impl<T, S, B> HttpService<T, S, B>
source§impl<T, S, B, X, U> HttpService<T, S, B, X, U>
impl<T, S, B, X, U> HttpService<T, S, B, X, U>
sourcepub fn expect<X1>(self, expect: X1) -> HttpService<T, S, B, X1, U>
pub fn expect<X1>(self, expect: X1) -> HttpService<T, S, B, X1, U>
Sets service for Expect: 100-Continue
handling.
An expect service is called with requests that contain an Expect
header. A successful
response type is also a request which will be forwarded to the main service.
sourcepub fn upgrade<U1>(self, upgrade: Option<U1>) -> HttpService<T, S, B, X, U1>
pub fn upgrade<U1>(self, upgrade: Option<U1>) -> HttpService<T, S, B, X, U1>
Sets service for custom Connection: Upgrade
handling.
If service is provided then normal requests handling get halted and this service get called with original request and framed object.
source§impl<S, B, X, U> HttpService<TcpStream, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<S, B, X, U> HttpService<TcpStream, S, B, X, U>where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
sourcepub fn tcp(
self
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
pub fn tcp( self ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
Creates TCP stream service from HTTP service.
The resulting service only supports HTTP/1.x.
sourcepub fn tcp_auto_h2c(
self
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
Available on crate feature http2
only.
pub fn tcp_auto_h2c( self ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
http2
only.Creates TCP stream service from HTTP service that automatically selects HTTP/1.x or HTTP/2 on plaintext connections.
Trait Implementations§
source§impl<T, S, B, X, U> ServiceFactory<(T, Protocol, Option<SocketAddr>)> for HttpService<T, S, B, X, U>where
T: AsyncRead + AsyncWrite + Unpin + 'static,
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
impl<T, S, B, X, U> ServiceFactory<(T, Protocol, Option<SocketAddr>)> for HttpService<T, S, B, X, U>where
T: AsyncRead + AsyncWrite + Unpin + 'static,
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::InitError: Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Response<BoxBody>>,
X::InitError: Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: Display + Into<Response<BoxBody>>,
U::InitError: Debug,
§type Error = DispatchError
type Error = DispatchError
§type Service = HttpServiceHandler<T, <S as ServiceFactory<Request>>::Service, B, <X as ServiceFactory<Request>>::Service, <U as ServiceFactory<(Request, Framed<T, Codec>)>>::Service>
type Service = HttpServiceHandler<T, <S as ServiceFactory<Request>>::Service, B, <X as ServiceFactory<Request>>::Service, <U as ServiceFactory<(Request, Framed<T, Codec>)>>::Service>
Service
created by this factory.§type Future = Pin<Box<dyn Future<Output = Result<<HttpService<T, S, B, X, U> as ServiceFactory<(T, Protocol, Option<SocketAddr>)>>::Service, <HttpService<T, S, B, X, U> as ServiceFactory<(T, Protocol, Option<SocketAddr>)>>::InitError>>>>
type Future = Pin<Box<dyn Future<Output = Result<<HttpService<T, S, B, X, U> as ServiceFactory<(T, Protocol, Option<SocketAddr>)>>::Service, <HttpService<T, S, B, X, U> as ServiceFactory<(T, Protocol, Option<SocketAddr>)>>::InitError>>>>
Service
instance.gsource§fn new_service(&self, _: ()) -> Self::Future
fn new_service(&self, _: ()) -> Self::Future
Auto Trait Implementations§
impl<T, S, B, X, U> Freeze for HttpService<T, S, B, X, U>
impl<T, S, B, X = ExpectHandler, U = UpgradeHandler> !RefUnwindSafe for HttpService<T, S, B, X, U>
impl<T, S, B, X = ExpectHandler, U = UpgradeHandler> !Send for HttpService<T, S, B, X, U>
impl<T, S, B, X = ExpectHandler, U = UpgradeHandler> !Sync for HttpService<T, S, B, X, U>
impl<T, S, B, X, U> Unpin for HttpService<T, S, B, X, U>
impl<T, S, B, X = ExpectHandler, U = UpgradeHandler> !UnwindSafe for HttpService<T, S, B, X, U>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
§fn into_factory(self) -> SF
fn into_factory(self) -> SF
Self
to a ServiceFactory