| 1 | package AnyEvent::FTP::Server::Role::TransferPrep; | ||||||||||
| 2 | |||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 23 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 23 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 23 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 23 | ▶ | use AnyEvent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 23 | ▶ | use AnyEvent::Socket qw( tcp_server tcp_connect ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 23 | ▶ | use AnyEvent::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: Interface for PASV, PORT and REST commands | ||||||||||
| 12 | 23 | our $VERSION = '0.19'; # VERSION | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 23 | has data => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | 23 | has restart_offset => ( | |||||||||
| 21 | is => 'rw', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | sub clear_data | ||||||||||
| 26 | { | ||||||||||
| 27 | 70 | ▶ | my($self) = @_; | ||||||||
|
Subroutine
clear_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | 70 | $self->data(undef); | |||||||||
| 29 | 70 | $self->restart_offset(undef); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 6 | ▶ | sub help_pasv { 'PASV (returns address/port)' } | ||||||||
|
Subroutine
help_pasv: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | |||||||||||
| 35 | sub cmd_pasv | ||||||||||
| 36 | { | ||||||||||
| 37 | 43 | ▶ | my($self, $con, $req) = @_; | ||||||||
|
Subroutine
cmd_pasv: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 43 | my $count = 0; | |||||||||
| 40 | |||||||||||
| 41 | tcp_server undef, undef, sub { | ||||||||||
| 42 | 41 | ▶ | my($fh, $host, $port) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.3
| |||||||||||
| 43 | 41 | ▶ | return close $fh if ++$count > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 41 | my $handle; | |||||||||
| 46 | $handle = AnyEvent::Handle->new( | ||||||||||
| 47 | fh => $fh, | ||||||||||
| 48 | on_error => sub { | ||||||||||
| 49 | 0 | ▶ | $_[0]->destroy; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 0 | undef $handle; | |||||||||
| 51 | }, | ||||||||||
| 52 | on_eof => sub { | ||||||||||
| 53 | 0 | ▶ | $handle->destroy; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 0 | undef $handle; | |||||||||
| 55 | }, | ||||||||||
| 56 | 41 | autocork => 1, | |||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | 41 | $self->data($handle); | |||||||||
| 60 | # TODO this should be with the 227 message below. | ||||||||||
| 61 | # demoting this to a TODO (was a F-I-X-M-E) | ||||||||||
| 62 | # since I can't remember why I thought it needed | ||||||||||
| 63 | # doing. plicease 12-05-2014 | ||||||||||
| 64 | 41 | $self->done; | |||||||||
| 65 | |||||||||||
| 66 | }, sub { | ||||||||||
| 67 | 43 | ▶ | my($fh, $host, $port) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 43 | my $ip_and_port = join(',', split(/\./, $con->ip), $port >> 8, $port & 0xff); | |||||||||
| 69 | |||||||||||
| 70 | 43 | my $w; | |||||||||
| 71 | $w = AnyEvent->timer(after => 0, cb => sub { | ||||||||||
| 72 | 43 | ▶ | $con->send_response(227 => "Entering Passive Mode ($ip_and_port)"); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 43 | undef $w; | |||||||||
| 74 | 43 | }); | |||||||||
| 75 | |||||||||||
| 76 | 43 | }; | |||||||||
| 77 | |||||||||||
| 78 | 43 | return; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | 6 | ▶ | sub help_port { 'PORT <sp> h1,h2,h3,h4,p1,p2' } | ||||||||
|
Subroutine
help_port: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 83 | |||||||||||
| 84 | sub cmd_port | ||||||||||
| 85 | { | ||||||||||
| 86 | 29 | ▶ | my($self, $con, $req) = @_; | ||||||||
|
Subroutine
cmd_port: called CC=2 SCAR=8.7
Pod
undocumented
| |||||||||||
| 87 | |||||||||||
| 88 | 29 | ▶ | if($req->args =~ /(\d+,\d+,\d+,\d+),(\d+),(\d+)/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | { | ||||||||||
| 90 | 29 | my $ip = join '.', split /,/, $1; | |||||||||
| 91 | 29 | my $port = $2*256 + $3; | |||||||||
| 92 | |||||||||||
| 93 | tcp_connect $ip, $port, sub { | ||||||||||
| 94 | 29 | ▶ | my($fh) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||
| 95 | 29 | ▶ | unless($fh) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | { | ||||||||||
| 97 | 0 | $con->send_response(500 => "Illegal PORT command"); | |||||||||
| 98 | 0 | $self->done; | |||||||||
| 99 | 0 | return; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 29 | my $handle; | |||||||||
| 103 | $handle = AnyEvent::Handle->new( | ||||||||||
| 104 | fh => $fh, | ||||||||||
| 105 | on_error => sub { | ||||||||||
| 106 | 0 | ▶ | $_[0]->destroy; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 107 | 0 | undef $handle; | |||||||||
| 108 | }, | ||||||||||
| 109 | on_eof => sub { | ||||||||||
| 110 | 0 | ▶ | $handle->destroy; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | undef $handle; | |||||||||
| 112 | }, | ||||||||||
| 113 | 29 | ); | |||||||||
| 114 | |||||||||||
| 115 | 29 | $self->data($handle); | |||||||||
| 116 | 29 | $con->send_response(200 => "Port command successful"); | |||||||||
| 117 | 29 | $self->done; | |||||||||
| 118 | |||||||||||
| 119 | 29 | }; | |||||||||
| 120 | |||||||||||
| 121 | } | ||||||||||
| 122 | else | ||||||||||
| 123 | { | ||||||||||
| 124 | 0 | $con->send_response(500 => "Illegal PORT command"); | |||||||||
| 125 | 0 | $self->done; | |||||||||
| 126 | 0 | return; | |||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | 6 | ▶ | sub help_rest { 'REST <sp> byte-count' } | ||||||||
|
Subroutine
help_rest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 132 | |||||||||||
| 133 | sub cmd_rest | ||||||||||
| 134 | { | ||||||||||
| 135 | 10 | ▶ | my($self, $con, $req) = @_; | ||||||||
|
Subroutine
cmd_rest: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 136 | |||||||||||
| 137 | 10 | ▶ | if($req->args =~ /^\s*(\d+)\s*$/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | { | ||||||||||
| 139 | 8 | my $offset = $1; | |||||||||
| 140 | 8 | $con->send_response(350 => "Restarting at $offset. Send STORE or RETRIEVE to initiate transfer"); | |||||||||
| 141 | 8 | $self->restart_offset($offset); | |||||||||
| 142 | } | ||||||||||
| 143 | else | ||||||||||
| 144 | { | ||||||||||
| 145 | 2 | $con->send_response(501 => "REST requires a value greater than or equal to 0"); | |||||||||
| 146 | } | ||||||||||
| 147 | 10 | $self->done; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 23 | 1; | |||||||||
| 151 | |||||||||||
| 152 | __END__ | ||||||||||