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
use crate::session::{Session, SessionClient, SessionRequest, SessionResponse};
use cookie::Cookie as RawCookie;
use failure::Fail;
use log::debug;
use reqwest;
use reqwest::header::{COOKIE, SET_COOKIE};
use url::Url;

impl SessionResponse for reqwest::Response {
    fn parse_set_cookie(&self) -> Option<Vec<RawCookie<'static>>> {
        self.headers().get(SET_COOKIE).map(|set_cookie| {
            set_cookie
                .to_str()
                .iter()
                .filter_map(|h_c| match RawCookie::parse(h_c.to_string()) {
                    Ok(raw_cookie) => Some(raw_cookie),
                    Err(e) => {
                        debug!("error parsing Set-Cookie {:?}: {:?}", h_c, e);
                        None
                    }
                })
                .collect::<Vec<_>>()
        })
    }

    fn final_url(&self) -> Option<&Url> {
        Some(self.url())
    }
}

impl SessionRequest for reqwest::RequestBuilder {
    fn add_cookies(self, cookies: Vec<&RawCookie<'static>>) -> Self {
        if cookies.is_empty() {
            debug!("no cookies to add to request");
            self
        } else {
            let cookies = cookies.iter().map(|rc| rc.encoded().to_string());
            let mut out = self;
            for cookie in cookies {
                out = out.header(COOKIE, cookie);
            }
            out
        }
    }
}

#[derive(Debug, Fail)]
pub enum ReqwestSessionError {
    #[fail(display = "URL parse error: {}", _0)]
    ParseUrlError(url::ParseError),
    #[fail(display = "Reqwest error: {}", _0)]
    ReqwestError(reqwest::Error),
}

impl From<url::ParseError> for ReqwestSessionError {
    fn from(e: url::ParseError) -> Self {
        ReqwestSessionError::ParseUrlError(e)
    }
}

impl From<reqwest::Error> for ReqwestSessionError {
    fn from(e: reqwest::Error) -> Self {
        ReqwestSessionError::ReqwestError(e)
    }
}

pub type ReqwestSession = Session<reqwest::Client>;

impl SessionClient for reqwest::Client {
    type Request = reqwest::RequestBuilder;
    type Response = reqwest::Response;
    type SendError = ReqwestSessionError;

    fn get_request(&self, url: &Url) -> Self::Request {
        self.get(url.clone())
    }
    fn put_request(&self, url: &Url) -> Self::Request {
        self.put(url.clone())
    }
    fn head_request(&self, url: &Url) -> Self::Request {
        self.head(url.clone())
    }
    fn delete_request(&self, url: &Url) -> Self::Request {
        self.delete(url.clone())
    }
    fn post_request(&self, url: &Url) -> Self::Request {
        self.post(url.clone())
    }

    fn send(&self, request: Self::Request) -> Result<Self::Response, Self::SendError> {
        request.send().map_err(ReqwestSessionError::from)
    }
}

#[cfg(test)]
mod tests {
    use env_logger;
    use reqwest;

    use super::ReqwestSession;

    macro_rules! dump {
        ($e: expr, $i: ident) => {{
            use serde_json;
            use time::now_utc;
            println!("");
            println!("==== {}: {} ====", $e, now_utc().rfc3339());
            for c in $i.store.iter_any() {
                println!(
                    "{} {}",
                    if c.is_expired() {
                        "XXXXX"
                    } else if c.is_persistent() {
                        "PPPPP"
                    } else {
                        "     "
                    },
                    serde_json::to_string(c).unwrap()
                );
                println!("----------------");
            }
            println!("================");
        }};
    }

    #[test]
    fn test_gets() {
        env_logger::init();
        let mut s = ReqwestSession::new(reqwest::Client::new());
        dump!("init", s);
        s.get("http://www.google.com").expect("www.google.com get failed");
        let c1 = s.store.iter_unexpired().count();
        assert!(c1 > 0);
        s.get("http://www.google.com").expect("www.google.com get failed");
        assert!(c1 == s.store.iter_unexpired().count()); // no new cookies on re-request
        dump!("after google", s);
        s.get("http://www.yahoo.com").expect("www.yahoo.com get failed");
        dump!("after yahoo", s);
        let c2 = s.store.iter_unexpired().count();
        assert!(c2 > 0);
        assert!(c2 == c1); // yahoo doesn't set any cookies; how nice of them
        s.get("http://www.msn.com").expect("www.msn.com get failed");
        dump!("after msn", s);
        let c3 = s.store.iter_unexpired().count();
        assert!(c3 > 0);
        assert!(c3 > c2);
    }
}