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
use crate::specifier::QualityValue;
use rama_utils::collections::NonEmptySmallVec;
rama_utils::macros::enums::enum_builder! {
/// Directive for the [`Te`] header.
@String
pub enum TeDirective {
/// A format using the [Lempel-Ziv-Welch](https://en.wikipedia.org/wiki/LZW) (LZW) algorithm
/// is accepted as a transfer coding name.
Compress => "compress",
/// Using the zlib structure (defined in [RFC 1950](https://datatracker.ietf.org/doc/html/rfc1950))
/// is accepted as a transfer coding name.
Deflate => "deflate",
/// A format using the [Lempel-Ziv coding](https://en.wikipedia.org/wiki/LZ77_and_LZ78#LZ77)(LZ77),
/// with a 32-bit CRC is accepted as a transfer coding name.
Gzip => "gzip",
/// Indicates that the client will not discard trailer fields in a
/// [chunked transfer coding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding#chunked).
Trailers => "trailers",
}
}
derive_non_empty_flat_csv_header! {
#[header(name = TE, sep = Comma)]
#[derive(Clone, Debug, PartialEq)]
/// `TE` header, defined in
/// [RFC7230](https://datatracker.ietf.org/doc/html/rfc7230#section-4.3)
///
/// As RFC7230 states, "The "TE" header field in a request indicates what transfer codings,
/// besides chunked, the client is willing to accept in response, and
/// whether or not the client is willing to accept trailer fields in a
/// chunked transfer coding."
///
/// For HTTP/1.1 compliant clients `chunked` transfer codings are assumed to be acceptable and
/// so should never appear in this header.
///
/// # ABNF
///
/// ```text
/// TE = "TE" ":" #( t-codings )
/// t-codings = "trailers" | ( transfer-extension [ accept-params ] )
/// ```
///
/// # Example values
/// * `trailers`
/// * `trailers, deflate;q=0.5`
pub struct Te(pub NonEmptySmallVec<2, QualityValue<TeDirective>>);
}
impl Te {
/// Create a `TE: trailers` header.
#[must_use]
pub fn trailers() -> Self {
Self(NonEmptySmallVec::new(QualityValue::new_value(
TeDirective::Trailers,
)))
}
}
#[cfg(test)]
mod tests {
use super::super::{test_decode, test_encode};
use super::*;
use crate::specifier::Quality;
#[test]
fn decode_header_compress() {
let Te(direcives) = test_decode(&["compress"]).unwrap();
assert_eq!(direcives.len(), 1);
assert_eq!(direcives[0].value.as_str(), "compress");
assert_eq!(direcives[0].quality, Quality::one());
}
#[test]
fn decode_header_trailers_deflate() {
let Te(direcives) = test_decode(&["trailers, deflate;q=0.5"]).unwrap();
assert_eq!(direcives.len(), 2);
assert_eq!(direcives[0].value.as_str(), "trailers");
assert_eq!(direcives[0].quality, Quality::one());
assert_eq!(direcives[1].value.as_str(), "deflate");
assert_eq!(direcives[1].quality, Quality::new_clamped(500));
}
#[test]
fn decode_header_trailers_deflate_split() {
let Te(direcives) = test_decode(&["trailers", "deflate;q=0.5"]).unwrap();
assert_eq!(direcives.len(), 2);
assert_eq!(direcives[0].value.as_str(), "trailers");
assert_eq!(direcives[0].quality, Quality::one());
assert_eq!(direcives[1].value.as_str(), "deflate");
assert_eq!(direcives[1].quality, Quality::new_clamped(500));
}
#[test]
fn encode() {
let te = Te::trailers();
let headers = test_encode(te);
assert_eq!(headers["te"], "trailers");
}
}