Browse Source

SplitHostPort before connecting to the remote.

This, way, we dont have to cleanup the tcp connection if SplitHostPort fails.
Julien Laffaye 10 years ago
parent
commit
e987451f99
1 changed files with 5 additions and 5 deletions
  1. 5 5
      ftp.go

+ 5 - 5
ftp.go

@@ -59,18 +59,18 @@ func Dial(addr string) (*ServerConn, error) {
 // It is generally followed by a call to Login() as most FTP commands require
 // It is generally followed by a call to Login() as most FTP commands require
 // an authenticated user.
 // an authenticated user.
 func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
 func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
-	tconn, err := net.DialTimeout("tcp", addr, timeout)
+	host, _, err := net.SplitHostPort(addr)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	conn := textproto.NewConn(tconn)
-
-	host, _, err := net.SplitHostPort(addr)
+	tconn, err := net.DialTimeout("tcp", addr, timeout)
 	if err != nil {
 	if err != nil {
-		conn.Close()
 		return nil, err
 		return nil, err
 	}
 	}
+
+	conn := textproto.NewConn(tconn)
+
 	c := &ServerConn{
 	c := &ServerConn{
 		conn:     conn,
 		conn:     conn,
 		host:     host,
 		host:     host,