http_request_validator/
lib.rs

1//! A generic HTTP request validator.
2
3#![no_std]
4
5/// The [`http::Request`] validator.
6///
7/// Runs over the buffered request body, so can be used to implement the request signature
8/// validation, or anything that needs a whole request available to conduct the validation.
9///
10/// You can provide your validation logic in this trait implementation.
11/// See the neighbouring crates for integrations with various web servers.
12pub trait Validator<Data: bytes::Buf> {
13    /// An error that can occur during validation.
14    type Error;
15
16    /// Validate the request header and buffered body.
17    fn validate<'a>(
18        &'a self,
19        parts: &'a http::request::Parts,
20        buffered_body: &'a Data,
21    ) -> impl core::future::Future<Output = Result<(), Self::Error>> + Send + 'a;
22}
23
24impl<T: ?Sized, Data> Validator<Data> for T
25where
26    T: core::ops::Deref + Send + Sync,
27    <T as core::ops::Deref>::Target: Validator<Data> + Send,
28    Data: bytes::Buf + Send + Sync,
29{
30    type Error = <<T as core::ops::Deref>::Target as Validator<Data>>::Error;
31
32    fn validate<'a>(
33        &'a self,
34        parts: &'a http::request::Parts,
35        buffered_body: &'a Data,
36    ) -> impl core::future::Future<Output = Result<(), Self::Error>> + 'a {
37        self.deref().validate(parts, buffered_body)
38    }
39}