reqwest/
cookie.rs

1//! HTTP Cookies
2
3use std::convert::TryInto;
4use std::fmt;
5use std::sync::RwLock;
6use std::time::SystemTime;
7
8use crate::header::{HeaderValue, SET_COOKIE};
9use bytes::Bytes;
10
11/// Actions for a persistent cookie store providing session support.
12pub trait CookieStore: Send + Sync {
13    /// Store a set of Set-Cookie header values received from `url`
14    fn set_cookies(&self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, url: &url::Url);
15    /// Get any Cookie values in the store for `url`
16    fn cookies(&self, url: &url::Url) -> Option<HeaderValue>;
17}
18
19/// A single HTTP cookie.
20pub struct Cookie<'a>(cookie_crate::Cookie<'a>);
21
22/// A good default `CookieStore` implementation.
23///
24/// This is the implementation used when simply calling `cookie_store(true)`.
25/// This type is exposed to allow creating one and filling it with some
26/// existing cookies more easily, before creating a `Client`.
27///
28/// For more advanced scenarios, such as needing to serialize the store or
29/// manipulate it between requests, you may refer to the
30/// [reqwest_cookie_store crate](https://crates.io/crates/reqwest_cookie_store).
31#[derive(Debug, Default)]
32pub struct Jar(RwLock<cookie_store::CookieStore>);
33
34// ===== impl Cookie =====
35
36impl<'a> Cookie<'a> {
37    fn parse(value: &'a HeaderValue) -> Result<Cookie<'a>, CookieParseError> {
38        std::str::from_utf8(value.as_bytes())
39            .map_err(cookie_crate::ParseError::from)
40            .and_then(cookie_crate::Cookie::parse)
41            .map_err(CookieParseError)
42            .map(Cookie)
43    }
44
45    /// The name of the cookie.
46    pub fn name(&self) -> &str {
47        self.0.name()
48    }
49
50    /// The value of the cookie.
51    pub fn value(&self) -> &str {
52        self.0.value()
53    }
54
55    /// Returns true if the 'HttpOnly' directive is enabled.
56    pub fn http_only(&self) -> bool {
57        self.0.http_only().unwrap_or(false)
58    }
59
60    /// Returns true if the 'Secure' directive is enabled.
61    pub fn secure(&self) -> bool {
62        self.0.secure().unwrap_or(false)
63    }
64
65    /// Returns true if  'SameSite' directive is 'Lax'.
66    pub fn same_site_lax(&self) -> bool {
67        self.0.same_site() == Some(cookie_crate::SameSite::Lax)
68    }
69
70    /// Returns true if  'SameSite' directive is 'Strict'.
71    pub fn same_site_strict(&self) -> bool {
72        self.0.same_site() == Some(cookie_crate::SameSite::Strict)
73    }
74
75    /// Returns the path directive of the cookie, if set.
76    pub fn path(&self) -> Option<&str> {
77        self.0.path()
78    }
79
80    /// Returns the domain directive of the cookie, if set.
81    pub fn domain(&self) -> Option<&str> {
82        self.0.domain()
83    }
84
85    /// Get the Max-Age information.
86    pub fn max_age(&self) -> Option<std::time::Duration> {
87        self.0.max_age().map(|d| {
88            d.try_into()
89                .expect("time::Duration into std::time::Duration")
90        })
91    }
92
93    /// The cookie expiration time.
94    pub fn expires(&self) -> Option<SystemTime> {
95        match self.0.expires() {
96            Some(cookie_crate::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
97            None | Some(cookie_crate::Expiration::Session) => None,
98        }
99    }
100}
101
102impl<'a> fmt::Debug for Cookie<'a> {
103    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104        self.0.fmt(f)
105    }
106}
107
108pub(crate) fn extract_response_cookie_headers<'a>(
109    headers: &'a hyper::HeaderMap,
110) -> impl Iterator<Item = &'a HeaderValue> + 'a {
111    headers.get_all(SET_COOKIE).iter()
112}
113
114pub(crate) fn extract_response_cookies<'a>(
115    headers: &'a hyper::HeaderMap,
116) -> impl Iterator<Item = Result<Cookie<'a>, CookieParseError>> + 'a {
117    headers
118        .get_all(SET_COOKIE)
119        .iter()
120        .map(|value| Cookie::parse(value))
121}
122
123/// Error representing a parse failure of a 'Set-Cookie' header.
124pub(crate) struct CookieParseError(cookie_crate::ParseError);
125
126impl<'a> fmt::Debug for CookieParseError {
127    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128        self.0.fmt(f)
129    }
130}
131
132impl<'a> fmt::Display for CookieParseError {
133    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134        self.0.fmt(f)
135    }
136}
137
138impl std::error::Error for CookieParseError {}
139
140// ===== impl Jar =====
141
142impl Jar {
143    /// Add a cookie to this jar.
144    ///
145    /// # Example
146    ///
147    /// ```
148    /// use reqwest::{cookie::Jar, Url};
149    ///
150    /// let cookie = "foo=bar; Domain=yolo.local";
151    /// let url = "https://yolo.local".parse::<Url>().unwrap();
152    ///
153    /// let jar = Jar::default();
154    /// jar.add_cookie_str(cookie, &url);
155    ///
156    /// // and now add to a `ClientBuilder`?
157    /// ```
158    pub fn add_cookie_str(&self, cookie: &str, url: &url::Url) {
159        let cookies = cookie_crate::Cookie::parse(cookie)
160            .ok()
161            .map(|c| c.into_owned())
162            .into_iter();
163        self.0.write().unwrap().store_response_cookies(cookies, url);
164    }
165}
166
167impl CookieStore for Jar {
168    fn set_cookies(&self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, url: &url::Url) {
169        let iter =
170            cookie_headers.filter_map(|val| Cookie::parse(val).map(|c| c.0.into_owned()).ok());
171
172        self.0.write().unwrap().store_response_cookies(iter, url);
173    }
174
175    fn cookies(&self, url: &url::Url) -> Option<HeaderValue> {
176        let s = self
177            .0
178            .read()
179            .unwrap()
180            .get_request_values(url)
181            .map(|(name, value)| format!("{name}={value}"))
182            .collect::<Vec<_>>()
183            .join("; ");
184
185        if s.is_empty() {
186            return None;
187        }
188
189        HeaderValue::from_maybe_shared(Bytes::from(s)).ok()
190    }
191}