Files
ureq/src/cookies.rs
Martin Algesten 9a9dd4ee6c Upgrade cookie to 0.15 and cookie_store to 0.13.0
cookie_store default features pulls in reqwest, so we stop that
by specifying the exact features wanted.
2021-03-14 19:00:28 +01:00

49 lines
1.3 KiB
Rust

use cookie_store::CookieStore;
use std::ops::Deref;
use std::sync::{RwLock, RwLockReadGuard};
use url::Url;
#[derive(Debug)]
pub(crate) struct CookieTin {
inner: RwLock<CookieStore>,
}
/// RAII guard for read access to the CookieStore.
pub struct CookieStoreGuard<'a>(RwLockReadGuard<'a, CookieStore>);
impl<'a> Deref for CookieStoreGuard<'a> {
type Target = CookieStore;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl CookieTin {
pub(crate) fn new(store: CookieStore) -> Self {
CookieTin {
inner: RwLock::new(store),
}
}
pub(crate) fn read_lock(&self) -> CookieStoreGuard<'_> {
let lock = self.inner.read().unwrap();
CookieStoreGuard(lock)
}
pub(crate) fn get_request_cookies(&self, url: &Url) -> Vec<cookie::Cookie> {
let store = self.inner.read().unwrap();
store
.get_request_cookies(url)
.map(|c| cookie::Cookie::new(c.name().to_owned(), c.value().to_owned()))
.collect()
}
pub(crate) fn store_response_cookies<I>(&self, cookies: I, url: &Url)
where
I: Iterator<Item = cookie::Cookie<'static>>,
{
let mut store = self.inner.write().unwrap();
store.store_response_cookies(cookies, url);
}
}