wreq_proto/ext/h1_reason_phrase.rs
1/// A reason phrase in an HTTP/1 response.
2///
3/// # Clients
4///
5/// For clients, a `ReasonPhrase` will be present in the extensions of the `http::Response`
6/// returned for a request if the reason phrase is different from the canonical reason phrase
7/// for the response's status code. For example, if a server returns `HTTP/1.1 200 Awesome`,
8/// the `ReasonPhrase` will be present and contain `Awesome`, but if a server returns `HTTP/1.1
9/// 200 OK`, the response will not contain a `ReasonPhrase`.
10use bytes::Bytes;
11
12/// A reason phrase in an HTTP/1 response.
13///
14/// # Clients
15///
16/// For clients, a `ReasonPhrase` will be present in the extensions of the `http::Response` returned
17/// for a request if the reason phrase is different from the canonical reason phrase for the
18/// response's status code. For example, if a server returns `HTTP/1.1 200 Awesome`, the
19/// `ReasonPhrase` will be present and contain `Awesome`, but if a server returns `HTTP/1.1 200 OK`,
20/// the response will not contain a `ReasonPhrase`.
21#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct ReasonPhrase(Bytes);
23
24impl ReasonPhrase {
25 // Not public on purpose.
26 /// Converts a `Bytes` directly into a `ReasonPhrase` without validating.
27 ///
28 /// Use with care; invalid bytes in a reason phrase can cause serious security problems if
29 /// emitted in a response.
30 #[inline]
31 pub(crate) fn from_bytes_unchecked(reason: Bytes) -> Self {
32 Self(reason)
33 }
34}
35
36impl AsRef<[u8]> for ReasonPhrase {
37 /// Gets the reason phrase as bytes.
38 #[inline]
39 fn as_ref(&self) -> &[u8] {
40 &self.0
41 }
42}