Handler

Trait Handler 

Source
pub trait Handler: Send + Sync {
    // Required method
    fn call(
        &self,
        request: Request<Body>,
        request_data: RequestData,
    ) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>>;

    // Provided methods
    fn prefers_raw_json_body(&self) -> bool { ... }
    fn prefers_parameter_extraction(&self) -> bool { ... }
    fn wants_headers(&self) -> bool { ... }
    fn wants_cookies(&self) -> bool { ... }
    fn wants_request_extensions(&self) -> bool { ... }
}
Expand description

Handler trait that all language bindings must implement

This trait is completely language-agnostic. Each binding (Python, Node, WASM) implements this trait to bridge their runtime to our HTTP server.

Required Methods§

Source

fn call( &self, request: Request<Body>, request_data: RequestData, ) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>>

Handle an HTTP request

Takes the extracted request data and returns a future that resolves to either:

  • Ok(Response): A successful HTTP response
  • Err((StatusCode, String)): An error with status code and message

Provided Methods§

Source

fn prefers_raw_json_body(&self) -> bool

Whether this handler prefers consuming RequestData::raw_body over the parsed RequestData::body for JSON requests.

When true, the server may skip eager JSON parsing when there is no request-body schema validator attached to the route.

Source

fn prefers_parameter_extraction(&self) -> bool

Whether this handler wants to perform its own parameter validation/extraction (path/query/header/cookie).

When true, the server will skip ParameterValidator::validate_and_extract in ValidatingHandler. This is useful for language bindings which need to transform validated parameters into language-specific values (e.g., Python kwargs) without duplicating work. When false, the server stores validated output in RequestData::validated_params.

Source

fn wants_headers(&self) -> bool

Whether this handler needs the parsed headers map in RequestData.

When false, the server may skip building RequestData::headers for requests without a body. (Requests with bodies still typically need Content-Type decisions.)

Source

fn wants_cookies(&self) -> bool

Whether this handler needs the parsed cookies map in RequestData.

When false, the server may skip parsing cookies for requests without a body.

Source

fn wants_request_extensions(&self) -> bool

Whether this handler needs RequestData stored in request extensions.

When false, the server avoids inserting RequestData into extensions to skip cloning in hot paths.

Implementors§