wreq-rb 0.3.0
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 +7 -0
- data/Cargo.lock +2688 -0
- data/Cargo.toml +6 -0
- data/README.md +179 -0
- data/ext/wreq_rb/Cargo.toml +39 -0
- data/ext/wreq_rb/extconf.rb +22 -0
- data/ext/wreq_rb/src/client.rs +565 -0
- data/ext/wreq_rb/src/error.rs +25 -0
- data/ext/wreq_rb/src/lib.rs +20 -0
- data/ext/wreq_rb/src/response.rs +132 -0
- data/lib/wreq-rb/version.rb +5 -0
- data/lib/wreq-rb.rb +17 -0
- data/patches/0001-add-transfer-size-tracking.patch +292 -0
- data/vendor/wreq/Cargo.toml +306 -0
- data/vendor/wreq/LICENSE +202 -0
- data/vendor/wreq/README.md +122 -0
- data/vendor/wreq/examples/cert_store.rs +77 -0
- data/vendor/wreq/examples/connect_via_lower_priority_tokio_runtime.rs +258 -0
- data/vendor/wreq/examples/emulation.rs +118 -0
- data/vendor/wreq/examples/form.rs +14 -0
- data/vendor/wreq/examples/http1_websocket.rs +37 -0
- data/vendor/wreq/examples/http2_websocket.rs +45 -0
- data/vendor/wreq/examples/json_dynamic.rs +41 -0
- data/vendor/wreq/examples/json_typed.rs +47 -0
- data/vendor/wreq/examples/keylog.rs +16 -0
- data/vendor/wreq/examples/request_with_emulation.rs +115 -0
- data/vendor/wreq/examples/request_with_interface.rs +37 -0
- data/vendor/wreq/examples/request_with_local_address.rs +16 -0
- data/vendor/wreq/examples/request_with_proxy.rs +13 -0
- data/vendor/wreq/examples/request_with_redirect.rs +22 -0
- data/vendor/wreq/examples/request_with_version.rs +15 -0
- data/vendor/wreq/examples/tor_socks.rs +24 -0
- data/vendor/wreq/examples/unix_socket.rs +33 -0
- data/vendor/wreq/src/client/body.rs +304 -0
- data/vendor/wreq/src/client/conn/conn.rs +231 -0
- data/vendor/wreq/src/client/conn/connector.rs +549 -0
- data/vendor/wreq/src/client/conn/http.rs +1023 -0
- data/vendor/wreq/src/client/conn/proxy/socks.rs +233 -0
- data/vendor/wreq/src/client/conn/proxy/tunnel.rs +260 -0
- data/vendor/wreq/src/client/conn/proxy.rs +39 -0
- data/vendor/wreq/src/client/conn/tls_info.rs +98 -0
- data/vendor/wreq/src/client/conn/uds.rs +44 -0
- data/vendor/wreq/src/client/conn/verbose.rs +149 -0
- data/vendor/wreq/src/client/conn.rs +323 -0
- data/vendor/wreq/src/client/core/body/incoming.rs +485 -0
- data/vendor/wreq/src/client/core/body/length.rs +118 -0
- data/vendor/wreq/src/client/core/body.rs +34 -0
- data/vendor/wreq/src/client/core/common/buf.rs +149 -0
- data/vendor/wreq/src/client/core/common/rewind.rs +141 -0
- data/vendor/wreq/src/client/core/common/watch.rs +76 -0
- data/vendor/wreq/src/client/core/common.rs +3 -0
- data/vendor/wreq/src/client/core/conn/http1.rs +342 -0
- data/vendor/wreq/src/client/core/conn/http2.rs +307 -0
- data/vendor/wreq/src/client/core/conn.rs +11 -0
- data/vendor/wreq/src/client/core/dispatch.rs +299 -0
- data/vendor/wreq/src/client/core/error.rs +435 -0
- data/vendor/wreq/src/client/core/ext.rs +201 -0
- data/vendor/wreq/src/client/core/http1.rs +178 -0
- data/vendor/wreq/src/client/core/http2.rs +483 -0
- data/vendor/wreq/src/client/core/proto/h1/conn.rs +988 -0
- data/vendor/wreq/src/client/core/proto/h1/decode.rs +1170 -0
- data/vendor/wreq/src/client/core/proto/h1/dispatch.rs +684 -0
- data/vendor/wreq/src/client/core/proto/h1/encode.rs +580 -0
- data/vendor/wreq/src/client/core/proto/h1/io.rs +879 -0
- data/vendor/wreq/src/client/core/proto/h1/role.rs +694 -0
- data/vendor/wreq/src/client/core/proto/h1.rs +104 -0
- data/vendor/wreq/src/client/core/proto/h2/client.rs +650 -0
- data/vendor/wreq/src/client/core/proto/h2/ping.rs +539 -0
- data/vendor/wreq/src/client/core/proto/h2.rs +379 -0
- data/vendor/wreq/src/client/core/proto/headers.rs +138 -0
- data/vendor/wreq/src/client/core/proto.rs +58 -0
- data/vendor/wreq/src/client/core/rt/bounds.rs +57 -0
- data/vendor/wreq/src/client/core/rt/timer.rs +150 -0
- data/vendor/wreq/src/client/core/rt/tokio.rs +99 -0
- data/vendor/wreq/src/client/core/rt.rs +25 -0
- data/vendor/wreq/src/client/core/upgrade.rs +267 -0
- data/vendor/wreq/src/client/core.rs +16 -0
- data/vendor/wreq/src/client/emulation.rs +161 -0
- data/vendor/wreq/src/client/http/client/error.rs +142 -0
- data/vendor/wreq/src/client/http/client/exec.rs +29 -0
- data/vendor/wreq/src/client/http/client/extra.rs +77 -0
- data/vendor/wreq/src/client/http/client/lazy.rs +79 -0
- data/vendor/wreq/src/client/http/client/pool.rs +1105 -0
- data/vendor/wreq/src/client/http/client/util.rs +104 -0
- data/vendor/wreq/src/client/http/client.rs +1003 -0
- data/vendor/wreq/src/client/http/future.rs +99 -0
- data/vendor/wreq/src/client/http.rs +1629 -0
- data/vendor/wreq/src/client/layer/config/options.rs +156 -0
- data/vendor/wreq/src/client/layer/config.rs +116 -0
- data/vendor/wreq/src/client/layer/cookie.rs +161 -0
- data/vendor/wreq/src/client/layer/decoder.rs +139 -0
- data/vendor/wreq/src/client/layer/redirect/future.rs +270 -0
- data/vendor/wreq/src/client/layer/redirect/policy.rs +63 -0
- data/vendor/wreq/src/client/layer/redirect.rs +145 -0
- data/vendor/wreq/src/client/layer/retry/classify.rs +105 -0
- data/vendor/wreq/src/client/layer/retry/scope.rs +51 -0
- data/vendor/wreq/src/client/layer/retry.rs +151 -0
- data/vendor/wreq/src/client/layer/timeout/body.rs +233 -0
- data/vendor/wreq/src/client/layer/timeout/future.rs +90 -0
- data/vendor/wreq/src/client/layer/timeout.rs +177 -0
- data/vendor/wreq/src/client/layer.rs +15 -0
- data/vendor/wreq/src/client/multipart.rs +717 -0
- data/vendor/wreq/src/client/request.rs +818 -0
- data/vendor/wreq/src/client/response.rs +534 -0
- data/vendor/wreq/src/client/ws/json.rs +99 -0
- data/vendor/wreq/src/client/ws/message.rs +453 -0
- data/vendor/wreq/src/client/ws.rs +714 -0
- data/vendor/wreq/src/client.rs +27 -0
- data/vendor/wreq/src/config.rs +140 -0
- data/vendor/wreq/src/cookie.rs +579 -0
- data/vendor/wreq/src/dns/gai.rs +249 -0
- data/vendor/wreq/src/dns/hickory.rs +78 -0
- data/vendor/wreq/src/dns/resolve.rs +180 -0
- data/vendor/wreq/src/dns.rs +69 -0
- data/vendor/wreq/src/error.rs +502 -0
- data/vendor/wreq/src/ext.rs +398 -0
- data/vendor/wreq/src/hash.rs +143 -0
- data/vendor/wreq/src/header.rs +506 -0
- data/vendor/wreq/src/into_uri.rs +187 -0
- data/vendor/wreq/src/lib.rs +586 -0
- data/vendor/wreq/src/proxy/mac.rs +82 -0
- data/vendor/wreq/src/proxy/matcher.rs +806 -0
- data/vendor/wreq/src/proxy/uds.rs +66 -0
- data/vendor/wreq/src/proxy/win.rs +31 -0
- data/vendor/wreq/src/proxy.rs +569 -0
- data/vendor/wreq/src/redirect.rs +575 -0
- data/vendor/wreq/src/retry.rs +198 -0
- data/vendor/wreq/src/sync.rs +129 -0
- data/vendor/wreq/src/tls/conn/cache.rs +123 -0
- data/vendor/wreq/src/tls/conn/cert_compression.rs +125 -0
- data/vendor/wreq/src/tls/conn/ext.rs +82 -0
- data/vendor/wreq/src/tls/conn/macros.rs +34 -0
- data/vendor/wreq/src/tls/conn/service.rs +138 -0
- data/vendor/wreq/src/tls/conn.rs +681 -0
- data/vendor/wreq/src/tls/keylog/handle.rs +64 -0
- data/vendor/wreq/src/tls/keylog.rs +99 -0
- data/vendor/wreq/src/tls/options.rs +464 -0
- data/vendor/wreq/src/tls/x509/identity.rs +122 -0
- data/vendor/wreq/src/tls/x509/parser.rs +71 -0
- data/vendor/wreq/src/tls/x509/store.rs +228 -0
- data/vendor/wreq/src/tls/x509.rs +68 -0
- data/vendor/wreq/src/tls.rs +154 -0
- data/vendor/wreq/src/trace.rs +55 -0
- data/vendor/wreq/src/util.rs +122 -0
- data/vendor/wreq/tests/badssl.rs +228 -0
- data/vendor/wreq/tests/brotli.rs +350 -0
- data/vendor/wreq/tests/client.rs +1098 -0
- data/vendor/wreq/tests/connector_layers.rs +227 -0
- data/vendor/wreq/tests/cookie.rs +306 -0
- data/vendor/wreq/tests/deflate.rs +347 -0
- data/vendor/wreq/tests/emulation.rs +260 -0
- data/vendor/wreq/tests/gzip.rs +347 -0
- data/vendor/wreq/tests/layers.rs +261 -0
- data/vendor/wreq/tests/multipart.rs +165 -0
- data/vendor/wreq/tests/proxy.rs +438 -0
- data/vendor/wreq/tests/redirect.rs +629 -0
- data/vendor/wreq/tests/retry.rs +135 -0
- data/vendor/wreq/tests/support/delay_server.rs +117 -0
- data/vendor/wreq/tests/support/error.rs +16 -0
- data/vendor/wreq/tests/support/layer.rs +183 -0
- data/vendor/wreq/tests/support/mod.rs +9 -0
- data/vendor/wreq/tests/support/server.rs +232 -0
- data/vendor/wreq/tests/timeouts.rs +281 -0
- data/vendor/wreq/tests/unix_socket.rs +135 -0
- data/vendor/wreq/tests/upgrade.rs +98 -0
- data/vendor/wreq/tests/zstd.rs +559 -0
- metadata +225 -0
|
@@ -0,0 +1,1003 @@
|
|
|
1
|
+
#[macro_use]
|
|
2
|
+
pub mod error;
|
|
3
|
+
mod exec;
|
|
4
|
+
pub mod extra;
|
|
5
|
+
mod lazy;
|
|
6
|
+
mod pool;
|
|
7
|
+
mod util;
|
|
8
|
+
|
|
9
|
+
use std::{
|
|
10
|
+
future::Future,
|
|
11
|
+
num::NonZeroU32,
|
|
12
|
+
pin::Pin,
|
|
13
|
+
sync::Arc,
|
|
14
|
+
task::{self, Poll},
|
|
15
|
+
time::Duration,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
use bytes::Bytes;
|
|
19
|
+
use futures_util::future::{Either, FutureExt, TryFutureExt};
|
|
20
|
+
use http::{
|
|
21
|
+
HeaderValue, Method, Request, Response, Uri, Version,
|
|
22
|
+
header::{HOST, PROXY_AUTHORIZATION},
|
|
23
|
+
};
|
|
24
|
+
use http_body::Body;
|
|
25
|
+
use pool::Ver;
|
|
26
|
+
use tokio::io::{AsyncRead, AsyncWrite};
|
|
27
|
+
use tower::util::Oneshot;
|
|
28
|
+
|
|
29
|
+
use self::{
|
|
30
|
+
error::{ClientConnectError, Error, ErrorKind, TrySendError},
|
|
31
|
+
exec::Exec,
|
|
32
|
+
extra::{ConnectExtra, ConnectIdentity},
|
|
33
|
+
lazy::{Started as Lazy, lazy},
|
|
34
|
+
};
|
|
35
|
+
use crate::{
|
|
36
|
+
client::{
|
|
37
|
+
conn::{Connected, Connection},
|
|
38
|
+
core::{
|
|
39
|
+
body::Incoming,
|
|
40
|
+
conn::{self, TrySendError as ConnTrySendError},
|
|
41
|
+
http1::Http1Options,
|
|
42
|
+
http2::Http2Options,
|
|
43
|
+
rt::{ArcTimer, Executor, Timer},
|
|
44
|
+
},
|
|
45
|
+
layer::config::RequestOptions,
|
|
46
|
+
},
|
|
47
|
+
config::RequestConfig,
|
|
48
|
+
error::BoxError,
|
|
49
|
+
hash::{HASHER, HashMemo},
|
|
50
|
+
tls::AlpnProtocol,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
|
|
54
|
+
|
|
55
|
+
/// Parameters required to initiate a new connection.
|
|
56
|
+
///
|
|
57
|
+
/// [`ConnectRequest`] holds the target URI and all connection-specific options
|
|
58
|
+
/// (protocol, proxy, TCP/TLS settings) needed to establish a new network connection.
|
|
59
|
+
/// Used by connectors to drive the connection setup process.
|
|
60
|
+
#[must_use]
|
|
61
|
+
#[derive(Clone)]
|
|
62
|
+
pub struct ConnectRequest {
|
|
63
|
+
uri: Uri,
|
|
64
|
+
identifier: ConnectIdentity,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ===== impl ConnectRequest =====
|
|
68
|
+
|
|
69
|
+
impl ConnectRequest {
|
|
70
|
+
/// Create a new [`ConnectRequest`] with the given URI and identifier.
|
|
71
|
+
#[inline]
|
|
72
|
+
fn new<T>(uri: Uri, identifier: T) -> ConnectRequest
|
|
73
|
+
where
|
|
74
|
+
T: Into<Option<RequestOptions>>,
|
|
75
|
+
{
|
|
76
|
+
ConnectRequest {
|
|
77
|
+
uri: uri.clone(),
|
|
78
|
+
identifier: Arc::new(HashMemo::with_hasher(
|
|
79
|
+
ConnectExtra::new(uri, identifier),
|
|
80
|
+
HASHER,
|
|
81
|
+
)),
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Returns a reference to the [`Uri`].
|
|
86
|
+
#[inline]
|
|
87
|
+
pub fn uri(&self) -> &Uri {
|
|
88
|
+
&self.uri
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Returns a mutable reference to the [`Uri`].
|
|
92
|
+
#[inline]
|
|
93
|
+
pub fn uri_mut(&mut self) -> &mut Uri {
|
|
94
|
+
&mut self.uri
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Returns a unique [`ConnectIdentity`].
|
|
98
|
+
#[inline]
|
|
99
|
+
pub(crate) fn identify(&self) -> ConnectIdentity {
|
|
100
|
+
self.identifier.clone()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Returns the [`ConnectExtra`] connection extra.
|
|
104
|
+
#[inline]
|
|
105
|
+
pub(crate) fn extra(&self) -> &ConnectExtra {
|
|
106
|
+
self.identifier.as_ref().as_ref()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// A HttpClient to make outgoing HTTP requests.
|
|
111
|
+
///
|
|
112
|
+
/// `HttpClient` is cheap to clone and cloning is the recommended way to share a `HttpClient`. The
|
|
113
|
+
/// underlying connection pool will be reused.
|
|
114
|
+
#[must_use]
|
|
115
|
+
pub struct HttpClient<C, B> {
|
|
116
|
+
config: Config,
|
|
117
|
+
connector: C,
|
|
118
|
+
exec: Exec,
|
|
119
|
+
h1_builder: conn::http1::Builder,
|
|
120
|
+
h2_builder: conn::http2::Builder<Exec>,
|
|
121
|
+
pool: pool::Pool<PoolClient<B>, ConnectIdentity>,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#[derive(Clone, Copy)]
|
|
125
|
+
struct Config {
|
|
126
|
+
retry_canceled_requests: bool,
|
|
127
|
+
set_host: bool,
|
|
128
|
+
ver: Ver,
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ===== impl HttpClient =====
|
|
132
|
+
|
|
133
|
+
impl HttpClient<(), ()> {
|
|
134
|
+
/// Create a builder to configure a new `HttpClient`.
|
|
135
|
+
pub fn builder<E>(executor: E) -> Builder
|
|
136
|
+
where
|
|
137
|
+
E: Executor<BoxSendFuture> + Send + Sync + Clone + 'static,
|
|
138
|
+
{
|
|
139
|
+
Builder::new(executor)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
impl<C, B> HttpClient<C, B>
|
|
144
|
+
where
|
|
145
|
+
C: tower::Service<ConnectRequest> + Clone + Send + Sync + 'static,
|
|
146
|
+
C::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
|
147
|
+
C::Error: Into<BoxError>,
|
|
148
|
+
C::Future: Unpin + Send + 'static,
|
|
149
|
+
B: Body + Send + 'static + Unpin,
|
|
150
|
+
B::Data: Send,
|
|
151
|
+
B::Error: Into<BoxError>,
|
|
152
|
+
{
|
|
153
|
+
/// Send a constructed `Request` using this `HttpClient`.
|
|
154
|
+
fn request(&self, mut req: Request<B>) -> ResponseFuture {
|
|
155
|
+
let is_http_connect = req.method() == Method::CONNECT;
|
|
156
|
+
// Validate HTTP version early
|
|
157
|
+
match req.version() {
|
|
158
|
+
Version::HTTP_10 if is_http_connect => {
|
|
159
|
+
warn!("CONNECT is not allowed for HTTP/1.0");
|
|
160
|
+
return ResponseFuture::new(futures_util::future::err(Error::new_kind(
|
|
161
|
+
ErrorKind::UserUnsupportedRequestMethod,
|
|
162
|
+
)));
|
|
163
|
+
}
|
|
164
|
+
Version::HTTP_10 | Version::HTTP_11 | Version::HTTP_2 => {}
|
|
165
|
+
// completely unsupported HTTP version (like HTTP/0.9)!
|
|
166
|
+
_unsupported => {
|
|
167
|
+
warn!("Request has unsupported version: {:?}", _unsupported);
|
|
168
|
+
return ResponseFuture::new(futures_util::future::err(Error::new_kind(
|
|
169
|
+
ErrorKind::UserUnsupportedVersion,
|
|
170
|
+
)));
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// Extract and normalize URI
|
|
175
|
+
let uri = match util::normalize_uri(&mut req, is_http_connect) {
|
|
176
|
+
Ok(uri) => uri,
|
|
177
|
+
Err(err) => return ResponseFuture::new(futures_util::future::err(err)),
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
let mut this = self.clone();
|
|
181
|
+
|
|
182
|
+
// Extract per-request options from the request extensions and apply them to the client
|
|
183
|
+
// builder. This allows each request to override HTTP/1 and HTTP/2 options as
|
|
184
|
+
// needed.
|
|
185
|
+
let options = RequestConfig::<RequestOptions>::remove(req.extensions_mut());
|
|
186
|
+
|
|
187
|
+
// Apply HTTP/1 and HTTP/2 options if provided
|
|
188
|
+
if let Some(opts) = options.as_ref().map(RequestOptions::transport_opts) {
|
|
189
|
+
if let Some(opts) = opts.http1_options() {
|
|
190
|
+
this.h1_builder.options(opts.clone());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if let Some(opts) = opts.http2_options() {
|
|
194
|
+
this.h2_builder.options(opts.clone());
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let connect_req = ConnectRequest::new(uri, options);
|
|
199
|
+
ResponseFuture::new(this.send_request(req, connect_req))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async fn send_request(
|
|
203
|
+
self,
|
|
204
|
+
mut req: Request<B>,
|
|
205
|
+
connect_req: ConnectRequest,
|
|
206
|
+
) -> Result<Response<Incoming>, Error> {
|
|
207
|
+
let uri = req.uri().clone();
|
|
208
|
+
|
|
209
|
+
loop {
|
|
210
|
+
req = match self.try_send_request(req, connect_req.clone()).await {
|
|
211
|
+
Ok(resp) => return Ok(resp),
|
|
212
|
+
Err(TrySendError::Nope(err)) => return Err(err),
|
|
213
|
+
Err(TrySendError::Retryable {
|
|
214
|
+
mut req,
|
|
215
|
+
error,
|
|
216
|
+
connection_reused,
|
|
217
|
+
}) => {
|
|
218
|
+
if !self.config.retry_canceled_requests || !connection_reused {
|
|
219
|
+
// if client disabled, don't retry
|
|
220
|
+
// a fresh connection means we definitely can't retry
|
|
221
|
+
return Err(error);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
trace!(
|
|
225
|
+
"unstarted request canceled, trying again (reason={:?})",
|
|
226
|
+
error
|
|
227
|
+
);
|
|
228
|
+
*req.uri_mut() = uri.clone();
|
|
229
|
+
req
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async fn try_send_request(
|
|
236
|
+
&self,
|
|
237
|
+
mut req: Request<B>,
|
|
238
|
+
connect_req: ConnectRequest,
|
|
239
|
+
) -> Result<Response<Incoming>, TrySendError<B>> {
|
|
240
|
+
let mut pooled = self
|
|
241
|
+
.connection_for(connect_req)
|
|
242
|
+
.await
|
|
243
|
+
// `connection_for` already retries checkout errors, so if
|
|
244
|
+
// it returns an error, there's not much else to retry
|
|
245
|
+
.map_err(TrySendError::Nope)?;
|
|
246
|
+
|
|
247
|
+
if pooled.is_http1() {
|
|
248
|
+
if req.version() == Version::HTTP_2 {
|
|
249
|
+
warn!("Connection is HTTP/1, but request requires HTTP/2");
|
|
250
|
+
return Err(TrySendError::Nope(
|
|
251
|
+
Error::new_kind(ErrorKind::UserUnsupportedVersion)
|
|
252
|
+
.with_connect_info(pooled.conn_info.clone()),
|
|
253
|
+
));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if self.config.set_host {
|
|
257
|
+
let uri = req.uri().clone();
|
|
258
|
+
req.headers_mut().entry(HOST).or_insert_with(|| {
|
|
259
|
+
let hostname = uri.host().expect("authority implies host");
|
|
260
|
+
if let Some(port) = util::get_non_default_port(&uri) {
|
|
261
|
+
let s = format!("{hostname}:{port}");
|
|
262
|
+
HeaderValue::from_maybe_shared(Bytes::from(s))
|
|
263
|
+
} else {
|
|
264
|
+
HeaderValue::from_str(hostname)
|
|
265
|
+
}
|
|
266
|
+
.expect("uri host is valid header value")
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// CONNECT always sends authority-form, so check it first...
|
|
271
|
+
if req.method() == Method::CONNECT {
|
|
272
|
+
util::authority_form(req.uri_mut());
|
|
273
|
+
} else if pooled.conn_info.is_proxied() {
|
|
274
|
+
if let Some(auth) = pooled.conn_info.proxy_auth() {
|
|
275
|
+
req.headers_mut()
|
|
276
|
+
.entry(PROXY_AUTHORIZATION)
|
|
277
|
+
.or_insert_with(|| auth.clone());
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if let Some(headers) = pooled.conn_info.proxy_headers() {
|
|
281
|
+
crate::util::replace_headers(req.headers_mut(), headers.clone());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
util::absolute_form(req.uri_mut());
|
|
285
|
+
} else {
|
|
286
|
+
util::origin_form(req.uri_mut());
|
|
287
|
+
}
|
|
288
|
+
} else if req.method() == Method::CONNECT && !pooled.is_http2() {
|
|
289
|
+
util::authority_form(req.uri_mut());
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let mut res = match pooled.try_send_request(req).await {
|
|
293
|
+
Ok(res) => res,
|
|
294
|
+
Err(mut err) => {
|
|
295
|
+
return if let Some(req) = err.take_message() {
|
|
296
|
+
Err(TrySendError::Retryable {
|
|
297
|
+
connection_reused: pooled.is_reused(),
|
|
298
|
+
error: Error::new(ErrorKind::Canceled, err.into_error())
|
|
299
|
+
.with_connect_info(pooled.conn_info.clone()),
|
|
300
|
+
req,
|
|
301
|
+
})
|
|
302
|
+
} else {
|
|
303
|
+
Err(TrySendError::Nope(
|
|
304
|
+
Error::new(ErrorKind::SendRequest, err.into_error())
|
|
305
|
+
.with_connect_info(pooled.conn_info.clone()),
|
|
306
|
+
))
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// If the Connector included 'extra' info, add to Response...
|
|
312
|
+
pooled.conn_info.set_extras(res.extensions_mut());
|
|
313
|
+
|
|
314
|
+
// If pooled is HTTP/2, we can toss this reference immediately.
|
|
315
|
+
//
|
|
316
|
+
// when pooled is dropped, it will try to insert back into the
|
|
317
|
+
// pool. To delay that, spawn a future that completes once the
|
|
318
|
+
// sender is ready again.
|
|
319
|
+
//
|
|
320
|
+
// This *should* only be once the related `Connection` has polled
|
|
321
|
+
// for a new request to start.
|
|
322
|
+
//
|
|
323
|
+
// It won't be ready if there is a body to stream.
|
|
324
|
+
if pooled.is_http2() || !pooled.is_pool_enabled() || pooled.is_ready() {
|
|
325
|
+
drop(pooled);
|
|
326
|
+
} else {
|
|
327
|
+
let on_idle = std::future::poll_fn(move |cx| pooled.poll_ready(cx)).map(|_| ());
|
|
328
|
+
self.exec.execute(on_idle);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
Ok(res)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async fn connection_for(
|
|
335
|
+
&self,
|
|
336
|
+
req: ConnectRequest,
|
|
337
|
+
) -> Result<pool::Pooled<PoolClient<B>, ConnectIdentity>, Error> {
|
|
338
|
+
loop {
|
|
339
|
+
match self.one_connection_for(req.clone()).await {
|
|
340
|
+
Ok(pooled) => return Ok(pooled),
|
|
341
|
+
Err(ClientConnectError::Normal(err)) => return Err(err),
|
|
342
|
+
Err(ClientConnectError::CheckoutIsClosed(reason)) => {
|
|
343
|
+
if !self.config.retry_canceled_requests {
|
|
344
|
+
return Err(Error::new(ErrorKind::Connect, reason));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
trace!(
|
|
348
|
+
"unstarted request canceled, trying again (reason={:?})",
|
|
349
|
+
reason,
|
|
350
|
+
);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async fn one_connection_for(
|
|
358
|
+
&self,
|
|
359
|
+
req: ConnectRequest,
|
|
360
|
+
) -> Result<pool::Pooled<PoolClient<B>, ConnectIdentity>, ClientConnectError> {
|
|
361
|
+
// Return a single connection if pooling is not enabled
|
|
362
|
+
if !self.pool.is_enabled() {
|
|
363
|
+
return self
|
|
364
|
+
.connect_to(req)
|
|
365
|
+
.await
|
|
366
|
+
.map_err(ClientConnectError::Normal);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// This actually races 2 different futures to try to get a ready
|
|
370
|
+
// connection the fastest, and to reduce connection churn.
|
|
371
|
+
//
|
|
372
|
+
// - If the pool has an idle connection waiting, that's used immediately.
|
|
373
|
+
// - Otherwise, the Connector is asked to start connecting to the destination Uri.
|
|
374
|
+
// - Meanwhile, the pool Checkout is watching to see if any other request finishes and tries
|
|
375
|
+
// to insert an idle connection.
|
|
376
|
+
// - If a new connection is started, but the Checkout wins after (an idle connection became
|
|
377
|
+
// available first), the started connection future is spawned into the runtime to
|
|
378
|
+
// complete, and then be inserted into the pool as an idle connection.
|
|
379
|
+
let checkout = self.pool.checkout(req.identify());
|
|
380
|
+
let connect = self.connect_to(req);
|
|
381
|
+
let is_ver_h2 = self.config.ver == Ver::Http2;
|
|
382
|
+
|
|
383
|
+
// The order of the `select` is depended on below...
|
|
384
|
+
|
|
385
|
+
match futures_util::future::select(checkout, connect).await {
|
|
386
|
+
// Checkout won, connect future may have been started or not.
|
|
387
|
+
//
|
|
388
|
+
// If it has, let it finish and insert back into the pool,
|
|
389
|
+
// so as to not waste the socket...
|
|
390
|
+
Either::Left((Ok(checked_out), connecting)) => {
|
|
391
|
+
// This depends on the `select` above having the correct
|
|
392
|
+
// order, such that if the checkout future were ready
|
|
393
|
+
// immediately, the connect future will never have been
|
|
394
|
+
// started.
|
|
395
|
+
//
|
|
396
|
+
// If it *wasn't* ready yet, then the connect future will
|
|
397
|
+
// have been started...
|
|
398
|
+
if connecting.started() {
|
|
399
|
+
let bg = connecting
|
|
400
|
+
.map_err(|_err| {
|
|
401
|
+
trace!("background connect error: {}", _err);
|
|
402
|
+
})
|
|
403
|
+
.map(|_pooled| {
|
|
404
|
+
// dropping here should just place it in
|
|
405
|
+
// the Pool for us...
|
|
406
|
+
});
|
|
407
|
+
// An execute error here isn't important, we're just trying
|
|
408
|
+
// to prevent a waste of a socket...
|
|
409
|
+
self.exec.execute(bg);
|
|
410
|
+
}
|
|
411
|
+
Ok(checked_out)
|
|
412
|
+
}
|
|
413
|
+
// Connect won, checkout can just be dropped.
|
|
414
|
+
Either::Right((Ok(connected), _checkout)) => Ok(connected),
|
|
415
|
+
// Either checkout or connect could get canceled:
|
|
416
|
+
//
|
|
417
|
+
// 1. Connect is canceled if this is HTTP/2 and there is an outstanding HTTP/2
|
|
418
|
+
// connecting task.
|
|
419
|
+
// 2. Checkout is canceled if the pool cannot deliver an idle connection reliably.
|
|
420
|
+
//
|
|
421
|
+
// In both cases, we should just wait for the other future.
|
|
422
|
+
Either::Left((Err(err), connecting)) => {
|
|
423
|
+
if err.is_canceled() {
|
|
424
|
+
connecting.await.map_err(ClientConnectError::Normal)
|
|
425
|
+
} else {
|
|
426
|
+
Err(ClientConnectError::Normal(Error::new(
|
|
427
|
+
ErrorKind::Connect,
|
|
428
|
+
err,
|
|
429
|
+
)))
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
Either::Right((Err(err), checkout)) => {
|
|
433
|
+
if err.is_canceled() {
|
|
434
|
+
checkout.await.map_err(move |err| {
|
|
435
|
+
if is_ver_h2 && err.is_canceled() {
|
|
436
|
+
ClientConnectError::CheckoutIsClosed(err)
|
|
437
|
+
} else {
|
|
438
|
+
ClientConnectError::Normal(Error::new(ErrorKind::Connect, err))
|
|
439
|
+
}
|
|
440
|
+
})
|
|
441
|
+
} else {
|
|
442
|
+
Err(ClientConnectError::Normal(err))
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
fn connect_to(
|
|
449
|
+
&self,
|
|
450
|
+
req: ConnectRequest,
|
|
451
|
+
) -> impl Lazy<Output = Result<pool::Pooled<PoolClient<B>, ConnectIdentity>, Error>>
|
|
452
|
+
+ Send
|
|
453
|
+
+ Unpin
|
|
454
|
+
+ 'static {
|
|
455
|
+
let executor = self.exec.clone();
|
|
456
|
+
let pool = self.pool.clone();
|
|
457
|
+
|
|
458
|
+
let h1_builder = self.h1_builder.clone();
|
|
459
|
+
let h2_builder = self.h2_builder.clone();
|
|
460
|
+
let ver = match req.extra().alpn_protocol() {
|
|
461
|
+
Some(AlpnProtocol::HTTP2) => Ver::Http2,
|
|
462
|
+
_ => self.config.ver,
|
|
463
|
+
};
|
|
464
|
+
let is_ver_h2 = ver == Ver::Http2;
|
|
465
|
+
let connector = self.connector.clone();
|
|
466
|
+
lazy(move || {
|
|
467
|
+
// Try to take a "connecting lock".
|
|
468
|
+
//
|
|
469
|
+
// If the pool_key is for HTTP/2, and there is already a
|
|
470
|
+
// connection being established, then this can't take a
|
|
471
|
+
// second lock. The "connect_to" future is Canceled.
|
|
472
|
+
let connecting = match pool.connecting(req.identify(), ver) {
|
|
473
|
+
Some(lock) => lock,
|
|
474
|
+
None => {
|
|
475
|
+
let canceled = Error::new_kind(ErrorKind::Canceled);
|
|
476
|
+
// HTTP/2 connection in progress.
|
|
477
|
+
return Either::Right(futures_util::future::err(canceled));
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
Either::Left(
|
|
481
|
+
Oneshot::new(connector, req)
|
|
482
|
+
.map_err(|src| Error::new(ErrorKind::Connect, src))
|
|
483
|
+
.and_then(move |io| {
|
|
484
|
+
let connected = io.connected();
|
|
485
|
+
// If ALPN is h2 and we aren't http2_only already,
|
|
486
|
+
// then we need to convert our pool checkout into
|
|
487
|
+
// a single HTTP2 one.
|
|
488
|
+
let connecting = if connected.is_negotiated_h2() && !is_ver_h2 {
|
|
489
|
+
match connecting.alpn_h2(&pool) {
|
|
490
|
+
Some(lock) => {
|
|
491
|
+
trace!("ALPN negotiated h2, updating pool");
|
|
492
|
+
lock
|
|
493
|
+
}
|
|
494
|
+
None => {
|
|
495
|
+
// Another connection has already upgraded,
|
|
496
|
+
// the pool checkout should finish up for us.
|
|
497
|
+
let canceled =Error::new(ErrorKind::Canceled, "ALPN upgraded to HTTP/2");
|
|
498
|
+
return Either::Right(futures_util::future::err(canceled));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
} else {
|
|
502
|
+
connecting
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
let is_h2 = is_ver_h2 || connected.is_negotiated_h2();
|
|
506
|
+
|
|
507
|
+
Either::Left(Box::pin(async move {
|
|
508
|
+
let tx = if is_h2 {
|
|
509
|
+
{
|
|
510
|
+
let (mut tx, conn) =
|
|
511
|
+
h2_builder.handshake(io).await.map_err(Error::tx)?;
|
|
512
|
+
|
|
513
|
+
trace!(
|
|
514
|
+
"http2 handshake complete, spawning background dispatcher task"
|
|
515
|
+
);
|
|
516
|
+
executor.execute(
|
|
517
|
+
conn.map_err(|_e| debug!("client connection error: {}", _e))
|
|
518
|
+
.map(|_| ()),
|
|
519
|
+
);
|
|
520
|
+
|
|
521
|
+
// Wait for 'conn' to ready up before we
|
|
522
|
+
// declare this tx as usable
|
|
523
|
+
tx.ready().await.map_err(Error::tx)?;
|
|
524
|
+
PoolTx::Http2(tx)
|
|
525
|
+
}
|
|
526
|
+
} else {
|
|
527
|
+
{
|
|
528
|
+
// Perform the HTTP/1.1 handshake on the provided I/O stream. More actions
|
|
529
|
+
// Uses the h1_builder to establish a connection, returning a sender (tx) for requests
|
|
530
|
+
// and a connection task (conn) that manages the connection lifecycle.
|
|
531
|
+
let (mut tx, conn) =
|
|
532
|
+
h1_builder.handshake(io).await.map_err(Error::tx)?;
|
|
533
|
+
|
|
534
|
+
// Log that the HTTP/1.1 handshake has completed successfully.
|
|
535
|
+
// This indicates the connection is established and ready for request processing.
|
|
536
|
+
trace!(
|
|
537
|
+
"http1 handshake complete, spawning background dispatcher task"
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
// Create a oneshot channel to communicate errors from the connection task.
|
|
541
|
+
// err_tx sends errors from the connection task, and err_rx receives them
|
|
542
|
+
// to correlate connection failures with request readiness errors.
|
|
543
|
+
let (err_tx, err_rx) = tokio::sync::oneshot::channel();
|
|
544
|
+
// Spawn the connection task in the background using the executor.
|
|
545
|
+
// The task manages the HTTP/1.1 connection, including upgrades (e.g., WebSocket).
|
|
546
|
+
// Errors are sent via err_tx to ensure they can be checked if the sender (tx) fails.
|
|
547
|
+
executor.execute(
|
|
548
|
+
conn.with_upgrades()
|
|
549
|
+
.map_err(|e| {
|
|
550
|
+
// Log the connection error at debug level for diagnostic purposes.
|
|
551
|
+
debug!("client connection error: {:?}", e);
|
|
552
|
+
// Log that the error is being sent to the error channel.
|
|
553
|
+
trace!("sending connection error to error channel");
|
|
554
|
+
// Send the error via the oneshot channel, ignoring send failures
|
|
555
|
+
// (e.g., if the receiver is dropped, which is handled later).
|
|
556
|
+
let _ = err_tx.send(e);
|
|
557
|
+
})
|
|
558
|
+
.map(|_| ()),
|
|
559
|
+
);
|
|
560
|
+
|
|
561
|
+
// Log that the client is waiting for the connection to be ready.
|
|
562
|
+
// Readiness indicates the sender (tx) can accept a request without blocking. More actions
|
|
563
|
+
trace!("waiting for connection to be ready");
|
|
564
|
+
|
|
565
|
+
// Check if the sender is ready to accept a request.
|
|
566
|
+
// This ensures the connection is fully established before proceeding.
|
|
567
|
+
// Wait for 'conn' to ready up before we
|
|
568
|
+
// declare this tx as usable
|
|
569
|
+
match tx.ready().await {
|
|
570
|
+
// If ready, the connection is usable for sending requests.
|
|
571
|
+
Ok(_) => {
|
|
572
|
+
// Log that the connection is ready for use.
|
|
573
|
+
trace!("connection is ready");
|
|
574
|
+
// Drop the error receiver, as it’s no longer needed since the sender is ready.
|
|
575
|
+
// This prevents waiting for errors that won’t occur in a successful case.
|
|
576
|
+
drop(err_rx);
|
|
577
|
+
// Wrap the sender in PoolTx::Http1 for use in the connection pool.
|
|
578
|
+
PoolTx::Http1(tx)
|
|
579
|
+
}
|
|
580
|
+
// If the sender fails with a closed channel error, check for a specific connection error.
|
|
581
|
+
// This distinguishes between a vague ChannelClosed error and an actual connection failure.
|
|
582
|
+
Err(e) if e.is_closed() => {
|
|
583
|
+
// Log that the channel is closed, indicating a potential connection issue.
|
|
584
|
+
trace!("connection channel closed, checking for connection error");
|
|
585
|
+
// Check the oneshot channel for a specific error from the connection task.
|
|
586
|
+
match err_rx.await {
|
|
587
|
+
// If an error was received, it’s a specific connection failure.
|
|
588
|
+
Ok(err) => {
|
|
589
|
+
// Log the specific connection error for diagnostics.
|
|
590
|
+
trace!("received connection error: {:?}", err);
|
|
591
|
+
// Return the error wrapped in Error::tx to propagate it.
|
|
592
|
+
return Err(Error::tx(err));
|
|
593
|
+
}
|
|
594
|
+
// If the error channel is closed, no specific error was sent.
|
|
595
|
+
// Fall back to the vague ChannelClosed error.
|
|
596
|
+
Err(_) => {
|
|
597
|
+
// Log that the error channel is closed, indicating no specific error.
|
|
598
|
+
trace!("error channel closed, returning the vague ChannelClosed error");
|
|
599
|
+
// Return the original error wrapped in Error::tx.
|
|
600
|
+
return Err(Error::tx(e));
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
// For other errors (e.g., timeout, I/O issues), propagate them directly.
|
|
605
|
+
// These are not ChannelClosed errors and don’t require error channel checks.
|
|
606
|
+
Err(e) => {
|
|
607
|
+
// Log the specific readiness failure for diagnostics.
|
|
608
|
+
trace!("connection readiness failed: {:?}", e);
|
|
609
|
+
// Return the error wrapped in Error::tx to propagate it.
|
|
610
|
+
return Err(Error::tx(e));
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
Ok(pool.pooled(
|
|
617
|
+
connecting,
|
|
618
|
+
PoolClient {
|
|
619
|
+
conn_info: connected,
|
|
620
|
+
tx,
|
|
621
|
+
},
|
|
622
|
+
))
|
|
623
|
+
}))
|
|
624
|
+
}),
|
|
625
|
+
)
|
|
626
|
+
})
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
impl<C, B> tower::Service<Request<B>> for HttpClient<C, B>
|
|
631
|
+
where
|
|
632
|
+
C: tower::Service<ConnectRequest> + Clone + Send + Sync + 'static,
|
|
633
|
+
C::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
|
634
|
+
C::Error: Into<BoxError>,
|
|
635
|
+
C::Future: Unpin + Send + 'static,
|
|
636
|
+
B: Body + Send + 'static + Unpin,
|
|
637
|
+
B::Data: Send,
|
|
638
|
+
B::Error: Into<BoxError>,
|
|
639
|
+
{
|
|
640
|
+
type Response = Response<Incoming>;
|
|
641
|
+
type Error = Error;
|
|
642
|
+
type Future = ResponseFuture;
|
|
643
|
+
|
|
644
|
+
fn poll_ready(&mut self, _: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
645
|
+
Poll::Ready(Ok(()))
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
fn call(&mut self, req: Request<B>) -> Self::Future {
|
|
649
|
+
self.request(req)
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
impl<C, B> tower::Service<Request<B>> for &'_ HttpClient<C, B>
|
|
654
|
+
where
|
|
655
|
+
C: tower::Service<ConnectRequest> + Clone + Send + Sync + 'static,
|
|
656
|
+
C::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
|
657
|
+
C::Error: Into<BoxError>,
|
|
658
|
+
C::Future: Unpin + Send + 'static,
|
|
659
|
+
B: Body + Send + 'static + Unpin,
|
|
660
|
+
B::Data: Send,
|
|
661
|
+
B::Error: Into<BoxError>,
|
|
662
|
+
{
|
|
663
|
+
type Response = Response<Incoming>;
|
|
664
|
+
type Error = Error;
|
|
665
|
+
type Future = ResponseFuture;
|
|
666
|
+
|
|
667
|
+
fn poll_ready(&mut self, _: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
668
|
+
Poll::Ready(Ok(()))
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
fn call(&mut self, req: Request<B>) -> Self::Future {
|
|
672
|
+
self.request(req)
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
impl<C: Clone, B> Clone for HttpClient<C, B> {
|
|
677
|
+
fn clone(&self) -> HttpClient<C, B> {
|
|
678
|
+
HttpClient {
|
|
679
|
+
config: self.config,
|
|
680
|
+
exec: self.exec.clone(),
|
|
681
|
+
h1_builder: self.h1_builder.clone(),
|
|
682
|
+
h2_builder: self.h2_builder.clone(),
|
|
683
|
+
connector: self.connector.clone(),
|
|
684
|
+
pool: self.pool.clone(),
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/// A pooled HTTP connection that can send requests
|
|
690
|
+
struct PoolClient<B> {
|
|
691
|
+
conn_info: Connected,
|
|
692
|
+
tx: PoolTx<B>,
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
enum PoolTx<B> {
|
|
696
|
+
Http1(conn::http1::SendRequest<B>),
|
|
697
|
+
Http2(conn::http2::SendRequest<B>),
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// ===== impl PoolClient =====
|
|
701
|
+
|
|
702
|
+
impl<B> PoolClient<B> {
|
|
703
|
+
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Error>> {
|
|
704
|
+
match self.tx {
|
|
705
|
+
PoolTx::Http1(ref mut tx) => tx.poll_ready(cx).map_err(Error::closed),
|
|
706
|
+
|
|
707
|
+
PoolTx::Http2(_) => Poll::Ready(Ok(())),
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
fn is_http1(&self) -> bool {
|
|
712
|
+
!self.is_http2()
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
fn is_http2(&self) -> bool {
|
|
716
|
+
match self.tx {
|
|
717
|
+
PoolTx::Http1(_) => false,
|
|
718
|
+
|
|
719
|
+
PoolTx::Http2(_) => true,
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
fn is_poisoned(&self) -> bool {
|
|
724
|
+
self.conn_info.poisoned()
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
fn is_ready(&self) -> bool {
|
|
728
|
+
match self.tx {
|
|
729
|
+
PoolTx::Http1(ref tx) => tx.is_ready(),
|
|
730
|
+
|
|
731
|
+
PoolTx::Http2(ref tx) => tx.is_ready(),
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
impl<B: Body + 'static> PoolClient<B> {
|
|
737
|
+
fn try_send_request(
|
|
738
|
+
&mut self,
|
|
739
|
+
req: Request<B>,
|
|
740
|
+
) -> impl Future<Output = Result<Response<Incoming>, ConnTrySendError<Request<B>>>>
|
|
741
|
+
where
|
|
742
|
+
B: Send,
|
|
743
|
+
{
|
|
744
|
+
match self.tx {
|
|
745
|
+
PoolTx::Http1(ref mut tx) => Either::Left(tx.try_send_request(req)),
|
|
746
|
+
PoolTx::Http2(ref mut tx) => Either::Right(tx.try_send_request(req)),
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
impl<B> pool::Poolable for PoolClient<B>
|
|
752
|
+
where
|
|
753
|
+
B: Send + 'static,
|
|
754
|
+
{
|
|
755
|
+
fn is_open(&self) -> bool {
|
|
756
|
+
!self.is_poisoned() && self.is_ready()
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
fn reserve(self) -> pool::Reservation<Self> {
|
|
760
|
+
match self.tx {
|
|
761
|
+
PoolTx::Http1(tx) => pool::Reservation::Unique(PoolClient {
|
|
762
|
+
conn_info: self.conn_info,
|
|
763
|
+
tx: PoolTx::Http1(tx),
|
|
764
|
+
}),
|
|
765
|
+
|
|
766
|
+
PoolTx::Http2(tx) => {
|
|
767
|
+
let b = PoolClient {
|
|
768
|
+
conn_info: self.conn_info.clone(),
|
|
769
|
+
tx: PoolTx::Http2(tx.clone()),
|
|
770
|
+
};
|
|
771
|
+
let a = PoolClient {
|
|
772
|
+
conn_info: self.conn_info,
|
|
773
|
+
tx: PoolTx::Http2(tx),
|
|
774
|
+
};
|
|
775
|
+
pool::Reservation::Shared(a, b)
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
fn can_share(&self) -> bool {
|
|
781
|
+
self.is_http2()
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/// A `Future` that will resolve to an HTTP Response.
|
|
786
|
+
#[must_use = "futures do nothing unless polled"]
|
|
787
|
+
pub struct ResponseFuture {
|
|
788
|
+
inner: Pin<Box<dyn Future<Output = Result<Response<Incoming>, Error>> + Send>>,
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// ===== impl ResponseFuture =====
|
|
792
|
+
|
|
793
|
+
impl ResponseFuture {
|
|
794
|
+
#[inline]
|
|
795
|
+
pub(super) fn new<F>(value: F) -> ResponseFuture
|
|
796
|
+
where
|
|
797
|
+
F: Future<Output = Result<Response<Incoming>, Error>> + Send + 'static,
|
|
798
|
+
{
|
|
799
|
+
ResponseFuture {
|
|
800
|
+
inner: Box::pin(value),
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
impl Future for ResponseFuture {
|
|
806
|
+
type Output = Result<Response<Incoming>, Error>;
|
|
807
|
+
|
|
808
|
+
#[inline]
|
|
809
|
+
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
|
810
|
+
self.inner.as_mut().poll(cx)
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/// A builder to configure a new [`HttpClient`].
|
|
815
|
+
#[derive(Clone)]
|
|
816
|
+
pub struct Builder {
|
|
817
|
+
config: Config,
|
|
818
|
+
exec: Exec,
|
|
819
|
+
|
|
820
|
+
h1_builder: conn::http1::Builder,
|
|
821
|
+
h2_builder: conn::http2::Builder<Exec>,
|
|
822
|
+
pool_config: pool::Config,
|
|
823
|
+
pool_timer: Option<ArcTimer>,
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// ===== impl Builder =====
|
|
827
|
+
|
|
828
|
+
impl Builder {
|
|
829
|
+
/// Construct a new Builder.
|
|
830
|
+
pub fn new<E>(executor: E) -> Self
|
|
831
|
+
where
|
|
832
|
+
E: Executor<BoxSendFuture> + Send + Sync + Clone + 'static,
|
|
833
|
+
{
|
|
834
|
+
let exec = Exec::new(executor);
|
|
835
|
+
Self {
|
|
836
|
+
config: Config {
|
|
837
|
+
retry_canceled_requests: true,
|
|
838
|
+
set_host: true,
|
|
839
|
+
ver: Ver::Auto,
|
|
840
|
+
},
|
|
841
|
+
exec: exec.clone(),
|
|
842
|
+
|
|
843
|
+
h1_builder: conn::http1::Builder::new(),
|
|
844
|
+
h2_builder: conn::http2::Builder::new(exec),
|
|
845
|
+
pool_config: pool::Config {
|
|
846
|
+
idle_timeout: Some(Duration::from_secs(90)),
|
|
847
|
+
max_idle_per_host: usize::MAX,
|
|
848
|
+
max_pool_size: None,
|
|
849
|
+
},
|
|
850
|
+
pool_timer: None,
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
/// Set an optional timeout for idle sockets being kept-alive.
|
|
854
|
+
/// A `Timer` is required for this to take effect. See `Builder::pool_timer`
|
|
855
|
+
///
|
|
856
|
+
/// Pass `None` to disable timeout.
|
|
857
|
+
///
|
|
858
|
+
/// Default is 90 seconds.
|
|
859
|
+
#[inline]
|
|
860
|
+
pub fn pool_idle_timeout<D>(mut self, val: D) -> Self
|
|
861
|
+
where
|
|
862
|
+
D: Into<Option<Duration>>,
|
|
863
|
+
{
|
|
864
|
+
self.pool_config.idle_timeout = val.into();
|
|
865
|
+
self
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/// Sets the maximum idle connection per host allowed in the pool.
|
|
869
|
+
///
|
|
870
|
+
/// Default is `usize::MAX` (no limit).
|
|
871
|
+
#[inline]
|
|
872
|
+
pub fn pool_max_idle_per_host(mut self, max_idle: usize) -> Self {
|
|
873
|
+
self.pool_config.max_idle_per_host = max_idle;
|
|
874
|
+
self
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/// Sets the maximum number of connections in the pool.
|
|
878
|
+
///
|
|
879
|
+
/// Default is `None` (no limit).
|
|
880
|
+
#[inline]
|
|
881
|
+
pub fn pool_max_size(mut self, max_size: impl Into<Option<NonZeroU32>>) -> Self {
|
|
882
|
+
self.pool_config.max_pool_size = max_size.into();
|
|
883
|
+
self
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/// Set whether the connection **must** use HTTP/2.
|
|
887
|
+
///
|
|
888
|
+
/// The destination must either allow HTTP2 Prior Knowledge, or the
|
|
889
|
+
/// `Connect` should be configured to do use ALPN to upgrade to `h2`
|
|
890
|
+
/// as part of the connection process. This will not make the `HttpClient`
|
|
891
|
+
/// utilize ALPN by itself.
|
|
892
|
+
///
|
|
893
|
+
/// Note that setting this to true prevents HTTP/1 from being allowed.
|
|
894
|
+
///
|
|
895
|
+
/// Default is false.
|
|
896
|
+
#[inline]
|
|
897
|
+
pub fn http2_only(mut self, val: bool) -> Self {
|
|
898
|
+
self.config.ver = if val { Ver::Http2 } else { Ver::Auto };
|
|
899
|
+
self
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/// Provide a timer to be used for http2
|
|
903
|
+
///
|
|
904
|
+
/// See the documentation of [`http2::client::Builder::timer`] for more
|
|
905
|
+
/// details.
|
|
906
|
+
///
|
|
907
|
+
/// [`http2::client::Builder::timer`]: https://docs.rs/http2/latest/http2/client/struct.Builder.html#method.timer
|
|
908
|
+
#[inline]
|
|
909
|
+
pub fn http2_timer<M>(mut self, timer: M) -> Self
|
|
910
|
+
where
|
|
911
|
+
M: Timer + Send + Sync + 'static,
|
|
912
|
+
{
|
|
913
|
+
self.h2_builder.timer(timer);
|
|
914
|
+
self
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/// Provide a configuration for HTTP/1.
|
|
918
|
+
#[inline]
|
|
919
|
+
pub fn http1_options<O>(mut self, opts: O) -> Self
|
|
920
|
+
where
|
|
921
|
+
O: Into<Option<Http1Options>>,
|
|
922
|
+
{
|
|
923
|
+
if let Some(opts) = opts.into() {
|
|
924
|
+
self.h1_builder.options(opts);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
self
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/// Provide a configuration for HTTP/2.
|
|
931
|
+
#[inline]
|
|
932
|
+
pub fn http2_options<O>(mut self, opts: O) -> Self
|
|
933
|
+
where
|
|
934
|
+
O: Into<Option<Http2Options>>,
|
|
935
|
+
{
|
|
936
|
+
if let Some(opts) = opts.into() {
|
|
937
|
+
self.h2_builder.options(opts);
|
|
938
|
+
}
|
|
939
|
+
self
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/// Provide a timer to be used for timeouts and intervals in connection pools.
|
|
943
|
+
#[inline]
|
|
944
|
+
pub fn pool_timer<M>(mut self, timer: M) -> Self
|
|
945
|
+
where
|
|
946
|
+
M: Timer + Clone + Send + Sync + 'static,
|
|
947
|
+
{
|
|
948
|
+
self.pool_timer = Some(ArcTimer::new(timer));
|
|
949
|
+
self
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/// Set whether to retry requests that get disrupted before ever starting
|
|
953
|
+
/// to write.
|
|
954
|
+
///
|
|
955
|
+
/// This means a request that is queued, and gets given an idle, reused
|
|
956
|
+
/// connection, and then encounters an error immediately as the idle
|
|
957
|
+
/// connection was found to be unusable.
|
|
958
|
+
///
|
|
959
|
+
/// When this is set to `false`, the related `ResponseFuture` would instead
|
|
960
|
+
/// resolve to an `Error::Cancel`.
|
|
961
|
+
///
|
|
962
|
+
/// Default is `true`.
|
|
963
|
+
#[inline]
|
|
964
|
+
pub fn retry_canceled_requests(mut self, val: bool) -> Self {
|
|
965
|
+
self.config.retry_canceled_requests = val;
|
|
966
|
+
self
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/// Set whether to automatically add the `Host` header to requests.
|
|
970
|
+
///
|
|
971
|
+
/// If true, and a request does not include a `Host` header, one will be
|
|
972
|
+
/// added automatically, derived from the authority of the `Uri`.
|
|
973
|
+
///
|
|
974
|
+
/// Default is `true`.
|
|
975
|
+
#[inline]
|
|
976
|
+
pub fn set_host(mut self, val: bool) -> Self {
|
|
977
|
+
self.config.set_host = val;
|
|
978
|
+
self
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/// Combine the configuration of this builder with a connector to create a `HttpClient`.
|
|
982
|
+
pub fn build<C, B>(self, connector: C) -> HttpClient<C, B>
|
|
983
|
+
where
|
|
984
|
+
C: tower::Service<ConnectRequest> + Clone + Send + Sync + 'static,
|
|
985
|
+
C::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
|
986
|
+
C::Error: Into<BoxError>,
|
|
987
|
+
C::Future: Unpin + Send + 'static,
|
|
988
|
+
B: Body + Send,
|
|
989
|
+
B::Data: Send,
|
|
990
|
+
{
|
|
991
|
+
let exec = self.exec.clone();
|
|
992
|
+
let timer = self.pool_timer.clone();
|
|
993
|
+
HttpClient {
|
|
994
|
+
config: self.config,
|
|
995
|
+
exec: exec.clone(),
|
|
996
|
+
|
|
997
|
+
h1_builder: self.h1_builder,
|
|
998
|
+
h2_builder: self.h2_builder,
|
|
999
|
+
connector,
|
|
1000
|
+
pool: pool::Pool::new(self.pool_config, exec, timer),
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
}
|