Skip to main content

HttpFilter

Trait HttpFilter 

Source
pub trait HttpFilter: Send + Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn on_request<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        ctx: &'life1 mut HttpFilterContext<'life2>,
    ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided methods
    fn on_response<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        ctx: &'life1 mut HttpFilterContext<'life2>,
    ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn request_body_access(&self) -> BodyAccess { ... }
    fn response_body_access(&self) -> BodyAccess { ... }
    fn request_body_mode(&self) -> BodyMode { ... }
    fn response_body_mode(&self) -> BodyMode { ... }
    fn needs_request_context(&self) -> bool { ... }
    fn apply_insecure_options(&self, _options: &InsecureOptions) { ... }
    fn compression_config(&self) -> Option<&CompressionConfig> { ... }
    fn on_request_body<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        ctx: &'life1 mut HttpFilterContext<'life2>,
        body: &'life3 mut Option<Bytes>,
        end_of_stream: bool,
    ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn on_response_body(
        &self,
        ctx: &mut HttpFilterContext<'_>,
        body: &mut Option<Bytes>,
        end_of_stream: bool,
    ) -> Result<FilterAction, FilterError> { ... }
}
Expand description

A filter that participates in HTTP request/response processing.

§Example

use async_trait::async_trait;
use praxis_filter::{FilterAction, FilterError, HttpFilter, HttpFilterContext};

struct NoopFilter;

#[async_trait]
impl HttpFilter for NoopFilter {
    fn name(&self) -> &'static str {
        "noop"
    }

    async fn on_request(
        &self,
        _ctx: &mut HttpFilterContext<'_>,
    ) -> Result<FilterAction, FilterError> {
        Ok(FilterAction::Continue)
    }
}

let filter = NoopFilter;
assert_eq!(filter.name(), "noop");

Required Methods§

Source

fn name(&self) -> &'static str

Unique name identifying this filter type (e.g. "router", "rate_limit").

Source

fn on_request<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 mut HttpFilterContext<'life2>, ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called for each incoming request, in pipeline order.

Provided Methods§

Source

fn on_response<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 mut HttpFilterContext<'life2>, ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called for each response, in reverse pipeline order.

Default: FilterAction::Continue

Source

fn request_body_access(&self) -> BodyAccess

Declares what access this filter needs to request bodies.

Return BodyAccess::None (the default) when the filter does not inspect or modify request bodies. Return BodyAccess::ReadOnly to receive body chunks in on_request_body without modification rights, or BodyAccess::ReadWrite to mutate body bytes in place.

Source

fn response_body_access(&self) -> BodyAccess

Declares what access this filter needs to response bodies.

Return BodyAccess::None (the default) when the filter does not inspect or modify response bodies. Return BodyAccess::ReadOnly to receive body chunks in on_response_body without modification rights, or BodyAccess::ReadWrite to mutate body bytes in place.

Source

fn request_body_mode(&self) -> BodyMode

Declares the delivery mode for request body chunks.

BodyMode::Stream (the default) delivers chunks as they arrive with minimal latency. BodyMode::StreamBuffer accumulates chunks and defers forwarding until the filter calls FilterAction::Release or end-of-stream; use this when the filter needs the full body before making a decision (e.g. JSON parsing for routing).

Source

fn response_body_mode(&self) -> BodyMode

Declares the delivery mode for response body chunks.

BodyMode::Stream (the default) delivers chunks as they arrive with minimal latency. BodyMode::StreamBuffer accumulates chunks and defers forwarding until the filter calls FilterAction::Release or end-of-stream; use this when the filter needs the complete response body (e.g. response persistence).

Source

fn needs_request_context(&self) -> bool

Whether this filter needs the original request context during body phases.

When true, the pipeline preserves the original request headers and URI so they are available in on_request_body and on_response_body. Enable this when body-phase decisions depend on request metadata (method, path, headers).

Source

fn apply_insecure_options(&self, _options: &InsecureOptions)

Apply global InsecureOptions to this filter.

Filters that support insecure overrides (e.g. CSRF log-only mode) override this. Default: no-op.

Source

fn compression_config(&self) -> Option<&CompressionConfig>

Returns the compression configuration if this filter enables response compression.

Return Some to activate response compression with the given algorithm and level settings. Return None (the default) to leave compression unmodified. Only one filter in a pipeline should return Some.

Source

fn on_request_body<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, ctx: &'life1 mut HttpFilterContext<'life2>, body: &'life3 mut Option<Bytes>, end_of_stream: bool, ) -> Pin<Box<dyn Future<Output = Result<FilterAction, FilterError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Called for each chunk of request body data, in pipeline order.

body contains the current chunk (None when empty). end_of_stream is true on the final chunk. Filters may safely modify body before end_of_stream when using BodyAccess::ReadWrite; buffered modes guarantee all bytes arrive in a single call with end_of_stream = true. Return FilterAction::Reject to abort with an error response.

§Errors

Returns FilterError if body processing fails. The pipeline converts errors into 500 responses.

Source

fn on_response_body( &self, ctx: &mut HttpFilterContext<'_>, body: &mut Option<Bytes>, end_of_stream: bool, ) -> Result<FilterAction, FilterError>

Called for each chunk of response body data, in reverse pipeline order.

body contains the current chunk (None when empty). end_of_stream is true on the final chunk. Filters may safely modify body before end_of_stream when using BodyAccess::ReadWrite; buffered modes guarantee all bytes arrive in a single call with end_of_stream = true. Return FilterAction::Reject to abort with an error response.

§Errors

Returns FilterError if body processing fails. The pipeline converts errors into 502 responses.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§