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

Handle connecting to ipv6 addresses correctly #386

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 33 additions & 2 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ use std::task::{Context, Poll};
use std::time::Duration;
use tokio::io::ErrorKind;
use tokio::time::sleep;
use url::Url;
use url::{Host, Url};

use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1165,7 +1165,15 @@ impl ServerAddr {

/// Returns the host.
pub fn host(&self) -> &str {
self.0.host_str().unwrap()
match self.0.host() {
Jarema marked this conversation as resolved.
Show resolved Hide resolved
Some(Host::Domain(_)) | Some(Host::Ipv4 { .. }) => self.0.host_str().unwrap(),
// `host_str()` for Ipv6 includes the []s
Some(Host::Ipv6 { .. }) => {
let host = self.0.host_str().unwrap();
&host[1..host.len() - 1]
}
None => "",
}
}

/// Returns the port.
Expand Down Expand Up @@ -1264,3 +1272,26 @@ pub(crate) enum Authorization {
CallbackArg1<String, std::result::Result<String, AuthError>>,
),
}

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

#[test]
fn server_address_ipv6() {
let address = ServerAddr::from_str("nats://[::]").unwrap();
assert_eq!(address.host(), "::")
}

#[test]
fn serverr_address_ipv4() {
let address = ServerAddr::from_str("nats://127.0.0.1").unwrap();
assert_eq!(address.host(), "127.0.0.1")
}

#[test]
fn serverr_address_domain() {
let address = ServerAddr::from_str("nats://example.com").unwrap();
assert_eq!(address.host(), "example.com")
}
}
30 changes: 28 additions & 2 deletions nats/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::str::FromStr;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use url::Url;
use url::{Host, Url};

use webpki::DNSNameRef;

Expand Down Expand Up @@ -625,7 +625,15 @@ impl ServerAddress {

/// Returns the host.
pub fn host(&self) -> &str {
self.0.host_str().unwrap()
match self.0.host() {
Copy link
Member

Choose a reason for hiding this comment

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

I think if with else would be cleaner here.

Some(Host::Domain(_)) | Some(Host::Ipv4 { .. }) => self.0.host_str().unwrap(),
// `host_str()` for Ipv6 includes the []s
Some(Host::Ipv6 { .. }) => {
let host = self.0.host_str().unwrap();
&host[1..host.len() - 1]
}
None => "",
}
}

/// Returns the port.
Expand Down Expand Up @@ -712,6 +720,24 @@ impl IntoServerList for io::Result<Vec<ServerAddress>> {
mod tests {
use super::*;

#[test]
fn server_address_ipv6() {
let address = ServerAddress::from_str("nats://[::]").unwrap();
assert_eq!(address.host(), "::")
}

#[test]
fn server_address_ipv4() {
let address = ServerAddress::from_str("nats://127.0.0.1").unwrap();
assert_eq!(address.host(), "127.0.0.1")
}

#[test]
fn server_address_domain() {
let address = ServerAddress::from_str("nats://example.com").unwrap();
assert_eq!(address.host(), "example.com")
}

#[test]
fn server_address_no_auth() {
let address = ServerAddress::from_str("nats://localhost").unwrap();
Expand Down