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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::fmt::{Formatter, Display, Result};

use hyper::error::{self, Error};
use hyper::header::{HeaderFormat, Header};

use FieldMap;
use field;

const USN_HEADER_NAME: &'static str = "USN";

/// Separator for multiple key/values in header fields.
const FIELD_PAIR_SEPARATOR: &'static str = "::";

/// Represents a header which specifies a unique service name.
///
/// Field value can hold up to two `FieldMap`'s.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct USN(pub FieldMap, pub Option<FieldMap>);

impl USN {
    pub fn new(field: FieldMap, opt_field: Option<FieldMap>) -> USN {
        USN(field, opt_field)
    }
}

impl Header for USN {
    fn header_name() -> &'static str {
        USN_HEADER_NAME
    }

    fn parse_header(raw: &[Vec<u8>]) -> error::Result<Self> {
        if raw.len() != 1 {
            return Err(Error::Header);
        }

        let (first, second) = match partition_pairs(raw[0][..].iter()) {
            Some((n, Some(u))) => (FieldMap::parse_bytes(&n[..]), FieldMap::parse_bytes(&u[..])),
            Some((n, None)) => (FieldMap::parse_bytes(&n[..]), None),
            None => return Err(Error::Header),
        };

        match first {
            Some(n) => Ok(USN(n, second)),
            None => Err(Error::Header),
        }
    }
}

impl HeaderFormat for USN {
    fn fmt_header(&self, fmt: &mut Formatter) -> Result {
        try!(Display::fmt(&self.0, fmt));

        if let Some(ref n) = self.1 {
            try!(fmt.write_fmt(format_args!("{}", FIELD_PAIR_SEPARATOR)));
            try!(Display::fmt(n, fmt));
        }

        Ok(())
    }
}

fn partition_pairs<'a, I>(header_iter: I) -> Option<(Vec<u8>, Option<Vec<u8>>)>
    where I: Iterator<Item = &'a u8>
{
    let mut second_partition = false;
    let mut header_iter = header_iter.peekable();

    let mut last_byte = match header_iter.peek() {
        Some(&&n) => n,
        None => return None,
    };

    // Seprate field into two vecs, store separators on end of first
    let (mut first, second): (Vec<u8>, Vec<u8>) = header_iter.cloned().partition(|&n| {
        if second_partition {
            false
        } else {
            second_partition = [last_byte, n] == FIELD_PAIR_SEPARATOR.as_bytes();
            last_byte = n;

            true
        }
    });

    // Remove up to two separators from end of first
    for _ in 0..2 {
        if let Some(&n) = first[..].last() {
            if n == field::PAIR_SEPARATOR as u8 {
                first.pop();
            }
        }
    }

    match (first.is_empty(), second.is_empty()) {
        (false, false) => Some((first, Some(second))),
        (false, true) => Some((first, None)),
        _ => None,
    }
}

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

    use super::USN;
    use FieldMap::{UPnP, UUID, URN, Unknown};

    #[test]
    fn positive_double_pair() {
        let double_pair_header = &["uuid:device-UUID::upnp:rootdevice".to_string().into_bytes()];
        let USN(first, second) = USN::parse_header(double_pair_header).unwrap();

        match first {
            UUID(n) => assert_eq!(n, "device-UUID"),
            _ => panic!("Didnt Match uuid"),
        };

        match second.unwrap() {
            UPnP(n) => assert_eq!(n, "rootdevice"),
            _ => panic!("Didnt Match upnp"),
        };
    }

    #[test]
    fn positive_single_pair() {
        let single_pair_header = &["urn:device-URN".to_string().into_bytes()];
        let USN(first, second) = USN::parse_header(single_pair_header).unwrap();

        match first {
            URN(n) => assert_eq!(n, "device-URN"),
            _ => panic!("Didnt Match urn"),
        };

        assert!(second.is_none());
    }

    #[test]
    fn positive_trailing_double_colon() {
        let trailing_double_colon_header = &["upnp:device-UPnP::".to_string().into_bytes()];
        let USN(first, second) = USN::parse_header(trailing_double_colon_header).unwrap();

        match first {
            UPnP(n) => assert_eq!(n, "device-UPnP"),
            _ => panic!("Didnt Match upnp"),
        };

        assert!(second.is_none());
    }

    #[test]
    fn positive_trailing_single_colon() {
        let trailing_single_colon_header = &["some-key:device-UPnP:".to_string().into_bytes()];
        let USN(first, second) = USN::parse_header(trailing_single_colon_header).unwrap();

        match first {
            Unknown(k, v) => {
                assert_eq!(k, "some-key");
                assert_eq!(v, "device-UPnP");
            }
            _ => panic!("Didnt Match upnp"),
        };

        assert!(second.is_none());
    }

    #[test]
    #[should_panic]
    fn negative_empty() {
        let empty_header = &["".to_string().into_bytes()];

        USN::parse_header(empty_header).unwrap();
    }

    #[test]
    #[should_panic]
    fn negative_colon() {
        let colon_header = &[":".to_string().into_bytes()];

        USN::parse_header(colon_header).unwrap();
    }

    #[test]
    #[should_panic]
    fn negative_double_colon() {
        let double_colon_header = &["::".to_string().into_bytes()];

        USN::parse_header(double_colon_header).unwrap();
    }

    #[test]
    #[should_panic]
    fn negative_double_colon_value() {
        let double_colon_value_header = &["uuid:::".to_string().into_bytes()];

        USN::parse_header(double_colon_value_header).unwrap();
    }
}