1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-09-02 01:31:57 +02:00

remove BodyEncoding trait (#2565)

This commit is contained in:
Rob Ede
2022-01-03 18:46:04 +00:00
committed by GitHub
parent 19a46e3925
commit 0bc4ae9158
20 changed files with 355 additions and 403 deletions

View File

@@ -17,8 +17,7 @@ fn check_slice_validity(slice: &str) -> bool {
slice.bytes().all(entity_validate_char)
}
/// An entity tag, defined
/// in [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
/// An entity tag, defined in [RFC 7232 §2.3].
///
/// An entity tag consists of a string enclosed by two literal double quotes.
/// Preceding the first double quote is an optional weakness indicator,
@@ -48,16 +47,20 @@ fn check_slice_validity(slice: &str) -> bool {
/// | `W/"1"` | `W/"2"` | no match | no match |
/// | `W/"1"` | `"1"` | no match | match |
/// | `"1"` | `"1"` | match | match |
#[derive(Clone, Debug, Eq, PartialEq)]
///
/// [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntityTag {
/// Weakness indicator for the tag
pub weak: bool,
/// The opaque string in between the DQUOTEs
tag: String,
}
impl EntityTag {
/// Constructs a new EntityTag.
/// Constructs a new `EntityTag`.
///
/// # Panics
/// If the tag contains invalid characters.
pub fn new(weak: bool, tag: String) -> EntityTag {
@@ -66,51 +69,64 @@ impl EntityTag {
}
/// Constructs a new weak EntityTag.
///
/// # Panics
/// If the tag contains invalid characters.
pub fn weak(tag: String) -> EntityTag {
pub fn new_weak(tag: String) -> EntityTag {
EntityTag::new(true, tag)
}
#[deprecated(since = "3.0.0", note = "Renamed to `new_weak`.")]
pub fn weak(tag: String) -> EntityTag {
Self::new_weak(tag)
}
/// Constructs a new strong EntityTag.
///
/// # Panics
/// If the tag contains invalid characters.
pub fn strong(tag: String) -> EntityTag {
pub fn new_strong(tag: String) -> EntityTag {
EntityTag::new(false, tag)
}
/// Get the tag.
#[deprecated(since = "3.0.0", note = "Renamed to `new_strong`.")]
pub fn strong(tag: String) -> EntityTag {
Self::new_strong(tag)
}
/// Returns tag.
pub fn tag(&self) -> &str {
self.tag.as_ref()
}
/// Set the tag.
/// Sets tag.
///
/// # Panics
/// If the tag contains invalid characters.
pub fn set_tag(&mut self, tag: String) {
pub fn set_tag(&mut self, tag: impl Into<String>) {
let tag = tag.into();
assert!(check_slice_validity(&tag), "Invalid tag: {:?}", tag);
self.tag = tag
}
/// For strong comparison two entity-tags are equivalent if both are not
/// weak and their opaque-tags match character-by-character.
/// For strong comparison two entity-tags are equivalent if both are not weak and their
/// opaque-tags match character-by-character.
pub fn strong_eq(&self, other: &EntityTag) -> bool {
!self.weak && !other.weak && self.tag == other.tag
}
/// For weak comparison two entity-tags are equivalent if their
/// opaque-tags match character-by-character, regardless of either or
/// both being tagged as "weak".
/// For weak comparison two entity-tags are equivalent if their opaque-tags match
/// character-by-character, regardless of either or both being tagged as "weak".
pub fn weak_eq(&self, other: &EntityTag) -> bool {
self.tag == other.tag
}
/// The inverse of `EntityTag.strong_eq()`.
/// Returns the inverse of `strong_eq()`.
pub fn strong_ne(&self, other: &EntityTag) -> bool {
!self.strong_eq(other)
}
/// The inverse of `EntityTag.weak_eq()`.
/// Returns inverse of `weak_eq()`.
pub fn weak_ne(&self, other: &EntityTag) -> bool {
!self.weak_eq(other)
}
@@ -178,23 +194,23 @@ mod tests {
// Expected success
assert_eq!(
"\"foobar\"".parse::<EntityTag>().unwrap(),
EntityTag::strong("foobar".to_owned())
EntityTag::new_strong("foobar".to_owned())
);
assert_eq!(
"\"\"".parse::<EntityTag>().unwrap(),
EntityTag::strong("".to_owned())
EntityTag::new_strong("".to_owned())
);
assert_eq!(
"W/\"weaktag\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("weaktag".to_owned())
EntityTag::new_weak("weaktag".to_owned())
);
assert_eq!(
"W/\"\x65\x62\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("\x65\x62".to_owned())
EntityTag::new_weak("\x65\x62".to_owned())
);
assert_eq!(
"W/\"\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("".to_owned())
EntityTag::new_weak("".to_owned())
);
}
@@ -214,19 +230,19 @@ mod tests {
#[test]
fn test_etag_fmt() {
assert_eq!(
format!("{}", EntityTag::strong("foobar".to_owned())),
format!("{}", EntityTag::new_strong("foobar".to_owned())),
"\"foobar\""
);
assert_eq!(format!("{}", EntityTag::strong("".to_owned())), "\"\"");
assert_eq!(format!("{}", EntityTag::new_strong("".to_owned())), "\"\"");
assert_eq!(
format!("{}", EntityTag::weak("weak-etag".to_owned())),
format!("{}", EntityTag::new_weak("weak-etag".to_owned())),
"W/\"weak-etag\""
);
assert_eq!(
format!("{}", EntityTag::weak("\u{0065}".to_owned())),
format!("{}", EntityTag::new_weak("\u{0065}".to_owned())),
"W/\"\x65\""
);
assert_eq!(format!("{}", EntityTag::weak("".to_owned())), "W/\"\"");
assert_eq!(format!("{}", EntityTag::new_weak("".to_owned())), "W/\"\"");
}
#[test]
@@ -237,29 +253,29 @@ mod tests {
// | `W/"1"` | `W/"2"` | no match | no match |
// | `W/"1"` | `"1"` | no match | match |
// | `"1"` | `"1"` | match | match |
let mut etag1 = EntityTag::weak("1".to_owned());
let mut etag2 = EntityTag::weak("1".to_owned());
let mut etag1 = EntityTag::new_weak("1".to_owned());
let mut etag2 = EntityTag::new_weak("1".to_owned());
assert!(!etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2));
assert!(!etag1.weak_ne(&etag2));
etag1 = EntityTag::weak("1".to_owned());
etag2 = EntityTag::weak("2".to_owned());
etag1 = EntityTag::new_weak("1".to_owned());
etag2 = EntityTag::new_weak("2".to_owned());
assert!(!etag1.strong_eq(&etag2));
assert!(!etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2));
assert!(etag1.weak_ne(&etag2));
etag1 = EntityTag::weak("1".to_owned());
etag2 = EntityTag::strong("1".to_owned());
etag1 = EntityTag::new_weak("1".to_owned());
etag2 = EntityTag::new_strong("1".to_owned());
assert!(!etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2));
assert!(!etag1.weak_ne(&etag2));
etag1 = EntityTag::strong("1".to_owned());
etag2 = EntityTag::strong("1".to_owned());
etag1 = EntityTag::new_strong("1".to_owned());
etag2 = EntityTag::new_strong("1".to_owned());
assert!(etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2));
assert!(!etag1.strong_ne(&etag2));

View File

@@ -31,7 +31,7 @@ crate::http::header::common_header! {
///
/// let mut builder = HttpResponse::Ok();
/// builder.insert_header(
/// ETag(EntityTag::new(false, "xyzzy".to_owned()))
/// ETag(EntityTag::new_strong("xyzzy".to_owned()))
/// );
/// ```
///
@@ -41,7 +41,7 @@ crate::http::header::common_header! {
///
/// let mut builder = HttpResponse::Ok();
/// builder.insert_header(
/// ETag(EntityTag::new(true, "xyzzy".to_owned()))
/// ETag(EntityTag::new_weak("xyzzy".to_owned()))
/// );
/// ```
(ETag, ETAG) => [EntityTag]
@@ -50,29 +50,29 @@ crate::http::header::common_header! {
// From the RFC
crate::http::header::common_header_test!(test1,
vec![b"\"xyzzy\""],
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
Some(ETag(EntityTag::new_strong("xyzzy".to_owned()))));
crate::http::header::common_header_test!(test2,
vec![b"W/\"xyzzy\""],
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
Some(ETag(EntityTag::new_weak("xyzzy".to_owned()))));
crate::http::header::common_header_test!(test3,
vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned()))));
Some(ETag(EntityTag::new_strong("".to_owned()))));
// Own tests
crate::http::header::common_header_test!(test4,
vec![b"\"foobar\""],
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
Some(ETag(EntityTag::new_strong("foobar".to_owned()))));
crate::http::header::common_header_test!(test5,
vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned()))));
Some(ETag(EntityTag::new_strong("".to_owned()))));
crate::http::header::common_header_test!(test6,
vec![b"W/\"weak-etag\""],
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
Some(ETag(EntityTag::new_weak("weak-etag".to_owned()))));
crate::http::header::common_header_test!(test7,
vec![b"W/\"\x65\x62\""],
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
Some(ETag(EntityTag::new_weak("\u{0065}\u{0062}".to_owned()))));
crate::http::header::common_header_test!(test8,
vec![b"W/\"\""],
Some(ETag(EntityTag::new(true, "".to_owned()))));
Some(ETag(EntityTag::new_weak("".to_owned()))));
crate::http::header::common_header_test!(test9,
vec![b"no-dquotes"],
None::<ETag>);

View File

@@ -54,14 +54,15 @@ common_header! {
test1,
vec![b"\"xyzzy\""],
Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned())])));
vec![EntityTag::new_strong("xyzzy".to_owned())])));
crate::http::header::common_header_test!(
test2,
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned()),
EntityTag::new(false, "r2d2xxxx".to_owned()),
EntityTag::new(false, "c3piozzzz".to_owned())])));
vec![EntityTag::new_strong("xyzzy".to_owned()),
EntityTag::new_strong("r2d2xxxx".to_owned()),
EntityTag::new_strong("c3piozzzz".to_owned())])));
crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
}
}

View File

@@ -82,8 +82,8 @@ mod tests {
if_none_match = Header::parse(&req);
let mut entities: Vec<EntityTag> = Vec::new();
let foobar_etag = EntityTag::new(false, "foobar".to_owned());
let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
let foobar_etag = EntityTag::new_strong("foobar".to_owned());
let weak_etag = EntityTag::new_weak("weak-etag".to_owned());
entities.push(foobar_etag);
entities.push(weak_etag);
assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));