Use net/textproto instead of net.

This commit is contained in:
jlaffaye 2011-05-07 12:56:42 +01:00
parent 3b5a440874
commit 7b2259fd5d

73
ftp.go
View File

@ -3,6 +3,7 @@ package ftp
import ( import (
"bufio" "bufio"
"net" "net"
"net/textproto"
"os" "os"
"fmt" "fmt"
"strconv" "strconv"
@ -16,8 +17,8 @@ const (
) )
type ServerConn struct { type ServerConn struct {
conn net.Conn conn *textproto.Conn
bio *bufio.Reader host string
} }
type Response struct { type Response struct {
@ -31,60 +32,31 @@ type Entry struct {
Size uint64 Size uint64
} }
// Check if the last status code is equal to the given code
// If it is the case, err is nil
// Returns the status line for further processing
func (c *ServerConn) checkStatus(expected int) (line string, err os.Error) {
line, err = c.bio.ReadString('\n')
if err != nil {
return
}
code, err := strconv.Atoi(line[:3]) // A status is 3 digits
if err != nil {
return
}
if code != expected {
err = os.NewError(fmt.Sprintf("%d %s", code, statusText[code]))
return
}
return
}
// Like send() but with formating.
func (c *ServerConn) sendf(str string, a ...interface{}) (os.Error) {
return c.send([]byte(fmt.Sprintf(str, a...)))
}
// Send a raw command on the connection.
func (c *ServerConn) send(data []byte) (os.Error) {
_, err := c.conn.Write(data)
return err
}
// 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(host, user, password string) (*ServerConn, os.Error) {
conn, err := net.Dial("tcp", host) conn, err := textproto.Dial("tcp", host)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := &ServerConn{conn, bufio.NewReader(conn)} a := strings.Split(host, ":", 2)
c := &ServerConn{conn, a[0]}
_, err = c.checkStatus(StatusReady) _, _, err = c.conn.ReadCodeLine(StatusReady)
if err != nil { if err != nil {
c.Close() c.Close()
return nil, err return nil, err
} }
c.sendf("USER %v\r\n", user) c.conn.Cmd("USER %s", user)
_, err = c.checkStatus(StatusUserOK) _, _, err = c.conn.ReadCodeLine(StatusUserOK)
if err != nil { if err != nil {
c.Close() c.Close()
return nil, err return nil, err
} }
c.sendf("PASS %v\r\n", password) c.conn.Cmd("PASS %s", password)
_, err = c.checkStatus(StatusLoggedIn) _, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
if err != nil { if err != nil {
c.Close() c.Close()
return nil, err return nil, err
@ -100,8 +72,8 @@ func ConnectAnonymous(host string) (*ServerConn, os.Error) {
// Enter extended passive mode // Enter extended passive mode
func (c *ServerConn) epsv() (port int, err os.Error) { func (c *ServerConn) epsv() (port int, err os.Error) {
c.send([]byte("EPSV\r\n")) c.conn.Cmd("EPSV")
line, err := c.checkStatus(StatusExtendedPassiveMode) _, line, err := c.conn.ReadCodeLine(StatusExtendedPassiveMode)
if err != nil { if err != nil {
return return
} }
@ -123,8 +95,7 @@ func (c *ServerConn) openDataConnection() (r *Response, err os.Error) {
} }
// Build the new net address string // Build the new net address string
a := strings.Split(c.conn.RemoteAddr().String(), ":", 2) addr := fmt.Sprintf("%s:%d", c.host, port)
addr := fmt.Sprintf("%v:%v", a[0], port)
conn, err := net.Dial("tcp", addr) conn, err := net.Dial("tcp", addr)
if err != nil { if err != nil {
@ -164,8 +135,8 @@ func (c *ServerConn) List() (entries []*Entry, err os.Error) {
} }
defer r.Close() defer r.Close()
c.send([]byte("LIST\r\n")) c.conn.Cmd("LIST")
_, err = c.checkStatus(StatusAboutToSend) _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
if err != nil { if err != nil {
return return
} }
@ -187,8 +158,8 @@ func (c *ServerConn) List() (entries []*Entry, err os.Error) {
} }
func (c *ServerConn) ChangeDir(path string) (err os.Error) { func (c *ServerConn) ChangeDir(path string) (err os.Error) {
c.sendf("CWD %s\r\n", path); c.conn.Cmd("CWD %s", path);
_, err = c.checkStatus(StatusRequestedFileActionOK) _, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
return return
} }
@ -198,20 +169,20 @@ func (c *ServerConn) Get(path string) (r *Response, err os.Error) {
return return
} }
c.sendf("RETR %s\r\n", path) c.conn.Cmd("RETR %s", path)
_, err = c.checkStatus(StatusAboutToSend) _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
return return
} }
func (c *ServerConn) Close() { func (c *ServerConn) Close() {
c.send([]byte("QUIT\r\n")) c.conn.Cmd("QUIT")
c.conn.Close() c.conn.Close()
} }
func (r *Response) Read(buf []byte) (int, os.Error) { func (r *Response) Read(buf []byte) (int, os.Error) {
n, err := r.conn.Read(buf) n, err := r.conn.Read(buf)
if err == os.EOF { if err == os.EOF {
_, err2 := r.c.checkStatus(StatusClosingDataConnection) _, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
if err2 != nil { if err2 != nil {
err = err2 err = err2
} }