Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): provide generic access to connect info #647

Merged
merged 3 commits into from
May 28, 2021

Conversation

davidpdrsn
Copy link
Member

@davidpdrsn davidpdrsn commented May 14, 2021

Provides generic access to "connection info" provided by the stream of incoming connections through request extensions.

The Connected trait is changed to:

/// Trait that connected IO resources implement and use to produce info about the connection.
///
/// [...]
pub trait Connected {
    /// The connection info type the IO resources generates.
    type ConnectInfo: Clone + Send + Sync + 'static;

    /// Create type holding information about the connection.
    fn connect_info(&self) -> Self::ConnectInfo;
}

TcpStream and AddrStream implement this and return TcpConnectInfo which has remote_addr: Option<SocketAddr>. TlsStream implements it by returning TlsConnectInfo<T> where T is some other connect info type. That has get_ref to access the inner type and peer_certs to get the certificates.

ServerIo is then changed to no longer box the IO type but instead be generic such that no type information is erased:

pub(crate) enum ServerIo<IO> {
    Io(IO),
    #[cfg(feature = "tls")]
    TlsIo(TlsStream<IO>),
}

Where IO and TlsStream<IO> implement Connected.

In MakeSvc we access the connect info from the IO object and set it as a request extension so its accessible later.

Should be fairly flexible since we don't have to keep adding methods to the Connected trait but can instead add those on the types produced.

It is slightly less discoverable but I've updated the uds example to demo it and the remote_addr and peer_certs methods still exist on Request.

Fixes #365

@davidpdrsn davidpdrsn added this to the 0.5 milestone May 14, 2021
@davidpdrsn davidpdrsn force-pushed the david/generic-connect-info branch 3 times, most recently from a8bc5af to 3803722 Compare May 14, 2021 18:11
@davidpdrsn davidpdrsn force-pushed the david/generic-connect-info branch from 39cf177 to f1c5257 Compare May 17, 2021 09:08
@davidpdrsn davidpdrsn marked this pull request as ready for review May 17, 2021 09:19
@davidpdrsn davidpdrsn requested a review from LucioFranco May 17, 2021 09:19
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slick, love it!

@davidpdrsn davidpdrsn merged commit e5e3118 into master May 28, 2021
@davidpdrsn davidpdrsn deleted the david/generic-connect-info branch May 28, 2021 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unix Domain Socket Credential Support with Connected Trait
2 participants