|
| 1 | +use std::str::FromStr; |
| 2 | +use std::fmt::{self, Display}; |
| 3 | + |
| 4 | +/// An entity tag |
| 5 | +/// |
| 6 | +/// An Etag consists of a string enclosed by two literal double quotes. |
| 7 | +/// Preceding the first double quote is an optional weakness indicator, |
| 8 | +/// which always looks like this: W/ |
| 9 | +/// See also: https://tools.ietf.org/html/rfc7232#section-2.3 |
| 10 | +#[derive(Clone, PartialEq, Debug)] |
| 11 | +pub struct EntityTag { |
| 12 | + /// Weakness indicator for the tag |
| 13 | + pub weak: bool, |
| 14 | + /// The opaque string in between the DQUOTEs |
| 15 | + pub tag: String |
| 16 | +} |
| 17 | + |
| 18 | +impl Display for EntityTag { |
| 19 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 20 | + if self.weak { |
| 21 | + try!(write!(fmt, "{}", "W/")); |
| 22 | + } |
| 23 | + write!(fmt, "{}", self.tag) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// check that each char in the slice is either: |
| 28 | +// 1. %x21, or |
| 29 | +// 2. in the range %x23 to %x7E, or |
| 30 | +// 3. in the range %x80 to %xFF |
| 31 | +fn check_slice_validity(slice: &str) -> bool { |
| 32 | + for c in slice.bytes() { |
| 33 | + match c { |
| 34 | + b'\x21' | b'\x23' ... b'\x7e' | b'\x80' ... b'\xff' => (), |
| 35 | + _ => { return false; } |
| 36 | + } |
| 37 | + } |
| 38 | + true |
| 39 | +} |
| 40 | + |
| 41 | +impl FromStr for EntityTag { |
| 42 | + type Err = (); |
| 43 | + fn from_str(s: &str) -> Result<EntityTag, ()> { |
| 44 | + let length: usize = s.len(); |
| 45 | + let slice = &s[]; |
| 46 | + |
| 47 | + // Early exits: |
| 48 | + // 1. The string is empty, or, |
| 49 | + // 2. it doesn't terminate in a DQUOTE. |
| 50 | + if slice.is_empty() || !slice.ends_with("\"") { |
| 51 | + return Err(()); |
| 52 | + } |
| 53 | + |
| 54 | + // The etag is weak if its first char is not a DQUOTE. |
| 55 | + if slice.char_at(0) == '"' /* '"' */ { |
| 56 | + // No need to check if the last char is a DQUOTE, |
| 57 | + // we already did that above. |
| 58 | + if check_slice_validity(slice.slice_chars(1, length-1)) { |
| 59 | + return Ok(EntityTag { |
| 60 | + weak: false, |
| 61 | + tag: slice.slice_chars(1, length-1).to_string() |
| 62 | + }); |
| 63 | + } else { |
| 64 | + return Err(()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if slice.slice_chars(0, 3) == "W/\"" { |
| 69 | + if check_slice_validity(slice.slice_chars(3, length-1)) { |
| 70 | + return Ok(EntityTag { |
| 71 | + weak: true, |
| 72 | + tag: slice.slice_chars(3, length-1).to_string() |
| 73 | + }); |
| 74 | + } else { |
| 75 | + return Err(()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Err(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::EntityTag; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_etag_successes() { |
| 90 | + // Expected successes |
| 91 | + let mut etag : EntityTag = "\"foobar\"".parse().unwrap(); |
| 92 | + assert_eq!(etag, (EntityTag { |
| 93 | + weak: false, |
| 94 | + tag: "foobar".to_string() |
| 95 | + })); |
| 96 | + |
| 97 | + etag = "\"\"".parse().unwrap(); |
| 98 | + assert_eq!(etag, EntityTag { |
| 99 | + weak: false, |
| 100 | + tag: "".to_string() |
| 101 | + }); |
| 102 | + |
| 103 | + etag = "W/\"weak-etag\"".parse().unwrap(); |
| 104 | + assert_eq!(etag, EntityTag { |
| 105 | + weak: true, |
| 106 | + tag: "weak-etag".to_string() |
| 107 | + }); |
| 108 | + |
| 109 | + etag = "W/\"\x65\x62\"".parse().unwrap(); |
| 110 | + assert_eq!(etag, EntityTag { |
| 111 | + weak: true, |
| 112 | + tag: "\u{0065}\u{0062}".to_string() |
| 113 | + }); |
| 114 | + |
| 115 | + etag = "W/\"\"".parse().unwrap(); |
| 116 | + assert_eq!(etag, EntityTag { |
| 117 | + weak: true, |
| 118 | + tag: "".to_string() |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_etag_failures() { |
| 124 | + // Expected failures |
| 125 | + let mut etag: Result<EntityTag,()>; |
| 126 | + |
| 127 | + etag = "no-dquotes".parse(); |
| 128 | + assert_eq!(etag, Err(())); |
| 129 | + |
| 130 | + etag = "w/\"the-first-w-is-case-sensitive\"".parse(); |
| 131 | + assert_eq!(etag, Err(())); |
| 132 | + |
| 133 | + etag = "".parse(); |
| 134 | + assert_eq!(etag, Err(())); |
| 135 | + |
| 136 | + etag = "\"unmatched-dquotes1".parse(); |
| 137 | + assert_eq!(etag, Err(())); |
| 138 | + |
| 139 | + etag = "unmatched-dquotes2\"".parse(); |
| 140 | + assert_eq!(etag, Err(())); |
| 141 | + |
| 142 | + etag = "matched-\"dquotes\"".parse(); |
| 143 | + assert_eq!(etag, Err(())); |
| 144 | + } |
| 145 | +} |
0 commit comments