Recent/FTP.pm

filter: Statementstmt 86.2% 119 / 138 Branchbran 64.2% 36 / 56 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 77.5% 180 / 232 CC 35 SCAR 18.9
CC
35
Cov
78%
_response
12.4
_cmdc_input
11.5
spawn
9.5
CRAP 6.6
« Recent/HTTP.pm ↑ Summary Recent.pm »
1 package POE::Component::SmokeBox::Recent::FTP;
2 10 $POE::Component::SmokeBox::Recent::FTP::VERSION = '1.52';
3 #ABSTRACT: an extremely minimal FTP client
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use POE qw(Filter::Line Component::Client::DNS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Net::IP::Minimal qw(ip_get_version);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Test::POE::Client::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub spawn {
13 4 ▶ my $package = shift;
Subroutine spawn: called CC=9 SCAR=22.5
Pod documented
14 4 my %opts = @_;
15 4 $opts{lc $_} = delete $opts{$_} for keys %opts;
16 4 ▶ croak( "You must provide the 'address' parameter\n" ) unless $opts{address};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"address"} 0 4 4
17 4 ▶ croak( "You must provide the 'path' parameter\n" ) unless $opts{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"path"} 0 4 4
18 4 my $options = delete $opts{options};
19 4 ▶ $opts{prefix} = 'ftp_' unless $opts{prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"prefix"} 4 0 4
20 4 ▶ $opts{prefix} .= '_' unless $opts{prefix} =~ /\_$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"prefix"} =~ /\_$/ 0 4 4
21 4 ▶ $opts{username} = 'anonymous' unless $opts{username};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"username"} 2 2 4
22 4 ▶ $opts{password} = 'anon@anon.org' unless $opts{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"password"} 2 2 4
23 4 my $self = bless \%opts, $package;
24 $self->{session_id} = POE::Session->create(
25 object_states => [
26 4 ▶ $self => { map { ($_,"_$_" ) } qw(cmdc_socket_failed cmdc_input cmdc_disconnected datac_connected datac_disconnected datac_input) },
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 0 4 4
27 $self => [qw(
28 _start
29 _retr_done
30 _resolve
31 _response
32 _connect
33 )],
34 ],
35 heap => $self,
36 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
37 )->ID();
38 4 return $self;
39 }
40  
41 sub _start {
42 4 ▶ my ($kernel,$sender,$self) = @_[KERNEL,SENDER,OBJECT];
Subroutine _start: called CC=5 SCAR=18.9
43 4 $self->{session_id} = $_[SESSION]->ID();
44 $self->{cmds} = [
45 [ '220', 'USER ' . $self->{username} ],
46 4 [ '331', 'PASS ' . $self->{password} ],
47 # [ '230', 'SIZE ' . $self->{path} ],
48 # [ '213', 'PASV' ],
49 [ '230', 'PASV' ],
50 ];
51 4 ▶ if ( $kernel == $sender and !$self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel == $sender and not $self->{"session"}) 0 4 4
Condition33%1/3
$kernel == $sender and not $self->{"session"}
!ll&&!rl&&r
400
MC/DC0%0/2
$kernel == $sender and not $self->{"session"}
$kernel == $sender not $self->{"session"}
Truth table33%1/3
A and not(B)
A= $kernel == $senderB= not $self->{"session"}
ABresult
0X0
100
111
52 0 croak "Not called from another POE session and 'session' wasn't set\n";
53 }
54 4 my $sender_id;
55 4 ▶ if ( $self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'session'}) { } 0 4 4
56 0 ▶ if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($self->{'session'})) { } 0 0 0
57 0 $sender_id = $ref->ID();
58 }
59 else {
60 0 croak "Could not resolve 'session' to a valid POE session\n";
61 }
62 }
63 else {
64 4 $sender_id = $sender->ID();
65 }
66 4 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
67 4 $self->{sender_id} = $sender_id;
68  
69 $self->{_resolver} = POE::Component::Client::DNS->spawn(
70 Alias => 'Resolver-' . $self->{session_id},
71 4 );
72  
73 4 $kernel->yield( '_resolve' );
74 4 return;
75 }
76  
77 sub _resolve {
78 4 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _resolve: called CC=3 SCAR=11.0
79 4 ▶ if ( ip_get_version( $self->{address} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ip_get_version($self->{"address"})) 3 1 4
80 # It is an address already
81 3 $kernel->yield( '_connect', $self->{address} );
82 3 return;
83 }
84 my $resp = $self->{_resolver}->resolve(
85 host => $self->{address},
86 1 context => { },
87 event => '_response',
88 );
89 1 ▶ $kernel->yield( '_response', $resp ) if $resp;
Branch: 1 branch
BranchTrueFalseTotal
if $resp 0 1 1
90 1 return;
91 }
92  
93 sub _response {
94 1 ▶ my ($kernel,$self,$resp) = @_[KERNEL,OBJECT,ARG0];
Subroutine _response: called CC=5 SCAR=25.2
95 1 ▶ if ( $resp->{error} and $resp->{error} ne 'NOERROR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"error"} and $resp->{"error"} ne "NOERROR") 1 0 1
Condition33%1/3
$resp->{"error"} and $resp->{"error"} ne "NOERROR"
!ll&&!rl&&r
001
MC/DC0%0/2
$resp->{"error"} and $resp->{"error"} ne "NOERROR"
$resp->{"error"} $resp->{"error"} ne "NOERROR"
Truth table33%1/3
A and B
A= $resp->{"error"}B= $resp->{"error"} ne "NOERROR"
ABresult
0X0
100
111
96 1 $kernel->yield( 'cmdc_socket_failed', $resp->{error} );
97 1 return;
98 }
99 0 my @answers = $resp->{response}->answer;
100 0 foreach my $answer ( $resp->{response}->answer() ) {
101 0 ▶ next if $answer->type !~ /^A/;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer->type =~ /^A/ 0 0 0
102 0 $kernel->yield( '_connect', $answer->rdatastr );
103 0 return;
104 }
105 0 $kernel->yield( 'cmdc_socket_failed', 'Could not resolve address' );
106 0 return;
107 }
108  
109 sub _connect {
110 3 ▶ my ($self,$address) = @_[OBJECT,ARG0];
Subroutine _connect: called CC=2 SCAR=7.1
111 $self->{cmdc} = Test::POE::Client::TCP->spawn(
112 address => $address,
113 3 ▶ port => $self->{port} || 21,
Condition50%1/2
$self->{'port'} || 21
l!l
30
MC/DC0%0/1
$self->{'port'} || 21
$self->{'port'}
Truth table50%1/2
A
A= $self->{'port'}
Aresult
11
00
114 prefix => 'cmdc',
115 autoconnect => 1,
116 filter => POE::Filter::Line->new( Literal => "\x0D\x0A" ),
117 );
118 3 return;
119 }
120  
121 sub _cmdc_socket_failed {
122 1 ▶ my ($kernel,$self,@errors) = @_[KERNEL,OBJECT,ARG0..$#_];
Subroutine _cmdc_socket_failed: called CC=2 SCAR=7.0
123 1 $self->_send_event( $self->{prefix} . 'sockerr', @errors );
124 1 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
125 1 ▶ $self->{cmdc}->shutdown() if $self->{cmdc};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cmdc"} 0 1 1
126 1 $self->{_resolver}->shutdown();
127 1 delete $self->{cmdc};
128 1 delete $self->{_resolver};
129 1 return;
130 }
131  
132 sub _cmdc_input {
133 33 ▶ my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
Subroutine _cmdc_input: called CC=11 SCAR=24.4
134 33 ▶ warn $input, "\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 33 33
135 33 my ($numeric) = $input =~ /^(\d+)\s+/;
136 33 ▶ return unless $numeric;
Branch: 1 branch
BranchTrueFalseTotal
unless $numeric 12 21 33
137 21 my $cmd = shift @{ $self->{cmds} };
138 21 ▶ if ( $cmd and $numeric eq $cmd->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd and $numeric eq $cmd->[0]) 9 12 21
Condition66%2/3
$cmd and $numeric eq $cmd->[0]
!ll&&!rl&&r
1209
MC/DC50%1/2
$cmd and $numeric eq $cmd->[0]
$cmd $numeric eq $cmd->[0]
Truth table66%2/3
A and B
A= $cmdB= $numeric eq $cmd->[0]
ABresult
0X0
100
111
139 9 ▶ warn ">>>>$cmd->[1]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 9 9
140 9 $self->{cmdc}->send_to_server( $cmd->[1] );
141 9 return;
142 }
143 12 ▶ if ( $numeric eq '227' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric eq 227) 3 9 12
144 3 my (@ip, @port);
145 3 (@ip[0..3], @port[0..1]) = $input =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/;
146 3 my $ip = join '.', @ip;
147 3 my $port = $port[0]*256 + $port[1];
148 3 $self->{datac} = Test::POE::Client::TCP->spawn(
149 address => $ip,
150 port => $port,
151 autoconnect => 1,
152 prefix => 'datac',
153 );
154 3 return;
155 }
156 9 ▶ if ( $numeric =~ /^5/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric =~ /^5/) 0 9 9
157 # Something went wrong
158 0 $self->{cmdc}->send_to_server( 'QUIT' );
159 0 $self->_send_event( $self->{prefix} . 'error', $input );
160 0 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
161 0 return;
162 }
163 9 ▶ if ( $numeric eq '150' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric eq 150) 3 6 9
164 # Transfer in progress
165 3 $self->{transfer} = 2;
166 }
167 9 ▶ if ( $numeric eq '226' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric eq 226) 3 6 9
168 3 $kernel->yield( '_retr_done' );
169 }
170 9 ▶ if ( $numeric eq '221' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric eq 221) 3 6 9
171 3 $self->{cmdc}->shutdown();
172 3 delete $self->{cmdc};
173 }
174 9 return;
175 }
176  
177 sub _cmdc_disconnected {
178 0 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _cmdc_disconnected: not called CC=1 SCAR=6.9
179 0 $self->{cmdc}->shutdown();
180 0 delete $self->{cmdc};
181 0 return;
182 }
183  
184 sub _datac_connected {
185 3 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _datac_connected: called CC=1 SCAR=0.0
186 3 $self->{cmdc}->send_to_server( 'RETR ' . $self->{path} );
187 3 return;
188 }
189  
190 sub _datac_disconnected {
191 3 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _datac_disconnected: called CC=2 SCAR=7.0
192 3 ▶ if ( $self->{transfer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transfer"}) 3 0 3
193 3 $kernel->yield( '_retr_done' );
194 }
195 3 $self->{datac}->shutdown();
196 3 delete $self->{datac};
197 3 return;
198 }
199  
200 sub _datac_input {
201 229 ▶ my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
Subroutine _datac_input: called CC=2 SCAR=7.0
202 229 ▶ warn $input, "\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 229 229
203 229 $self->_send_event( $self->{prefix} . 'data', $input );
204 229 return;
205 }
206  
207 sub _retr_done {
208 6 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _retr_done: called CC=3 SCAR=11.0
209 6 $self->{transfer}--;
210 6 ▶ unless ( $self->{transfer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"transfer"}) 3 3 6
211 3 $self->_send_event( $self->{prefix} . 'done' );
212 3 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
213 3 ▶ warn "Transfer complete\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
214 3 $self->{cmdc}->send_to_server( 'QUIT' );
215 3 return;
216 }
217 3 return;
218 }
219  
220 sub _send_event {
221 233 ▶ my $self = shift;
Subroutine _send_event: called CC=1 SCAR=0.0
222 233 $poe_kernel->post( $self->{sender_id}, @_ );
223 233 return;
224 }
225  
226 10 'Get me that file, sucker'
227  
228 __END__
« Recent/HTTP.pm ↑ Summary Recent.pm »