From 3248ab4c9ae8e044984037e818fb0a42245a9d8e Mon Sep 17 00:00:00 2001 From: VincenzoLaSpesa Date: Thu, 28 May 2015 12:35:46 +0200 Subject: [PATCH] resolved connetion issues --- ftp.go | 53 +++++++++++++++++++++++++++++++++++++++++------------ ftp_test.go | 52 +++++++++++----------------------------------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/ftp.go b/ftp.go index 58a8ad3..321ef6e 100644 --- a/ftp.go +++ b/ftp.go @@ -113,18 +113,21 @@ func (ftp *FTP) Noop() (err error) { // Send raw commands, return response as string and response code as int func (ftp *FTP) RawCmd(command string, args ...interface{}) (code int, line string) { + if ftp.debug { + log.Printf("Raw-> %s\n", fmt.Sprintf(command, args...), code) + } + code = -1 var err error if err = ftp.send(command, args...); err != nil { return code, "" } - if line, err = ftp.receive(); err != nil { return code, "" } code, err = strconv.Atoi(line[:3]) if ftp.debug { - log.Printf("Raw-> %s -> %d \n", fmt.Sprintf(command, args...), code) + log.Printf("Raw<- <- %d \n", code) } return code, line } @@ -264,12 +267,28 @@ func (ftp *FTP) receive() (string, error) { } if (len(line) >= 4) && (line[3] == '-') { - nextLine := "" - // This is a continuation of output line - nextLine, err = ftp.receive() - line = line + nextLine + //Multiline response + closingCode := line[:3] + " " + for { + str, err := ftp.receiveLine() + line = line + str + if err != nil { + return line, err + } + if len(str) < 4 { + if ftp.debug { + log.Println("Uncorrectly terminated response") + } + break + } else { + if str[:4] == closingCode { + break + } + } + } } - + ftp.ReadAndDiscard() + //fmt.Println(line) return line, err } @@ -503,6 +522,9 @@ func (ftp *FTP) List(path string) (files []string, err error) { return } +/* + + // login on server with strange login behavior func (ftp *FTP) SmartLogin(username string, password string) (err error) { var code int @@ -515,11 +537,13 @@ func (ftp *FTP) SmartLogin(username string, password string) (err error) { if code == 530 { // ok, let's login code, _ = ftp.RawCmd("USER %s", username) - code, _ = ftp.RawCmd("USER %s", username) + code, _ = ftp.RawCmd("NOOP") if code == 331 { // user accepted, password required code, _ = ftp.RawCmd("PASS %s", password) + code, _ = ftp.RawCmd("PASS %s", password) if code == 230 { + code, _ = ftp.RawCmd("NOOP") return } } @@ -530,6 +554,8 @@ func (ftp *FTP) SmartLogin(username string, password string) (err error) { return ftp.Login(username, password) } +*/ + // login to the server func (ftp *FTP) Login(username string, password string) (err error) { if _, err = ftp.cmd("331", "USER %s", username); err != nil { @@ -561,9 +587,11 @@ func Connect(addr string) (*FTP, error) { writer := bufio.NewWriter(conn) reader := bufio.NewReader(conn) - reader.ReadString('\n') + //reader.ReadString('\n') + object := &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: false} + object.receive() - return &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: false}, nil + return object, nil } // connect to server, debug is ON @@ -580,9 +608,10 @@ func ConnectDbg(addr string) (*FTP, error) { var line string - line, err = reader.ReadString('\n') + object := &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: false} + line, _ = object.receive() log.Print(line) - return &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: true}, nil + return object, nil } diff --git a/ftp_test.go b/ftp_test.go index e25df02..62db27f 100644 --- a/ftp_test.go +++ b/ftp_test.go @@ -2,15 +2,16 @@ package goftp import "testing" -import "fmt" +//import "fmt" var goodServer string +var uglyServer string var badServer string func init() { goodServer = "bo.mirror.garr.it:21" - //badServer = "ftp.packardbell.com:21" - badServer = "ftp.musicbrainz.org:21" + badServer = "ftp.packardbell.com:21" + uglyServer = "ftp.musicbrainz.org:21" } func standard(host string) (msg string) { @@ -18,66 +19,35 @@ func standard(host string) (msg string) { var connection *FTP if connection, err = Connect(host); err != nil { - return "Can't connect" + return "Can't connect ->" + err.Error() } if err = connection.Login("anonymous", "anonymous"); err != nil { - return "Can't login" + return "Can't login ->" + err.Error() } - connection.ReadAndDiscard() if _, err = connection.List(""); err != nil { - return "Can't list" + return "Can't list ->" + err.Error() } connection.Close() return "" } -func smart(host string) (msg string) { - var err error - var connection *FTP - - if connection, err = Connect(host); err != nil { - return "Can't connect" - } - if err = connection.SmartLogin("anonymous", "anonymous"); err != nil { - return "Can't login" - } - connection.ReadAndDiscard() - if _, err = connection.List(""); err != nil { - return "Can't list" - } - connection.Close() - return "" -} - -func TestLogin00(t *testing.T) { - fmt.Printf("--- Standard login on good server\n") +func TestLogin_good(t *testing.T) { str := standard(goodServer) if len(str) > 0 { t.Error(str) } } -func TestLogin01(t *testing.T) { - fmt.Printf("--- Standard login on bad server\n") +func TestLogin_bad(t *testing.T) { str := standard(badServer) if len(str) > 0 { t.Error(str) } } -func TestLogin02(t *testing.T) { - fmt.Printf("--- Smart login on good server\n") - str := smart(goodServer) +func TestLogin_ugly(t *testing.T) { + str := standard(uglyServer) if len(str) > 0 { t.Error(str) } } - -func TestLogin03(t *testing.T) { - fmt.Printf("--- Smart login on bad server\n") - str := smart(badServer) - if len(str) > 0 { - t.Error(str) - } - -}