SimpleFTP.pm

filter: Statementstmt 19.8% 97 / 489 Branchbran 0.6% 1 / 162 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 23.8% 15 / 63 Podpod 25.0% 1 / 4 totaltotal 15.2% 114 / 748 CC 89 SCAR 32.6
CC
89
Cov
15%
__ANON__
132.0
BUILD
72.0
_ftpd_complex_start
42.0
CRAP 25.9
↑ Summary SimpleFTP/Utils.pm »
1 #
2 # This file is part of POE-Component-Client-SimpleFTP
3 #
4 # This software is copyright (c) 2014 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::Client::SimpleFTP;
11 # git description: release-0.003-11-g9ec2c28
12 1 $POE::Component::Client::SimpleFTP::VERSION = '0.004';
13 1 our $AUTHORITY = 'cpan:APOCAL';
14  
15 # ABSTRACT: A simple FTP client library for POE
16  
17 1 ▶use POE::Component::Client::SimpleFTP::Utils qw( :code EOL );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use MooseX::POE::SweetArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use POE::Filter::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Socket qw( INADDR_ANY AF_INET SOCK_STREAM unpack_sockaddr_in inet_ntoa );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
28  
29 #pod =func DEBUG
30 #pod
31 #pod Enable this if you want to get debugging output. Do it like this:
32 #pod
33 #pod sub POE::Component::Client::SimpleFTP::DEBUG () { 1 }
34 #pod use POE::Component::Client::SimpleFTP;
35 #pod
36 #pod The default is: false
37 #pod
38 #pod =cut
39  
40 1 ▶ if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::Client::SimpleFTP::DEBUG) 1 0 1
41 }
42  
43 #pod =attr alias
44 #pod
45 #pod The alias this component will use. You can send commands to the ftpd in 2 ways:
46 #pod
47 #pod my $ftp = POE::Component::Client::SimpleFTP->new( alias => "ftp", ... );
48 #pod $poe_kernel->post( 'ftp', 'cd', 'foobar' );
49 #pod
50 #pod # Or, you can use the yield sub:
51 #pod $ftp->yield( 'cd', 'foobar' );
52 #pod
53 #pod The default is: ftp
54 #pod
55 #pod =cut
56  
57 1 has alias => (
58 isa => 'Str',
59 is => 'ro',
60 default => 'ftp',
61 );
62  
63 #pod =attr username
64 #pod
65 #pod The FTP username you will be sending to the server.
66 #pod
67 #pod required.
68 #pod
69 #pod =cut
70  
71 1 has username => (
72 isa => 'Str',
73 is => 'ro',
74 required => 1,
75 );
76  
77 #pod =attr password
78 #pod
79 #pod The FTP password you will be sending to the server.
80 #pod
81 #pod required.
82 #pod
83 #pod =cut
84  
85 1 has password => (
86 isa => 'Str',
87 is => 'ro',
88 required => 1,
89 );
90  
91 #pod =attr remote_addr
92 #pod
93 #pod The IP address of the FTP server to connect to. Can be a DNS hostname or IPv4/6 string.
94 #pod
95 #pod required.
96 #pod
97 #pod =cut
98  
99 1 has remote_addr => (
100 isa => 'Str',
101 is => 'ro',
102 required => 1,
103 );
104  
105 #pod =attr remote_port
106 #pod
107 #pod The port of the FTP server to connect to.
108 #pod
109 #pod The default is: 21
110 #pod
111 #pod =cut
112  
113 1 has remote_port => (
114 isa => 'Int',
115 is => 'ro',
116 default => 21,
117 );
118  
119 #pod =attr local_addr
120 #pod
121 #pod The local IP address to bind to for all connections to the server.
122 #pod
123 #pod The default is: INADDR_ANY ( let the OS decide )
124 #pod
125 #pod =cut
126  
127 1 has local_addr => (
128 isa => 'Str',
129 is => 'ro',
130 default => INADDR_ANY,
131 );
132  
133 #pod =attr local_port
134 #pod
135 #pod The local port to bind to for the control connection to the server. If you need to change the data connection's port, please
136 #pod change the L</local_data_port> attribute.
137 #pod
138 #pod The default is: 0 ( let the OS decide )
139 #pod
140 #pod =cut
141  
142 1 has local_port => (
143 isa => 'Int',
144 is => 'ro',
145 default => 0,
146 );
147  
148 #pod =attr local_data_port
149 #pod
150 #pod The local port to bind to for the data connection to the server. Must be a different port than the L</local_port> attribute!
151 #pod
152 #pod The default is: 0 ( let the OS decide )
153 #pod
154 #pod =cut
155  
156 1 has local_data_port => (
157 isa => 'Int',
158 is => 'ro',
159 default => 0,
160 );
161  
162 #pod =attr tls_cmd
163 #pod
164 #pod A boolean value to enable/disable TLS encryption of the command connection. If you want to use this,
165 #pod you must have L<POE::Component::SSLify> installed!
166 #pod
167 #pod The default is: false
168 #pod
169 #pod =cut
170  
171 1 has tls_cmd => (
172 isa => 'Bool',
173 is => 'ro',
174 writer => '_set_tls_cmd',
175 default => 0,
176 );
177  
178 #pod =attr tls_data
179 #pod
180 #pod A boolean value to enable/disable TLS encryption of the data connection. If you want to use this,
181 #pod you must have L<POE::Component::SSLify> installed!
182 #pod
183 #pod The default is: false
184 #pod
185 #pod =cut
186  
187 1 has tls_data => (
188 isa => 'Bool',
189 is => 'ro',
190 writer => '_set_tls_data',
191 default => 0,
192 );
193  
194 #pod =attr timeout
195 #pod
196 #pod A value specifying the timeout in seconds for the initial connection to the FTP server.
197 #pod
198 #pod The default is: 120
199 #pod
200 #pod =cut
201  
202 1 has timeout => (
203 isa => 'Int',
204 is => 'ro',
205 default => 120,
206 );
207  
208 #pod =attr connection_mode
209 #pod
210 #pod Determine what connection mode we will be using when opening the data connection to the server. In "active" mode,
211 #pod the server will be connecting to us. In "passive" mode we will be connecting to the server. You usually need "passive" mode
212 #pod if you are behind a firewall.
213 #pod
214 #pod The default is: passive
215 #pod
216 #pod =cut
217  
218 {
219 1 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 1 has connection_mode => (
222 isa => enum( [ qw( active passive ) ] ),
223 is => 'ro',
224 default => 'passive',
225 );
226 }
227  
228 ### internal attributes
229  
230 # the socketfactory/readwrite wheels for the command connection
231 1 has cmd_sf => (
232 isa => 'Maybe[POE::Wheel::SocketFactory]',
233 is => 'rw',
234 init_arg => undef,
235 );
236  
237 1 has cmd_rw => (
238 isa => 'Maybe[POE::Wheel::ReadWrite]',
239 is => 'rw',
240 init_arg => undef,
241 );
242  
243 # the socketfactory/readwrite wheels for the data connection
244 1 has data_sf => (
245 isa => 'Maybe[POE::Wheel::SocketFactory]',
246 is => 'rw',
247 init_arg => undef,
248 );
249  
250 1 has data_rw => (
251 isa => 'Maybe[POE::Wheel::ReadWrite]',
252 is => 'rw',
253 init_arg => undef,
254 );
255  
256 {
257 1 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 # it is valid until the next complex command, then we check this to see
260 # what we need to do
261 # A = ascii
262 # I = image
263 1 has data_type => (
264 isa => enum( [ qw( A I ) ] ),
265 is => 'rw',
266 init_arg => undef,
267 predicate => '_has_data_type',
268 );
269 }
270  
271 # holds data for multi-line replies
272 1 has input_buffer => (
273 isa => 'Maybe[Str]',
274 is => 'rw',
275 init_arg => undef,
276 default => undef,
277 );
278  
279 1 has input_buffer_code => (
280 isa => 'Int',
281 is => 'rw',
282 init_arg => undef,
283 );
284  
285 # the session that spawned us and receives events from us
286 1 has _master => (
287 isa => 'Int',
288 is => 'rw',
289 init_arg => undef,
290 );
291  
292 # the internal state of the connection
293 1 has state => (
294 isa => 'Str',
295 is => 'rw',
296 default => 'connect',
297 init_arg => undef,
298 # ( DEBUG ? ( trigger => sub {
299 # my( $self, $new, $old ) = @_;
300 # warn "switching from state($old) to state($new)\n";
301 # return;
302 # } ) : () ),
303 );
304  
305 # holds what "simple" command we are processing when state is 'simple_command'
306 1 has simple_command => (
307 isa => 'Str',
308 is => 'rw',
309 init_arg => undef,
310 # ( DEBUG ? ( trigger => sub {
311 # my( $self, $new, $old ) = @_;
312 # warn "switching from simple_command($old) to simple_command($new)\n";
313 # return;
314 # } ) : () ),
315 );
316  
317 # holds whatever data the command needs
318 has command_data => (
319 isa => 'Any',
320 is => 'rw',
321 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 1 init_arg => undef,
323 # ( DEBUG ? ( trigger => sub {
324 # my( $self, $new, $old ) = @_;
325 #use Data::Dumper::Concise;
326 # warn "switching from command_data(" . Dumper($old) . ") to command_data(" . Dumper($new) . ")\n";
327 # return;
328 # } ) : () ),
329 );
330  
331 # translation from posted events to ftp commands
332 1 my %command_map = (
333 'cd' => "CWD",
334 'mkdir' => "MKD",
335 'rmdir' => "RMD",
336 'ls' => "LIST",
337 'dir' => "NLST",
338 'get' => "RETR",
339 'put' => "STOR",
340 'delete' => "DELE",
341 'quote' => "QUOT",
342 'disconnect' => "QUIT",
343 'features' => "FEAT",
344 'options' => "OPTS",
345 );
346  
347 1 my @simple_commands = ( qw(
348 cdup mdtm noop pwd site size stat syst help acct smnt
349 cd cwd
350 dele delete
351 mkd mkdir
352 rmd rmdir
353 quot quote
354 quit disconnect
355 feat features
356 opts options
357 ) );
358  
359 1 my @complex_commands = ( qw(
360 list ls
361 nlst dir
362 retr get
363 stor stou put
364 ) );
365  
366 # a simple command that forces shutdown
367 0 ▶event shutdown => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 my $self = shift;
369  
370 0 $self->_shutdown;
371 0 return;
372 1 };
373  
374 # build our "simple" command handlers
375 1 foreach my $cmd ( @simple_commands ) {
376 0 ▶ event $cmd => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
377 0 my( $self, @args ) = @_;
378 0 my $command = $cmd;
379  
380 # ignore commands if we are shutting down
381 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
382  
383 # are we already sending a command?
384 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
385 # special-case the quit/disconnect methods
386 0 ▶ if ( $cmd =~ /^(?:quit|disconnect)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^(?:quit|disconnect)$/) { } 0 0 0
387 0 $self->_shutdown;
388 0 return;
389 } else {
390 0 die "Unable to send '$cmd' because we are processing " . $self->state;
391 }
392 }
393  
394 # do we need to translate the command to the actual FTP command?
395 0 ▶ if ( exists $command_map{ $cmd } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_map{$cmd}) 0 0 0
396 0 $command = $command_map{ $cmd };
397 }
398  
399 # store the command we are processing then send it
400 0 warn "doing simple_command($cmd) with data(" . join( ' ', @args ) . ")\n" if DEBUG;
401 0 $self->simple_command( $cmd );
402 0 $self->command_data( \@args );
403 0 $self->command( 'simple_command', $command, @args );
404 0 return;
405 27 };
406 }
407  
408 # build our "complex" command handlers ( they require a data connection )
409 1 foreach my $cmd ( @complex_commands ) {
410 0 ▶ event $cmd => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
411 0 my( $self, @args ) = @_;
412  
413 # ignore commands if we are shutting down
414 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
415  
416 # are we already sending a command?
417 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
418 0 die "Unable to send '$cmd' because we are processing " . $self->state;
419 }
420  
421 # start doing this command!
422 0 warn "doing complex command($cmd) with data(" . join( ' ', @args ) . ")\n" if DEBUG;
423 0 $self->command_data( {
424 'cmd' => $cmd,
425 'data' => \@args,
426 } );
427 0 ▶ if ( $cmd =~ /^(?:ls|dir|list|nlst)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ /^(?:ls|dir|list|nlst)$/) { } 0 0 0
elsif ($cmd =~ /^(?:get|put|retr|stor|stou)$/) { } 0 0 0
428 0 $self->_prepare_listing;
429 } elsif ( $cmd =~ /^(?:get|put|retr|stor|stou)$/ ) {
430 0 $self->_prepare_transfer;
431 }
432  
433 0 return;
434 9 };
435 }
436  
437 # rename support
438 1 foreach my $cmd ( qw( rename mv ) ) {
439 0 ▶ event $cmd => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
440 0 my( $self, $from, $to ) = @_;
441  
442 # ignore commands if we are shutting down
443 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
444  
445 # are we already sending a command?
446 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
447 0 die "Unable to send '$cmd' because we are processing " . $self->state;
448 }
449  
450 # Start the rename!
451 $self->command_data( {
452 0 from => $from,
453 to => $to,
454 } );
455 0 $self->command( 'rename_start', 'RNFR', $from );
456  
457 0 return;
458 2 };
459 }
460  
461 0 ▶sub _prepare_listing {
Subroutine _prepare_listing: not called CC=3 SCAR=24.8
462 0 my $self = shift;
463  
464 # do we need to set the TYPE?
465 0 ▶ if ( ! $self->_has_data_type or $self->data_type eq 'I' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_data_type or $self->data_type eq 'I') { } 0 0 0
Condition0%0/3
not $self->_has_data_type or $self->data_type eq "I"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->_has_data_type or $self->data_type eq "I"
not $self->_has_data_type $self->data_type eq "I"
Truth table0%0/3
not(A) or B
A= not $self->_has_data_typeB= $self->data_type eq "I"
ABresult
1X1
011
000
466 0 $self->command_data->{'type'} = 'A';
467 0 $self->command( 'complex_type', 'TYPE', 'A' );
468 } else {
469 # Okay, proceed to start the data connection stuff
470 0 $self->_start_data_connection;
471 }
472 }
473  
474 0 ▶sub _prepare_transfer {
Subroutine _prepare_transfer: not called CC=3 SCAR=24.8
475 0 my $self = shift;
476  
477 # do we need to set the TYPE?
478 0 ▶ if ( ! $self->_has_data_type or $self->data_type eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_data_type or $self->data_type eq 'A') { } 0 0 0
Condition0%0/3
not $self->_has_data_type or $self->data_type eq "A"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->_has_data_type or $self->data_type eq "A"
not $self->_has_data_type $self->data_type eq "A"
Truth table0%0/3
not(A) or B
A= not $self->_has_data_typeB= $self->data_type eq "A"
ABresult
1X1
011
000
479 0 $self->command_data->{'type'} = 'I';
480 0 $self->command( 'complex_type', 'TYPE', 'I' );
481 } else {
482 # Okay, proceed to start the data connection stuff
483 0 $self->_start_data_connection;
484 }
485 }
486  
487 0 ▶sub _start_data_connection {
Subroutine _start_data_connection: not called CC=2 SCAR=17.9
488 0 my $self = shift;
489  
490 # okay, we go ahead with the PASV/PORT command
491 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'passive') { } 0 0 0
492 0 $self->command( 'complex_pasv', 'PASV' );
493 } else {
494 # Okay, create our listening socket
495 0 $self->_create_data_connection;
496 }
497 }
498  
499 # build our data complex command handlers
500 1 foreach my $cmd ( qw( put stor stou ) ) {
501 0 ▶ event "${cmd}_data" => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
502 0 my( $self, $input ) = @_;
503  
504 # don't print the input as it could be binary stuff
505 0 warn "received ${cmd}_data\n" if DEBUG;
506  
507 # ignore commands if we are shutting down
508 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
509  
510 # should only happen in complex state
511 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
512 # This should only happen for put commands
513 0 ▶ if ( $self->command_data->{'cmd'} eq $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} eq $cmd) { } 0 0 0
514 # send the data to our rw wheel
515 0 ▶ if ( defined $self->data_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->data_rw) { } 0 0 0
516 0 $self->data_rw->put( $input );
517 } else {
518 # TODO maybe we shouldn't die here? Just warn and let the user handle the error?
519 0 die "got ${cmd}_data when we are not connected!";
520 }
521 } else {
522 0 die "got ${cmd}_data when we are not doing a STOR";
523 }
524 } else {
525 0 die "got ${cmd}_data when we are in wrong state: " . $self->state;
526 }
527  
528 0 return;
529 3 };
530  
531 0 ▶ event "${cmd}_close" => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
532 0 my $self = shift;
533  
534 0 warn "received ${cmd}_close\n" if DEBUG;
535  
536 # ignore commands if we are shutting down
537 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
538  
539 # should only happen in complex state
540 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
541 # This should only happen for put commands
542 0 ▶ if ( $self->command_data->{'cmd'} eq $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} eq $cmd) { } 0 0 0
543 # kill the rw wheel, disconnecting from the server
544 0 ▶ if ( defined $self->data_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->data_rw) { } 0 0 0
545 0 $self->_process_complex_closed;
546 } else {
547 # maybe a timing issue, server killed the connection while this event was in the queue?
548 # then the data_rw_error event would have caught this and sent the appropriate error message
549 0 warn "unable to ${cmd}_close as wheel is gone\n" if DEBUG;
550 }
551 } else {
552 0 die "got ${cmd}_close when we are not doing a STOR";
553 }
554 } else {
555 0 die "got ${cmd}_close when we are in wrong state: " . $self->state;
556 }
557  
558 0 return;
559 3 };
560 }
561  
562 0 ▶sub BUILD {
Subroutine BUILD: not called CC=8 SCAR=42.8
Pod undocumented
563 0 my $self = shift;
564  
565 # Did we enable TLS mode?
566 0 ▶ if ( $self->tls_cmd or $self->tls_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_cmd or $self->tls_data) 0 0 0
Condition0%0/3
$self->tls_cmd or $self->tls_data
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->tls_cmd or $self->tls_data
$self->tls_cmd $self->tls_data
Truth table0%0/3
A or B
A= $self->tls_cmdB= $self->tls_data
ABresult
1X1
011
000
567 0 eval 'require POE::Component::SSLify';
568 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
569 0 warn "Unable to use SSLify: $@\n";
570 0 $self->_set_tls_cmd( 0 );
571 0 $self->_set_tls_data( 0 );
572 }
573 }
574  
575 # Make sure that the local_port and local_data_port is different!
576 0 ▶ if ( $self->local_port == $self->local_data_port and $self->local_port != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_port == $self->local_data_port and $self->local_port != 0) 0 0 0
Condition0%0/3
$self->local_port == $self->local_data_port and $self->local_port != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->local_port == $self->local_data_port and $self->local_port != 0
$self->local_port == $self->local_data_port $self->local_port != 0
Truth table0%0/3
A and B
A= $self->local_port == $self->local_data_portB= $self->local_port != 0
ABresult
0X0
100
111
577 0 die "Please specify different local_port and local_data_port settings!";
578 }
579  
580 # In order to use active mode connection, we MUST provide a local IP address to bind
581 # otherwise getsockname() on INADDR_ANY unsurprisingly returns 0.0.0.0 which is worthless for the PORT command!
582 0 ▶ if ( $self->connection_mode eq 'active' and $self->local_addr eq '0.0.0.0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0") 0 0 0
Condition0%0/3
$self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0"
$self->connection_mode eq "active" $self->local_addr eq "0.0.0.0"
Truth table0%0/3
A and B
A= $self->connection_mode eq "active"B= $self->local_addr eq "0.0.0.0"
ABresult
0X0
100
111
583 0 die "Please specify a local_addr address to bind to for active connections!";
584 }
585  
586 # Figure out who called us so we store it for events
587 0 $self->_master( $poe_kernel->get_active_session->ID );
588 }
589  
590 # Okay, go connect to the host!
591 0 ▶sub START {
Subroutine START: not called CC=1 SCAR=6.9
Pod undocumented
592 0 my $self = shift;
593  
594 0 warn "starting\n" if DEBUG;
595  
596 0 $poe_kernel->alias_set( $self->alias );
597  
598 # set a timeout before trying to connect
599 0 $poe_kernel->delay( 'timeout_event' => $self->timeout );
600 0 $self->cmd_sf( POE::Wheel::SocketFactory->new(
601 SocketDomain => AF_INET,
602 SocketType => SOCK_STREAM,
603 SocketProtocol => 'tcp',
604 RemoteAddress => $self->remote_addr,
605 RemotePort => $self->remote_port,
606 BindAddr => $self->local_addr,
607 BindPort => $self->local_port,
608 SuccessEvent => 'cmd_sf_connected',
609 FailureEvent => 'cmd_sf_error'
610 ) );
611  
612 0 return;
613 }
614  
615 # helper sub to simplify sending events to the master
616 0 ▶sub _send_master {
Subroutine _send_master: not called CC=1 SCAR=6.9
617 0 my( $self, $event, @args ) = @_;
618  
619 0 warn "telling master about event $event\n" if DEBUG;
620  
621 0 $poe_kernel->post( $self->_master, $event, @args );
622 }
623  
624 # shutdown the connection
625 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=1 SCAR=6.9
626 0 my $self = shift;
627  
628 0 warn "shutdown\n" if DEBUG;
629 0 $self->state( 'shutdown' );
630  
631 # destroy our wheels
632 0 $self->cmd_sf( undef );
633 0 $self->cmd_rw( undef );
634 0 $self->data_sf( undef );
635 0 $self->data_rw( undef );
636  
637 # remove the timeout if it exists
638 0 $poe_kernel->delay( 'timeout_event' );
639  
640 0 $poe_kernel->alias_remove( $self->alias );
641 }
642  
643 #pod =method yield
644 #pod
645 #pod This method provides an alternative object based means of posting events to the component.
646 #pod First argument is the event to post, following arguments are sent as arguments to the resultant post.
647 #pod
648 #pod my $ftp = POE::Component::Client::SimpleFTP->new( alias => "ftp", ... );
649 #pod $ftp->yield( 'cd', 'foobar' );
650 #pod
651 #pod # equivalent to:
652 #pod $poe_kernel->post( $ftp->alias, 'cd', 'foobar' );
653 #pod
654 #pod =cut
655  
656 0 ▶sub yield {
Subroutine yield: not called CC=1 SCAR=6.9
Pod documented
657 0 my( $self, @args ) = @_;
658 0 $poe_kernel->post( $self->get_session_id, @args );
659 }
660  
661 0 ▶event _child => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
662 0 return;
663 1 };
664  
665 0 ▶event timeout_event => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
666 0 my $self = shift;
667  
668 # Okay, we timed out doing something
669 0 ▶ if ( $self->state eq 'connect' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'connect') { } 0 0 0
elsif ($self->state eq 'complex_sf') { } 0 0 0
670 # failed to connect to the server
671 0 $self->_send_master( 'connect_error', 0, 'timedout' );
672  
673 # nothing else to do...
674 0 $self->_shutdown;
675 } elsif ( $self->state eq 'complex_sf' ) {
676 # timed out waiting for the data connection
677  
678 # since this is a pre-data-connection error, the complex command is done
679 0 $self->_process_complex_error( 0, 'timedout' );
680 0 $self->state( 'idle' );
681 } else {
682 0 die "unknown state in timeout_event: " . $self->state;
683 }
684  
685 0 return;
686 1 };
687  
688 0 ▶event cmd_sf_connected => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
689 0 my( $self, $fh, $host, $port, $wheel_id ) = @_;
690  
691 0 warn "cmd_sf_connected\n" if DEBUG;
692  
693 # remove the timeout
694 0 $poe_kernel->delay( 'timeout_event' );
695  
696 # convert it into a readwrite wheel
697 0 $self->cmd_rw( POE::Wheel::ReadWrite->new(
698 Handle => $fh,
699 Filter => POE::Filter::Line->new( Literal => EOL ),
700 Driver => POE::Driver::SysRW->new,
701 InputEvent => 'cmd_rw_input',
702 ErrorEvent => 'cmd_rw_error',
703 ) );
704  
705 0 return;
706 1 };
707  
708 0 ▶event cmd_sf_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
709 0 my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
710  
711 0 warn "cmd_sf_error $operation $errnum $errstr\n" if DEBUG;
712  
713 0 $self->_send_master( 'connect_error', 0, "$operation error $errnum: $errstr" );
714  
715 # nothing else to do...
716 0 $self->_shutdown;
717  
718 0 return;
719 1 };
720  
721 0 ▶event cmd_rw_input => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
722 0 my( $self, $input, $wheel_id ) = @_;
723  
724 0 warn "cmd_rw_input(" . $self->state . "): '$input'\n" if DEBUG;
725  
726 # some ftpds are zany!
727 #calling _ftpd_simple_command to process 221:Goodbye.
728 #shutdown
729 #switching from state(simple_command) to state(shutdown)
730 #cmd_rw_input(shutdown): '500 OOPS: priv_sock_get_cmd'
731 #calling _ftpd_shutdown to process 500:OOPS: priv_sock_get_cmd
732 #Can't locate object method "_ftpd_shutdown" via package "POE::Component::Client::SimpleFTP" at /usr/local/share/perl/5.18.2/POE/Component/Client/SimpleFTP.pm line 790.
733 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
734  
735 # parse the input according to RFC 959
736 # TODO put this code in POE::Filter::FTP or something?
737 0 my( $code, $line );
738 0 ▶ if ( $input =~ /^(\d\d\d)(\-?)(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^(\d\d\d)(\-?)(.*)$/) { } 0 0 0
739 0 $code = $1;
740 0 my( $minus, $string ) = ( $2, $3 );
741 0 $string =~ s/^\s+//;
742 0 $string =~ s/\s+$//;
743  
744 # handle multi-line replies
745 0 ▶ if ( length $minus ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $minus) { } 0 0 0
746 0 ▶ if ( defined $self->input_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->input_buffer) { } 0 0 0
747 0 ▶ die "ftpd sent different multi-line codes!" if $self->input_buffer_code ne $code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->input_buffer_code ne $code 0 0 0
748 0 $self->input_buffer( $self->input_buffer . "\n" . $string );
749 } else {
750 0 warn "begin of multi-line($code): '$string'\n" if DEBUG;
751 0 $self->input_buffer( $string );
752 }
753 0 $self->input_buffer_code( $code );
754 0 return;
755 } else {
756 # end of multi-line reply?
757 0 ▶ if ( defined $self->input_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->input_buffer) { } 0 0 0
758 # Make sure the code matches!
759 0 ▶ if ( $self->input_buffer_code != $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input_buffer_code != $code) { } 0 0 0
760 0 die "ftpd sent invalid reply: $input";
761 } else {
762 0 warn "end of multi-line: '$string'\n" if DEBUG;
763 0 $line = $self->input_buffer . "\n" . $string;
764 0 $self->input_buffer( undef );
765 }
766 } else {
767 0 $line = $string;
768 }
769 }
770 } else {
771 # If we are in a multi-line reply, just collect the input
772 0 ▶ if ( defined $self->input_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->input_buffer) { } 0 0 0
773 # per the RFC, the first character should be padded by a space if needed
774 0 $input =~ s/^\s//;
775 0 warn "got multi-line input: '$input'\n" if DEBUG;
776 0 $self->input_buffer( $self->input_buffer . "\n" . $input );
777 0 return;
778 } else {
779 0 die "ftpd sent invalid reply: $input";
780 }
781 }
782  
783 # process the input, depending on our state
784 0 my $subref = "_ftpd_" . $self->state;
785 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq "complex_data") 0 0 0
786 # okay, we got input from the ftpd before the complex data is closed, so we buffer it...
787 0 ▶ if ( exists $self->command_data->{'cmd_pending'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{'cmd_pending'}) { } 0 0 0
788 # ftpd sent data faster than we could get the command!
789 0 warn "executing pending command data\n" if DEBUG;
790 0 $subref = '_ftpd_complex_done';
791 } else {
792 0 warn "buffering command data\n" if DEBUG;
793 0 $self->command_data->{'cmd_buffer'} = [$code, $line];
794 0 return;
795 }
796 }
797  
798 0 warn "calling $subref to process $code:$line\n" if DEBUG;
799 0 $self->$subref( $code, $line );
800  
801 0 return;
802 1 };
803  
804 0 ▶event cmd_rw_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
805 0 my( $self, $operation, $errnum, $errstr, $wheel_id) = @_;
806  
807 0 warn "cmd_rw_error $operation $errnum $errstr\n" if DEBUG;
808  
809 0 $self->_send_master( 'connect_error', 0, "$operation error $errnum: $errstr" );
810  
811 # nothing else to do...
812 0 $self->_shutdown;
813  
814 0 return;
815 1 };
816  
817 # sets the state for a command and sends it over the control connection
818 0 ▶sub command {
Subroutine command: not called CC=3 SCAR=24.8
Pod undocumented
819 0 my( $self, $state, $cmd, @args ) = @_;
820  
821 # If we don't have a readwrite wheel, then we can't send anything!
822 0 ▶ if ( ! defined $self->cmd_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->cmd_rw) 0 0 0
823 0 die "Unable to send '$cmd' as we aren't connected!";
824 }
825  
826 # change to the specified state, then send the args!
827 0 $self->state( $state );
828 0 ▶ if ( $cmd eq 'QUOT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "QUOT") 0 0 0
829 # user-defined string, send it as-is!
830 0 $cmd = shift @args;
831 }
832  
833 0 $cmd = uc $cmd; # to make sure
834 0 my $cmdstr = join( ' ', $cmd, @args );
835 0 warn "sending command '$cmdstr'\n" if DEBUG;
836 0 $self->cmd_rw->put( $cmdstr );
837 }
838  
839 0 ▶sub _ftpd_idle {
Subroutine _ftpd_idle: not called CC=1 SCAR=6.9
840 0 my( $self, $code, $reply ) = @_;
841  
842 0 die "unexpected text while we are idle: $code $reply";
843 }
844  
845 # the first line of text we received from the ftpd ( the greeting )
846 0 ▶sub _ftpd_connect {
Subroutine _ftpd_connect: not called CC=3 SCAR=24.8
847 0 my( $self, $code, $reply ) = @_;
848  
849 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
850 0 $self->_send_master( 'connected', $reply );
851  
852 # do we want TLS?
853 0 ▶ if ( $self->tls_cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_cmd) { } 0 0 0
854 # begin TLS authentication procedure, as per RFC 2228 / 4217
855 0 $self->command( 'tls_cmd', 'AUTH', 'TLS' );
856 } else {
857 # send the username!
858 0 $self->command( 'user', 'USER', $self->username );
859 }
860 } else {
861 0 $self->_send_master( 'connect_error', $code, $reply );
862  
863 # nothing else to do...
864 0 $self->_shutdown;
865 }
866 }
867  
868 0 ▶sub _ftpd_tls_cmd {
Subroutine _ftpd_tls_cmd: not called CC=3 SCAR=24.8
869 0 my( $self, $code, $reply ) = @_;
870  
871 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
872 # Okay, time to SSLify the connection!
873 0 my $socket = $self->cmd_rw->get_input_handle;
874 0 $self->cmd_rw( undef );
875  
876 0 eval { $socket = POE::Component::SSLify::Client_SSLify( $socket, 'tlsv1' ) };
877 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
878 0 die "Unable to SSLify control connection: $@";
879 }
880  
881 # set up the rw wheel again
882 0 $self->cmd_rw( POE::Wheel::ReadWrite->new(
883 Handle => $socket,
884 Filter => POE::Filter::Line->new( Literal => EOL ),
885 Driver => POE::Driver::SysRW->new,
886 InputEvent => 'rw_input',
887 ErrorEvent => 'rw_error',
888 ) );
889  
890 # Now, send the username!
891 0 $self->command( 'user', 'USER', $self->username );
892 } else {
893 # server probably doesn't support AUTH TLS
894 0 $self->_send_master( 'login_error', $code, $reply );
895 0 $self->state( 'idle' );
896 }
897 }
898  
899 0 ▶sub _ftpd_pbsz {
Subroutine _ftpd_pbsz: not called CC=2 SCAR=17.9
900 0 my( $self, $code, $reply ) = @_;
901  
902 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
903 0 $self->command( 'prot', 'PROT', 'P' );
904 } else {
905 0 $self->_send_master( 'login_error', $code, $reply );
906 0 $self->state( 'idle' );
907 }
908 }
909  
910 0 ▶sub _ftpd_prot {
Subroutine _ftpd_prot: not called CC=3 SCAR=24.8
911 0 my( $self, $code, $reply ) = @_;
912  
913 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
914 0 my $banner = $reply;
915 0 ▶ if ( exists $self->command_data->{'orig_banner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{"orig_banner"}) 0 0 0
916 0 $banner = (delete $self->command_data->{'orig_banner'}) . "\n" . $reply;
917 }
918 0 $self->_send_master( 'authenticated', $banner );
919 } else {
920 0 $self->_send_master( 'login_error', $code, $reply );
921 }
922  
923 0 $self->state( 'idle' );
924 }
925  
926 0 ▶sub _ftpd_user {
Subroutine _ftpd_user: not called CC=3 SCAR=24.8
927 0 my( $self, $code, $reply ) = @_;
928  
929 0 ▶ if ( code_success( $code ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
elsif (code_intermediate($code)) { } 0 0 0
930 # no need for password ( probably anonymous account )
931 0 $self->_prepare_tls_stuff( $reply );
932 } elsif ( code_intermediate( $code ) ) {
933 # send the password!
934 0 $self->command( 'password', 'PASS', $self->password );
935 } else {
936 0 $self->_send_master( 'login_error', $code, $reply );
937 0 $self->state( 'idle' );
938 }
939 }
940  
941 0 ▶sub _prepare_tls_stuff {
Subroutine _prepare_tls_stuff: not called CC=2 SCAR=17.9
942 0 my( $self, $reply ) = @_;
943  
944 # do we need to setup the data channel TLS stuff?
945 0 ▶ if ( $self->tls_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_data) { } 0 0 0
946 # cache the original reply
947 0 $self->command_data->{'orig_banner'} = $reply;
948  
949 # TODO is 0 a good default?
950 0 $self->command( 'pbsz', 'PBSZ', 0 );
951 } else {
952 0 $self->_send_master( 'authenticated', $reply );
953 0 $self->state( 'idle' );
954 }
955 }
956  
957 0 ▶sub _ftpd_password {
Subroutine _ftpd_password: not called CC=2 SCAR=17.9
958 0 my( $self, $code, $reply ) = @_;
959  
960 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
961 0 $self->_prepare_tls_stuff( $reply );
962 } else {
963 0 $self->_send_master( 'login_error', $code, $reply );
964 0 $self->state( 'idle' );
965 }
966 }
967  
968 0 ▶sub _ftpd_simple_command {
Subroutine _ftpd_simple_command: not called CC=3 SCAR=24.8
969 0 my( $self, $code, $reply ) = @_;
970  
971 # special-case for quit
972 0 ▶ if ( $self->simple_command =~ /^(?:quit|disconnect)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simple_command =~ /^(?:quit|disconnect)$/) 0 0 0
973 0 $self->_shutdown;
974 0 return;
975 }
976  
977 0 my $event = $self->simple_command;
978 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
979 0 $event .= '_error';
980 }
981 0 $self->_send_master( $event, $code, $reply, @{ $self->command_data } );
982  
983 0 $self->command_data( undef );
984 0 $self->state( 'idle' );
985 }
986  
987 0 ▶sub _ftpd_complex_type {
Subroutine _ftpd_complex_type: not called CC=2 SCAR=17.9
988 0 my( $self, $code, $reply ) = @_;
989  
990 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
991 0 $self->data_type( delete $self->command_data->{'type'} );
992 0 $self->_start_data_connection;
993 } else {
994 # since this is a pre-data-connection error, the complex command is done
995 0 $self->_process_complex_error( $code, $reply );
996 0 $self->state( 'idle' );
997 }
998 }
999  
1000 0 ▶sub _ftpd_complex_pasv {
Subroutine _ftpd_complex_pasv: not called CC=2 SCAR=17.9
1001 0 my( $self, $code, $reply ) = @_;
1002  
1003 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
1004 # Got the server's data!
1005 # TODO the RFC is fuzzy about it, but can the port digit be negative?!?!
1006 # http://cr.yp.to/ftp/retr.html
1007 0 my @data = $reply =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/;
1008 0 $self->command_data->{'ip'} = join '.', @data[0 .. 3];
1009 0 $self->command_data->{'port'} = $data[4]*256 + $data[5];
1010  
1011 # Okay, create our listening socket
1012 0 $self->_create_data_connection;
1013 } else {
1014 # since this is a pre-data-connection error, the complex command is done
1015 0 $self->_process_complex_error( $code, $reply );
1016 0 $self->state( 'idle' );
1017 }
1018 }
1019  
1020 0 ▶sub _ftpd_complex_port {
Subroutine _ftpd_complex_port: not called CC=2 SCAR=17.9
1021 0 my( $self, $code, $reply ) = @_;
1022  
1023 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
1024 # now, send the actual complex command :)
1025 0 $self->_process_complex_command;
1026 } else {
1027 # since this is a pre-data-connection error, the complex command is done
1028 0 $self->_process_complex_error( $code, $reply );
1029 0 $self->state( 'idle' );
1030 }
1031 }
1032  
1033 0 ▶sub _create_data_connection {
Subroutine _create_data_connection: not called CC=3 SCAR=24.8
1034 0 my $self = shift;
1035  
1036 # we now transition to the "complex" state
1037 # the "real" state is kept in $self->command_data->{cmd}
1038  
1039 # the arguments to socketfactory depend on whether we are in active or passive mode
1040 0 my %sf_args = (
1041 SocketDomain => AF_INET,
1042 SocketType => SOCK_STREAM,
1043 SocketProtocol => 'tcp',
1044 SuccessEvent => 'data_sf_connected',
1045 FailureEvent => 'data_sf_error',
1046  
1047 BindAddr => $self->local_addr,
1048 BindPort => $self->local_data_port,
1049 );
1050  
1051 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'passive') { } 0 0 0
1052 # use the ip/port we already received
1053 0 $sf_args{ RemoteAddress } = $self->command_data->{'ip'};
1054 0 $sf_args{ RemotePort } = $self->command_data->{'port'};
1055 } else {
1056 # enable the Reuse param so we can sanely use the same local port
1057 0 $sf_args{ Reuse } = 1;
1058 }
1059  
1060 # create the socketfactory!
1061 0 $poe_kernel->delay( 'timeout_event' => $self->timeout );
1062 0 $self->data_sf( POE::Wheel::SocketFactory->new( %sf_args ) );
1063  
1064 # Now that we've created the SF, do we need to send the PORT data?
1065 0 ▶ if ( $self->connection_mode eq 'active' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'active') { } 0 0 0
1066 # TODO what if SF had an error binding to the socket?
1067 0 my $socket = $self->data_sf->getsockname;
1068 0 my( $port, $addr ) = unpack_sockaddr_in( $socket );
1069  
1070 # TODO why won't getsockname give me the right ip????
1071 # warn "GOT " . inet_ntoa( $addr ) . ":$port" if DEBUG;
1072 # $addr = inet_ntoa( $addr ); ( always returns 0.0.0.0, but netstat shows it binding on the right ip! )
1073 0 $addr = $self->local_addr;
1074  
1075 0 my @addr = split( /\./, $addr );
1076 0 my @port = ( int( $port / 256 ), $port % 256 );
1077 0 $self->command( 'complex_port', 'PORT', join( ',', @addr, @port ) );
1078 } else {
1079 # wait for the connection to server
1080 0 $self->state( 'complex_sf' );
1081 }
1082 }
1083  
1084 0 ▶event data_sf_connected => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1085 0 my( $self, $fh, $host, $port, $wheel_id ) = @_;
1086  
1087 # TODO what if we get multiple connections?
1088 # probably can happen if an attacker attacks us while we use active PORT connection
1089  
1090 0 warn "data_sf_connected\n" if DEBUG;
1091  
1092 # all done with the SF wheel
1093 0 $self->data_sf( undef );
1094  
1095 # kill the timeout timer
1096 0 $poe_kernel->delay( 'timeout_event' );
1097  
1098 # args for the RW wheel
1099 0 my %rw_args = (
1100 Handle => $fh,
1101 Driver => POE::Driver::SysRW->new,
1102 InputEvent => 'data_rw_input',
1103 ErrorEvent => 'data_rw_error',
1104 FlushedEvent => 'data_rw_flushed',
1105 );
1106 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:ls|dir|list|nlst)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:ls|dir|list|nlst)$/) { } 0 0 0
1107 0 $rw_args{'Filter'} = POE::Filter::Line->new( InputLiteral => EOL );
1108 } else {
1109 0 $rw_args{'Filter'} = POE::Filter::Stream->new;
1110 }
1111  
1112 # convert it into a readwrite wheel
1113 0 $self->data_rw( POE::Wheel::ReadWrite->new( %rw_args ) );
1114  
1115 # do we need to send the actual command?
1116 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq "passive") 0 0 0
1117 0 $self->_process_complex_command;
1118 }
1119  
1120 0 return;
1121 1 };
1122  
1123 0 ▶sub _process_complex_command {
Subroutine _process_complex_command: not called CC=3 SCAR=24.8
1124 0 my $self = shift;
1125  
1126 0 my $cmd = $self->command_data->{'cmd'};
1127  
1128 # do we need to translate the command?
1129 0 ▶ if ( exists $command_map{ $cmd } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_map{$cmd}) { } 0 0 0
1130 0 $cmd = $command_map{ $cmd };
1131 } else {
1132 0 $cmd = uc( $cmd );
1133 }
1134  
1135 # since the code in sub command doesn't like sending undef's we have to check it here
1136 0 ▶ if ( defined $self->command_data->{'data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->command_data->{'data'}) { } 0 0 0
1137 0 $self->command( 'complex_start', $cmd, @{ $self->command_data->{'data'} } );
1138 } else {
1139 0 $self->command( 'complex_start', $cmd );
1140 }
1141 }
1142  
1143 0 ▶event data_sf_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1144 0 my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
1145  
1146 0 warn "data_sf_error: $operation $errnum $errstr\n" if DEBUG;
1147  
1148 # some sort of error?
1149 0 ▶ if ( $self->state eq 'complex_sf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_sf') { } 0 0 0
1150 0 $self->_process_complex_error( undef, "$operation error $errnum: $errstr" );
1151 } else {
1152 0 die "unexpected data_sf_error in wrong state: " . $self->state;
1153 }
1154 1 };
1155  
1156 0 ▶sub _ftpd_complex_start {
Subroutine _ftpd_complex_start: not called CC=6 SCAR=37.4
1157 0 my( $self, $code, $reply ) = @_;
1158  
1159 # actually process the "start" of the command
1160 0 ▶ if ( code_preliminary( $code ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (code_preliminary($code)) { } 0 0 0
elsif (code_success($code)) { } 0 0 0
1161 # let the master know it's ready to send/receive stuff!
1162 0 $self->_send_master( $self->command_data->{'cmd'} . '_connected', @{ $self->command_data->{'data'} } );
1163 0 $self->state( 'complex_data' );
1164  
1165 # do we have any buffered data?
1166 0 ▶ if ( exists $self->command_data->{'buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{"buffer"}) 0 0 0
1167 0 warn "sending buffered chunks\n" if DEBUG;
1168 0 foreach my $chunk ( @{ $self->command_data->{'buffer'} } ) {
1169 0 $self->_send_master( $self->command_data->{'cmd'} . '_data', $chunk, @{ $self->command_data->{'data'} } );
1170 }
1171 0 delete $self->command_data->{'buffer'};
1172  
1173 # check for server sending faster than we can process
1174 0 ▶ if ( exists $self->command_data->{'cmd_pending'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{"cmd_pending"}) 0 0 0
1175 0 warn "fixing pending command data\n" if DEBUG;
1176 0 $self->state( 'complex_done' );
1177 }
1178 }
1179 } elsif ( code_success( $code ) ) {
1180 0 die "unexpected success for start of complex command: $code $reply";
1181 } else {
1182 0 $self->_process_complex_error( $code, $reply );
1183 }
1184 }
1185  
1186 0 ▶sub _ftpd_complex_error {
Subroutine _ftpd_complex_error: not called CC=2 SCAR=17.9
1187 0 my( $self, $code, $reply ) = @_;
1188  
1189 # we are supposed to get some kind of error from the ftpd
1190 # because something screwed up while doing the data connection
1191 0 ▶ if ( code_failure( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_failure($code)) { } 0 0 0
1192 # okay, all done!
1193 0 $self->state( 'idle' );
1194 0 $self->command_data( undef );
1195 } else {
1196 0 die "unexpected input while in complex_error state: $code $reply";
1197 }
1198 }
1199  
1200 0 ▶sub _ftpd_complex_done {
Subroutine _ftpd_complex_done: not called CC=2 SCAR=17.9
1201 0 my( $self, $code, $reply ) = @_;
1202  
1203 # got the final result of the complex command!
1204 0 my $event = $self->command_data->{'cmd'};
1205 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
1206 0 $event .= '_error';
1207 }
1208 0 $self->_send_master( $event, $code, $reply, @{ $self->command_data->{'data'} } );
1209  
1210 # clear all data for this complex command
1211 0 $self->state( 'idle' );
1212 0 $self->command_data( undef );
1213 }
1214  
1215 0 ▶sub _process_complex_error {
Subroutine _process_complex_error: not called CC=1 SCAR=6.9
1216 0 my( $self, $code, $reply ) = @_;
1217  
1218 # go to the error state, so we can receive whatever the ftpd wants to send to us
1219 0 $self->state( 'complex_error' );
1220  
1221 0 $self->data_sf( undef );
1222 0 $self->data_rw( undef );
1223  
1224 0 $self->_send_master( $self->command_data->{'cmd'} . '_error', $code, $reply, @{ $self->command_data->{'data'} } );
1225  
1226 # all done processing this complex command
1227 0 $self->command_data( undef );
1228 }
1229  
1230 0 ▶sub _process_complex_closed {
Subroutine _process_complex_closed: not called CC=2 SCAR=17.9
1231 0 my $self = shift;
1232  
1233 # Okay, we are done with this command!
1234 0 $self->state( 'complex_done' );
1235 0 $self->data_rw( undef );
1236  
1237 # Did we buffer any commands?
1238 0 ▶ if ( exists $self->command_data->{'cmd_buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{"cmd_buffer"}) 0 0 0
1239 0 warn "executing buffered command reply\n" if DEBUG;
1240 0 $self->_ftpd_complex_done( @{ delete $self->command_data->{'cmd_buffer'} } );
1241 }
1242  
1243 # TODO should we send an event_closed command? I think it's superfluous...
1244 }
1245  
1246 0 ▶event data_rw_input => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1247 0 my( $self, $input, $wheel_id ) = @_;
1248  
1249 0 warn "data_rw_input: '$input'\n" if DEBUG;
1250  
1251 # should only happen in complex state
1252 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
elsif ($self->state eq 'complex_start') { } 0 0 0
1253 # send it back to the master
1254 0 $self->_send_master( $self->command_data->{'cmd'} . '_data', $input, @{ $self->command_data->{'data'} } );
1255 } elsif ( $self->state eq 'complex_start' ) {
1256 # oh boy, the server immediately sent us some data while we were processing the start
1257 # that means we have to buffer it so we correctly send it *after* we send the connected event
1258 0 warn "storing input for buffer\n" if DEBUG;
1259 0 ▶ if ( ! exists $self->command_data->{'buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->command_data->{'buffer'}) { } 0 0 0
1260 0 $self->command_data->{'buffer'} = [ $input ];
1261 } else {
1262 0 push( @{ $self->command_data->{'buffer'} }, $input );
1263 }
1264 } else {
1265 0 die "unexpected data_rw_input in wrong state: " . $self->state;
1266 }
1267  
1268 0 return;
1269 1 };
1270  
1271 0 ▶event data_rw_error => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
1272 0 my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
1273  
1274 0 warn "data_rw_error: $operation $errnum $errstr\n" if DEBUG;
1275  
1276 # should only happen in complex state
1277 0 ▶ if ( $self->state eq 'complex_start' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'complex_start') { } 0 0 0
elsif ($self->state eq 'complex_data') { } 0 0 0
1278 # woah, sent the data faster than we started!
1279 0 $self->command_data->{'cmd_pending'} = 1;
1280 } elsif ( $self->state eq 'complex_data' ) {
1281 # Is it a normal EOF or an error?
1282 0 ▶ if ( $operation eq "read" and $errnum == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'read' and $errnum == 0) { } 0 0 0
Condition0%0/3
$operation eq "read" and $errnum == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$operation eq "read" and $errnum == 0
$operation eq "read" $errnum == 0
Truth table0%0/3
A and B
A= $operation eq "read"B= $errnum == 0
ABresult
0X0
100
111
1283 # only in the put state is this a real error
1284 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/) { } 0 0 0
1285 0 $self->_process_complex_error( undef, "$operation error $errnum: $errstr" );
1286 } else {
1287 # otherwise it was a listing/get which means the data stream is done
1288 0 $self->_process_complex_closed;
1289 }
1290 } else {
1291 0 $self->_process_complex_error( undef, "$operation error $errnum: $errstr" );
1292 }
1293 } else {
1294 0 die "unexpected data_rw_error in wrong state: " . $self->state;
1295 }
1296  
1297 0 return;
1298 1 };
1299  
1300 0 ▶event data_rw_flushed => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1301 0 my( $self, $wheel_id ) = @_;
1302  
1303 0 warn "data_rw_flushed\n" if DEBUG;
1304  
1305 # should only happen in complex state
1306 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
1307 # This should only happen for put commands
1308 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/) { } 0 0 0
1309 0 $self->_send_master( $self->command_data->{'cmd'} . '_flushed', @{ $self->command_data->{'data'} } );
1310 } else {
1311 0 die "unexpected data_rw_flushed for complex command:" . $self->command_data->{'cmd'};
1312 }
1313 } else {
1314 0 die "unexpected data_rw_flushed in wrong state: " . $self->state;
1315 }
1316  
1317 0 return;
1318 1 };
1319  
1320 0 ▶sub _ftpd_rename_start {
Subroutine _ftpd_rename_start: not called CC=2 SCAR=17.9
1321 0 my( $self, $code, $reply ) = @_;
1322  
1323 0 ▶ if ( code_intermediate( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_intermediate($code)) { } 0 0 0
1324 # TODO should we send a rename_partial event? I think it's superfluous...
1325 0 $self->command( 'rename_done', 'RNTO', $self->command_data->{'to'} );
1326 } else {
1327 0 $self->_send_master( 'rename_error', $code, $reply, $self->command_data->{'from'}, $self->command_data->{'to'} );
1328 0 $self->command_data( undef );
1329 0 $self->state( 'idle' );
1330 }
1331 }
1332  
1333 0 ▶sub _ftpd_rename_done {
Subroutine _ftpd_rename_done: not called CC=2 SCAR=17.9
1334 0 my( $self, $code, $reply ) = @_;
1335  
1336 0 my $event = 'rename';
1337 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
1338 0 $event .= '_error';
1339 }
1340 0 $self->_send_master( $event, $code, $reply, $self->command_data->{'from'}, $self->command_data->{'to'} );
1341  
1342 0 $self->command_data( undef );
1343 0 $self->state( 'idle' );
1344 }
1345  
1346 1 ▶no MooseX::POE::SweetArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
1347 1 __PACKAGE__->meta->make_immutable;
1348 1 1;
1349  
1350 __END__
↑ Summary SimpleFTP/Utils.pm »