AnyEvent/FTP/Client.pm

filter: Statementstmt 87.7% 158 / 180 Branchbran 68.7% 22 / 32 Conditioncond 50.0% 9 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 85.0% 51 / 60 Podpod 97.0% 33 / 34 totaltotal 82.0% 275 / 335 SCAR 33.1
CC
24
Cov
82%
__ANON__
8.9
connect
6.3
__ANON__
4.0
CRAP 27.3
« AnyEvent/FTP/Server/Context/Memory.pm ↑ Summary AnyEvent/FTP/Server/Context/FS.pm »
1 package AnyEvent::FTP::Client;
2  
3 24 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use AnyEvent::Socket qw( tcp_connect );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Socket qw( unpack_sockaddr_in inet_ntoa );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Simple asynchronous ftp client
12 24 our $VERSION = '0.19'; # VERSION
13  
14  
15 24 with 'AnyEvent::FTP::Role::Event';
16 24 with 'AnyEvent::FTP::Client::Role::ResponseBuffer';
17 24 with 'AnyEvent::FTP::Client::Role::RequestBuffer';
18  
19  
20 24 __PACKAGE__->define_events(qw( error close send greeting ));
21  
22 has _connected => (
23 is => 'rw',
24 63 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 24 init_arg => undef,
26 );
27  
28  
29 has timeout => (
30 is => 'rw',
31 63 ▶ default => sub { 30 },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 24 );
33  
34  
35 has passive => (
36 is => 'ro',
37 45 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 24 );
39  
40 24 foreach my $xfer (qw( Store Fetch List ))
41 {
42 my $cb = sub {
43 return shift->passive
44 26 ▶ ? 'AnyEvent::FTP::Client::Transfer::Passive::'.$xfer
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
(shift())->passive ? : 15 11 26
45 : 'AnyEvent::FTP::Client::Transfer::Active::'.$xfer;
46 72 };
47 72 has '_'.lc($xfer) => ( is => 'ro', lazy => 1, default => $cb, init_arg => undef ),
48 }
49  
50 sub BUILD
51 {
52 63 ▶ my($self) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
53 63 ▶ $self->on_error(sub { warn shift });
Subroutine __ANON__: not called CC=1 SCAR=1.2
54 $self->on_close(sub {
55 41 ▶ $self->clear_command;
Subroutine __ANON__: called CC=1 SCAR=0.0
56 41 $self->_connected(0);
57 41 delete $self->{handle};
58 63 });
59  
60 63 ▶ require ($self->passive
Branch: 1 branch
BranchTrueFalseTotal
$self->passive ? : 54 9 63
61 ? 'AnyEvent/FTP/Client/Transfer/Passive.pm'
62 : 'AnyEvent/FTP/Client/Transfer/Active.pm');
63  
64 63 return;
65 }
66  
67  
68 sub connect
69 {
70 69 ▶ my($self, $host, $port) = @_;
Subroutine connect: called CC=6 SCAR=18.4
Pod documented
71  
72 69 ▶ if($host =~ /^ftp:/)
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^ftp:/) 31 38 69
73 {
74 31 require URI;
75 31 $host = URI->new($host);
76 }
77  
78 69 my $uri;
79  
80 69 ▶ if(ref($host) && eval { $host->isa('URI') })
Branch: 1 branch
BranchTrueFalseTotal
if (ref $host and eval { do { $host->isa('URI') } }) { } 31 38 69
Condition66%2/3
ref $host and eval { do { $host->isa("URI") } }
!ll&&!rl&&r
38031
MC/DC50%1/2
ref $host and eval { do { $host->isa("URI") } }
ref $host eval { do { $host->isa("URI") } }
Truth table66%2/3
A and B
A= ref $hostB= eval { do { $host->isa("URI") } }
ABresult
0X0
100
111
81 {
82 31 $uri = $host;
83 31 $host = $uri->host;
84 31 $port = $uri->port;
85 }
86 else
87 {
88 38 ▶ $port //= 21;
Condition50%1/2
$port //= 21
l!l
380
MC/DC0%0/1
$port //= 21
$port
Truth table50%1/2
A
A= $port
Aresult
11
00
89 }
90  
91 69 ▶ croak "Tried to reconnect while connected" if $self->_connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connected 0 69 69
92  
93 69 my $cv = AnyEvent->condvar;
94 69 $self->_connected(1);
95  
96 tcp_connect $host, $port, sub {
97 69 ▶ my($fh) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.3
98 69 ▶ unless($fh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 69 69
99 {
100 0 $cv->croak("unable to connect: $!");
101 0 $self->_connected(0);
102 0 $self->clear_command;
103 0 return;
104 }
105  
106 # Get the IP address we are sending from for when
107 # we use the PORT command (passive=0).
108 69 $self->{my_ip} = do {
109 69 my($port, $addr) = unpack_sockaddr_in getsockname $fh;
110 69 inet_ntoa $addr;
111 };
112  
113 $self->{handle} = AnyEvent::Handle->new(
114 fh => $fh,
115 on_error => sub {
116 0 ▶ my ($hdl, $fatal, $msg) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 $_[0]->destroy;
118 0 $self->emit('error', $msg);
119 0 $self->emit('close');
120 },
121 on_eof => sub {
122 41 ▶ $self->{handle}->destroy;
Subroutine __ANON__: called CC=1 SCAR=0.0
123 41 $self->emit('close');
124 },
125 69 );
126  
127 $self->on_next_response(sub {
128 69 ▶ my $res = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
129 69 ▶ return $cv->croak($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 69 69
130 69 $self->emit(greeting => $res);
131 69 ▶ if(defined $uri)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 31 38 69
132 {
133 31 my @start_commands = (
134 [USER => $uri->user],
135 [PASS => $uri->password],
136 );
137 31 ▶ push @start_commands, [CWD => $uri->path] if $uri->path ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $uri->path ne "" 4 27 31
138 31 $self->unshift_command(@start_commands, $cv);
139 }
140 else
141 {
142 38 $cv->send($res);
143 38 $self->pop_command;
144 }
145 69 });
146  
147 $self->{handle}->on_read(sub {
148 $self->{handle}->push_read( line => sub {
149 825 ▶ my($handle, $line) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
150 825 $self->process_message_line($line);
151 825 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
152 69 });
153  
154 }, sub {
155 69 ▶ $self->timeout;
Subroutine __ANON__: called CC=1 SCAR=0.0
156 69 };
157  
158 69 return $cv;
159 }
160  
161  
162 sub login
163 {
164 32 ▶ my($self, $user, $pass) = @_;
Subroutine login: called CC=1 SCAR=0.0
Pod documented
165 32 $self->push_command(
166 [ USER => $user ],
167 [ PASS => $pass ]
168 );
169 }
170  
171  
172 sub retr
173 {
174 21 ▶ my($self, $filename, $local) = (shift, shift, shift);
Subroutine retr: called CC=2 SCAR=7.6
Pod documented
175 21 ▶ my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 21 21
176 $self->_fetch->new({
177 command => [ RETR => $filename ],
178 local => $local,
179 client => $self,
180 restart => $args->{restart},
181 21 });
182 }
183  
184  
185 sub stor
186 {
187 15 ▶ my($self, $filename, $local) = @_;
Subroutine stor: called CC=1 SCAR=0.0
Pod documented
188 15 $self->_store->new(
189 command => [STOR => $filename],
190 local => $local,
191 client => $self,
192 );
193 }
194  
195  
196 sub stou
197 {
198 3 ▶ my($self, $filename, $local) = @_;
Subroutine stou: called CC=1 SCAR=0.0
Pod documented
199 3 my $xfer;
200 my $cb = sub {
201 6 ▶ my $name = shift->get_file;
Subroutine __ANON__: called CC=2 SCAR=6.9
202 6 ▶ $xfer->{remote_name} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 3 3 6
203 6 return;
204 3 };
205 3 $xfer = $self->_store->new(
206 command => [STOU => $filename, $cb],
207 local => $local,
208 client => $self,
209 );
210 }
211  
212  
213 # for this to work under ProFTPd: AllowStoreRestart off
214 sub appe
215 {
216 7 ▶ my($self, $filename, $local) = @_;
Subroutine appe: called CC=1 SCAR=0.0
Pod documented
217 7 $self->_store->new(
218 command => [APPE => $filename],
219 local => $local,
220 client => $self,
221 );
222 }
223  
224  
225 sub list
226 {
227 24 ▶ my($self, $location, $verb) = @_;
Subroutine list: called CC=2 SCAR=6.9
Pod documented
228 24 ▶ $verb //= 'LIST';
Condition100%2/2
$verb //= "LIST"
l!l
168
MC/DC100%1/1
$verb //= "LIST"
$verb
Truth table100%2/2
A
A= $verb
Aresult
11
00
229 24 my @lines;
230 24 my $cv = AnyEvent->condvar;
231 $self->_list->new(
232 command => [ $verb => $location ],
233 local => \@lines,
234 client => $self,
235 )->cb(sub {
236 24 ▶ my $res = eval { shift->recv };
Subroutine __ANON__: called CC=2 SCAR=7.0
237 24 ▶ $cv->croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 24 24
238 24 $cv->send(\@lines);
239 24 });
240 24 $cv;
241 }
242  
243  
244 sub nlst
245 {
246 16 ▶ my($self, $location) = @_;
Subroutine nlst: called CC=1 SCAR=0.0
Pod documented
247 16 $self->list($location, 'NLST');
248 }
249  
250  
251 sub rename
252 {
253 3 ▶ my($self, $from, $to) = @_;
Subroutine rename: called CC=1 SCAR=0.0
Pod documented
254 3 $self->push_command(
255 [ RNFR => $from ],
256 [ RNTO => $to ],
257 );
258 }
259  
260  
261 sub pwd
262 {
263 7 ▶ my($self) = @_;
Subroutine pwd: called CC=1 SCAR=0.2
Pod documented
264 7 my $cv = AnyEvent->condvar;
265 $self->push_command(['PWD'])->cb(sub {
266 7 ▶ my $res = eval { shift->recv } // $@;
Subroutine __ANON__: called CC=3 SCAR=12.3
Condition33%1/3
eval { do { (shift())->recv } } // $@
l!l&&r!l&&!r
700
MC/DC0%0/2
eval { do { (shift())->recv } } // $@
eval { do { (shift())->recv } } $@
Truth table33%1/3
A // B
A= eval { do { (shift())->recv } }B= $@
ABresult
1X1
011
000
267 7 my $dir = $res->get_dir;
268 7 ▶ if($dir) { $cv->send($dir) }
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 7 0 7
269 0 else { $cv->croak($res) }
270 7 });
271 7 $cv;
272 }
273  
274  
275 sub size
276 {
277 6 ▶ my($self, $path) = @_;
Subroutine size: called CC=1 SCAR=0.1
Pod documented
278 6 my $cv = AnyEvent->condvar;
279 $self->push_command(['SIZE', $path])->cb(sub {
280 6 ▶ my $res = eval { shift->recv };
Subroutine __ANON__: called CC=2 SCAR=7.4
281 6 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) { } 0 6 6
282 0 { $cv->croak($error) }
283 else
284 6 { $cv->send($res->message->[0]) }
285 6 });
286 6 $cv;
287 }
288  
289  
290 (eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_)) // die $@
291 0 ▶ for qw( CWD CDUP NOOP ALLO SYST TYPE STRU MODE REST MKD RMD STAT HELP DELE RNFR RNTO USER PASS ACCT MDTM );
Subroutine acct: not called CC=1 SCAR=0.3
Subroutine allo: called CC=1 SCAR=0.3
Subroutine cdup: called CC=1 SCAR=0.3
Subroutine cwd: called CC=1 SCAR=0.3
Subroutine dele: called CC=1 SCAR=0.3
Subroutine help: called CC=1 SCAR=0.3
Subroutine mdtm: not called CC=1 SCAR=0.3
Subroutine mkd: called CC=1 SCAR=0.3
Subroutine mode: not called CC=1 SCAR=0.3
Subroutine noop: called CC=1 SCAR=0.3
Subroutine pass: not called CC=1 SCAR=0.3
Subroutine rest: called CC=1 SCAR=0.3
Subroutine rmd: called CC=1 SCAR=0.3
Subroutine rnfr: called CC=1 SCAR=0.3
Subroutine rnto: called CC=1 SCAR=0.3
Subroutine stat: called CC=1 SCAR=0.3
Subroutine stru: not called CC=1 SCAR=0.3
Subroutine syst: called CC=1 SCAR=0.3
Subroutine type: called CC=1 SCAR=0.3
Subroutine user: not called CC=1 SCAR=0.3
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Condition50%1/2
eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_) // die($@)
l!l
4800
MC/DC0%0/1
eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_) // die($@)
eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_)
Truth table50%1/2
A
A= eval sprintf('sub %s { shift->push_command([ %s => @_])};1', lc $_, $_)
Aresult
11
00
292  
293  
294 sub quit
295 {
296 38 ▶ my($self) = @_;
Subroutine quit: called CC=1 SCAR=0.5
Pod documented
297 38 my $cv = AnyEvent->condvar;
298  
299 38 my $res;
300  
301 $self->push_command(['QUIT'])->cb(sub {
302 38 ▶ $res = eval { shift->recv } // $@;
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
eval { do { (shift())->recv } } // $@
l!l&&r!l&&!r
3800
MC/DC0%0/2
eval { do { (shift())->recv } } // $@
eval { do { (shift())->recv } } $@
Truth table33%1/3
A // B
A= eval { do { (shift())->recv } }B= $@
ABresult
1X1
011
000
303 38 });
304  
305 38 my $save = $self->{event}->{close};
306 $self->{event}->{close} = [ sub {
307 38 ▶ if(defined $res && $res->is_success)
Subroutine __ANON__: called CC=5 SCAR=21.9
Branch: 2 branches
BranchTrueFalseTotal
if (defined $res and $res->is_success) { } 38 0 38
elsif (defined $res) { } 0 0 0
Condition33%1/3
defined $res and $res->is_success
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $res and $res->is_success
defined $res $res->is_success
Truth table33%1/3
A and B
A= defined $resB= $res->is_success
ABresult
0X0
100
111
308 38 { $cv->send($res) }
309 elsif(defined $res)
310 0 { $cv->croak($res) }
311 else
312 0 { $cv->croak("did not receive QUIT response from server") }
313 38 $_->() for @$save;
314 38 $self->{event}->{close} = $save;
315 38 } ];
316  
317 38 return $cv;
318 }
319  
320  
321 sub site
322 {
323 0 ▶ require AnyEvent::FTP::Client::Site;
Subroutine site: not called CC=1 SCAR=6.9
Pod documented
324 0 AnyEvent::FTP::Client::Site->new(shift);
325 }
326  
327 24 1;
328  
329 __END__
« AnyEvent/FTP/Server/Context/Memory.pm ↑ Summary AnyEvent/FTP/Server/Context/FS.pm »