Skip to content

Commit

Permalink
chore(tonic): Refactor api to get addr from request
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Dec 9, 2023
1 parent c1443e2 commit 1870ba1
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,19 @@ impl<T> Request<T> {
/// This currently only works on the server side.
#[cfg(feature = "transport")]
pub fn local_addr(&self) -> Option<SocketAddr> {
let addr = self
.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr());

#[cfg(feature = "tls")]
{
let addr = addr.or_else(|| {
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().local_addr())
})
}
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().local_addr())
});

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
}
addr
}

/// Get the remote address of this connection.
Expand All @@ -238,24 +233,19 @@ impl<T> Request<T> {
/// This currently only works on the server side.
#[cfg(feature = "transport")]
pub fn remote_addr(&self) -> Option<SocketAddr> {
let addr = self
.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr());

#[cfg(feature = "tls")]
{
let addr = addr.or_else(|| {
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().remote_addr())
})
}
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().remote_addr())
});

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
}
addr
}

/// Get the peer certificates of the connected client.
Expand Down

0 comments on commit 1870ba1

Please sign in to comment.