File tree 2 files changed +31
-4
lines changed
2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -186,9 +186,18 @@ impl Future for HttpConnecting {
186
186
let state;
187
187
match self . state {
188
188
State :: Lazy ( ref executor, ref mut host, port) => {
189
- let host = mem:: replace ( host, String :: new ( ) ) ;
190
- let work = dns:: Work :: new ( host, port) ;
191
- state = State :: Resolving ( oneshot:: spawn ( work, executor) ) ;
189
+ // If the host is already an IP addr (v4 or v6),
190
+ // skip resolving the dns and start connecting right away.
191
+ if let Some ( addrs) = dns:: IpAddrs :: try_parse ( host, port) {
192
+ state = State :: Connecting ( ConnectingTcp {
193
+ addrs : addrs,
194
+ current : None
195
+ } )
196
+ } else {
197
+ let host = mem:: replace ( host, String :: new ( ) ) ;
198
+ let work = dns:: Work :: new ( host, port) ;
199
+ state = State :: Resolving ( oneshot:: spawn ( work, executor) ) ;
200
+ }
192
201
} ,
193
202
State :: Resolving ( ref mut future) => {
194
203
match try!( future. poll ( ) ) {
Original file line number Diff line number Diff line change 1
1
use std:: io;
2
- use std:: net:: { SocketAddr , ToSocketAddrs } ;
2
+ use std:: net:: {
3
+ Ipv4Addr , Ipv6Addr ,
4
+ SocketAddr , ToSocketAddrs ,
5
+ SocketAddrV4 , SocketAddrV6 ,
6
+ } ;
3
7
use std:: vec;
4
8
5
9
use :: futures:: { Async , Future , Poll } ;
@@ -30,6 +34,20 @@ pub struct IpAddrs {
30
34
iter : vec:: IntoIter < SocketAddr > ,
31
35
}
32
36
37
+ impl IpAddrs {
38
+ pub fn try_parse ( host : & str , port : u16 ) -> Option < IpAddrs > {
39
+ if let Ok ( addr) = host. parse :: < Ipv4Addr > ( ) {
40
+ let addr = SocketAddrV4 :: new ( addr, port) ;
41
+ return Some ( IpAddrs { iter : vec ! [ SocketAddr :: V4 ( addr) ] . into_iter ( ) } )
42
+ }
43
+ if let Ok ( addr) = host. parse :: < Ipv6Addr > ( ) {
44
+ let addr = SocketAddrV6 :: new ( addr, port, 0 , 0 ) ;
45
+ return Some ( IpAddrs { iter : vec ! [ SocketAddr :: V6 ( addr) ] . into_iter ( ) } )
46
+ }
47
+ None
48
+ }
49
+ }
50
+
33
51
impl Iterator for IpAddrs {
34
52
type Item = SocketAddr ;
35
53
#[ inline]
You can’t perform that action at this time.
0 commit comments