Browse Source

Use net.JoinHostPort and net.SplitHostPort

Arnaud Ysmal 12 years ago
parent
commit
a3bb5b8714
1 changed files with 8 additions and 5 deletions
  1. 8 5
      ftp.go

+ 8 - 5
ftp.go

@@ -4,7 +4,6 @@ package ftp
 import (
 import (
 	"bufio"
 	"bufio"
 	"errors"
 	"errors"
-	"fmt"
 	"io"
 	"io"
 	"net"
 	"net"
 	"net/textproto"
 	"net/textproto"
@@ -53,10 +52,14 @@ func Connect(addr string) (*ServerConn, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	a := addr[:strings.LastIndex(addr, ":")]
+	host, _, err := net.SplitHostPort(addr)
+	if err != nil {
+		conn.Close()
+		return nil, err
+	}
 	c := &ServerConn{
 	c := &ServerConn{
 		conn:     conn,
 		conn:     conn,
-		host:     a,
+		host:     host,
 		features: make(map[string]string),
 		features: make(map[string]string),
 	}
 	}
 
 
@@ -198,7 +201,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) {
 	_, nat6Supported := c.features["nat6"]
 	_, nat6Supported := c.features["nat6"]
 	_, epsvSupported := c.features["EPSV"]
 	_, epsvSupported := c.features["EPSV"]
 	// If host is IPv6 => EPSV
 	// If host is IPv6 => EPSV
-	if c.host[0] == '[' {
+	if strings.ContainsAny(c.host, ":%") {
 		epsvSupported = true
 		epsvSupported = true
 	}
 	}
 	if nat6Supported || epsvSupported {
 	if nat6Supported || epsvSupported {
@@ -214,7 +217,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) {
 	}
 	}
 
 
 	// Build the new net address string
 	// Build the new net address string
-	addr := fmt.Sprintf("%s:%d", c.host, port)
+	addr := net.JoinHostPort(c.host, strconv.Itoa(port))
 
 
 	conn, err := net.Dial("tcp", addr)
 	conn, err := net.Dial("tcp", addr)
 	if err != nil {
 	if err != nil {