Make "OPTS UTF8 ON" optional (#172)

This commit is contained in:
CrazyMax 2020-05-05 13:04:49 +02:00
parent b9f3ade291
commit 60012218fd
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
2 changed files with 35 additions and 33 deletions

View File

@ -26,6 +26,11 @@ if err != nil {
log.Fatal(err)
}
err = c.SetUTF8()
if err != nil {
log.Fatal(err)
}
// Do something with the FTP conn
if err := c.Quit(); err != nil {

63
ftp.go
View File

@ -279,9 +279,6 @@ func (c *ServerConn) Login(user, password string) error {
return err
}
// Switch to UTF-8
err = c.setUTF8()
// If using implicit TLS, make data connections also use TLS
if c.options.tlsConfig != nil {
c.cmd(StatusCommandOK, "PBSZ 0")
@ -334,36 +331,6 @@ func (c *ServerConn) feat() error {
return nil
}
// setUTF8 issues an "OPTS UTF8 ON" command.
func (c *ServerConn) setUTF8() error {
if _, ok := c.features["UTF8"]; !ok {
return nil
}
code, message, err := c.cmd(-1, "OPTS UTF8 ON")
if err != nil {
return err
}
// Workaround for FTP servers, that does not support this option.
if code == StatusBadArguments {
return nil
}
// The ftpd "filezilla-server" has FEAT support for UTF8, but always returns
// "202 UTF8 mode is always enabled. No need to send this command." when
// trying to use it. That's OK
if code == StatusCommandNotImplemented {
return nil
}
if code != StatusCommandOK {
return errors.New(message)
}
return nil
}
// epsv issues an "EPSV" command to get a port number for a data connection.
func (c *ServerConn) epsv() (port int, err error) {
_, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
@ -564,6 +531,36 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) {
return
}
// SetUTF8 issues an "OPTS UTF8 ON" command.
func (c *ServerConn) SetUTF8() error {
if _, ok := c.features["UTF8"]; !ok {
return nil
}
code, message, err := c.cmd(-1, "OPTS UTF8 ON")
if err != nil {
return err
}
// Workaround for FTP servers, that does not support this option.
if code == StatusBadArguments {
return nil
}
// The ftpd "filezilla-server" has FEAT support for UTF8, but always returns
// "202 UTF8 mode is always enabled. No need to send this command." when
// trying to use it. That's OK
if code == StatusCommandNotImplemented {
return nil
}
if code != StatusCommandOK {
return errors.New(message)
}
return nil
}
// ChangeDir issues a CWD FTP command, which changes the current directory to
// the specified path.
func (c *ServerConn) ChangeDir(path string) error {