pub struct FtpTransmitter { /* private fields */ }
Expand description
“Active” side of FTP protocol, i.e. fill buffer with desired FTP commands for further delivery to remote server.
Implementations§
Source§impl FtpTransmitter
impl FtpTransmitter
Sourcepub fn to_receiver(self) -> FtpReceiver
pub fn to_receiver(self) -> FtpReceiver
Sometimes you need to manually advance to Receiver
e.g. in case of LIST
or file get commands, servers sends
start data transfer and end data transfer responses.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_login(
self,
buffer: &mut [u8],
count: &mut usize,
login: &str,
) -> FtpReceiver
pub fn send_login( self, buffer: &mut [u8], count: &mut usize, login: &str, ) -> FtpReceiver
Fills the output buffer with the login command (takes login
string argument ),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_password(
self,
buffer: &mut [u8],
count: &mut usize,
pass: &str,
) -> FtpReceiver
pub fn send_password( self, buffer: &mut [u8], count: &mut usize, pass: &str, ) -> FtpReceiver
Fills the output buffer with the password command (takes password
string argument ),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_pwd_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
pub fn send_pwd_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
Fills the output buffer with the PWD command (take current working directory on remote server),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn get_wd(&self) -> &str
pub fn get_wd(&self) -> &str
Returns current working directory. Assumes either that send_pwd_req
or send_cwd_req
has been sent and succeeded.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_type_req(
self,
buffer: &mut [u8],
count: &mut usize,
data_type: DataMode,
) -> FtpReceiver
pub fn send_type_req( self, buffer: &mut [u8], count: &mut usize, data_type: DataMode, ) -> FtpReceiver
Fills the output buffer with the data transfer mode request (binary or text),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn get_type(&self) -> &DataMode
pub fn get_type(&self) -> &DataMode
Returns current data mode. Assumes either that send_type_req
has been sent and succeeded.
Sourcepub fn send_system_req(
self,
buffer: &mut [u8],
count: &mut usize,
) -> FtpReceiver
pub fn send_system_req( self, buffer: &mut [u8], count: &mut usize, ) -> FtpReceiver
Fills the output buffer with the remote system request;
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn get_system(&self) -> (&String, &String)
pub fn get_system(&self) -> (&String, &String)
Returns remote system with subtype. Assumes either that send_system_req
has been sent and succeeded.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_pasv_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
pub fn send_pasv_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
Fills the output buffer with the PASS requests to allow further data transfer (LIST
or get file)
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_get_req(
self,
buffer: &mut [u8],
count: &mut usize,
file_path: &str,
) -> FtpReceiver
pub fn send_get_req( self, buffer: &mut [u8], count: &mut usize, file_path: &str, ) -> FtpReceiver
Fills the output buffer with get remove file command (takes path
string argument ),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_cwd_req(
self,
buffer: &mut [u8],
count: &mut usize,
path: &str,
) -> FtpReceiver
pub fn send_cwd_req( self, buffer: &mut [u8], count: &mut usize, path: &str, ) -> FtpReceiver
Fills the output buffer with change remote working directory command (takes path
string argument ),
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn take_endpoint(&mut self) -> (Ipv4Addr, u16)
pub fn take_endpoint(&mut self) -> (Ipv4Addr, u16)
Takes pair of IP-address and port, where TCP-connection can be opened to.
Assumes send_pasv_req
has been invoked before.
Examples found in repository?
35fn main() {
36 let url = env::args().nth(1).unwrap();
37 println!("url: {}", url);
38 let ftp_url = Url::parse(&url).unwrap();
39 assert!(ftp_url.scheme() == "ftp");
40
41 let mut username = ftp_url.username();
42 if username == "" { username = "anonymous" };
43
44 let password = match ftp_url.password() {
45 Some(value) => value,
46 None => "unspecified",
47 };
48
49 assert!(ftp_url.path() != "");
50
51 let host = ftp_url.host().unwrap();
52 let port:u16 = ftp_url.port().or(Some(21)).unwrap();
53 let filename = ftp_url.path_segments().unwrap().last().unwrap();
54 let remote_path = ftp_url.path_segments().unwrap()
55 .take_while(|part| part.to_string() != filename.to_string())
56 .fold(String::new(), |mut acc, part| { acc.push_str("/"); acc.push_str(part); acc } );
57
58 println!("start dowloading {} at {}...", filename, remote_path);
59
60 let mut tx_buff:[u8; 1024] = [0; 1024];
61 let mut tx_count = 0;
62 let mut rx_buff:[u8; 1024] = [0; 1024];
63
64 let host_port = format!("{}:{}", host, port);
65 let mut stream = TcpStream::connect(host_port.as_str()).unwrap();
66 let mut ftp_receiver = FtpReceiver::new();
67
68 let mut transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
69 println!("connected to {}:{}", host, port);
70
71 ftp_receiver = transmitter.send_login(&mut tx_buff, &mut tx_count, username);
72 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
73 println!("login sent...");
74
75 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
76 println!("expecting password...");
77
78 ftp_receiver = transmitter.send_password(&mut tx_buff, &mut tx_count, password);
79 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
80 println!("password sent...");
81
82 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
83 println!("logged in...");
84
85 ftp_receiver = transmitter.send_system_req(&mut tx_buff, &mut tx_count);
86 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
87 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
88 {
89 let (system, subtype) = transmitter.get_system().clone();
90 println!("remote system {} / {}", system, subtype);
91 }
92
93 ftp_receiver = transmitter.send_cwd_req(&mut tx_buff, &mut tx_count, &remote_path);
94 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
95 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
96 println!("cwd to {}", remote_path);
97
98 ftp_receiver = transmitter.send_pwd_req(&mut tx_buff, &mut tx_count);
99 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
100 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
101 println!("changed remote directory is {}", transmitter.get_wd());
102
103 ftp_receiver = transmitter.send_type_req(&mut tx_buff, &mut tx_count, DataMode::Binary);
104 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
105 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
106 println!("switched to binary mode");
107
108 let mut data_stream = {
109 ftp_receiver = transmitter.send_pasv_req(&mut tx_buff, &mut tx_count);
110 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
111 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
112 let (addr, port) = transmitter.take_endpoint().clone();
113 println!("confirmed passive connection on {}:{}", addr, port);
114 TcpStream::connect((addr, port)).unwrap()
115 };
116 println!("passive connection opened");
117
118 ftp_receiver = transmitter.send_get_req(&mut tx_buff, &mut tx_count, ftp_url.path());
119
120 let _ = stream.write_all(&tx_buff[0 .. tx_count]).unwrap();
121 transmitter = get_reply(&mut stream, &mut rx_buff, ftp_receiver);
122 println!("starting downloading file {}", filename);
123
124 let mut local_file = File::create(filename).unwrap();
125 let mut eof = false;
126 let mut data_in = [0; 40960];
127 while !eof {
128 let count = data_stream.read(&mut data_in).unwrap();
129 // println!("got {} bytes", count);
130 eof = count == 0;
131 if !eof { let _ = local_file.write(&data_in[0 .. count]).unwrap(); };
132 }
133 local_file.flush().unwrap();
134 println!("");
135
136 println!("got file {}", filename);
137 let _ = get_reply(&mut stream, &mut rx_buff, transmitter.to_receiver());
138 println!("Success ... ");
139
140}
Sourcepub fn send_list_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
pub fn send_list_req(self, buffer: &mut [u8], count: &mut usize) -> FtpReceiver
Fills the output buffer with LIST
command to get directory listing of current remote working directory;
modifies count
variable with the count of written bytes and returns FtpReceiver
.
Sourcepub fn parse_list(&self, data: &[u8]) -> Result<Vec<RemoteFile>, FtpError>
pub fn parse_list(&self, data: &[u8]) -> Result<Vec<RemoteFile>, FtpError>
Parses remote directory listing, requested by send_list_req
command.