-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ext): introduce extensions, starting with ConnectionInfo
- Adds `client::Builder::set_conn_info` to opt-in to having connection info added to `Response`s from clients. - Adds `ext::ConnectionInfo` that allows querying types (like a `Response`) for connection info. Closes #1402
- Loading branch information
1 parent
e06dc52
commit d253456
Showing
7 changed files
with
248 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ use http::uri::Scheme; | |
use body::{Body, Payload}; | ||
use common::Exec; | ||
use common::lazy as hyper_lazy; | ||
use self::connect::{Connect, Destination}; | ||
use self::connect::{Connect, Connected, Destination}; | ||
use self::pool::{Pool, Poolable, Reservation}; | ||
|
||
#[cfg(feature = "runtime")] pub use self::connect::HttpConnector; | ||
|
@@ -113,6 +113,7 @@ pub struct Client<C, B = Body> { | |
h1_title_case_headers: bool, | ||
pool: Pool<PoolClient<B>>, | ||
retry_canceled_requests: bool, | ||
set_conn_info: bool, | ||
set_host: bool, | ||
ver: Ver, | ||
} | ||
|
@@ -304,7 +305,7 @@ where C: Connect + Sync + 'static, | |
}) | ||
.map(move |tx| { | ||
pool.pooled(connecting, PoolClient { | ||
is_proxied: connected.is_proxied, | ||
conn_info: connected, | ||
tx: match ver { | ||
Ver::Http1 => PoolTx::Http1(tx), | ||
Ver::Http2 => PoolTx::Http2(tx.into_http2()), | ||
|
@@ -380,14 +381,16 @@ where C: Connect + Sync + 'static, | |
} | ||
}); | ||
|
||
let set_conn_info = self.set_conn_info; | ||
|
||
let executor = self.executor.clone(); | ||
let resp = race.and_then(move |mut pooled| { | ||
let conn_reused = pooled.is_reused(); | ||
if ver == Ver::Http1 { | ||
// CONNECT always sends origin-form, so check it first... | ||
if req.method() == &Method::CONNECT { | ||
authority_form(req.uri_mut()); | ||
} else if pooled.is_proxied { | ||
} else if pooled.conn_info.is_proxied { | ||
absolute_form(req.uri_mut()); | ||
} else { | ||
origin_form(req.uri_mut()); | ||
|
@@ -401,6 +404,17 @@ where C: Connect + Sync + 'static, | |
|
||
let fut = pooled.send_request_retryable(req); | ||
|
||
let conn_info = pooled.conn_info.clone(); | ||
let fut = fut.map(move |mut res| { | ||
if set_conn_info { | ||
let info = ::ext::ConnectionInfo { | ||
remote_addr: conn_info.remote_addr, | ||
}; | ||
info.set(&mut res); | ||
} | ||
res | ||
}); | ||
|
||
// As of [email protected], there is a race condition in the mpsc | ||
// channel, such that sending when the receiver is closing can | ||
// result in the message being stuck inside the queue. It won't | ||
|
@@ -498,6 +512,7 @@ impl<C, B> Clone for Client<C, B> { | |
h1_title_case_headers: self.h1_title_case_headers, | ||
pool: self.pool.clone(), | ||
retry_canceled_requests: self.retry_canceled_requests, | ||
set_conn_info: self.set_conn_info, | ||
set_host: self.set_host, | ||
ver: self.ver, | ||
} | ||
|
@@ -584,7 +599,7 @@ where | |
} | ||
|
||
struct PoolClient<B> { | ||
is_proxied: bool, | ||
conn_info: Connected, | ||
tx: PoolTx<B>, | ||
} | ||
|
||
|
@@ -644,17 +659,17 @@ where | |
match self.tx { | ||
PoolTx::Http1(tx) => { | ||
Reservation::Unique(PoolClient { | ||
is_proxied: self.is_proxied, | ||
conn_info: self.conn_info, | ||
tx: PoolTx::Http1(tx), | ||
}) | ||
}, | ||
PoolTx::Http2(tx) => { | ||
let b = PoolClient { | ||
is_proxied: self.is_proxied, | ||
conn_info: self.conn_info.clone(), | ||
tx: PoolTx::Http2(tx.clone()), | ||
}; | ||
let a = PoolClient { | ||
is_proxied: self.is_proxied, | ||
conn_info: self.conn_info, | ||
tx: PoolTx::Http2(tx), | ||
}; | ||
Reservation::Shared(a, b) | ||
|
@@ -751,6 +766,7 @@ pub struct Builder { | |
//TODO: make use of max_idle config | ||
max_idle: usize, | ||
retry_canceled_requests: bool, | ||
set_conn_info: bool, | ||
set_host: bool, | ||
ver: Ver, | ||
} | ||
|
@@ -765,6 +781,7 @@ impl Default for Builder { | |
h1_title_case_headers: false, | ||
max_idle: 5, | ||
retry_canceled_requests: true, | ||
set_conn_info: false, | ||
set_host: true, | ||
ver: Ver::Http1, | ||
} | ||
|
@@ -851,6 +868,16 @@ impl Builder { | |
self | ||
} | ||
|
||
|
||
/// Set whether to automatically add [`ConnectionInfo`](::ext::ConnectionInfo) | ||
/// to `Response`s. | ||
/// | ||
/// Default is `false`. | ||
pub fn set_conn_info(&mut self, val: bool) -> &mut Self { | ||
self.set_conn_info = val; | ||
self | ||
} | ||
|
||
/// Set whether to automatically add the `Host` header to requests. | ||
/// | ||
/// If true, and a request does not include a `Host` header, one will be | ||
|
@@ -902,6 +929,7 @@ impl Builder { | |
h1_title_case_headers: self.h1_title_case_headers, | ||
pool: Pool::new(self.keep_alive, self.keep_alive_timeout, &self.exec), | ||
retry_canceled_requests: self.retry_canceled_requests, | ||
set_conn_info: self.set_conn_info, | ||
set_host: self.set_host, | ||
ver: self.ver, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::net::SocketAddr; | ||
|
||
use super::Ext; | ||
|
||
/// dox | ||
#[derive(Debug)] | ||
pub struct ConnectionInfo { | ||
pub(crate) remote_addr: Option<SocketAddr>, | ||
} | ||
|
||
// The private type that gets put into extensions() | ||
// | ||
// The reason for the public and private types is to, for now, prevent | ||
// a public API contract that crates could depend on. If the public type | ||
// were inserted into the Extensions directly, a user could depend on | ||
// `req.extensions().get::<ConnectionInfo>()`, and it's not clear that | ||
// we want this contract yet. | ||
#[derive(Copy, Clone, Default)] | ||
struct ConnInfo { | ||
remote_addr: Option<SocketAddr>, | ||
} | ||
|
||
impl ConnectionInfo { | ||
/// dox | ||
pub fn get<E>(extend: &E) -> ConnectionInfo | ||
where | ||
E: Ext, | ||
{ | ||
let info = extend | ||
.ext() | ||
.get::<ConnInfo>() | ||
.map(|&info| info) | ||
.unwrap_or_default(); | ||
|
||
ConnectionInfo { | ||
remote_addr: info.remote_addr, | ||
} | ||
} | ||
|
||
/// dox | ||
pub(crate) fn set<E>(self, extend: &mut E) | ||
where | ||
E: Ext, | ||
{ | ||
let info = ConnInfo { | ||
remote_addr: self.remote_addr, | ||
}; | ||
|
||
extend.ext_mut().insert(info); | ||
} | ||
|
||
/// dox | ||
pub fn remote_addr(&self) -> Option<SocketAddr> { | ||
self.remote_addr | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! dox | ||
use http::{Extensions, Request, Response}; | ||
|
||
use self::sealed::{Ext, Sealed}; | ||
|
||
mod conn_info; | ||
|
||
pub use self::conn_info::ConnectionInfo; | ||
|
||
|
||
mod sealed { | ||
use http::Extensions; | ||
|
||
pub trait Sealed { | ||
fn ext(&self) -> &Extensions; | ||
fn ext_mut(&mut self) -> &mut Extensions; | ||
} | ||
|
||
pub trait Ext: Sealed {} | ||
} | ||
|
||
impl<B> Sealed for Request<B> { | ||
fn ext(&self) -> &Extensions { | ||
self.extensions() | ||
} | ||
|
||
fn ext_mut(&mut self) -> &mut Extensions { | ||
self.extensions_mut() | ||
} | ||
} | ||
|
||
impl<B> Ext for Request<B> {} | ||
|
||
impl<B> Sealed for Response<B> { | ||
fn ext(&self) -> &Extensions { | ||
self.extensions() | ||
} | ||
|
||
fn ext_mut(&mut self) -> &mut Extensions { | ||
self.extensions_mut() | ||
} | ||
} | ||
|
||
impl<B> Ext for Response<B> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.