Files
ureq/src/cookies.rs
2020-11-08 11:31:49 +01:00

57 lines
1.4 KiB
Rust

#[cfg(feature = "cookies")]
use {
std::ops::Deref,
std::sync::{RwLock, RwLockReadGuard},
};
#[cfg(feature = "cookies")]
use cookie_store::CookieStore;
#[cfg(feature = "cookies")]
use url::Url;
#[cfg(feature = "cookies")]
#[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
}
}
#[cfg(feature = "cookies")]
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)
}
}