reinstate accept-encoding header

This commit is contained in:
Rob Ede 2021-12-02 03:29:17 +00:00
parent 4f8edb5a9c
commit d33dad1dc1
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 30 additions and 24 deletions

View File

@ -1,8 +1,9 @@
// TODO: reinstate module use actix_http::header::QualityItem;
use header::{Encoding, QualityItem}; use super::{common_header, Encoding};
use crate::http::header;
header! { common_header! {
/// `Accept-Encoding` header, defined /// `Accept-Encoding` header, defined
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4) /// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
/// ///
@ -65,14 +66,14 @@ header! {
test_parse_and_format { test_parse_and_format {
// From the RFC // From the RFC
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]); common_header_test!(test1, vec![b"compress, gzip"]);
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![]))); common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
crate::http::header::common_header_test!(test3, vec![b"*"]); common_header_test!(test3, vec![b"*"]);
// Note: Removed quality 1 from gzip // Note: Removed quality 1 from gzip
crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]); common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
// Note: Removed quality 1 from gzip // Note: Removed quality 1 from gzip
crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]); common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
} }
} }

View File

@ -45,10 +45,13 @@ common_header! {
/// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())), /// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())),
/// ])); /// ]));
/// ``` /// ```
(CacheControl, header::CACHE_CONTROL) => (CacheDirective)+ (CacheControl, header::CACHE_CONTROL) => (CacheDirective)+
test_parse_and_format { test_parse_and_format {
common_header_test!(no_headers, vec![b""; 0], None);
common_header_test!(empty_header, vec![b""; 1], None);
common_header_test!(bad_syntax, vec![b"foo="], None);
common_header_test!( common_header_test!(
multiple_headers, multiple_headers,
vec![&b"no-cache"[..], &b"private"[..]], vec![&b"no-cache"[..], &b"private"[..]],
@ -76,19 +79,14 @@ common_header! {
])) ]))
); );
common_header_test!(bad_syntax, vec![b"foo="], None);
common_header_test!(empty_header, vec![b""], None);
#[test] #[test]
fn parse_quote_form() { fn parse_quote_form() {
use actix_http::test::TestRequest; let req = test::TestRequest::default()
let req = TestRequest::default()
.insert_header((header::CACHE_CONTROL, "max-age=\"200\"")) .insert_header((header::CACHE_CONTROL, "max-age=\"200\""))
.finish(); .finish();
let cache = Header::parse(&req);
assert_eq!( assert_eq!(
cache.ok(), Header::parse(&req).ok(),
Some(CacheControl(vec![CacheDirective::MaxAge(200)])) Some(CacheControl(vec![CacheDirective::MaxAge(200)]))
) )
} }

View File

@ -4,26 +4,33 @@ pub use self::Encoding::{
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd, Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
}; };
/// A value to represent an encoding used in `Transfer-Encoding` /// A value to represent an encoding used in `Transfer-Encoding` or `Accept-Encoding` header.
/// or `Accept-Encoding` header. #[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Debug)]
pub enum Encoding { pub enum Encoding {
/// The `chunked` encoding. /// The `chunked` encoding.
Chunked, Chunked,
/// The `br` encoding. /// The `br` encoding.
Brotli, Brotli,
/// The `gzip` encoding. /// The `gzip` encoding.
Gzip, Gzip,
/// The `deflate` encoding. /// The `deflate` encoding.
Deflate, Deflate,
/// The `compress` encoding. /// The `compress` encoding.
Compress, Compress,
/// The `identity` encoding. /// The `identity` encoding.
Identity, Identity,
/// The `trailers` encoding. /// The `trailers` encoding.
Trailers, Trailers,
/// The `zstd` encoding. /// The `zstd` encoding.
Zstd, Zstd,
/// Some other encoding that is less common, can be any String. /// Some other encoding that is less common, can be any String.
EncodingExt(String), EncodingExt(String),
} }

View File

@ -9,7 +9,7 @@ macro_rules! common_header_test_module {
use ::mime::*; use ::mime::*;
use $crate::http::header::{self, *}; use $crate::http::header::{self, *};
use super::$id as HeaderField; use super::{$id as HeaderField, *};
$($tf)* $($tf)*
} }
@ -149,7 +149,7 @@ macro_rules! common_header {
#[inline] #[inline]
fn parse<T: $crate::HttpMessage>(msg: &T) -> Result<Self, $crate::error::ParseError>{ fn parse<T: $crate::HttpMessage>(msg: &T) -> Result<Self, $crate::error::ParseError>{
let headers = msg.headers().get_all(Self::name()); let headers = msg.headers().get_all(Self::name());
println!("{:?}", &headers);
$crate::http::header::from_comma_delimited(headers) $crate::http::header::from_comma_delimited(headers)
.and_then(|items| { .and_then(|items| {
if items.is_empty() { if items.is_empty() {

View File

@ -17,9 +17,9 @@ use bytes::{Bytes, BytesMut};
// - header parsing utils // - header parsing utils
pub use actix_http::http::header::*; pub use actix_http::http::header::*;
mod accept_charset;
// mod accept_encoding;
mod accept; mod accept;
mod accept_charset;
mod accept_encoding;
mod accept_language; mod accept_language;
mod allow; mod allow;
mod any_or_some; mod any_or_some;