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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#![forbid(unsafe_code, future_incompatible)]
#![deny(
    missing_docs,
    missing_debug_implementations,
    nonstandard_style,
    missing_copy_implementations,
    unused_qualifications
)]

//! # A middleware for sending received cookies in surf
//!
//! see [`CookieMiddleware`] for details
//!
use async_dup::{Arc, Mutex};
use async_std::fs::{File, OpenOptions};
use async_std::prelude::*;
use async_std::sync::RwLock;
use std::convert::TryInto;
use std::io::Cursor;
use std::io::SeekFrom;
use surf::http::headers::{COOKIE, SET_COOKIE};
use surf::middleware::{Middleware, Next};
use surf::{Client, Request, Response, Result, Url};

pub use cookie_store;
pub use cookie_store::CookieStore;

/// # A middleware for sending received cookies in surf
///
/// ## File system persistence
///
/// This middleware can optionally be constructed with a file or path
/// to enable writing "persistent cookies" to disk after every
/// received response.
///
/// ## Cloning semantics
///
/// All clones of this middleware will refer to the same data and fd
/// (if persistence is enabled).
///
/// # Usage example
///
/// ```rust
/// use surf::Client;
/// use surf_cookie_middleware::CookieMiddleware;
/// let client = Client::new().with(CookieMiddleware::new());
/// // client.get(...).await?;
/// // client.get(...).await?; <- this request will send any appropriate
/// //                            cookies received from the first request,
/// //                            based on request url
/// ```

#[derive(Default, Clone, Debug)]
pub struct CookieMiddleware {
    cookie_store: Arc<RwLock<CookieStore>>,
    file: Option<Arc<Mutex<File>>>,
}

#[surf::utils::async_trait]
impl Middleware for CookieMiddleware {
    async fn handle(&self, mut req: Request, client: Client, next: Next<'_>) -> Result<Response> {
        let url = req.url().clone();
        self.set_cookies(&mut req).await;
        let res = next.run(req, client).await?;
        self.store_cookies(&url, &res).await?;
        Ok(res)
    }
}

impl CookieMiddleware {
    /// Builds a new CookieMiddleware
    ///
    /// # Example
    ///
    /// ```rust
    /// use surf_cookie_middleware::CookieMiddleware;
    ///
    /// let client = surf::Client::new().with(CookieMiddleware::new());
    ///
    /// // client.get(...).await?;
    /// // client.get(...).await?; <- this request will send any appropriate
    /// //                            cookies received from the first request,
    /// //                            based on request url
    /// ```
    pub fn new() -> Self {
        Self::with_cookie_store(Default::default())
    }

    /// Builds a CookieMiddleware with an existing [`cookie_store::CookieStore`]
    ///
    /// # Example
    ///
    /// ```rust
    /// use surf_cookie_middleware::{CookieStore, CookieMiddleware};
    ///
    /// let cookie_store = CookieStore::default();
    /// let client = surf::Client::new()
    ///     .with(CookieMiddleware::with_cookie_store(cookie_store));
    /// ```
    pub fn with_cookie_store(cookie_store: CookieStore) -> Self {
        Self {
            cookie_store: Arc::new(RwLock::new(cookie_store)),
            file: None,
        }
    }

    /// Builds a CookieMiddleware from a path to a filesystem cookie
    /// jar. These jars are stored in [ndjson](http://ndjson.org/)
    /// format. If the file does not exist, it will be created. If the
    /// file does exist, the cookie jar will be initialized with those
    /// cookies.
    ///
    /// Currently this only persists "persistent cookies" -- cookies
    /// with an expiry. "Session cookies" (without an expiry) are not
    /// persisted to disk, nor are expired cookies.
    ///
    /// # Example
    ///
    /// ```rust
    /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
    /// use surf_cookie_middleware::{CookieStore, CookieMiddleware};
    ///
    /// let cookie_store = CookieStore::default();
    /// let client = surf::Client::new()
    ///     .with(CookieMiddleware::from_path("./cookies.ndjson").await?);
    /// # Ok(()) }) }
    /// ```
    pub async fn from_path(path: impl Into<std::path::PathBuf>) -> std::io::Result<Self> {
        let path = path.into();
        let file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(&path)
            .await?;

        Self::from_file(file).await
    }

    async fn load_from_file(file: &mut File) -> Option<CookieStore> {
        let mut buf = Vec::new();
        file.read_to_end(&mut buf).await.ok();
        CookieStore::load_json(Cursor::new(&buf[..])).ok()
    }

    /// Builds a CookieMiddleware from a File (either
    /// [`async_std::fs::File`] or [`std::fs::File`]) that represents
    /// a filesystem cookie jar. These jars are stored in
    /// [ndjson](http://ndjson.org/) format. The cookie jar will be
    /// initialized with any cookies contained in this file, and
    /// persisted to the file after every request.
    ///
    /// Currently this only persists "persistent cookies" -- cookies
    /// with an expiry. "Session cookies" (without an expiry) are not
    /// persisted to disk, nor are expired cookies.
    ///
    /// # Example
    ///
    /// ```rust
    /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
    /// use surf::Client;
    /// use surf_cookie_middleware::{CookieStore, CookieMiddleware};
    /// let cookie_store = CookieStore::default();
    /// let file = std::fs::File::create("./cookies.ndjson")?;
    /// let client = Client::new()
    ///     .with(CookieMiddleware::from_file(file).await?);
    /// # Ok(()) }) }
    /// ```
    pub async fn from_file(file: impl Into<File>) -> std::io::Result<Self> {
        let mut file = file.into();
        let cookie_store = Self::load_from_file(&mut file).await;
        Ok(Self {
            file: Some(Arc::new(Mutex::new(file))),
            cookie_store: Arc::new(RwLock::new(cookie_store.unwrap_or_default())),
        })
    }

    async fn save(&self) -> Result<()> {
        if let Some(ref file) = self.file {
            let mut string: Vec<u8> = vec![0];
            let mut cursor = std::io::Cursor::new(&mut string);

            self.cookie_store
                .read()
                .await
                .save_json(&mut cursor)
                .unwrap();

            let mut file = file.lock();
            file.seek(SeekFrom::Start(0)).await?;
            file.write_all(&string[..]).await?;
            file.set_len(string.len().try_into()?).await?;
            file.sync_all().await?;
        }
        Ok(())
    }

    async fn set_cookies(&self, req: &mut Request) {
        let cookie_store = self.cookie_store.read().await;
        let mut matches = cookie_store.matches(req.url());

        // clients "SHOULD" sort by path length
        matches.sort_by(|a, b| b.path.len().cmp(&a.path.len()));

        let values = matches
            .iter()
            .map(|cookie| format!("{}={}", cookie.name(), cookie.value()))
            .collect::<Vec<_>>()
            .join("; ");

        req.insert_header(COOKIE, values);
    }

    async fn store_cookies(&self, request_url: &Url, res: &Response) -> Result<()> {
        if let Some(set_cookies) = res.header(SET_COOKIE) {
            let mut cookie_store = self.cookie_store.write().await;
            for cookie in set_cookies {
                match cookie_store.parse(cookie.as_str(), &request_url) {
                    Ok(action) => log::trace!("cookie action: {:?}", action),
                    Err(e) => log::trace!("cookie parse error: {:?}", e),
                }
            }
        }

        self.save().await?;

        Ok(())
    }
}