ftp: fix error of ftp.connect() (fix parts of #7914) (#7915)

pull/7929/head
yuyi 2021-01-07 01:53:25 +08:00 committed by GitHub
parent 5683af821e
commit f751271e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 52 deletions

View File

@ -17,7 +17,6 @@ basic ftp module
dtp.close()
ftp.close()
*/
import net
import io
@ -94,13 +93,12 @@ fn (mut ftp FTP) read() ?(int, string) {
}
pub fn (mut ftp FTP) connect(ip string) ?bool {
conn := net.dial_tcp('$ip:21')?
ftp.conn = conn
ftp.conn = net.dial_tcp('$ip:21') ?
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
code, _ := ftp.read() ?
if code == connected {
return true
}
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
return false
}
@ -147,9 +145,7 @@ pub fn ( mut ftp FTP) pwd() ?string {
}
pub fn (mut ftp FTP) cd(dir string) ? {
ftp.write('CWD $dir') or {
return
}
ftp.write('CWD $dir') or { return }
mut code, mut data := ftp.read() ?
match int(code) {
denied {
@ -172,11 +168,10 @@ fn new_dtp(msg string) ?DTP {
return error('Bad message')
}
ip, port := get_host_ip_from_dtp_message(msg)
conn := net.dial_tcp('$ip:$port') or {
return error('Cannot connect to the data channel')
}
conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
dtp := DTP{
conn: conn
reader: io.new_buffered_reader(reader: io.make_reader(conn))
ip: ip
port: port
}
@ -184,8 +179,7 @@ fn new_dtp(msg string) ?DTP {
}
fn (mut ftp FTP) pasv() ?DTP {
ftp.write('PASV') or {
}
ftp.write('PASV') ?
code, data := ftp.read() ?
$if debug {
println('pass: $data')
@ -198,22 +192,19 @@ fn ( mut ftp FTP) pasv() ?DTP {
}
pub fn (mut ftp FTP) dir() ?[]string {
mut dtp := ftp.pasv() or {
return error('cannot establish data connection')
}
ftp.write('LIST') or {
}
mut dtp := ftp.pasv() or { return error('Cannot establish data connection') }
ftp.write('LIST') ?
code, _ := ftp.read() ?
if code == denied {
return error('LIST denied')
return error('`LIST` denied')
}
if code != open_data_connection {
return error('data channel empty')
return error('Data channel empty')
}
list_dir := dtp.read() ?
result, _ := ftp.read() ?
if result != close_data_connection {
println('LIST not ok')
println('`LIST` not ok')
}
dtp.close()
mut dir := []string{}
@ -228,11 +219,8 @@ pub fn ( mut ftp FTP) dir() ?[]string {
}
pub fn (mut ftp FTP) get(file string) ?[]byte {
mut dtp := ftp.pasv() or {
return error('Cannot stablish data connection')
}
ftp.write('RETR $file') or {
}
mut dtp := ftp.pasv() or { return error('Cannot stablish data connection') }
ftp.write('RETR $file') ?
code, _ := ftp.read() ?
if code == denied {
return error('Permission denied')