rama_http/layer/validate_request/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Middleware that validates requests.
//!
//! # Example
//!
//! ```
//! use rama_http::layer::validate_request::ValidateRequestHeaderLayer;
//! use rama_http::{Body, Request, Response, StatusCode, header::ACCEPT};
//! use rama_core::service::service_fn;
//! use rama_core::{Context, Service, Layer};
//! use rama_core::error::BoxError;
//!
//! async fn handle(request: Request) -> Result<Response, BoxError> {
//!     Ok(Response::new(Body::empty()))
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), BoxError> {
//! let mut service = (
//!     // Require the `Accept` header to be `application/json`, `*/*` or `application/*`
//!     ValidateRequestHeaderLayer::accept("application/json"),
//! ).layer(service_fn(handle));
//!
//! // Requests with the correct value are allowed through
//! let request = Request::builder()
//!     .header(ACCEPT, "application/json")
//!     .body(Body::empty())
//!     .unwrap();
//!
//! let response = service
//!     .serve(Context::default(), request)
//!     .await?;
//!
//! assert_eq!(StatusCode::OK, response.status());
//!
//! // Requests with an invalid value get a `406 Not Acceptable` response
//! let request = Request::builder()
//!     .header(ACCEPT, "text/strings")
//!     .body(Body::empty())
//!     .unwrap();
//!
//! let response = service
//!     .serve(Context::default(), request)
//!     .await?;
//!
//! assert_eq!(StatusCode::NOT_ACCEPTABLE, response.status());
//! # Ok(())
//! # }
//! ```
//!
//! Custom validation can be made by implementing [`ValidateRequest`]:
//!
//! ```
//! use rama_http::layer::validate_request::{ValidateRequestHeaderLayer, ValidateRequest};
//! use rama_http::{Body, Request, Response, StatusCode, header::ACCEPT};
//! use rama_core::service::service_fn;
//! use rama_core::{Context, Service, Layer};
//! use rama_core::error::BoxError;
//!
//! #[derive(Clone, Copy)]
//! pub struct MyHeader { /* ...  */ }
//!
//! impl<S, B> ValidateRequest<S, B> for MyHeader
//!     where
//!         S: Clone + Send + Sync + 'static,
//!         B: Send + 'static,
//! {
//!     type ResponseBody = Body;
//!
//!     async fn validate(
//!         &self,
//!         ctx: Context<S>,
//!         req: Request<B>,
//!     ) -> Result<(Context<S>, Request<B>), Response<Self::ResponseBody>> {
//!         // validate the request...
//!         # Ok::<_, Response>((ctx, req))
//!     }
//! }
//!
//! async fn handle(request: Request) -> Result<Response, BoxError> {
//!     # Ok(Response::builder().body(Body::empty()).unwrap())
//!     // ...
//! }
//!
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), BoxError> {
//! let service = (
//!     // Validate requests using `MyHeader`
//!     ValidateRequestHeaderLayer::custom(MyHeader { /* ... */ }),
//! ).layer(service_fn(handle));
//!
//! # let request = Request::builder()
//! #     .body(Body::empty())
//! #     .unwrap();
//!
//! let response = service
//!     .serve(Context::default(), request)
//!     .await?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! Or using a closure:
//!
//! ```
//! use bytes::Bytes;
//! use rama_http::{Body, Request, Response, StatusCode, header::ACCEPT};
//! use rama_http::layer::validate_request::{ValidateRequestHeaderLayer, ValidateRequest};
//! use rama_core::service::service_fn;
//! use rama_core::{Context, Service, Layer};
//! use rama_core::error::BoxError;
//!
//! async fn handle(request: Request) -> Result<Response, BoxError> {
//!     # Ok(Response::builder().body(Body::empty()).unwrap())
//!     // ...
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), BoxError> {
//! let service = (
//!     ValidateRequestHeaderLayer::custom_fn(|request: Request| async move {
//!         // Validate the request
//!         # Ok::<_, Response>(request)
//!     }),
//! ).layer(service_fn(handle));
//!
//! # let request = Request::builder()
//! #     .body(Body::empty())
//! #     .unwrap();
//!
//! let response = service
//!     .serve(Context::default(), request)
//!     .await?;
//!
//! # Ok(())
//! # }
//! ```

mod accept_header;
mod validate;
mod validate_fn;
mod validate_request_header;

#[doc(inline)]
pub use accept_header::AcceptHeader;
#[doc(inline)]
pub use validate::ValidateRequest;
#[doc(inline)]
pub use validate_fn::{BoxValidateRequestFn, ValidateRequestFn};
#[doc(inline)]
pub use validate_request_header::{ValidateRequestHeader, ValidateRequestHeaderLayer};