1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use header::{Header, HeaderFormat};
use std::fmt::{self};
use header::shared::util::from_one_raw_str;

/// The `Etag` header.
///
/// An Etag consists of a string enclosed by two literal double quotes.
/// Preceding the first double quote is an optional weakness indicator,
/// which always looks like this: W/
/// See also: https://tools.ietf.org/html/rfc7232#section-2.3
#[derive(Clone, PartialEq, Show)]
pub struct Etag {
    /// Weakness indicator for the tag
    pub weak: bool,
    /// The opaque string in between the DQUOTEs
    pub tag: String
}

impl Header for Etag {
    fn header_name(_: Option<Etag>) -> &'static str {
        "Etag"
    }

    fn parse_header(raw: &[Vec<u8>]) -> Option<Etag> {
        // check that each char in the slice is either:
        // 1. %x21, or
        // 2. in the range %x23 to %x7E, or
        // 3. in the range %x80 to %xFF
        fn check_slice_validity(slice: &str) -> bool {
            for c in slice.bytes() {
                match c {
                    b'\x21' | b'\x23' ... b'\x7e' | b'\x80' ... b'\xff' => (),
                    _ => { return false; }
                }
            }
            true
        }


        from_one_raw_str(raw).and_then(|s: String| {
            let length: usize = s.len();
            let slice = &s[];

            // Early exits:
            // 1. The string is empty, or,
            // 2. it doesn't terminate in a DQUOTE.
            if slice.is_empty() || !slice.ends_with("\"") {
                return None;
            }

            // The etag is weak if its first char is not a DQUOTE.
            if slice.char_at(0) == '"' {
                // No need to check if the last char is a DQUOTE,
                // we already did that above.
                if check_slice_validity(slice.slice_chars(1, length-1)) {
                    return Some(Etag {
                        weak: false,
                        tag: slice.slice_chars(1, length-1).to_string()
                    });
                } else {
                    return None;
                }
            }

            if slice.slice_chars(0, 3) == "W/\"" {
                if check_slice_validity(slice.slice_chars(3, length-1)) {
                    return Some(Etag {
                        weak: true,
                        tag: slice.slice_chars(3, length-1).to_string()
                    });
                } else {
                    return None;
                }
            }

            None
        })
    }
}

impl HeaderFormat for Etag {
    fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        if self.weak {
            try!(fmt.write_str("W/"));
        }
        write!(fmt, "\"{}\"", self.tag)
    }
}

#[cfg(test)]
mod tests {
    use super::Etag;
    use header::Header;

    #[test]
    fn test_etag_successes() {
        // Expected successes
        let mut etag: Option<Etag>;

        etag = Header::parse_header([b"\"foobar\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag {
            weak: false,
            tag: "foobar".to_string()
        }));

        etag = Header::parse_header([b"\"\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag {
            weak: false,
            tag: "".to_string()
        }));

        etag = Header::parse_header([b"W/\"weak-etag\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag {
            weak: true,
            tag: "weak-etag".to_string()
        }));

        etag = Header::parse_header([b"W/\"\x65\x62\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag {
            weak: true,
            tag: "\u{0065}\u{0062}".to_string()
        }));

        etag = Header::parse_header([b"W/\"\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag {
            weak: true,
            tag: "".to_string()
        }));
    }

    #[test]
    fn test_etag_failures() {
        // Expected failures
        let mut etag: Option<Etag>;

        etag = Header::parse_header([b"no-dquotes".to_vec()].as_slice());
        assert_eq!(etag, None);

        etag = Header::parse_header([b"w/\"the-first-w-is-case-sensitive\"".to_vec()].as_slice());
        assert_eq!(etag, None);

        etag = Header::parse_header([b"".to_vec()].as_slice());
        assert_eq!(etag, None);

        etag = Header::parse_header([b"\"unmatched-dquotes1".to_vec()].as_slice());
        assert_eq!(etag, None);

        etag = Header::parse_header([b"unmatched-dquotes2\"".to_vec()].as_slice());
        assert_eq!(etag, None);

        etag = Header::parse_header([b"matched-\"dquotes\"".to_vec()].as_slice());
        assert_eq!(etag, None);
    }
}

bench_header!(bench, Etag, { vec![b"W/\"nonemptytag\"".to_vec()] });