-
-
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
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.