Skip to content

Commit f1859df

Browse files
almielczarekseanmonstar
authored andcommitted
feat(headers): add TE header struct (#1150)
The `TE` header is used by a client to specify which transfer encodings other than `chunked` it will accept. It also specifies whether HTTP trailers are acceptable. This commit also adds a `Trailers` variant to the `hyper::header::shared::Encoding` enum. Closes #1109 BREAKING CHANGE: The `Encoding` enum has an additional variant, `Trailers`.
1 parent 4148599 commit f1859df

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/header/common/te.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use header::{Encoding, QualityItem};
2+
3+
header! {
4+
/// `TE` header, defined in
5+
/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-4.3)
6+
///
7+
/// As RFC7230 states, "The "TE" header field in a request indicates what transfer codings,
8+
/// besides chunked, the client is willing to accept in response, and
9+
/// whether or not the client is willing to accept trailer fields in a
10+
/// chunked transfer coding."
11+
///
12+
/// For HTTP/1.1 compliant clients `chunked` transfer codings are assumed to be acceptable and
13+
/// so should never appear in this header.
14+
///
15+
/// # ABNF
16+
/// ```plain
17+
/// TE = "TE" ":" #( t-codings )
18+
/// t-codings = "trailers" | ( transfer-extension [ accept-params ] )
19+
/// ```
20+
///
21+
/// # Example values
22+
/// * `trailers`
23+
/// * `trailers, deflate;q=0.5`
24+
/// * ``
25+
///
26+
/// # Examples
27+
/// ```
28+
/// use hyper::header::{Headers, TE, Encoding, qitem};
29+
///
30+
/// let mut headers = Headers::new();
31+
/// headers.set(
32+
/// TE(vec![qitem(Encoding::Trailers)])
33+
/// );
34+
/// ```
35+
/// ```
36+
/// use hyper::header::{Headers, TE, Encoding, qitem};
37+
///
38+
/// let mut headers = Headers::new();
39+
/// headers.set(
40+
/// TE(vec![
41+
/// qitem(Encoding::Trailers),
42+
/// qitem(Encoding::Gzip),
43+
/// qitem(Encoding::Deflate),
44+
/// ])
45+
/// );
46+
/// ```
47+
/// ```
48+
/// use hyper::header::{Headers, TE, Encoding, QualityItem, Quality, qitem};
49+
///
50+
/// let mut headers = Headers::new();
51+
/// headers.set(
52+
/// TE(vec![
53+
/// qitem(Encoding::Trailers),
54+
/// QualityItem::new(Encoding::Gzip, Quality(600)),
55+
/// QualityItem::new(Encoding::EncodingExt("*".to_owned()), Quality(0)),
56+
/// ])
57+
/// );
58+
/// ```
59+
(TE, "TE") => (QualityItem<Encoding>)*
60+
61+
test_te {
62+
// From the RFC
63+
test_header!(test1, vec![b"trailers"]);
64+
test_header!(test2, vec![b"trailers, deflate;q=0.5"]);
65+
test_header!(test3, vec![b""]);
66+
}
67+
}

src/header/shared/encoding.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::str;
33

4-
pub use self::Encoding::{Chunked, Brotli, Gzip, Deflate, Compress, Identity, EncodingExt};
4+
pub use self::Encoding::{Chunked, Brotli, Gzip, Deflate, Compress, Identity, EncodingExt, Trailers};
55

66
/// A value to represent an encoding used in `Transfer-Encoding`
77
/// or `Accept-Encoding` header.
@@ -19,6 +19,8 @@ pub enum Encoding {
1919
Compress,
2020
/// The `identity` encoding.
2121
Identity,
22+
/// The `trailers` encoding.
23+
Trailers,
2224
/// Some other encoding that is less common, can be any String.
2325
EncodingExt(String)
2426
}
@@ -32,6 +34,7 @@ impl fmt::Display for Encoding {
3234
Deflate => "deflate",
3335
Compress => "compress",
3436
Identity => "identity",
37+
Trailers => "trailers",
3538
EncodingExt(ref s) => s.as_ref()
3639
})
3740
}
@@ -47,6 +50,7 @@ impl str::FromStr for Encoding {
4750
"gzip" => Ok(Gzip),
4851
"compress" => Ok(Compress),
4952
"identity" => Ok(Identity),
53+
"trailers" => Ok(Trailers),
5054
_ => Ok(EncodingExt(s.to_owned()))
5155
}
5256
}

0 commit comments

Comments
 (0)