Login() is a function on its own.

It allows to try multiple logins without closing the connection.
Drop the useless ConnectAnonymous.
This commit is contained in:
jlaffaye 2011-09-06 18:27:30 +02:00
parent b70c76ed28
commit 6aaa275d08
2 changed files with 16 additions and 18 deletions

View File

@ -11,7 +11,12 @@ const (
) )
func TestConn(t *testing.T) { func TestConn(t *testing.T) {
c, err := ConnectAnonymous("localhost:21") c, err := Connect("localhost:21")
if err != nil {
t.Fatal(err)
}
err = c.Login("anonymous", "anonymous")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

27
ftp.go
View File

@ -36,13 +36,13 @@ type response struct {
} }
// Connect to a ftp server and returns a ServerConn handler. // Connect to a ftp server and returns a ServerConn handler.
func Connect(host, user, password string) (*ServerConn, os.Error) { func Connect(addr string) (*ServerConn, os.Error) {
conn, err := textproto.Dial("tcp", host) conn, err := textproto.Dial("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
a := strings.SplitN(host, ":", 2) a := strings.SplitN(addr, ":", 2)
c := &ServerConn{conn, a[0]} c := &ServerConn{conn, a[0]}
_, _, err = c.conn.ReadCodeLine(StatusReady) _, _, err = c.conn.ReadCodeLine(StatusReady)
@ -51,26 +51,19 @@ func Connect(host, user, password string) (*ServerConn, os.Error) {
return nil, err return nil, err
} }
return c, nil
}
func (c *ServerConn) Login(user, password string) os.Error {
c.conn.Cmd("USER %s", user) c.conn.Cmd("USER %s", user)
_, _, err = c.conn.ReadCodeLine(StatusUserOK) _, _, err := c.conn.ReadCodeLine(StatusUserOK)
if err != nil { if err != nil {
c.Quit() return err
return nil, err
} }
c.conn.Cmd("PASS %s", password) c.conn.Cmd("PASS %s", password)
_, _, err = c.conn.ReadCodeLine(StatusLoggedIn) _, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
if err != nil { return err
c.Quit()
return nil, err
}
return c, nil
}
// Like Connect() but with anonymous credentials.
func ConnectAnonymous(host string) (*ServerConn, os.Error) {
return Connect(host, "anonymous", "anonymous")
} }
// Enter extended passive mode // Enter extended passive mode