use crate::header::{HeaderValue, SET_COOKIE};
#[cfg(feature = "cookies")]
use antidote::RwLock;
pub use cookie_crate::{Cookie as RawCookie, Expiration, SameSite, time::Duration};
use std::borrow::Cow;
use std::convert::TryInto;
use std::fmt;
use std::time::SystemTime;
pub trait CookieStore: Send + Sync {
fn set_cookies(&self, url: &url::Url, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>);
fn set_cookie(&self, _url: &url::Url, _cookie: &dyn IntoCookie) {}
fn cookies(&self, url: &url::Url) -> Option<HeaderValue>;
fn remove(&self, _url: &url::Url, _name: &str) {}
fn clear(&self) {}
}
pub trait IntoCookie {
fn into(&self) -> Result<Cow<'_, Cookie<'_>>, crate::Error>;
}
#[derive(Debug, Clone)]
pub struct Cookie<'a>(RawCookie<'a>);
#[derive(Debug, Clone)]
pub struct CookieBuilder<'a>(cookie_crate::CookieBuilder<'a>);
#[cfg(feature = "cookies")]
#[derive(Debug)]
pub struct Jar(RwLock<cookie_store::CookieStore>);
impl<'a> Cookie<'a> {
pub fn parse<V>(value: &'a V) -> Result<Cookie<'a>, crate::Error>
where
V: AsRef<[u8]> + ?Sized,
{
std::str::from_utf8(value.as_ref())
.map_err(cookie_crate::ParseError::from)
.and_then(RawCookie::parse)
.map(Cookie)
.map_err(Into::into)
}
#[inline(always)]
pub fn builder<N, V>(name: N, value: V) -> CookieBuilder<'a>
where
N: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
CookieBuilder::new(name, value)
}
#[inline(always)]
pub fn new<N, V>(name: N, value: V) -> Cookie<'a>
where
N: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
Cookie(RawCookie::new(name, value))
}
#[inline(always)]
pub fn name(&self) -> &str {
self.0.name()
}
#[inline(always)]
pub fn value(&self) -> &str {
self.0.value()
}
#[inline(always)]
pub fn http_only(&self) -> bool {
self.0.http_only().unwrap_or(false)
}
#[inline(always)]
pub fn secure(&self) -> bool {
self.0.secure().unwrap_or(false)
}
#[inline(always)]
pub fn same_site_lax(&self) -> bool {
self.0.same_site() == Some(cookie_crate::SameSite::Lax)
}
#[inline(always)]
pub fn same_site_strict(&self) -> bool {
self.0.same_site() == Some(cookie_crate::SameSite::Strict)
}
#[inline(always)]
pub fn path(&self) -> Option<&str> {
self.0.path()
}
#[inline(always)]
pub fn domain(&self) -> Option<&str> {
self.0.domain()
}
#[inline(always)]
pub fn max_age(&self) -> Option<std::time::Duration> {
self.0.max_age().and_then(|d| d.try_into().ok())
}
#[inline(always)]
pub fn expires(&self) -> Option<SystemTime> {
match self.0.expires() {
Some(cookie_crate::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
None | Some(cookie_crate::Expiration::Session) => None,
}
}
#[inline(always)]
pub fn into_owned(self) -> Cookie<'static> {
Cookie(self.0.into_owned())
}
#[inline(always)]
pub fn into_inner(self) -> RawCookie<'a> {
self.0
}
}
impl fmt::Display for Cookie<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'c> CookieBuilder<'c> {
pub fn new<N, V>(name: N, value: V) -> Self
where
N: Into<Cow<'c, str>>,
V: Into<Cow<'c, str>>,
{
CookieBuilder(cookie_crate::CookieBuilder::new(name, value))
}
#[inline(always)]
pub fn http_only(mut self, enabled: bool) -> Self {
self.0 = self.0.http_only(enabled);
self
}
#[inline(always)]
pub fn secure(mut self, enabled: bool) -> Self {
self.0 = self.0.secure(enabled);
self
}
#[inline(always)]
pub fn same_site(mut self, same_site: cookie_crate::SameSite) -> Self {
self.0 = self.0.same_site(same_site);
self
}
#[inline(always)]
pub fn path<P>(mut self, path: P) -> Self
where
P: Into<Cow<'c, str>>,
{
self.0 = self.0.path(path);
self
}
#[inline(always)]
pub fn domain<D>(mut self, domain: D) -> Self
where
D: Into<Cow<'c, str>>,
{
self.0 = self.0.domain(domain);
self
}
#[inline(always)]
pub fn max_age(mut self, max_age: Duration) -> Self {
self.0 = self.0.max_age(max_age);
self
}
#[inline(always)]
pub fn expires<E>(mut self, expires: E) -> Self
where
E: Into<Expiration>,
{
self.0 = self.0.expires(expires);
self
}
#[inline(always)]
pub fn build(self) -> Cookie<'c> {
Cookie(self.0.build())
}
}
pub(crate) fn extract_response_cookie_headers(
headers: &hyper2::HeaderMap,
) -> impl Iterator<Item = &'_ HeaderValue> {
headers.get_all(SET_COOKIE).iter()
}
pub(crate) fn extract_response_cookies(
headers: &hyper2::HeaderMap,
) -> impl Iterator<Item = Result<Cookie<'_>, crate::Error>> {
headers.get_all(SET_COOKIE).iter().map(Cookie::parse)
}
impl IntoCookie for &HeaderValue {
#[inline]
fn into(&self) -> Result<Cow<'_, Cookie<'_>>, crate::Error> {
Cookie::parse(self).map(Cow::Owned)
}
}
impl IntoCookie for HeaderValue {
#[inline]
fn into(&self) -> Result<Cow<'_, Cookie<'_>>, crate::Error> {
Cookie::parse(self).map(Cow::Owned)
}
}
impl IntoCookie for &Cookie<'_> {
#[inline]
fn into(&self) -> Result<Cow<'_, Cookie<'_>>, crate::Error> {
Ok(Cow::Borrowed(self))
}
}
impl IntoCookie for Cookie<'_> {
#[inline]
fn into(&self) -> Result<Cow<'_, Cookie<'_>>, crate::Error> {
Ok(Cow::Borrowed(self))
}
}
#[cfg(feature = "cookies")]
impl Jar {
pub fn add_cookie_str(&self, cookie: &str, url: &url::Url) {
let cookies = RawCookie::parse(cookie)
.ok()
.map(|c| c.into_owned())
.into_iter();
self.0.write().store_response_cookies(cookies, url);
}
}
#[cfg(feature = "cookies")]
impl CookieStore for Jar {
fn set_cookies(&self, url: &url::Url, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>) {
let iter = cookie_headers
.filter_map(|val| Cookie::parse(val.as_bytes()).map(|c| c.0.into_owned()).ok());
self.0.write().store_response_cookies(iter, url);
}
fn cookies(&self, url: &url::Url) -> Option<HeaderValue> {
let lock = self.0.read();
let mut iter = lock.get_request_values(url);
let (first_name, first_value) = iter.next()?;
let mut cookie = String::with_capacity(32);
cookie.push_str(first_name);
cookie.push('=');
cookie.push_str(first_value);
for (name, value) in iter {
cookie.push_str("; ");
cookie.push_str(name);
cookie.push('=');
cookie.push_str(value);
}
HeaderValue::from_maybe_shared(bytes::Bytes::from(cookie)).ok()
}
fn set_cookie<'c>(&self, url: &url::Url, cookie: &dyn IntoCookie) {
if let Ok(cookie) = cookie.into() {
let _ = self.0.write().insert_raw(&cookie.0, url);
}
}
fn remove(&self, url: &url::Url, name: &str) {
if let Some(domain) = url.host_str() {
self.0.write().remove(domain, url.path(), name);
}
}
fn clear(&self) {
self.0.write().clear();
}
}
#[cfg(feature = "cookies")]
impl Default for Jar {
fn default() -> Self {
Self(RwLock::new(cookie_store::CookieStore::default()))
}
}