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
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
diff --git a/src/client.rs b/src/client.rs
|
|
2
|
+
index c5eda4c6..629f1433 100644
|
|
3
|
+
--- a/src/client.rs
|
|
4
|
+
+++ b/src/client.rs
|
|
5
|
+
@@ -48,7 +48,9 @@ use self::{
|
|
6
|
+
config::{ConfigService, ConfigServiceLayer},
|
|
7
|
+
redirect::{FollowRedirect, FollowRedirectLayer},
|
|
8
|
+
retry::RetryPolicy,
|
|
9
|
+
- timeout::{Timeout, TimeoutLayer, TimeoutOptions, body::TimeoutBody},
|
|
10
|
+
+ timeout::{
|
|
11
|
+
+ Timeout, TimeoutLayer, TimeoutOptions, body::TimeoutBody, fetch_timeout_options,
|
|
12
|
+
+ },
|
|
13
|
+
transfer_size::{CountingBody, TransferSizeLayer, TransferSizeService},
|
|
14
|
+
},
|
|
15
|
+
request::{Request, RequestBuilder},
|
|
16
|
+
@@ -60,6 +62,7 @@ use crate::cookie;
|
|
17
|
+
use crate::dns::hickory::HickoryDnsResolver;
|
|
18
|
+
use crate::{
|
|
19
|
+
IntoUri, Method, Proxy,
|
|
20
|
+
+ config::RequestConfig,
|
|
21
|
+
conn::{
|
|
22
|
+
BoxedConnectorLayer, BoxedTransportConnector, Conn, Unnameable,
|
|
23
|
+
connector::{Connector, ConnectorBuilder},
|
|
24
|
+
@@ -68,6 +71,7 @@ use crate::{
|
|
25
|
+
},
|
|
26
|
+
dns::{DnsResolverWithOverrides, DynResolver, GaiResolver, IntoResolve, Resolve},
|
|
27
|
+
error::{self, Error},
|
|
28
|
+
+ ext::UriExt,
|
|
29
|
+
header::OrigHeaderMap,
|
|
30
|
+
http1::Http1Options,
|
|
31
|
+
http2::Http2Options,
|
|
32
|
+
@@ -154,7 +158,23 @@ type BoxedClientServiceLayer = BoxCloneSyncServiceLayer<
|
|
33
|
+
///
|
|
34
|
+
/// [`Rc`]: std::rc::Rc
|
|
35
|
+
#[derive(Clone)]
|
|
36
|
+
-pub struct Client(Arc<Either<ClientService, BoxedClientService>>);
|
|
37
|
+
+pub struct Client(Arc<ClientRef>);
|
|
38
|
+
+
|
|
39
|
+
+struct ClientRef {
|
|
40
|
+
+ service: Either<ClientService, BoxedClientService>,
|
|
41
|
+
+ preconnector: Preconnector,
|
|
42
|
+
+}
|
|
43
|
+
+
|
|
44
|
+
+/// Direct handle to the connection pool used by [`Client::preconnect`].
|
|
45
|
+
+///
|
|
46
|
+
+/// Preconnecting bypasses the request middleware stack, so the checks that
|
|
47
|
+
+/// stack would apply before a connection is opened are replicated here.
|
|
48
|
+
+struct Preconnector {
|
|
49
|
+
+ http: HttpClient<Connector, Body>,
|
|
50
|
+
+ https_only: bool,
|
|
51
|
+
+ timer: Timer,
|
|
52
|
+
+ timeout: RequestConfig<TimeoutOptions>,
|
|
53
|
+
+}
|
|
54
|
+
|
|
55
|
+
/// A [`ClientBuilder`] can be used to create a [`Client`] with custom configuration.
|
|
56
|
+
#[must_use]
|
|
57
|
+
@@ -430,7 +450,67 @@ impl Client {
|
|
58
|
+
let req = http::Request::<Body>::from(request);
|
|
59
|
+
Pending::Request {
|
|
60
|
+
uri: Some(req.uri().clone()),
|
|
61
|
+
- fut: Box::pin(Oneshot::new((*self.0).clone(), req)),
|
|
62
|
+
+ fut: Box::pin(Oneshot::new(self.0.service.clone(), req)),
|
|
63
|
+
+ }
|
|
64
|
+
+ }
|
|
65
|
+
+
|
|
66
|
+
+ /// Establishes a connection to the given URI ahead of time, without sending a request.
|
|
67
|
+
+ ///
|
|
68
|
+
+ /// The TCP connection (plus proxy tunnel and TLS handshake, where applicable) is
|
|
69
|
+
+ /// completed and the connection is kept idle in the pool, so that a subsequent
|
|
70
|
+
+ /// request to the same origin can reuse it and skip connection setup.
|
|
71
|
+
+ ///
|
|
72
|
+
+ /// If an idle connection to the origin already exists, it is reused and no new
|
|
73
|
+
+ /// connection is opened. The connection is subject to the pool's idle timeout and
|
|
74
|
+
+ /// has no effect if pooling is disabled.
|
|
75
|
+
+ ///
|
|
76
|
+
+ /// Use [`RequestBuilder::preconnect`] to preconnect with per-request options such as
|
|
77
|
+
+ /// a proxy or emulation, which must match the options of the later request for the
|
|
78
|
+
+ /// connection to be reused.
|
|
79
|
+
+ ///
|
|
80
|
+
+ /// # Errors
|
|
81
|
+
+ ///
|
|
82
|
+
+ /// This method fails if the URI is invalid or the connection cannot be established.
|
|
83
|
+
+ #[inline]
|
|
84
|
+
+ pub fn preconnect<U: IntoUri>(&self, uri: U) -> impl Future<Output = crate::Result<()>> {
|
|
85
|
+
+ self.get(uri).preconnect()
|
|
86
|
+
+ }
|
|
87
|
+
+
|
|
88
|
+
+ pub(crate) fn execute_preconnect(
|
|
89
|
+
+ &self,
|
|
90
|
+
+ request: Request,
|
|
91
|
+
+ ) -> impl Future<Output = crate::Result<()>> + Send + 'static + use<> {
|
|
92
|
+
+ let req = http::Request::<Body>::from(request);
|
|
93
|
+
+ let client = self.0.clone();
|
|
94
|
+
+ async move {
|
|
95
|
+
+ let preconnector = &client.preconnector;
|
|
96
|
+
+ let uri = req.uri().clone();
|
|
97
|
+
+
|
|
98
|
+
+ if !(uri.is_http() || uri.is_https()) || (preconnector.https_only && !uri.is_https()) {
|
|
99
|
+
+ return Err(Error::uri_bad_scheme(uri));
|
|
100
|
+
+ }
|
|
101
|
+
+
|
|
102
|
+
+ let (total_timeout, _) = fetch_timeout_options(&preconnector.timeout, req.extensions());
|
|
103
|
+
+ let connect = preconnector.http.preconnect(req);
|
|
104
|
+
+ let res = match total_timeout {
|
|
105
|
+
+ Some(timeout) => {
|
|
106
|
+
+ let sleep = wreq_proto::rt::Timer::sleep(&preconnector.timer, timeout);
|
|
107
|
+
+ match futures_util::future::select(connect, sleep).await {
|
|
108
|
+
+ futures_util::future::Either::Left((res, _)) => res,
|
|
109
|
+
+ futures_util::future::Either::Right(_) => {
|
|
110
|
+
+ Err(Error::request(error::TimedOut).into())
|
|
111
|
+
+ }
|
|
112
|
+
+ }
|
|
113
|
+
+ }
|
|
114
|
+
+ None => connect.await,
|
|
115
|
+
+ };
|
|
116
|
+
+
|
|
117
|
+
+ res.map_err(|err| {
|
|
118
|
+
+ let err = err
|
|
119
|
+
+ .downcast::<Error>()
|
|
120
|
+
+ .map_or_else(Error::request, |err| *err);
|
|
121
|
+
+ if err.uri().is_none() { err.with_uri(uri) } else { err }
|
|
122
|
+
+ })
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
@@ -588,6 +668,13 @@ impl ClientBuilder {
|
|
127
|
+
.build(connector)
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
+ let preconnector = Preconnector {
|
|
131
|
+
+ http: service.clone(),
|
|
132
|
+
+ https_only: config.https_only,
|
|
133
|
+
+ timer: config.timer.clone(),
|
|
134
|
+
+ timeout: RequestConfig::new(Some(config.timeout_options)),
|
|
135
|
+
+ };
|
|
136
|
+
+
|
|
137
|
+
// Configured client service with layers
|
|
138
|
+
let client = {
|
|
139
|
+
let service = ServiceBuilder::new()
|
|
140
|
+
@@ -645,7 +732,10 @@ impl ClientBuilder {
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
- Ok(Client(Arc::new(client)))
|
|
145
|
+
+ Ok(Client(Arc::new(ClientRef {
|
|
146
|
+
+ service: client,
|
|
147
|
+
+ preconnector,
|
|
148
|
+
+ })))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Higher-level options
|
|
152
|
+
diff --git a/src/client/layer/client.rs b/src/client/layer/client.rs
|
|
153
|
+
index 8b000486..8e5177a2 100644
|
|
154
|
+
--- a/src/client/layer/client.rs
|
|
155
|
+
+++ b/src/client/layer/client.rs
|
|
156
|
+
@@ -145,32 +145,50 @@ where
|
|
157
|
+
B::Data: Send,
|
|
158
|
+
B::Error: Into<BoxError>,
|
|
159
|
+
{
|
|
160
|
+
- fn request(
|
|
161
|
+
- &self,
|
|
162
|
+
- mut req: Request<B>,
|
|
163
|
+
- ) -> BoxFuture<'static, Result<Response<Incoming>, BoxError>> {
|
|
164
|
+
+ fn request(&self, mut req: Request<B>) -> BoxFuture<'static, Result<Response<Incoming>, BoxError>> {
|
|
165
|
+
+ match self.prepare(&mut req) {
|
|
166
|
+
+ Ok((this, descriptor)) => {
|
|
167
|
+
+ Box::pin(this.send_request(req, descriptor).map_err(Into::into))
|
|
168
|
+
+ }
|
|
169
|
+
+ Err(err) => Box::pin(future::err(err.into())),
|
|
170
|
+
+ }
|
|
171
|
+
+ }
|
|
172
|
+
+
|
|
173
|
+
+ /// Establishes (or reuses) a pooled connection for the request's destination
|
|
174
|
+
+ /// without sending the request, leaving the connection idle in the pool.
|
|
175
|
+
+ ///
|
|
176
|
+
+ /// The request is only used to derive the connection descriptor, so per-request
|
|
177
|
+
+ /// options (proxy, TLS/emulation, HTTP version, ...) produce the same pool key a
|
|
178
|
+
+ /// subsequent real request would use.
|
|
179
|
+
+ pub(crate) fn preconnect(&self, mut req: Request<B>) -> BoxFuture<'static, Result<(), BoxError>> {
|
|
180
|
+
+ match self.prepare(&mut req) {
|
|
181
|
+
+ Ok((this, descriptor)) => Box::pin(async move {
|
|
182
|
+
+ // Dropping the pooled connection returns it to the pool as idle.
|
|
183
|
+
+ this.connection_for(descriptor).await.map(drop).map_err(Into::into)
|
|
184
|
+
+ }),
|
|
185
|
+
+ Err(err) => Box::pin(future::err(err.into())),
|
|
186
|
+
+ }
|
|
187
|
+
+ }
|
|
188
|
+
+
|
|
189
|
+
+ #[allow(clippy::result_large_err)]
|
|
190
|
+
+ fn prepare(&self, req: &mut Request<B>) -> Result<(Self, ConnectionDescriptor), Error> {
|
|
191
|
+
let is_http_connect = req.method() == Method::CONNECT;
|
|
192
|
+
// Validate HTTP version early
|
|
193
|
+
match req.version() {
|
|
194
|
+
Version::HTTP_10 if is_http_connect => {
|
|
195
|
+
warn!("CONNECT is not allowed for HTTP/1.0");
|
|
196
|
+
- return Box::pin(future::err(e!(UserUnsupportedRequestMethod).into()));
|
|
197
|
+
+ return Err(e!(UserUnsupportedRequestMethod));
|
|
198
|
+
}
|
|
199
|
+
Version::HTTP_10 | Version::HTTP_11 | Version::HTTP_2 => {}
|
|
200
|
+
// completely unsupported HTTP version (like HTTP/0.9)!
|
|
201
|
+
_unsupported => {
|
|
202
|
+
warn!("Request has unsupported version: {:?}", _unsupported);
|
|
203
|
+
- return Box::pin(future::err(e!(UserUnsupportedVersion).into()));
|
|
204
|
+
+ return Err(e!(UserUnsupportedVersion));
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// Extract and normalize URI
|
|
209
|
+
- let uri = match normalize_uri(&mut req, is_http_connect) {
|
|
210
|
+
- Ok(uri) => uri,
|
|
211
|
+
- Err(err) => {
|
|
212
|
+
- return Box::pin(future::err(e!(UserAbsoluteUriRequired, err).into()));
|
|
213
|
+
- }
|
|
214
|
+
- };
|
|
215
|
+
+ let uri = normalize_uri(req, is_http_connect).map_err(|err| e!(UserAbsoluteUriRequired, err))?;
|
|
216
|
+
|
|
217
|
+
let mut this = self.clone();
|
|
218
|
+
|
|
219
|
+
@@ -196,7 +214,7 @@ where
|
|
220
|
+
ConnectionDescriptor::new(uri, group, proxy, version, tls_options, socket_bind_options)
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
- Box::pin(this.send_request(req, descriptor).map_err(Into::into))
|
|
224
|
+
+ Ok((this, descriptor))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async fn send_request(
|
|
228
|
+
diff --git a/src/client/layer/timeout.rs b/src/client/layer/timeout.rs
|
|
229
|
+
index b978d75b..0d3f340c 100644
|
|
230
|
+
--- a/src/client/layer/timeout.rs
|
|
231
|
+
+++ b/src/client/layer/timeout.rs
|
|
232
|
+
@@ -105,7 +105,7 @@ where
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
-fn fetch_timeout_options(
|
|
237
|
+
+pub(crate) fn fetch_timeout_options(
|
|
238
|
+
opts: &RequestConfig<TimeoutOptions>,
|
|
239
|
+
extensions: &http::Extensions,
|
|
240
|
+
) -> (Option<Duration>, Option<Duration>) {
|
|
241
|
+
diff --git a/src/client/request.rs b/src/client/request.rs
|
|
242
|
+
index fe02a938..6a76c3a4 100644
|
|
243
|
+
--- a/src/client/request.rs
|
|
244
|
+
+++ b/src/client/request.rs
|
|
245
|
+
@@ -847,6 +847,21 @@ impl RequestBuilder {
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
+ /// Establishes a connection for this request ahead of time, without sending it.
|
|
250
|
+
+ ///
|
|
251
|
+
+ /// Only connection-level options (such as proxy, emulation/TLS options, HTTP
|
|
252
|
+
+ /// version and timeout) are taken into account; the method, headers and body are
|
|
253
|
+
+ /// ignored. See [`Client::preconnect`] for details.
|
|
254
|
+
+ ///
|
|
255
|
+
+ /// # Errors
|
|
256
|
+
+ ///
|
|
257
|
+
+ /// This method fails if the request is invalid or the connection cannot be
|
|
258
|
+
+ /// established.
|
|
259
|
+
+ pub fn preconnect(self) -> impl Future<Output = crate::Result<()>> + Send + 'static {
|
|
260
|
+
+ let fut = self.request.map(|req| self.client.execute_preconnect(req));
|
|
261
|
+
+ async move { fut?.await }
|
|
262
|
+
+ }
|
|
263
|
+
+
|
|
264
|
+
/// Attempt to clone the RequestBuilder.
|
|
265
|
+
///
|
|
266
|
+
/// `None` is returned if the RequestBuilder can not be cloned,
|
data/vendor/wreq/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "wreq"
|
|
3
|
-
version = "0.16.
|
|
3
|
+
version = "0.16.5"
|
|
4
4
|
description = "An ergonomic, privacy-aware Rust HTTP Client"
|
|
5
5
|
keywords = ["http", "client", "websocket", "ja3", "ja4"]
|
|
6
6
|
categories = ["web-programming::http-client"]
|
|
@@ -33,6 +33,9 @@ charset = ["dep:encoding_rs", "dep:mime"]
|
|
|
33
33
|
# Enable cookies store support.
|
|
34
34
|
cookies = ["dep:cookie"]
|
|
35
35
|
|
|
36
|
+
# Restrict cookie domains using the Public Suffix List.
|
|
37
|
+
cookies-psl = ["cookies", "dep:psl"]
|
|
38
|
+
|
|
36
39
|
# Enable gzip decompression support.
|
|
37
40
|
gzip = ["dep:tower-http", "tower-http?/decompression-gzip"]
|
|
38
41
|
|
|
@@ -98,7 +101,7 @@ pin-project-lite = "0.2.17"
|
|
|
98
101
|
futures-util = { version = "0.3.33", default-features = false }
|
|
99
102
|
socket2 = { version = "0.6.3", features = ["all"] }
|
|
100
103
|
ipnet = "2.12.0"
|
|
101
|
-
lru = "0.
|
|
104
|
+
lru = "0.19.0"
|
|
102
105
|
btls = "0.5.6"
|
|
103
106
|
btls-sys = "0.5.6"
|
|
104
107
|
tokio-btls = "0.5.6"
|
|
@@ -134,6 +137,7 @@ webpki-root-certs = { version = "1.0.7", optional = true }
|
|
|
134
137
|
|
|
135
138
|
## cookies
|
|
136
139
|
cookie = { version = "0.18", optional = true }
|
|
140
|
+
psl = { version = "2.1", optional = true }
|
|
137
141
|
|
|
138
142
|
## tower http
|
|
139
143
|
tower-http = { version = "0.6.8", default-features = false, optional = true }
|
|
@@ -270,6 +274,11 @@ required-features = ["multipart", "stream"]
|
|
|
270
274
|
name = "retry"
|
|
271
275
|
path = "tests/retry.rs"
|
|
272
276
|
|
|
277
|
+
[[test]]
|
|
278
|
+
name = "websocket"
|
|
279
|
+
path = "tests/websocket.rs"
|
|
280
|
+
required-features = ["ws"]
|
|
281
|
+
|
|
273
282
|
[[example]]
|
|
274
283
|
name = "json_dynamic"
|
|
275
284
|
path = "examples/json_dynamic.rs"
|
|
@@ -348,8 +357,3 @@ path = "examples/keylog.rs"
|
|
|
348
357
|
[[example]]
|
|
349
358
|
name = "unix_socket"
|
|
350
359
|
path = "examples/unix_socket.rs"
|
|
351
|
-
|
|
352
|
-
[[example]]
|
|
353
|
-
name = "rt"
|
|
354
|
-
path = "examples/rt.rs"
|
|
355
|
-
required-features = ["compio-rt"]
|
|
@@ -79,6 +79,8 @@ impl Server {
|
|
|
79
79
|
}
|
|
80
80
|
accept = listener.accept() => {
|
|
81
81
|
if let Ok((socket, _peer_addr)) = accept {
|
|
82
|
+
// Nagle would hold small response writes until the client's delayed ACK.
|
|
83
|
+
socket.set_nodelay(true)?;
|
|
82
84
|
let tls_acceptor = self.tls_acceptor.clone();
|
|
83
85
|
let builder = self.builder.clone();
|
|
84
86
|
join_set.spawn(async move {
|
|
@@ -140,7 +140,7 @@ where
|
|
|
140
140
|
let Some(location) = res
|
|
141
141
|
.headers()
|
|
142
142
|
.get(LOCATION)
|
|
143
|
-
.and_then(|loc| loc.
|
|
143
|
+
.and_then(|loc| std::str::from_utf8(loc.as_bytes()).ok())
|
|
144
144
|
.and_then(|loc| resolve_uri(loc, request.uri()))
|
|
145
145
|
else {
|
|
146
146
|
return Poll::Ready(Ok(res));
|
|
@@ -54,13 +54,14 @@ pin_project! {
|
|
|
54
54
|
pin_project! {
|
|
55
55
|
/// A body wrapper that enforces a timeout for each read operation.
|
|
56
56
|
///
|
|
57
|
-
/// The
|
|
58
|
-
///
|
|
57
|
+
/// The deadline starts when a frame poll begins and is disarmed once it completes,
|
|
58
|
+
/// so time spent between reads is not counted.
|
|
59
59
|
pub struct ReadTimeoutBody<B> {
|
|
60
60
|
#[pin]
|
|
61
61
|
body: B,
|
|
62
62
|
#[pin]
|
|
63
63
|
sleep: Option<Pin<Box<dyn Sleep>>>,
|
|
64
|
+
armed: bool,
|
|
64
65
|
timeout: Duration,
|
|
65
66
|
timer: Timer,
|
|
66
67
|
}
|
|
@@ -83,6 +84,7 @@ impl<B> TimeoutBody<B> {
|
|
|
83
84
|
body: ReadTimeoutBody {
|
|
84
85
|
body,
|
|
85
86
|
sleep: None,
|
|
87
|
+
armed: false,
|
|
86
88
|
timeout: read_timeout,
|
|
87
89
|
timer,
|
|
88
90
|
},
|
|
@@ -95,6 +97,7 @@ impl<B> TimeoutBody<B> {
|
|
|
95
97
|
body: ReadTimeoutBody {
|
|
96
98
|
timeout,
|
|
97
99
|
sleep: None,
|
|
100
|
+
armed: false,
|
|
98
101
|
body,
|
|
99
102
|
timer,
|
|
100
103
|
},
|
|
@@ -208,10 +211,14 @@ where
|
|
|
208
211
|
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
|
|
209
212
|
let mut this = self.project();
|
|
210
213
|
|
|
211
|
-
//
|
|
212
|
-
if this.
|
|
214
|
+
// Arm the deadline when a new frame poll begins, reusing the sleep after the first read.
|
|
215
|
+
if !*this.armed {
|
|
213
216
|
let deadline = this.timer.now() + *this.timeout;
|
|
214
|
-
this.sleep.
|
|
217
|
+
match this.sleep.as_mut().as_pin_mut() {
|
|
218
|
+
Some(sleep) => this.timer.reset(sleep.get_mut(), deadline),
|
|
219
|
+
None => this.sleep.set(Some(this.timer.sleep_until(deadline))),
|
|
220
|
+
}
|
|
221
|
+
*this.armed = true;
|
|
215
222
|
}
|
|
216
223
|
|
|
217
224
|
// Error if the timeout has expired.
|
|
@@ -221,19 +228,10 @@ where
|
|
|
221
228
|
return Poll::Ready(Some(Err(Error::body(TimedOut).into())));
|
|
222
229
|
}
|
|
223
230
|
|
|
224
|
-
//
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if let Some(sleep) = this.sleep.as_mut().as_pin_mut() {
|
|
229
|
-
let deadline = this.timer.now() + *this.timeout;
|
|
230
|
-
this.timer.reset(sleep.get_mut(), deadline);
|
|
231
|
-
}
|
|
232
|
-
Poll::Ready(Some(Ok(frame)))
|
|
233
|
-
}
|
|
234
|
-
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
|
|
235
|
-
None => Poll::Ready(None),
|
|
236
|
-
}
|
|
231
|
+
// A completed frame poll disarms the deadline until the next read starts.
|
|
232
|
+
let frame = ready!(this.body.poll_frame(cx));
|
|
233
|
+
*this.armed = false;
|
|
234
|
+
Poll::Ready(frame.map(|res| res.map_err(Into::into)))
|
|
237
235
|
}
|
|
238
236
|
|
|
239
237
|
#[inline(always)]
|
|
@@ -45,8 +45,8 @@ where
|
|
|
45
45
|
return Poll::Ready(Err(Error::request(TimedOut).into()));
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
// Before headers arrive, the read timer limits that wait. The body
|
|
49
|
-
//
|
|
48
|
+
// Before headers arrive, the read timer limits that wait. The body rearms
|
|
49
|
+
// the timer when each frame read begins.
|
|
50
50
|
if let Some(timeout) = this.read_timeout_fut.as_mut()
|
|
51
51
|
&& timeout.as_mut().poll(cx).is_ready()
|
|
52
52
|
{
|
|
@@ -374,9 +374,9 @@ impl RequestBuilder {
|
|
|
374
374
|
|
|
375
375
|
/// Enables a read timeout.
|
|
376
376
|
///
|
|
377
|
-
/// The
|
|
378
|
-
///
|
|
379
|
-
/// overrides
|
|
377
|
+
/// The timeout applies while waiting for response headers and reading each body frame. Time
|
|
378
|
+
/// spent by the caller between body reads is not counted. It affects only this request and
|
|
379
|
+
/// overrides `ClientBuilder::read_timeout()`.
|
|
380
380
|
pub fn read_timeout(mut self, timeout: Duration) -> RequestBuilder {
|
|
381
381
|
if let Ok(ref mut req) = self.request {
|
|
382
382
|
req.config_mut::<TimeoutOptions>()
|
|
@@ -394,6 +394,7 @@ impl RequestBuilder {
|
|
|
394
394
|
/// overwritten if the same key is used. The key will simply show up
|
|
395
395
|
/// twice in the query string.
|
|
396
396
|
/// Calling `.query(&[("foo", "a"), ("foo", "b")])` gives `"foo=a&foo=b"`.
|
|
397
|
+
/// Existing URI components and query encoding are preserved.
|
|
397
398
|
///
|
|
398
399
|
/// # Note
|
|
399
400
|
/// This method does not support serializing a single key-value
|
|
@@ -402,22 +403,33 @@ impl RequestBuilder {
|
|
|
402
403
|
/// and maps into a key-value pair.
|
|
403
404
|
///
|
|
404
405
|
/// # Errors
|
|
405
|
-
/// This method
|
|
406
|
-
///
|
|
406
|
+
/// This method fails if the parameters cannot be serialized or the
|
|
407
|
+
/// resulting URI is invalid.
|
|
407
408
|
#[cfg(feature = "query")]
|
|
408
409
|
#[cfg_attr(docsrs, doc(cfg(feature = "query")))]
|
|
409
410
|
pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
|
|
410
|
-
let mut
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
411
|
+
if let Ok(ref mut req) = self.request
|
|
412
|
+
&& let Err(err) = serde_html_form::to_string(query)
|
|
413
|
+
.map_err(Error::builder)
|
|
414
|
+
.and_then(|query| {
|
|
415
|
+
if query.is_empty() {
|
|
416
|
+
return Ok(());
|
|
417
|
+
}
|
|
418
|
+
|
|
414
419
|
let uri = req.uri_mut();
|
|
415
|
-
uri.
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
420
|
+
let path = uri.path();
|
|
421
|
+
let path_and_query = match uri.query().filter(|query| !query.is_empty()) {
|
|
422
|
+
Some(existing) => format!("{path}?{existing}&{query}"),
|
|
423
|
+
None => format!("{path}?{query}"),
|
|
424
|
+
};
|
|
425
|
+
let path_and_query = http::uri::PathAndQuery::try_from(path_and_query)
|
|
426
|
+
.map_err(Error::builder)?;
|
|
427
|
+
let mut parts = uri.clone().into_parts();
|
|
428
|
+
parts.path_and_query = Some(path_and_query);
|
|
429
|
+
*uri = Uri::from_parts(parts).map_err(Error::builder)?;
|
|
430
|
+
Ok(())
|
|
431
|
+
})
|
|
432
|
+
{
|
|
421
433
|
self.request = Err(err);
|
|
422
434
|
}
|
|
423
435
|
self
|
|
@@ -275,6 +275,7 @@ impl WebSocketRequestBuilder {
|
|
|
275
275
|
/// overwritten if the same key is used. The key will simply show up
|
|
276
276
|
/// twice in the query string.
|
|
277
277
|
/// Calling `.query(&[("foo", "a"), ("foo", "b")])` gives `"foo=a&foo=b"`.
|
|
278
|
+
/// Existing URI components and query encoding are preserved.
|
|
278
279
|
///
|
|
279
280
|
/// # Note
|
|
280
281
|
/// This method does not support serializing a single key-value
|
|
@@ -283,8 +284,8 @@ impl WebSocketRequestBuilder {
|
|
|
283
284
|
/// and maps into a key-value pair.
|
|
284
285
|
///
|
|
285
286
|
/// # Errors
|
|
286
|
-
/// This method
|
|
287
|
-
///
|
|
287
|
+
/// This method fails if the parameters cannot be serialized or the
|
|
288
|
+
/// resulting URI is invalid.
|
|
288
289
|
#[inline]
|
|
289
290
|
#[cfg(feature = "query")]
|
|
290
291
|
#[cfg_attr(docsrs, doc(cfg(feature = "query")))]
|
|
@@ -754,7 +755,7 @@ impl Stream for WebSocket {
|
|
|
754
755
|
return Poll::Ready(Some(Ok(msg)));
|
|
755
756
|
}
|
|
756
757
|
}
|
|
757
|
-
Some(Err(err)) => return Poll::Ready(Some(Err(Error::
|
|
758
|
+
Some(Err(err)) => return Poll::Ready(Some(Err(Error::websocket(err)))),
|
|
758
759
|
None => return Poll::Ready(None),
|
|
759
760
|
}
|
|
760
761
|
}
|
data/vendor/wreq/src/client.rs
CHANGED
|
@@ -73,7 +73,7 @@ use crate::{
|
|
|
73
73
|
proxy::Matcher as ProxyMatcher,
|
|
74
74
|
redirect::{self, FollowRedirectPolicy},
|
|
75
75
|
retry,
|
|
76
|
-
rt::{
|
|
76
|
+
rt::{Executor, Timer},
|
|
77
77
|
tls::{
|
|
78
78
|
AlpnProtocol, TlsOptions, TlsVersion,
|
|
79
79
|
keylog::KeyLog,
|
|
@@ -641,28 +641,6 @@ impl ClientBuilder {
|
|
|
641
641
|
Ok(Client(Arc::new(client)))
|
|
642
642
|
}
|
|
643
643
|
|
|
644
|
-
// Runtime options
|
|
645
|
-
|
|
646
|
-
/// Provide a timer to be used for timeouts and intervals in client.
|
|
647
|
-
#[inline]
|
|
648
|
-
pub fn timer<M>(mut self, timer: M) -> Self
|
|
649
|
-
where
|
|
650
|
-
M: wreq_proto::rt::Timer + Send + Sync + 'static,
|
|
651
|
-
{
|
|
652
|
-
self.config.timer = Timer::new(timer);
|
|
653
|
-
self
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
/// Provide an executor to run background tasks in the client.
|
|
657
|
-
#[inline]
|
|
658
|
-
pub fn executor<E>(mut self, executor: E) -> Self
|
|
659
|
-
where
|
|
660
|
-
E: wreq_proto::rt::Executor<BoxSendFuture> + Send + Sync + 'static,
|
|
661
|
-
{
|
|
662
|
-
self.config.executor = Executor::new(executor);
|
|
663
|
-
self
|
|
664
|
-
}
|
|
665
|
-
|
|
666
644
|
// Higher-level options
|
|
667
645
|
|
|
668
646
|
/// Sets the `User-Agent` header to be used by this client.
|
|
@@ -1034,7 +1012,9 @@ impl ClientBuilder {
|
|
|
1034
1012
|
self
|
|
1035
1013
|
}
|
|
1036
1014
|
|
|
1037
|
-
///
|
|
1015
|
+
/// Sets a timeout for waiting for response headers and reading each body frame.
|
|
1016
|
+
///
|
|
1017
|
+
/// Time spent by the caller between body reads is not counted.
|
|
1038
1018
|
///
|
|
1039
1019
|
/// Default is `None`.
|
|
1040
1020
|
#[inline]
|