wreq-rb 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +47 -21
- data/README.md +36 -0
- data/ext/wreq_rb/Cargo.toml +7 -4
- data/ext/wreq_rb/extconf.rb +10 -3
- data/ext/wreq_rb/src/client.rs +145 -14
- data/ext/wreq_rb/src/response.rs +28 -3
- data/lib/wreq-rb/version.rb +1 -1
- data/patches/0002-add-preconnect.patch +266 -0
- data/vendor/wreq/Cargo.toml +11 -7
- data/vendor/wreq/bench/support/server.rs +2 -0
- data/vendor/wreq/src/client/layer/redirect/future.rs +1 -1
- data/vendor/wreq/src/client/layer/timeout/body.rs +16 -18
- data/vendor/wreq/src/client/layer/timeout/future.rs +2 -2
- data/vendor/wreq/src/client/request.rs +27 -15
- data/vendor/wreq/src/client/ws.rs +4 -3
- data/vendor/wreq/src/client.rs +4 -24
- data/vendor/wreq/src/conn/net/tcp.rs +16 -66
- data/vendor/wreq/src/conn/proxy/socks.rs +11 -1
- data/vendor/wreq/src/conn/proxy/tunnel.rs +14 -11
- data/vendor/wreq/src/conn/timeout.rs +1 -1
- data/vendor/wreq/src/cookie/jar.rs +170 -36
- data/vendor/wreq/src/cookie/store.rs +66 -4
- data/vendor/wreq/src/cookie.rs +48 -17
- data/vendor/wreq/src/dns.rs +2 -4
- data/vendor/wreq/src/error.rs +100 -0
- data/vendor/wreq/src/ext.rs +0 -49
- data/vendor/wreq/src/lib.rs +2 -0
- data/vendor/wreq/src/rt.rs +1 -14
- data/vendor/wreq/src/tls/trust/store.rs +35 -2
- data/vendor/wreq/tests/badssl.rs +3 -239
- data/vendor/wreq/tests/cert_store.rs +168 -0
- data/vendor/wreq/tests/client.rs +89 -0
- data/vendor/wreq/tests/proxy.rs +149 -3
- data/vendor/wreq/tests/redirect.rs +35 -0
- data/vendor/wreq/tests/support/tls_cert.rs +56 -0
- data/vendor/wreq/tests/timeouts.rs +25 -0
- data/vendor/wreq/tests/unix_socket.rs +109 -81
- data/vendor/wreq/tests/websocket.rs +40 -0
- metadata +9 -6
- data/vendor/wreq/examples/rt.rs +0 -23
|
@@ -716,59 +716,26 @@ pub(crate) struct TcpOptions {
|
|
|
716
716
|
#[derive(Default, Debug, Clone, Copy)]
|
|
717
717
|
pub(crate) struct TcpKeepaliveOptions {
|
|
718
718
|
pub time: Option<Duration>,
|
|
719
|
-
#[cfg(any(
|
|
720
|
-
target_os = "android",
|
|
721
|
-
target_os = "dragonfly",
|
|
722
|
-
target_os = "freebsd",
|
|
723
|
-
target_os = "fuchsia",
|
|
724
|
-
target_os = "illumos",
|
|
725
|
-
target_os = "ios",
|
|
726
|
-
target_os = "visionos",
|
|
727
|
-
target_os = "linux",
|
|
728
|
-
target_os = "macos",
|
|
729
|
-
target_os = "netbsd",
|
|
730
|
-
target_os = "tvos",
|
|
731
|
-
target_os = "watchos",
|
|
732
|
-
target_os = "windows",
|
|
733
|
-
target_os = "cygwin",
|
|
734
|
-
))]
|
|
735
719
|
pub interval: Option<Duration>,
|
|
736
|
-
#[cfg(any(
|
|
737
|
-
target_os = "android",
|
|
738
|
-
target_os = "dragonfly",
|
|
739
|
-
target_os = "freebsd",
|
|
740
|
-
target_os = "fuchsia",
|
|
741
|
-
target_os = "illumos",
|
|
742
|
-
target_os = "ios",
|
|
743
|
-
target_os = "visionos",
|
|
744
|
-
target_os = "linux",
|
|
745
|
-
target_os = "macos",
|
|
746
|
-
target_os = "netbsd",
|
|
747
|
-
target_os = "tvos",
|
|
748
|
-
target_os = "watchos",
|
|
749
|
-
target_os = "cygwin",
|
|
750
|
-
target_os = "windows",
|
|
751
|
-
))]
|
|
752
720
|
pub retries: Option<u32>,
|
|
753
721
|
}
|
|
754
722
|
|
|
755
723
|
impl TcpKeepaliveOptions {
|
|
756
724
|
/// Converts into a `socket2::TcpKeealive` if there is any keep alive configuration.
|
|
757
725
|
pub(crate) fn into_tcpkeepalive(self) -> Option<TcpKeepalive> {
|
|
726
|
+
let Self {
|
|
727
|
+
time,
|
|
728
|
+
interval: _interval,
|
|
729
|
+
retries: _retries,
|
|
730
|
+
} = self;
|
|
731
|
+
|
|
758
732
|
let mut dirty = false;
|
|
759
733
|
let mut ka = TcpKeepalive::new();
|
|
760
|
-
if let Some(time) =
|
|
734
|
+
if let Some(time) = time {
|
|
761
735
|
ka = ka.with_time(time);
|
|
762
736
|
dirty = true
|
|
763
737
|
}
|
|
764
738
|
|
|
765
|
-
// Set the value of the `TCP_KEEPINTVL` option. On Windows, this sets the
|
|
766
|
-
// value of the `tcp_keepalive` struct's `keepaliveinterval` field.
|
|
767
|
-
//
|
|
768
|
-
// Sets the time interval between TCP keepalive probes.
|
|
769
|
-
//
|
|
770
|
-
// Some platforms specify this value in seconds, so sub-second
|
|
771
|
-
// specifications may be omitted.
|
|
772
739
|
#[cfg(any(
|
|
773
740
|
target_os = "android",
|
|
774
741
|
target_os = "dragonfly",
|
|
@@ -786,36 +753,19 @@ impl TcpKeepaliveOptions {
|
|
|
786
753
|
target_os = "cygwin",
|
|
787
754
|
))]
|
|
788
755
|
{
|
|
789
|
-
|
|
756
|
+
// Set the time between TCP keepalive probes. Some platforms specify
|
|
757
|
+
// this value in seconds, so sub-second values may be omitted.
|
|
758
|
+
if let Some(interval) = _interval {
|
|
790
759
|
dirty = true;
|
|
791
760
|
ka = ka.with_interval(interval)
|
|
792
761
|
};
|
|
793
|
-
}
|
|
794
762
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
target_os = "dragonfly",
|
|
802
|
-
target_os = "freebsd",
|
|
803
|
-
target_os = "fuchsia",
|
|
804
|
-
target_os = "illumos",
|
|
805
|
-
target_os = "ios",
|
|
806
|
-
target_os = "visionos",
|
|
807
|
-
target_os = "linux",
|
|
808
|
-
target_os = "macos",
|
|
809
|
-
target_os = "netbsd",
|
|
810
|
-
target_os = "tvos",
|
|
811
|
-
target_os = "watchos",
|
|
812
|
-
target_os = "cygwin",
|
|
813
|
-
target_os = "windows",
|
|
814
|
-
))]
|
|
815
|
-
if let Some(retries) = self.retries {
|
|
816
|
-
dirty = true;
|
|
817
|
-
ka = ka.with_retries(retries)
|
|
818
|
-
};
|
|
763
|
+
// Set how many unanswered probes are sent before dropping the connection.
|
|
764
|
+
if let Some(retries) = _retries {
|
|
765
|
+
dirty = true;
|
|
766
|
+
ka = ka.with_retries(retries)
|
|
767
|
+
};
|
|
768
|
+
}
|
|
819
769
|
|
|
820
770
|
if dirty { Some(ka) } else { None }
|
|
821
771
|
}
|
|
@@ -52,7 +52,17 @@ impl std::fmt::Display for SocksError {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
impl std::error::Error for SocksError {
|
|
55
|
+
impl std::error::Error for SocksError {
|
|
56
|
+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
57
|
+
match self {
|
|
58
|
+
Self::ConnectFailed(e) | Self::DnsResolveFailure(e) => Some(&**e),
|
|
59
|
+
Self::Socks(e) => Some(e),
|
|
60
|
+
Self::Io(e) => Some(e),
|
|
61
|
+
Self::Utf8(e) => Some(e),
|
|
62
|
+
Self::DnsFailure | Self::MissingHost => None,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
56
66
|
|
|
57
67
|
impl From<std::io::Error> for SocksError {
|
|
58
68
|
fn from(err: std::io::Error) -> Self {
|
|
@@ -38,7 +38,7 @@ pub enum TunnelError {
|
|
|
38
38
|
MissingHost,
|
|
39
39
|
ProxyAuthRequired,
|
|
40
40
|
TunnelUnexpectedEof,
|
|
41
|
-
TunnelUnsuccessful,
|
|
41
|
+
TunnelUnsuccessful(Option<u16>),
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
impl<C> TunnelConnector<C> {
|
|
@@ -191,7 +191,7 @@ where
|
|
|
191
191
|
httparse::Status::Complete(_) => match res.code {
|
|
192
192
|
Some(200) => return Ok(conn),
|
|
193
193
|
Some(407) => return Err(TunnelError::ProxyAuthRequired),
|
|
194
|
-
|
|
194
|
+
status => return Err(TunnelError::TunnelUnsuccessful(status)),
|
|
195
195
|
},
|
|
196
196
|
}
|
|
197
197
|
}
|
|
@@ -201,15 +201,18 @@ impl std::fmt::Display for TunnelError {
|
|
|
201
201
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
202
202
|
f.write_str("tunnel error: ")?;
|
|
203
203
|
|
|
204
|
-
|
|
205
|
-
TunnelError::MissingHost => "missing destination host",
|
|
206
|
-
TunnelError::ProxyAuthRequired => "proxy authorization required",
|
|
207
|
-
TunnelError::Parse(_) => "invalid proxy response",
|
|
208
|
-
TunnelError::TunnelUnexpectedEof => "unexpected end of file",
|
|
209
|
-
TunnelError::TunnelUnsuccessful =>
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
204
|
+
match self {
|
|
205
|
+
TunnelError::MissingHost => f.write_str("missing destination host"),
|
|
206
|
+
TunnelError::ProxyAuthRequired => f.write_str("proxy authorization required"),
|
|
207
|
+
TunnelError::Parse(_) => f.write_str("invalid proxy response"),
|
|
208
|
+
TunnelError::TunnelUnexpectedEof => f.write_str("unexpected end of file"),
|
|
209
|
+
TunnelError::TunnelUnsuccessful(Some(status)) => {
|
|
210
|
+
write!(f, "unsuccessful (status {status})")
|
|
211
|
+
}
|
|
212
|
+
TunnelError::TunnelUnsuccessful(None) => f.write_str("unsuccessful"),
|
|
213
|
+
TunnelError::ConnectFailed(_) => f.write_str("failed to create underlying connection"),
|
|
214
|
+
TunnelError::Io(_) => f.write_str("io error establishing tunnel"),
|
|
215
|
+
}
|
|
213
216
|
}
|
|
214
217
|
}
|
|
215
218
|
|
|
@@ -155,7 +155,7 @@ mod tests {
|
|
|
155
155
|
TimeoutLayer::new(Timer::new(ReadyTimer), Some(Duration::from_millis(50)))
|
|
156
156
|
.layer(tower::service_fn(|()| pending::<Result<(), BoxError>>()));
|
|
157
157
|
|
|
158
|
-
let mut future =
|
|
158
|
+
let mut future = std::pin::pin!(service.call(()));
|
|
159
159
|
let mut cx = Context::from_waker(Waker::noop());
|
|
160
160
|
let Poll::Ready(Err(error)) = future.as_mut().poll(&mut cx) else {
|
|
161
161
|
panic!("connect timeout did not complete");
|
|
@@ -5,6 +5,8 @@ use cookie::{
|
|
|
5
5
|
};
|
|
6
6
|
use http::{Uri, Version};
|
|
7
7
|
|
|
8
|
+
#[cfg(feature = "cookies-psl")]
|
|
9
|
+
use super::store::is_public_suffix;
|
|
8
10
|
use super::{
|
|
9
11
|
Cookie, CookieStore, Cookies, IntoCookie,
|
|
10
12
|
store::{DEFAULT_PATH, Store, canonical_host, cookie_is_expired, domain_match, normalize_path},
|
|
@@ -49,7 +51,7 @@ impl Jar {
|
|
|
49
51
|
let host = canonical_host(uri.host()?)?;
|
|
50
52
|
let now = OffsetDateTime::now_utc();
|
|
51
53
|
let store = self.0.read();
|
|
52
|
-
let
|
|
54
|
+
let entry = store
|
|
53
55
|
.cookies
|
|
54
56
|
.get(&host)?
|
|
55
57
|
.get(uri.path())?
|
|
@@ -57,7 +59,7 @@ impl Jar {
|
|
|
57
59
|
.filter(|entry| !cookie_is_expired(&entry.cookie, now))
|
|
58
60
|
.min_by_key(|entry| entry.creation_index)?;
|
|
59
61
|
|
|
60
|
-
Some(Cookie::from(
|
|
62
|
+
Some(Cookie::from(entry.cookie.clone()).with_host(host))
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
/// Returns whether an unexpired cookie exists for an exact URI scope.
|
|
@@ -70,6 +72,7 @@ impl Jar {
|
|
|
70
72
|
let Some(host) = uri.host().and_then(canonical_host) else {
|
|
71
73
|
return false;
|
|
72
74
|
};
|
|
75
|
+
|
|
73
76
|
let now = OffsetDateTime::now_utc();
|
|
74
77
|
|
|
75
78
|
self.0
|
|
@@ -84,20 +87,26 @@ impl Jar {
|
|
|
84
87
|
})
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
/// Returns all unexpired cookies in
|
|
90
|
+
/// Returns all unexpired cookies in creation order.
|
|
88
91
|
///
|
|
89
|
-
///
|
|
90
|
-
///
|
|
91
|
-
/// not broaden its domain scope. Snapshots are returned in creation order.
|
|
92
|
+
/// Each snapshot preserves its effective `Path` and canonical storage host. Host-only cookies
|
|
93
|
+
/// keep `Domain` absent; use [`Cookie::host`] when restoring them.
|
|
92
94
|
///
|
|
93
95
|
/// # Example
|
|
94
96
|
/// ```
|
|
95
97
|
/// use wreq::cookie::Jar;
|
|
96
|
-
/// let
|
|
97
|
-
///
|
|
98
|
-
///
|
|
99
|
-
///
|
|
98
|
+
/// let source = Jar::default();
|
|
99
|
+
/// source.add("session=abc; Secure", "https://example.com");
|
|
100
|
+
///
|
|
101
|
+
/// let target = Jar::default();
|
|
102
|
+
/// for cookie in source.get_all() {
|
|
103
|
+
/// if let Some(host) = cookie.host() {
|
|
104
|
+
/// let scheme = if cookie.secure() { "https" } else { "http" };
|
|
105
|
+
/// let uri = format!("{scheme}://{host}/");
|
|
106
|
+
/// target.add(cookie, uri);
|
|
107
|
+
/// }
|
|
100
108
|
/// }
|
|
109
|
+
/// assert_eq!(target.get_all().count(), 1);
|
|
101
110
|
/// ```
|
|
102
111
|
pub fn get_all(&self) -> impl Iterator<Item = Cookie<'static>> {
|
|
103
112
|
let now = OffsetDateTime::now_utc();
|
|
@@ -105,15 +114,16 @@ impl Jar {
|
|
|
105
114
|
.0
|
|
106
115
|
.read()
|
|
107
116
|
.cookies
|
|
108
|
-
.
|
|
109
|
-
.flat_map(|path_map| {
|
|
110
|
-
path_map.values().flat_map(|cookie_map| {
|
|
111
|
-
cookie_map.values().filter_map(|entry| {
|
|
117
|
+
.iter()
|
|
118
|
+
.flat_map(|(host, path_map)| {
|
|
119
|
+
path_map.values().flat_map(move |cookie_map| {
|
|
120
|
+
cookie_map.values().filter_map(move |entry| {
|
|
112
121
|
if cookie_is_expired(&entry.cookie, now) {
|
|
113
122
|
return None;
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
|
|
125
|
+
let cookie = Cookie::from(entry.cookie.clone()).with_host(host.clone());
|
|
126
|
+
Some((entry.creation_index, cookie))
|
|
117
127
|
})
|
|
118
128
|
})
|
|
119
129
|
})
|
|
@@ -187,7 +197,7 @@ impl Jar {
|
|
|
187
197
|
let store = self.0.read();
|
|
188
198
|
store
|
|
189
199
|
.matching_cookies(&uri, &host, now)
|
|
190
|
-
.map(|(
|
|
200
|
+
.map(|(host, _, entry)| Cookie::from(entry.cookie.clone()).with_host(host.clone()))
|
|
191
201
|
.collect::<Vec<_>>()
|
|
192
202
|
.into_iter()
|
|
193
203
|
}
|
|
@@ -234,8 +244,6 @@ impl Jar {
|
|
|
234
244
|
let Some(host) = uri.host().and_then(canonical_host) else {
|
|
235
245
|
return;
|
|
236
246
|
};
|
|
237
|
-
let host_only = cookie.domain().is_none();
|
|
238
|
-
|
|
239
247
|
// If the canonicalized request-host does not domain-match the
|
|
240
248
|
// domain-attribute:
|
|
241
249
|
// Ignore the cookie entirely and abort these steps.
|
|
@@ -251,11 +259,24 @@ impl Jar {
|
|
|
251
259
|
return;
|
|
252
260
|
}
|
|
253
261
|
|
|
254
|
-
|
|
262
|
+
#[cfg(feature = "cookies-psl")]
|
|
263
|
+
let public_suffix = is_public_suffix(&domain);
|
|
264
|
+
#[cfg(not(feature = "cookies-psl"))]
|
|
265
|
+
let public_suffix = false;
|
|
266
|
+
|
|
267
|
+
if public_suffix {
|
|
268
|
+
if domain != host {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
cookie.unset_domain();
|
|
272
|
+
} else {
|
|
273
|
+
cookie.set_domain(domain.to_string());
|
|
274
|
+
}
|
|
255
275
|
domain
|
|
256
276
|
} else {
|
|
257
277
|
host
|
|
258
278
|
};
|
|
279
|
+
let host_only = cookie.domain().is_none();
|
|
259
280
|
|
|
260
281
|
// Max-Age takes precedence over Expires and is relative to when the cookie is
|
|
261
282
|
// received. Store its effective deadline so every read path applies the same
|
|
@@ -294,6 +315,7 @@ impl Jar {
|
|
|
294
315
|
.to_owned();
|
|
295
316
|
|
|
296
317
|
let mut store = self.0.write();
|
|
318
|
+
store.evict_expired(now);
|
|
297
319
|
if !secure_origin
|
|
298
320
|
&& store.would_overlay_secure_cookie(cookie.name(), &domain, &path, now)
|
|
299
321
|
{
|
|
@@ -304,7 +326,7 @@ impl Jar {
|
|
|
304
326
|
store.remove_stored_cookie(&domain, &path, cookie.name(), host_only);
|
|
305
327
|
} else {
|
|
306
328
|
cookie.set_path(path.clone());
|
|
307
|
-
store.insert_stored_cookie(domain, path, cookie);
|
|
329
|
+
store.insert_stored_cookie(domain, path, cookie, now);
|
|
308
330
|
}
|
|
309
331
|
}
|
|
310
332
|
}
|
|
@@ -335,6 +357,7 @@ impl Jar {
|
|
|
335
357
|
};
|
|
336
358
|
let cookie = cookie.into();
|
|
337
359
|
let mut store = self.0.write();
|
|
360
|
+
store.evict_expired(OffsetDateTime::now_utc());
|
|
338
361
|
store.remove_stored_cookies(&host, uri.path(), cookie.name());
|
|
339
362
|
}
|
|
340
363
|
}
|
|
@@ -434,9 +457,14 @@ impl CookieStore for Jar {
|
|
|
434
457
|
|
|
435
458
|
#[cfg(test)]
|
|
436
459
|
mod tests {
|
|
437
|
-
use std::{
|
|
460
|
+
use std::{
|
|
461
|
+
net::{Ipv4Addr, Ipv6Addr},
|
|
462
|
+
thread,
|
|
463
|
+
time::Duration as StdDuration,
|
|
464
|
+
};
|
|
438
465
|
|
|
439
466
|
use http::{Uri, Version};
|
|
467
|
+
use url::Host;
|
|
440
468
|
|
|
441
469
|
use super::{CookieStore, Cookies, Jar};
|
|
442
470
|
|
|
@@ -514,6 +542,37 @@ mod tests {
|
|
|
514
542
|
assert_eq!(cookies[0].domain(), Some("example.com"));
|
|
515
543
|
}
|
|
516
544
|
|
|
545
|
+
#[test]
|
|
546
|
+
fn jar_applies_public_suffix_policy_only_with_psl() {
|
|
547
|
+
let psl_enabled = cfg!(feature = "cookies-psl");
|
|
548
|
+
for (cookie, uri) in [
|
|
549
|
+
("session=abc; Domain=com", "https://example.com/"),
|
|
550
|
+
("session=abc; Domain=com.", "https://example.com./"),
|
|
551
|
+
("session=abc; Domain=co.uk", "https://example.co.uk/"),
|
|
552
|
+
(
|
|
553
|
+
"session=abc; Domain=github.io",
|
|
554
|
+
"https://example.github.io/",
|
|
555
|
+
),
|
|
556
|
+
] {
|
|
557
|
+
let jar = Jar::default();
|
|
558
|
+
jar.add(cookie, uri);
|
|
559
|
+
assert_eq!(jar.is_empty(), psl_enabled, "{cookie} from {uri}");
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
let jar = Jar::default();
|
|
563
|
+
let origin = "https://co.uk/";
|
|
564
|
+
jar.add("session=abc; Domain=co.uk", origin);
|
|
565
|
+
let cookie = jar.get("session", origin).unwrap();
|
|
566
|
+
assert_eq!(cookie.domain().is_none(), psl_enabled);
|
|
567
|
+
assert_eq!(
|
|
568
|
+
jar.matches("https://sub.co.uk/").next().is_none(),
|
|
569
|
+
psl_enabled
|
|
570
|
+
);
|
|
571
|
+
|
|
572
|
+
jar.add("session=abc; Domain=co.uk; Max-Age=0", origin);
|
|
573
|
+
assert!(jar.is_empty());
|
|
574
|
+
}
|
|
575
|
+
|
|
517
576
|
#[test]
|
|
518
577
|
fn jar_rejects_secure_cookie_from_insecure_origin() {
|
|
519
578
|
let jar = Jar::default();
|
|
@@ -602,30 +661,60 @@ mod tests {
|
|
|
602
661
|
}
|
|
603
662
|
|
|
604
663
|
#[test]
|
|
605
|
-
fn
|
|
664
|
+
fn jar_get_all_preserves_storage_scope() {
|
|
606
665
|
let source = Jar::default();
|
|
607
666
|
source.add("session=abc", "http://example.com/foo/bar");
|
|
667
|
+
source.add("pref=dark; Domain=example.com", "http://www.example.com/");
|
|
668
|
+
source.add("secure=1; Secure", "https://secure.example/");
|
|
669
|
+
source.add("v6=1", "http://[::1]:8080/");
|
|
670
|
+
source.add("v4=1", "http://127.0.0.1:8080/");
|
|
608
671
|
|
|
609
672
|
let exported = source.get_all().collect::<Vec<_>>();
|
|
610
|
-
assert_eq!(exported.len(),
|
|
611
|
-
|
|
612
|
-
|
|
673
|
+
assert_eq!(exported.len(), 5);
|
|
674
|
+
|
|
675
|
+
let session = &exported[0];
|
|
676
|
+
assert_eq!(session.host(), Some(Host::Domain("example.com")));
|
|
677
|
+
assert_eq!(session.domain(), None);
|
|
678
|
+
assert_eq!(session.path(), Some("/foo"));
|
|
679
|
+
|
|
680
|
+
let pref = &exported[1];
|
|
681
|
+
assert_eq!(pref.host(), Some(Host::Domain("example.com")));
|
|
682
|
+
assert_eq!(pref.domain(), Some("example.com"));
|
|
683
|
+
assert_eq!(exported[2].host(), Some(Host::Domain("secure.example")));
|
|
684
|
+
assert_eq!(exported[3].host(), Some(Host::Ipv6(Ipv6Addr::LOCALHOST)));
|
|
685
|
+
assert_eq!(exported[4].host(), Some(Host::Ipv4(Ipv4Addr::LOCALHOST)));
|
|
686
|
+
|
|
687
|
+
let exact = source.get("session", "http://example.com/foo").unwrap();
|
|
688
|
+
assert_eq!(exact.host(), Some(Host::Domain("example.com")));
|
|
689
|
+
|
|
690
|
+
let matched = source
|
|
691
|
+
.matches("http://example.com/foo/bar")
|
|
692
|
+
.find(|cookie| cookie.name() == "session")
|
|
693
|
+
.unwrap();
|
|
694
|
+
assert_eq!(matched.host(), Some(Host::Domain("example.com")));
|
|
613
695
|
|
|
614
696
|
let target = Jar::default();
|
|
615
697
|
for cookie in exported {
|
|
616
|
-
|
|
698
|
+
let scheme = if cookie.secure() { "https" } else { "http" };
|
|
699
|
+
let uri = format!("{scheme}://{}/", cookie.host().unwrap());
|
|
700
|
+
target.add(cookie, uri);
|
|
617
701
|
}
|
|
618
702
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
703
|
+
for uri in [
|
|
704
|
+
"http://example.com/foo/bar",
|
|
705
|
+
"http://api.example.com/",
|
|
706
|
+
"https://secure.example/",
|
|
707
|
+
"http://secure.example/",
|
|
708
|
+
"http://[::1]:9999/",
|
|
709
|
+
"http://127.0.0.1:9999/",
|
|
710
|
+
] {
|
|
711
|
+
let uri = uri.parse::<Uri>().unwrap();
|
|
712
|
+
assert_eq!(
|
|
713
|
+
format!("{:?}", target.cookies(&uri, Version::HTTP_11)),
|
|
714
|
+
format!("{:?}", source.cookies(&uri, Version::HTTP_11)),
|
|
715
|
+
"restored jar should select the same cookies for {uri}"
|
|
716
|
+
);
|
|
717
|
+
}
|
|
629
718
|
}
|
|
630
719
|
|
|
631
720
|
#[test]
|
|
@@ -946,6 +1035,51 @@ mod tests {
|
|
|
946
1035
|
}
|
|
947
1036
|
}
|
|
948
1037
|
|
|
1038
|
+
#[test]
|
|
1039
|
+
fn jar_evicts_expired_cookies_and_reorders_reinserted_cookies() {
|
|
1040
|
+
use cookie::{
|
|
1041
|
+
Cookie as RawCookie,
|
|
1042
|
+
time::{Duration, OffsetDateTime},
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
use super::canonical_host;
|
|
1046
|
+
|
|
1047
|
+
let jar = Jar::default();
|
|
1048
|
+
let host = canonical_host("example.com").unwrap();
|
|
1049
|
+
let expired_host = canonical_host("expired.example.com").unwrap();
|
|
1050
|
+
let now = OffsetDateTime::UNIX_EPOCH;
|
|
1051
|
+
let first_expiry = now + Duration::seconds(1);
|
|
1052
|
+
let expiry = first_expiry + Duration::seconds(1);
|
|
1053
|
+
{
|
|
1054
|
+
let mut store = jar.0.write();
|
|
1055
|
+
let mut obsolete = RawCookie::new("obsolete", "1");
|
|
1056
|
+
obsolete.set_expires(first_expiry);
|
|
1057
|
+
store.insert_stored_cookie(expired_host.clone(), "/".into(), obsolete.clone(), now);
|
|
1058
|
+
store.insert_stored_cookie(host.clone(), "/obsolete".into(), obsolete, now);
|
|
1059
|
+
|
|
1060
|
+
let mut session = RawCookie::new("session", "1");
|
|
1061
|
+
session.set_expires(expiry);
|
|
1062
|
+
store.insert_stored_cookie(host.clone(), "/".into(), session, now);
|
|
1063
|
+
store.insert_stored_cookie(host.clone(), "/".into(), RawCookie::new("other", "1"), now);
|
|
1064
|
+
|
|
1065
|
+
store.evict_expired(first_expiry);
|
|
1066
|
+
assert!(!store.cookies.contains_key(&expired_host));
|
|
1067
|
+
assert!(!store.cookies[&host].contains_key("/obsolete"));
|
|
1068
|
+
|
|
1069
|
+
// The next sweep is rate limited, so insertion must recognize the expired entry.
|
|
1070
|
+
store.evict_expired(expiry);
|
|
1071
|
+
let scope = &store.cookies[&host]["/"];
|
|
1072
|
+
assert!(scope.entries("session").next().is_some());
|
|
1073
|
+
store.insert_stored_cookie(host, "/".into(), RawCookie::new("session", "1"), expiry);
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
let uri = Uri::from_static("https://example.com/");
|
|
1077
|
+
match jar.cookies(&uri, Version::HTTP_11) {
|
|
1078
|
+
Cookies::Compressed(value) => assert_eq!(value, "other=1; session=1"),
|
|
1079
|
+
other => panic!("expected compressed Cookie field, got {other:?}"),
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
949
1083
|
#[test]
|
|
950
1084
|
fn jar_sends_parent_domain_cookie_to_subdomain() {
|
|
951
1085
|
let jar = Jar::default();
|
|
@@ -11,6 +11,9 @@ use crate::ext::UriExt;
|
|
|
11
11
|
|
|
12
12
|
pub const DEFAULT_PATH: &str = "/";
|
|
13
13
|
|
|
14
|
+
/// Minimum interval between expiration sweeps of the store.
|
|
15
|
+
const EVICTION_INTERVAL: Duration = Duration::seconds(60);
|
|
16
|
+
|
|
14
17
|
/// Canonical immutable host used as a cookie domain key.
|
|
15
18
|
type CanonicalHost = Host<Box<str>>;
|
|
16
19
|
type NameMap = HashMap<Box<str>, CookieEntry>;
|
|
@@ -85,6 +88,11 @@ impl CookieScopeMap {
|
|
|
85
88
|
pub fn is_empty(&self) -> bool {
|
|
86
89
|
self.host_only.is_empty() && self.domain.is_empty()
|
|
87
90
|
}
|
|
91
|
+
|
|
92
|
+
fn retain(&mut self, mut keep: impl FnMut(&CookieEntry) -> bool) {
|
|
93
|
+
self.host_only.retain(|_, entry| keep(entry));
|
|
94
|
+
self.domain.retain(|_, entry| keep(entry));
|
|
95
|
+
}
|
|
88
96
|
}
|
|
89
97
|
|
|
90
98
|
/// Stores cookies by domain, path, host-only scope, and name.
|
|
@@ -92,21 +100,58 @@ impl CookieScopeMap {
|
|
|
92
100
|
pub struct Store {
|
|
93
101
|
pub cookies: DomainMap,
|
|
94
102
|
next_creation_index: u64,
|
|
103
|
+
/// Earliest known deadline. Removing a cookie can leave it earlier than necessary.
|
|
104
|
+
next_expiry: Option<OffsetDateTime>,
|
|
105
|
+
last_eviction: Option<OffsetDateTime>,
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
impl Store {
|
|
109
|
+
/// Prunes expired cookies and empty scopes at most once per eviction interval.
|
|
110
|
+
pub fn evict_expired(&mut self, now: OffsetDateTime) {
|
|
111
|
+
let due = self.next_expiry.is_some_and(|expiry| expiry <= now)
|
|
112
|
+
&& self
|
|
113
|
+
.last_eviction
|
|
114
|
+
.is_none_or(|last| now - last >= EVICTION_INTERVAL);
|
|
115
|
+
if !due {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
self.last_eviction = Some(now);
|
|
119
|
+
|
|
120
|
+
let mut next_expiry = None;
|
|
121
|
+
self.cookies.retain(|_, path_map| {
|
|
122
|
+
path_map.retain(|_, cookie_map| {
|
|
123
|
+
cookie_map.retain(|entry| {
|
|
124
|
+
if cookie_is_expired(&entry.cookie, now) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
track_expiry(&mut next_expiry, &entry.cookie);
|
|
128
|
+
true
|
|
129
|
+
});
|
|
130
|
+
!cookie_map.is_empty()
|
|
131
|
+
});
|
|
132
|
+
!path_map.is_empty()
|
|
133
|
+
});
|
|
134
|
+
self.next_expiry = next_expiry;
|
|
135
|
+
|
|
136
|
+
let capacity = self.cookies.capacity();
|
|
137
|
+
if capacity > 16 && self.cookies.len() <= capacity / 4 {
|
|
138
|
+
self.cookies.shrink_to_fit();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
98
142
|
/// Inserts or replaces a cookie in its domain and path scope.
|
|
99
143
|
pub fn insert_stored_cookie(
|
|
100
144
|
&mut self,
|
|
101
145
|
domain: CanonicalHost,
|
|
102
146
|
path: String,
|
|
103
147
|
cookie: RawCookie<'static>,
|
|
148
|
+
now: OffsetDateTime,
|
|
104
149
|
) {
|
|
105
150
|
let host_only = cookie.domain().is_none();
|
|
151
|
+
track_expiry(&mut self.next_expiry, &cookie);
|
|
106
152
|
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
// paths.
|
|
153
|
+
// During the interval between sweeps an expired entry can still occupy this slot.
|
|
154
|
+
// Chromium inherits the creation time for a live replacement with an unchanged value.
|
|
110
155
|
// https://chromium.googlesource.com/chromium/src/+/main/net/cookies/cookie_monster.cc
|
|
111
156
|
if let Some(entry) = self
|
|
112
157
|
.cookies
|
|
@@ -114,7 +159,7 @@ impl Store {
|
|
|
114
159
|
.and_then(|path_map| path_map.get_mut(path.as_str()))
|
|
115
160
|
.and_then(|cookie_map| cookie_map.get_mut(cookie.name(), host_only))
|
|
116
161
|
{
|
|
117
|
-
if entry.cookie.value() != cookie.value() {
|
|
162
|
+
if cookie_is_expired(&entry.cookie, now) || entry.cookie.value() != cookie.value() {
|
|
118
163
|
entry.creation_index = self.next_creation_index;
|
|
119
164
|
self.next_creation_index = self.next_creation_index.saturating_add(1);
|
|
120
165
|
}
|
|
@@ -231,6 +276,12 @@ impl Store {
|
|
|
231
276
|
}
|
|
232
277
|
}
|
|
233
278
|
|
|
279
|
+
fn track_expiry(next_expiry: &mut Option<OffsetDateTime>, cookie: &RawCookie<'_>) {
|
|
280
|
+
if let Some(expiry) = cookie.expires_datetime() {
|
|
281
|
+
*next_expiry = Some(next_expiry.map_or(expiry, |current| current.min(expiry)));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
234
285
|
/// Applies the RFC 6265 request selection rules supported by this HTTP client.
|
|
235
286
|
fn request_matches_cookie(
|
|
236
287
|
uri: &Uri,
|
|
@@ -281,6 +332,17 @@ pub fn domain_match(host: &CanonicalHost, domain: &CanonicalHost) -> bool {
|
|
|
281
332
|
&& host.ends_with(domain.as_ref())
|
|
282
333
|
}
|
|
283
334
|
|
|
335
|
+
/// Checks the public suffix list, including its private entries, for a DNS domain.
|
|
336
|
+
/// https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3
|
|
337
|
+
#[cfg(feature = "cookies-psl")]
|
|
338
|
+
pub fn is_public_suffix(domain: &CanonicalHost) -> bool {
|
|
339
|
+
let Host::Domain(domain) = domain else {
|
|
340
|
+
return false;
|
|
341
|
+
};
|
|
342
|
+
let domain = domain.strip_suffix('.').unwrap_or(domain);
|
|
343
|
+
psl::suffix_str(domain).is_some_and(|suffix| suffix == domain)
|
|
344
|
+
}
|
|
345
|
+
|
|
284
346
|
/// Determines whether `request_path` path-matches `cookie_path` under RFC 6265 section 5.1.4.
|
|
285
347
|
///
|
|
286
348
|
/// https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.4
|