diff --git a/src/host.rs b/src/host.rs index 79e9bb89c..6aa820911 100644 --- a/src/host.rs +++ b/src/host.rs @@ -11,9 +11,7 @@ use parser::{ParseError, ParseResult}; use percent_encoding::{percent_decode, utf8_percent_encode, SIMPLE_ENCODE_SET}; use std::cmp; use std::fmt::{self, Formatter}; -use std::io; -use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; -use std::vec; +use std::net::{Ipv4Addr, Ipv6Addr}; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum HostInternal { @@ -228,81 +226,6 @@ impl> fmt::Display for Host { } } -/// This mostly exists because coherence rules don’t allow us to implement -/// `ToSocketAddrs for (Host, u16)`. -#[derive(Clone, Debug)] -pub struct HostAndPort { - pub host: Host, - pub port: u16, -} - -impl<'a> HostAndPort<&'a str> { - /// Return a copy of `self` that owns an allocated `String` but does not borrow an `&Url`. - pub fn to_owned(&self) -> HostAndPort { - HostAndPort { - host: self.host.to_owned(), - port: self.port, - } - } -} - -impl> fmt::Display for HostAndPort { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - self.host.fmt(f)?; - f.write_str(":")?; - self.port.fmt(f) - } -} - -impl> ToSocketAddrs for HostAndPort { - type Iter = SocketAddrs; - - fn to_socket_addrs(&self) -> io::Result { - let port = self.port; - match self.host { - Host::Domain(ref domain) => Ok(SocketAddrs { - // FIXME: use std::net::lookup_host when it’s stable. - state: SocketAddrsState::Domain((domain.as_ref(), port).to_socket_addrs()?), - }), - Host::Ipv4(address) => Ok(SocketAddrs { - state: SocketAddrsState::One(SocketAddr::V4(SocketAddrV4::new(address, port))), - }), - Host::Ipv6(address) => Ok(SocketAddrs { - state: SocketAddrsState::One(SocketAddr::V6(SocketAddrV6::new( - address, port, 0, 0, - ))), - }), - } - } -} - -/// Socket addresses for an URL. -#[derive(Debug)] -pub struct SocketAddrs { - state: SocketAddrsState, -} - -#[derive(Debug)] -enum SocketAddrsState { - Domain(vec::IntoIter), - One(SocketAddr), - Done, -} - -impl Iterator for SocketAddrs { - type Item = SocketAddr; - fn next(&mut self) -> Option { - match self.state { - SocketAddrsState::Domain(ref mut iter) => iter.next(), - SocketAddrsState::One(s) => { - self.state = SocketAddrsState::Done; - Some(s) - } - SocketAddrsState::Done => None, - } - } -} - fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result { let segments = addr.segments(); let (compress_start, compress_end) = longest_zero_sequence(&segments); diff --git a/src/lib.rs b/src/lib.rs index cf3de0ce1..8d13807b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,14 +128,13 @@ use std::cmp; use std::error::Error; use std::fmt::{self, Debug, Formatter, Write}; use std::hash; -use std::io; use std::mem; -use std::net::{IpAddr, ToSocketAddrs}; +use std::net::IpAddr; use std::ops::{Range, RangeFrom, RangeTo}; use std::path::{Path, PathBuf}; use std::str; -pub use host::{Host, HostAndPort, SocketAddrs}; +pub use host::Host; pub use origin::{OpaqueOrigin, Origin}; pub use parser::{ParseError, SyntaxViolation}; pub use path_segments::PathSegmentsMut; @@ -963,51 +962,6 @@ impl Url { self.port.or_else(|| parser::default_port(self.scheme())) } - /// If the URL has a host, return something that implements `ToSocketAddrs`. - /// - /// If the URL has no port number and the scheme’s default port number is not known - /// (see `Url::port_or_known_default`), - /// the closure is called to obtain a port number. - /// Typically, this closure can match on the result `Url::scheme` - /// to have per-scheme default port numbers, - /// and panic for schemes it’s not prepared to handle. - /// For example: - /// - /// ```rust - /// # use url::Url; - /// # use std::net::TcpStream; - /// # use std::io; - /// fn connect(url: &Url) -> io::Result { - /// TcpStream::connect(url.with_default_port(default_port)?) - /// } - /// - /// fn default_port(url: &Url) -> Result { - /// match url.scheme() { - /// "git" => Ok(9418), - /// "git+ssh" => Ok(22), - /// "git+https" => Ok(443), - /// "git+http" => Ok(80), - /// _ => Err(()), - /// } - /// } - /// ``` - pub fn with_default_port(&self, f: F) -> io::Result> - where - F: FnOnce(&Url) -> Result, - { - Ok(HostAndPort { - host: self - .host() - .ok_or(()) - .or_else(|()| io_error("URL has no host"))?, - port: self - .port_or_known_default() - .ok_or(()) - .or_else(|()| f(self)) - .or_else(|()| io_error("URL has no port number"))?, - }) - } - /// Return the path for this URL, as a percent-encoded ASCII string. /// For cannot-be-a-base URLs, this is an arbitrary string that doesn’t start with '/'. /// For other URLs, this starts with a '/' slash @@ -2204,15 +2158,6 @@ impl Url { } } -/// Return an error if `Url::host` or `Url::port_or_known_default` return `None`. -impl ToSocketAddrs for Url { - type Iter = SocketAddrs; - - fn to_socket_addrs(&self) -> io::Result { - self.with_default_port(|_| Err(()))?.to_socket_addrs() - } -} - /// Parse a string as an URL, without a base URL or encoding override. impl str::FromStr for Url { type Err = ParseError; @@ -2519,10 +2464,6 @@ fn file_url_segments_to_pathbuf_windows( Ok(path) } -fn io_error(reason: &str) -> io::Result { - Err(io::Error::new(io::ErrorKind::InvalidData, reason)) -} - /// Implementation detail of `Url::query_pairs_mut`. Typically not used directly. #[derive(Debug)] pub struct UrlQuery<'a> { diff --git a/tests/unit.rs b/tests/unit.rs index ff2033d3d..a326953dd 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -16,7 +16,7 @@ use std::borrow::Cow; use std::cell::{Cell, RefCell}; use std::net::{Ipv4Addr, Ipv6Addr}; use std::path::{Path, PathBuf}; -use url::{form_urlencoded, Host, HostAndPort, Url}; +use url::{form_urlencoded, Host, Url}; #[test] fn size() { @@ -329,42 +329,6 @@ fn form_urlencoded_custom_encoding_override() { assert_eq!(encoded, "FOO=BAR"); } -#[test] -fn host_and_port_display() { - assert_eq!( - format!( - "{}", - HostAndPort { - host: Host::Domain("www.mozilla.org"), - port: 80 - } - ), - "www.mozilla.org:80" - ); - assert_eq!( - format!( - "{}", - HostAndPort:: { - host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), - port: 65535 - } - ), - "1.35.33.49:65535" - ); - assert_eq!( - format!( - "{}", - HostAndPort:: { - host: Host::Ipv6(Ipv6Addr::new( - 0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344 - )), - port: 1337 - } - ), - "[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337" - ) -} - #[test] /// https://github.com/servo/rust-url/issues/61 fn issue_61() {