فهرست منبع

Login() is a function on its own.

It allows to try multiple logins without closing the connection.
Drop the useless ConnectAnonymous.
jlaffaye 14 سال پیش
والد
کامیت
6aaa275d08
2فایلهای تغییر یافته به همراه16 افزوده شده و 18 حذف شده
  1. 6 1
      client_test.go
  2. 10 17
      ftp.go

+ 6 - 1
client_test.go

@@ -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)
 	}
 	}

+ 10 - 17
ftp.go

@@ -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) {
-	conn, err := textproto.Dial("tcp", host)
+func Connect(addr string) (*ServerConn, os.Error) {
+	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 nil, err
+		return 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 {
-		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")
+	return err
 }
 }
 
 
 // Enter extended passive mode
 // Enter extended passive mode