Skip to main content

Service

Trait Service 

Source
pub trait Service:
    Send
    + Sync
    + 'static {
    // Required method
    fn call(
        &self,
        request: Request,
    ) -> Pin<Box<dyn Future<Output = Result<Response, ServiceError>> + Send + '_>>;

    // Provided method
    fn request_body_policy(&self, _head: &RequestHead) -> RequestBodyPolicy { ... }
}
Expand description

A transport-independent service that handles HTTP requests.

Services are invoked by the runtime after request parsing and validation. They receive a canonical Request (head, body, and connection metadata) and must return a canonical Response.

§Contract

  • The service is called once per request.
  • The service must not write to raw sockets or access transport internals.
  • Panics are caught at the task boundary (JoinSet) and logged; the connection is dropped without a response.
  • The response goes through runtime normalization (hop-by-hop stripping, content-length computation) before transport.

§Thread safety

Services must be Send + Sync to be shared across connection tasks.

Required Methods§

Source

fn call( &self, request: Request, ) -> Pin<Box<dyn Future<Output = Result<Response, ServiceError>> + Send + '_>>

Handle an HTTP request.

Returns a future that resolves to a response or a service error.

Provided Methods§

Source

fn request_body_policy(&self, _head: &RequestHead) -> RequestBodyPolicy

Returns the service’s preferred request body policy.

The runtime uses this to select the effective body policy before service invocation. The runtime enforces a hard global ceiling (max_request_body_bytes) that no service can exceed. Services may only lower the ceiling, not raise it.

The default implementation returns Reject (no body accepted), which is the safe default for static file services.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Service for StaticService

Source§

impl<F, Fut> Service for ServiceFn<F>
where F: Fn(Request) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Response, ServiceError>> + Send + 'static,