1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

View File

@ -17,7 +17,6 @@ basic ftp module
dtp.close() dtp.close()
ftp.close() ftp.close()
*/ */
import net import net
import io import io
@ -37,15 +36,15 @@ const (
struct DTP { struct DTP {
mut: mut:
conn net.TcpConn conn net.TcpConn
reader io.BufferedReader reader io.BufferedReader
ip string ip string
port int port int
} }
fn (mut dtp DTP) read() ?[]byte { fn (mut dtp DTP) read() ?[]byte {
mut data := []byte{len: 1024} mut data := []byte{len: 1024}
dtp.reader.read(mut data)? dtp.reader.read(mut data) ?
return data return data
} }
@ -56,7 +55,7 @@ fn (dtp DTP) close() {
struct FTP { struct FTP {
mut: mut:
conn net.TcpConn conn net.TcpConn
reader io.BufferedReader reader io.BufferedReader
buffer_size int buffer_size int
} }
@ -70,11 +69,11 @@ fn (mut ftp FTP) write(data string) ? {
$if debug { $if debug {
println('FTP.v >>> $data') println('FTP.v >>> $data')
} }
ftp.conn.write('$data\r\n'.bytes())? ftp.conn.write('$data\r\n'.bytes()) ?
} }
fn (mut ftp FTP) read() ?(int, string) { fn (mut ftp FTP) read() ?(int, string) {
mut data := ftp.reader.read_line()? mut data := ftp.reader.read_line() ?
$if debug { $if debug {
println('FTP.v <<< $data') println('FTP.v <<< $data')
} }
@ -84,7 +83,7 @@ fn (mut ftp FTP) read() ?(int, string) {
code := data[..3].int() code := data[..3].int()
if data[3] == `-` { if data[3] == `-` {
for { for {
data = ftp.reader.read_line()? data = ftp.reader.read_line() ?
if data[..3].int() == code && data[3] != `-` { if data[..3].int() == code && data[3] != `-` {
break break
} }
@ -94,13 +93,12 @@ fn (mut ftp FTP) read() ?(int, string) {
} }
pub fn (mut ftp FTP) connect(ip string) ?bool { pub fn (mut ftp FTP) connect(ip string) ?bool {
conn := net.dial_tcp('$ip:21')? ftp.conn = net.dial_tcp('$ip:21') ?
ftp.conn = conn ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
code, _ := ftp.read()? code, _ := ftp.read() ?
if code == connected { if code == connected {
return true return true
} }
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
return false return false
} }
@ -111,7 +109,7 @@ pub fn (mut ftp FTP) login(user string, passwd string) ?bool {
} }
return false return false
} }
mut code, _ := ftp.read()? mut code, _ := ftp.read() ?
if code == logged_in { if code == logged_in {
return true return true
} }
@ -124,21 +122,21 @@ pub fn (mut ftp FTP) login(user string, passwd string) ?bool {
} }
return false return false
} }
code, _ = ftp.read()? code, _ = ftp.read() ?
if code == logged_in { if code == logged_in {
return true return true
} }
return false return false
} }
pub fn ( mut ftp FTP) close() ? { pub fn (mut ftp FTP) close() ? {
ftp.write('QUIT')? ftp.write('QUIT') ?
ftp.conn.close() ftp.conn.close()
} }
pub fn ( mut ftp FTP) pwd() ?string { pub fn (mut ftp FTP) pwd() ?string {
ftp.write('PWD')? ftp.write('PWD') ?
_, data := ftp.read()? _, data := ftp.read() ?
spl := data.split('"') // " spl := data.split('"') // "
if spl.len >= 2 { if spl.len >= 2 {
return spl[1] return spl[1]
@ -146,11 +144,9 @@ pub fn ( mut ftp FTP) pwd() ?string {
return data return data
} }
pub fn ( mut ftp FTP) cd(dir string) ? { pub fn (mut ftp FTP) cd(dir string) ? {
ftp.write('CWD $dir') or { ftp.write('CWD $dir') or { return }
return mut code, mut data := ftp.read() ?
}
mut code, mut data := ftp.read()?
match int(code) { match int(code) {
denied { denied {
$if debug { $if debug {
@ -158,7 +154,7 @@ pub fn ( mut ftp FTP) cd(dir string) ? {
} }
} }
complete { complete {
code, data = ftp.read()? code, data = ftp.read() ?
} }
else {} else {}
} }
@ -172,21 +168,19 @@ fn new_dtp(msg string) ?DTP {
return error('Bad message') return error('Bad message')
} }
ip, port := get_host_ip_from_dtp_message(msg) ip, port := get_host_ip_from_dtp_message(msg)
conn := net.dial_tcp('$ip:$port') or { conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
return error('Cannot connect to the data channel')
}
dtp := DTP{ dtp := DTP{
conn: conn conn: conn
reader: io.new_buffered_reader(reader: io.make_reader(conn))
ip: ip ip: ip
port: port port: port
} }
return dtp return dtp
} }
fn ( mut ftp FTP) pasv() ?DTP { fn (mut ftp FTP) pasv() ?DTP {
ftp.write('PASV') or { ftp.write('PASV') ?
} code, data := ftp.read() ?
code, data := ftp.read()?
$if debug { $if debug {
println('pass: $data') println('pass: $data')
} }
@ -197,23 +191,20 @@ fn ( mut ftp FTP) pasv() ?DTP {
return dtp return dtp
} }
pub fn ( mut ftp FTP) dir() ?[]string { pub fn (mut ftp FTP) dir() ?[]string {
mut dtp := ftp.pasv() or { mut dtp := ftp.pasv() or { return error('Cannot establish data connection') }
return error('cannot establish data connection') ftp.write('LIST') ?
} code, _ := ftp.read() ?
ftp.write('LIST') or {
}
code, _ := ftp.read()?
if code == denied { if code == denied {
return error('LIST denied') return error('`LIST` denied')
} }
if code != open_data_connection { if code != open_data_connection {
return error('data channel empty') return error('Data channel empty')
} }
list_dir := dtp.read()? list_dir := dtp.read() ?
result, _ := ftp.read()? result, _ := ftp.read() ?
if result != close_data_connection { if result != close_data_connection {
println('LIST not ok') println('`LIST` not ok')
} }
dtp.close() dtp.close()
mut dir := []string{} mut dir := []string{}
@ -228,19 +219,16 @@ pub fn ( mut ftp FTP) dir() ?[]string {
} }
pub fn (mut ftp FTP) get(file string) ?[]byte { pub fn (mut ftp FTP) get(file string) ?[]byte {
mut dtp := ftp.pasv() or { mut dtp := ftp.pasv() or { return error('Cannot stablish data connection') }
return error('Cannot stablish data connection') ftp.write('RETR $file') ?
} code, _ := ftp.read() ?
ftp.write('RETR $file') or {
}
code, _ := ftp.read()?
if code == denied { if code == denied {
return error('Permission denied') return error('Permission denied')
} }
if code != open_data_connection { if code != open_data_connection {
return error('Data connection not ready') return error('Data connection not ready')
} }
blob := dtp.read()? blob := dtp.read() ?
dtp.close() dtp.close()
return blob return blob
} }