ftp: cosmetic fixes
Use single quotes for strings. Use string interpolation instead of concatenation. Don't print messages in non-debug mode. Fix typos in error messages. Add space after comma, add spaces between operators. extract code to separate functionspull/3250/head
parent
c09ce9cb42
commit
f40f023487
|
@ -75,8 +75,8 @@ fn (ftp FTP) write(data string) ?int {
|
|||
$if debug {
|
||||
println('FTP.v >>> $data')
|
||||
}
|
||||
n := ftp.sock.send_string(data + '\n') or {
|
||||
return error('cannot send data')
|
||||
n := ftp.sock.send_string('$data\n') or {
|
||||
return error('Cannot send data')
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
@ -119,9 +119,10 @@ pub fn (ftp mut FTP) connect(ip string) bool {
|
|||
}
|
||||
|
||||
pub fn (ftp FTP) login(user, passwd string) bool {
|
||||
|
||||
ftp.write('USER '+user) or {
|
||||
ftp.write('USER $user') or {
|
||||
$if debug {
|
||||
println('ERROR sending user')
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -137,8 +138,10 @@ pub fn (ftp FTP) login(user, passwd string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
ftp.write('PASS '+passwd) or {
|
||||
ftp.write('PASS $passwd') or {
|
||||
$if debug {
|
||||
println('ERROR sending password')
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -174,7 +177,9 @@ pub fn (ftp FTP) cd(dir string) {
|
|||
mut code, mut data := ftp.read()
|
||||
match code {
|
||||
Denied {
|
||||
println("CD $dir denied!")
|
||||
$if debug {
|
||||
println('CD $dir denied!')
|
||||
}
|
||||
}
|
||||
Complete {
|
||||
code, data = ftp.read()
|
||||
|
@ -182,23 +187,20 @@ pub fn (ftp FTP) cd(dir string) {
|
|||
else {}
|
||||
}
|
||||
|
||||
println('cd $data')
|
||||
$if debug {
|
||||
println('CD $data')
|
||||
}
|
||||
}
|
||||
|
||||
fn new_dtp(msg string) ?DTP {
|
||||
// it receives a control message 227 like:
|
||||
// '227 Entering Passive Mode (209,132,183,61,48,218)'
|
||||
|
||||
if !msg.contains('(') || !msg.contains(')') || !msg.contains(',') {
|
||||
return error('bad message')
|
||||
if !is_dtp_message_valid(msg) {
|
||||
return error('Bad message')
|
||||
}
|
||||
|
||||
t := msg.split('(')[1].split(')')[0].split(',')
|
||||
ip := t[0]+'.'+t[1]+'.'+t[2]+'.'+t[3]
|
||||
port := t[4].int()*256+t[5].int()
|
||||
ip, port := get_host_ip_from_dtp_message(msg)
|
||||
|
||||
sock := net.dial(ip, port) or {
|
||||
return error('Cant connect to the data channel')
|
||||
return error('Cannot connect to the data channel')
|
||||
}
|
||||
|
||||
dtp := DTP {
|
||||
|
@ -212,7 +214,9 @@ fn new_dtp(msg string) ?DTP {
|
|||
fn (ftp FTP) pasv() ?DTP {
|
||||
ftp.write('PASV') or {}
|
||||
code, data := ftp.read()
|
||||
println("pass: $data")
|
||||
$if debug {
|
||||
println('pass: $data')
|
||||
}
|
||||
|
||||
if code != PassiveMode {
|
||||
return error('pasive mode not allowed')
|
||||
|
@ -231,7 +235,7 @@ pub fn (ftp FTP) dir() ?[]string {
|
|||
ftp.write('LIST') or {}
|
||||
code, _ := ftp.read()
|
||||
if code == Denied {
|
||||
return error('list denied')
|
||||
return error('LIST denied')
|
||||
}
|
||||
if code != OpenDataConnection {
|
||||
return error('data channel empty')
|
||||
|
@ -258,18 +262,18 @@ pub fn (ftp FTP) dir() ?[]string {
|
|||
|
||||
pub fn (ftp FTP) get(file string) ?[]byte {
|
||||
dtp := ftp.pasv() or {
|
||||
return error('cant stablish data connection')
|
||||
return error('Cannot stablish data connection')
|
||||
}
|
||||
|
||||
ftp.write('RETR $file') or {}
|
||||
code, _ := ftp.read()
|
||||
|
||||
if code == Denied {
|
||||
return error('permission denied')
|
||||
return error('Permission denied')
|
||||
}
|
||||
|
||||
if code != OpenDataConnection {
|
||||
return error('data connection not ready')
|
||||
return error('Data connection not ready')
|
||||
}
|
||||
|
||||
blob := dtp.read()
|
||||
|
@ -277,3 +281,28 @@ pub fn (ftp FTP) get(file string) ?[]byte {
|
|||
|
||||
return blob
|
||||
}
|
||||
|
||||
fn is_dtp_message_valid(msg string) bool {
|
||||
// An example of message:
|
||||
// '227 Entering Passive Mode (209,132,183,61,48,218)'
|
||||
return msg.contains('(') && msg.contains(')') && msg.contains(',')
|
||||
}
|
||||
|
||||
fn get_host_ip_from_dtp_message(msg string) (string, int) {
|
||||
mut par_start_idx := -1
|
||||
mut par_end_idx := -1
|
||||
|
||||
for i, c in msg {
|
||||
if c == `(` {
|
||||
par_start_idx = i + 1
|
||||
} else if c == `)` {
|
||||
par_end_idx = i
|
||||
}
|
||||
}
|
||||
data := msg[par_start_idx..par_end_idx].split(',')
|
||||
|
||||
ip := data[0..4].join('.')
|
||||
port := data[4].int() * 256 + data[5].int()
|
||||
|
||||
return ip, port
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue