You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 21, 2020. It is now read-only.
/// Deflate is the most preferred encoding present.
46
+
/// Deflate is the most preferred encoding.
47
47
Deflate,
48
-
/// Brotli is the most preferred encoding present.
48
+
/// Brotli is the most preferred encoding.
49
49
Brotli,
50
+
/// Zstd is the most preferred encoding.
51
+
Zstd,
50
52
/// No encoding is preferred.
51
53
Identity,
52
54
/// No preference is expressed on which encoding to use. Either the `Accept-Encoding` header is not present, or `*` is set as the most preferred encoding.
53
-
None,
55
+
Any,
54
56
}
55
57
56
58
implEncoding{
@@ -60,63 +62,100 @@ impl Encoding {
60
62
"gzip" => Ok(Encoding::Gzip),
61
63
"deflate" => Ok(Encoding::Deflate),
62
64
"br" => Ok(Encoding::Brotli),
65
+
"zstd" => Ok(Encoding::Zstd),
63
66
"identity" => Ok(Encoding::Identity),
64
-
"*" => Ok(Encoding::None),
67
+
"*" => Ok(Encoding::Any),
65
68
_ => Err(ErrorKind::UnknownEncoding)?,
66
69
}
67
70
}
68
71
69
72
/// Converts the encoding into its' corresponding header value.
70
73
///
71
-
/// Note that [`Encoding::None`] will return a HeaderValue with the content `*`.
74
+
/// Note that [`Encoding::Any`] will return a HeaderValue with the content `*`.
72
75
/// This is likely not what you want if you are using this to generate the `Content-Encoding` header to be included in an encoded response.
/// Parse a set of HTTP headers into an `Encoding`.
88
+
/// Parse a set of HTTP headers into a single `Encoding` that the client prefers.
89
+
///
90
+
/// If you're looking for an easy way to determine the best encoding for the client and support every [`Encoding`] listed, this is likely what you want.
for header_value in headers.get_all(ACCEPT_ENCODING).iter(){
90
-
let header_value = header_value.to_str().context(ErrorKind::InvalidEncoding)?;
91
-
for v in header_value.split(',').map(str::trim){
92
-
letmut v = v.splitn(2,";q=");
93
-
let encoding = v.next().unwrap();
94
-
95
-
matchEncoding::parse(encoding){
96
-
Ok(encoding) => {
97
-
ifletSome(qval) = v.next(){
98
-
let qval = match qval.parse::<f32>(){
99
-
Ok(f) => f,
100
-
Err(_) => returnErr(ErrorKind::InvalidEncoding)?,
101
-
};
102
-
if(qval - 1.0f32).abs() < 0.01{
103
-
preferred_encoding = encoding;
104
-
break;
105
-
}elseif qval > 1.0{
106
-
returnErr(ErrorKind::InvalidEncoding)?;// q-values over 1 are unacceptable
107
-
}elseif qval > max_qval {
108
-
preferred_encoding = encoding;
109
-
max_qval = qval;
110
-
}
111
-
}else{
112
-
preferred_encoding = encoding;
113
-
break;
114
-
}
115
-
}
116
-
Err(_) => continue,// ignore unknown encodings for now
117
-
}
95
+
for(encoding, qval)inencodings(headers)? {
96
+
if(qval - 1.0f32).abs() < 0.01{
97
+
preferred_encoding = encoding;
98
+
break;
99
+
}elseif qval > max_qval {
100
+
preferred_encoding = encoding;
101
+
max_qval = qval;
118
102
}
119
103
}
120
104
121
105
Ok(preferred_encoding)
122
106
}
107
+
108
+
/// Parse a set of HTTP headers into a vector containing tuples of encodings and their corresponding q-values.
109
+
///
110
+
/// If you're looking for more fine-grained control over what encoding to choose for the client, or if you don't support every [`Encoding`] listed, this is likely what you want.
111
+
/// ## Example
112
+
/// ```rust
113
+
/// # use failure::Error;
114
+
/// use accept_encoding::Encoding;
115
+
/// use http::header::{HeaderMap, HeaderValue, ACCEPT_ENCODING};
0 commit comments