use crate::{
Error, Result,
cookies::{Cookie, CookieAcceptPolicy},
};
use reqwest::cookie::CookieStore;
use std::sync::Arc;
use url::Url;
#[derive(Debug, Clone)]
pub struct ReqwestCookieStorage {
jar: Arc<reqwest::cookie::Jar>,
}
impl ReqwestCookieStorage {
pub fn new() -> Self {
Self {
jar: Arc::new(reqwest::cookie::Jar::default()),
}
}
pub fn all_cookies(&self) -> Vec<Cookie> {
Vec::new()
}
pub fn cookies_for_url(&self, url: &str) -> Result<Vec<Cookie>> {
let url = Url::parse(url).map_err(|_| Error::InvalidUrl)?;
let mut cookies = Vec::new();
if let Some(cookie_header) = self.jar.cookies(&url) {
if let Some(cookie_str) = cookie_header.to_str().ok() {
for cookie_part in cookie_str.split(';') {
let cookie_part = cookie_part.trim();
if let Some((name, value)) = cookie_part.split_once('=') {
cookies.push(Cookie::new(name.trim(), value.trim()));
}
}
}
}
Ok(cookies)
}
pub fn add_cookie(&self, cookie: Cookie) -> Result<()> {
let scheme = if cookie.secure { "https" } else { "http" };
let domain = if cookie.domain.is_empty() {
"localhost"
} else {
&cookie.domain
};
let url_str = format!("{}://{}{}", scheme, domain, cookie.path);
let url = Url::parse(&url_str).map_err(|_| Error::InvalidUrl)?;
let mut cookie_str = format!("{}={}", cookie.name, cookie.value);
if !cookie.domain.is_empty() {
cookie_str.push_str(&format!("; Domain={}", cookie.domain));
}
if cookie.path != "/" {
cookie_str.push_str(&format!("; Path={}", cookie.path));
}
if cookie.secure {
cookie_str.push_str("; Secure");
}
if cookie.http_only {
cookie_str.push_str("; HttpOnly");
}
if let Some(expires) = cookie.expires {
cookie_str.push_str(&format!("; Expires={}", expires));
}
self.jar.add_cookie_str(&cookie_str, &url);
Ok(())
}
pub fn remove_cookie(&self, _cookie: Cookie) -> Result<()> {
Ok(())
}
pub fn clear(&self) {
}
pub fn set_cookie_accept_policy(&self, _policy: CookieAcceptPolicy) {
}
}