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<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>
Sourcepub fn new<F: IntoServiceFactory<S, Request>>(service: F) -> Self
 
pub fn new<F: IntoServiceFactory<S, Request>>(service: F) -> Self
Constructs new HttpService instance from service with default config.
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 = ()>
 
pub fn tcp_auto_h2c( self, ) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
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,
Source§type Error = DispatchError
 
type Error = DispatchError
Source§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.Source§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
Source§impl<T> Instrument for T
 
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
 
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
 
fn in_current_span(self) -> Instrumented<Self>
Source§impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
    SF: ServiceFactory<Req>,
 
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
    SF: ServiceFactory<Req>,
Source§fn into_factory(self) -> SF
 
fn into_factory(self) -> SF
Self to a ServiceFactory