HttpHandler

Trait HttpHandler 

Source
pub trait HttpHandler<CX, B>: SealedT<HttpSeal, (CX, B)> {
    type Body;
    type Error;

    // Required method
    fn handle(
        &self,
        request: Request<B>,
        ctx: CX,
    ) -> impl Future<Output = Result<ResponseWithContinue<Self::Body>, Self::Error>>;
}
Expand description

A trait for HTTP request handlers.

This trait defines the interface for processing HTTP requests and generating responses. It is designed to work with asynchronous services and supports context-aware handling.

Implementors of this trait can process HTTP requests and return responses along with a boolean flag indicating whether to continue processing the connection.

§Type Parameters

  • CX: The context type for additional request processing information.
  • B: The body type of the incoming request.

§Associated Types

  • Body: The body type of the outgoing response.
  • Error: The error type that may occur during request handling.

§Examples

use your_crate::{HttpHandler, ResponseWithContinue};
use http::{Request, Response};

struct MyHandler;

impl HttpHandler<(), Vec<u8>> for MyHandler {
    type Body = Vec<u8>;
    type Error = std::io::Error;

    async fn handle(&self, request: Request<Vec<u8>>, ctx: ())
        -> Result<ResponseWithContinue<Self::Body>, Self::Error> {
        // Process the request and generate a response
        let response = Response::new(Vec::new());
        Ok((response, true))
    }
}

The HttpHandler trait is automatically implemented for types that implement the Service trait with request type (Request<B>, CX) and return type ResponseWithContinue.

Required Associated Types§

Required Methods§

Source

fn handle( &self, request: Request<B>, ctx: CX, ) -> impl Future<Output = Result<ResponseWithContinue<Self::Body>, Self::Error>>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, CX, IB, OB> HttpHandler<CX, IB> for T
where T: Service<(Request<IB>, CX), Response = ResponseWithContinue<OB>>,

Source§

type Body = OB

Source§

type Error = <T as Service<(Request<IB>, CX)>>::Error