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
//!Response extractors

use crate::header;

///Cookie extractor.
///
///As it returns references they would tie
///up original response, if you want avoid it
///you can use `Cookie::into_owned()`
pub struct CookieIter<'a> {
    iter: header::ValueIter<'a, header::HeaderValue>,
}

impl<'a> CookieIter<'a> {
    ///Creates new instance from `http::header::ValueIter`
    pub fn new(iter: header::ValueIter<'a, header::HeaderValue>) -> Self {
        Self {
            iter
        }
    }
}

impl<'a> Iterator for CookieIter<'a> {
    type Item = Result<cookie::Cookie<'a>, cookie::ParseError>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        use percent_encoding::percent_decode;

        if let Some(cook) = self.iter.by_ref().next() {
            let cook = percent_decode(cook.as_bytes());
            let cook = cook.decode_utf8().map_err(|error| cookie::ParseError::Utf8Error(error))
                                         .and_then(|cook| cookie::Cookie::parse(cook));
            Some(cook)
        } else {
            None
        }
    }
}