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

impl SessionResponse for reqwest::blocking::Response {
    type Url = url::Url;
    fn parse_set_cookie(&self) -> Vec<RawCookie<'static>> {
        self.headers()
            .get_all(SET_COOKIE)
            .iter()
            .filter_map(|set_cookie| {
                set_cookie
                    .to_str()
                    .map_err(|e| {
                        debug!(
                            "error parsing Set-Cookie to String {:?}: {:?}",
                            set_cookie, e
                        );
                        e
                    })
                    .ok()
                    .and_then(|sc| match RawCookie::parse(sc.to_owned()) {
                        Ok(raw_cookie) => Some(raw_cookie),
                        Err(e) => {
                            debug!(
                                "error parsing Set-Cookie to RawCookie {:?}: {:?}",
                                set_cookie, e
                            );
                            None
                        }
                    })
            })
            .collect::<Vec<_>>()
    }

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

impl SessionRequest for reqwest::blocking::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)]
pub enum ReqwestSessionError {
    ParseUrlError(url::ParseError),
    ReqwestError(reqwest::Error),
}

impl std::fmt::Display for ReqwestSessionError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ReqwestSessionError::ParseUrlError(e) => write!(f, "URL parse error: {}", e),
            ReqwestSessionError::ReqwestError(e) => write!(f, "Reqwest error: {}", e),
        }
    }
}

impl std::error::Error for ReqwestSessionError {}

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::blocking::Client>;

impl SessionClient for reqwest::blocking::Client {
    type Request = reqwest::blocking::RequestBuilder;
    type Response = reqwest::blocking::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;
            println!("");
            println!(
                "==== {}: {} ====",
                $e,
                time::OffsetDateTime::now_utc().format("%Y-%m-%dT%H:%M:%SZ")
            );
            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!("================");
        }};
    }

    fn assert_cookies_count(session: &mut ReqwestSession, url: &str, add: bool) {
        let cookies_count_origin = session.store.iter_unexpired().count();
        session
            .get(url)
            .unwrap_or_else(|_| panic!("session get {} failed", url));
        let cookies_count = session.store.iter_unexpired().count();
        let cookies_count_added = if add {
            reqwest::blocking::Client::new()
                .get(url)
                .send()
                .unwrap_or_else(|_| panic!("cilent get {} failed", url))
                .headers()
                .get_all(reqwest::header::SET_COOKIE)
                .iter()
                .count()
        } else {
            0
        };
        let cookies_count_expected = cookies_count_origin + cookies_count_added;
        assert_eq!(cookies_count, cookies_count_expected);
    }

    #[test]
    fn test_gets() {
        env_logger::init();
        let mut s = ReqwestSession::new(reqwest::blocking::Client::new());
        dump!("init", s);
        assert_cookies_count(&mut s, "http://www.google.com", true);
        dump!("after google", s);
        assert_cookies_count(&mut s, "http://www.google.com", false);
        dump!("after google again", s);
        // yahoo doesn't set any cookies; how nice of them
        assert_cookies_count(&mut s, "http://www.yahoo.com", false);
        dump!("after yahoo", s);
        assert_cookies_count(&mut s, "http://www.msn.com", true);
        dump!("after msn", s);
    }
}