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
use ;
use crate::;
/// Byte-buffer types re-exported for use as request body extractors and as
/// response bodies.
pub use ;
/// An incoming HTTP request, carrying a [`Body`] by default.
pub type Request<T = Body> = Request;
/// A type that can be built from an incoming request.
///
/// A page or route handler may take a single `FromRequest` value as its request
/// body parameter, optionally alongside `cx: &Cx`. The built-in extractors
/// ([`Json`](crate::Json), [`Form`](crate::Form), [`Bytes`],
/// [`String`], [`Body`], and more) all implement this trait; implement it
/// yourself for request-specific parsing the built-ins don't cover.
///
/// Because the body is a stream that can only be read once, a handler may have
/// at most one `FromRequest` parameter. This is the request-side counterpart of
/// [`IntoResponse`](crate::IntoResponse).
///
/// # Examples
///
/// Implement it to parse a request in a way the built-ins don't cover. Here,
/// JSON whose body is verified against an `x-signature` header before it is
/// deserialized:
///
/// ```rust
/// # #[derive(serde::Deserialize)]
/// # struct CreateUser { name: String }
/// # fn verify_signature(_signature: &str, _bytes: &[u8]) -> topcoat::Result<()> { Ok(()) }
/// use serde::de::DeserializeOwned;
/// use topcoat::{
/// Result,
/// context::Cx,
/// router::{Body, FromRequest, bad_request, headers, route, to_bytes},
/// };
///
/// struct SignedJson<T>(T);
///
/// impl<T> FromRequest for SignedJson<T>
/// where
/// T: DeserializeOwned,
/// {
/// async fn from_request(cx: &Cx, body: Body) -> Result<Self> {
/// let signature = headers(cx)
/// .get("x-signature")
/// .and_then(|value| value.to_str().ok())
/// .ok_or_else(|| bad_request("missing x-signature header"))?;
///
/// let bytes = to_bytes(body, usize::MAX)
/// .await
/// .map_err(|error| bad_request(format!("failed to read body: {error}")))?;
///
/// verify_signature(signature, &bytes)?;
///
/// Ok(Self(serde_json::from_slice(&bytes)?))
/// }
/// }
///
/// // Once implemented, use it like the built-in extractors:
/// #[route(POST "/api/signed")]
/// async fn signed(SignedJson(input): SignedJson<CreateUser>) -> Result<&'static str> {
/// let _ = input;
/// Ok("ok")
/// }
/// ```
/// Yields the request body unchanged, leaving it unbuffered for the handler to
/// read or forward itself.
/// Buffers the entire request body into memory.
/// Buffers the entire request body into a mutable buffer.
/// Buffers the request body and decodes it as UTF-8, rejecting a non-UTF-8 body
/// with `400 Bad Request`.
/// Customizes the behavior of `Option<Self>` as a [`FromRequest`] extractor.
///
/// Implementing this trait lets `Option<Self>` be extracted from a request,
/// yielding `None` when the request carries no value for the extractor (for
/// example, a missing body) while still surfacing an error for values that are
/// present but malformed.
/// Makes any [`OptionalFromRequest`] extractor optional, yielding `None` when
/// the request carries no value of that kind while still surfacing an error for
/// a value that is present but malformed.