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
use header::{EntityTag, Header, HeaderFormat};
use std::fmt::{self};
use header::parsing::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, Debug)]
pub struct Etag(pub EntityTag);

deref!(Etag => EntityTag);

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

    fn parse_header(raw: &[Vec<u8>]) -> Option<Etag> {

        from_one_raw_str(raw).and_then(|s: String| {
            s.parse::<EntityTag>().and_then(|x| Ok(Etag(x))).ok()
        })
    }
}

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

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

    #[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(EntityTag{
            weak: false,
            tag: "foobar".to_string()
        })));

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

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

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

        etag = Header::parse_header([b"W/\"\"".to_vec()].as_slice());
        assert_eq!(etag, Some(Etag(EntityTag{
            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()] });