Skip to content

Commit

Permalink
resolved connetion issues
Browse files Browse the repository at this point in the history
  • Loading branch information
VincenzoLaSpesa committed May 28, 2015
1 parent 41edc78 commit 3248ab4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 53 deletions.
53 changes: 41 additions & 12 deletions ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {

This comment has been minimized.

Copy link
@zaz600

zaz600 Jan 2, 2016

Contributor

this will break loop for blank line like in #8
screenshot_20

on my screen i replace break with continue

if ftp.debug {
log.Println("Uncorrectly terminated response")
}
break
} else {
if str[:4] == closingCode {
break
}
}
}
}

ftp.ReadAndDiscard()
//fmt.Println(line)
return line, err
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
52 changes: 11 additions & 41 deletions ftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,52 @@ 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) {
var err error
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)
}

}

0 comments on commit 3248ab4

Please sign in to comment.