Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion tonic/src/transport/channel/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ impl ClientTlsConfig {
pub(crate) fn into_tls_connector(self, uri: &Uri) -> Result<TlsConnector, crate::BoxError> {
let domain = match &self.domain {
Some(domain) => domain,
None => uri.host().ok_or_else(Error::new_invalid_uri)?,
None => {
let host = uri.host().ok_or_else(Error::new_invalid_uri)?;
// host() returns the host including brackets if it's an IPv6 address
if host.starts_with('[') && host.ends_with(']') {
&host[1..host.len() - 1]
} else {
host
}
}
};
TlsConnector::new(
self.certs,
Expand All @@ -153,3 +161,39 @@ impl ClientTlsConfig {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_into_tls_connector_with_ipv4() {
let config = ClientTlsConfig::new();
let uri = "https://192.168.1.1:443".parse::<Uri>().unwrap();
config.into_tls_connector(&uri).unwrap();
}

#[test]
fn test_into_tls_connector_with_ipv6_() {
let config = ClientTlsConfig::new();
let uri = "https://[::1]:443".parse::<Uri>().unwrap();

config.into_tls_connector(&uri).unwrap();
}

#[test]
fn test_into_tls_connector_with_domain_name() {
let config = ClientTlsConfig::new();
let uri = "https://example.com:443".parse::<Uri>().unwrap();

config.into_tls_connector(&uri).unwrap();
}

#[test]
fn test_into_tls_connector_with_explicit_domain() {
let config = ClientTlsConfig::new().domain_name("example.com");
let uri = "https://[2001:db8::1]:443".parse::<Uri>().unwrap();

config.into_tls_connector(&uri).unwrap();
}
}