| 1 | package LWP::Protocol::sftp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # BEGIN { local $| =1; print "loading LWP::Protocol::sftp\n"; } | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(LWP::Protocol); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | LWP::Protocol::implementor(sftp => __PACKAGE__); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | require LWP::MediaTypes; | |||||||||||||||||||||||||||
| 15 | 1 | require HTTP::Request; | |||||||||||||||||||||||||||
| 16 | 1 | require HTTP::Response; | |||||||||||||||||||||||||||
| 17 | 1 | require HTTP::Status; | |||||||||||||||||||||||||||
| 18 | 1 | require HTTP::Date; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | require URI::Escape; | |||||||||||||||||||||||||||
| 21 | 1 | require HTML::Entities; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Net::SFTP::Foreign; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Net::SFTP::Foreign::Constants qw(:flags :status); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Fcntl qw(S_ISDIR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use constant PUT_BLOCK_SIZE => 8192; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | our %DEFAULTS = ( ls => [], | |||||||||||||||||||||||||||
| 30 | new => [] ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | my $dont_escape_in_paths = '^A-Za-z0-9\-\._~/'; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub request | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=29 SCAR=67.7
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my($self, $request, $proxy, $arg, $size) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # print __PACKAGE__."->request($self, $request, $proxy, $arg, $size)\n"; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $size = 4096 unless defined $size and $size > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $size and $size > 0
MC/DC0%0/2
defined $size and $size > 0
defined $size
$size > 0
Truth table0%0/3
A and B
A= defined $sizeB= $size > 0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # check proxy | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | defined $proxy and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | return HTTP::Response->new(HTTP::Status::RC_BAD_REQUEST, | ||||||||||||||||||||||||||||
| 45 | 'You can not proxy through the sftp subsystem'); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # check method | ||||||||||||||||||||||||||||
| 48 | 0 | my $method = $request->method; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # check url | ||||||||||||||||||||||||||||
| 51 | 0 | my $url = $request->url; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $scheme = $url->scheme; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($scheme ne 'sftp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR, | |||||||||||||||||||||||||||
| 56 | "LWP::Protocol::sftp::request called for '$scheme'") | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $host = $url->host; | |||||||||||||||||||||||||||
| 60 | 0 | my $port = $url->port; | |||||||||||||||||||||||||||
| 61 | 0 | my $user = $url->user; | |||||||||||||||||||||||||||
| 62 | 0 | my $password = $url->password; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $path = $url->path; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | $path = '/' unless defined $path and length $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $sftp = Net::SFTP::Foreign->new(host => $host, | ||||||||||||||||||||||||||||
| 68 | user => $user, | ||||||||||||||||||||||||||||
| 69 | port => $port, | ||||||||||||||||||||||||||||
| 70 | password => $password, | ||||||||||||||||||||||||||||
| 71 | 0 | @{$DEFAULTS{new}}); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($sftp->error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | return HTTP::Response->new(HTTP::Status::RC_SERVICE_UNAVAILABLE, | |||||||||||||||||||||||||||
| 74 | "unable to establish SSH connection to remote machine (".$sftp->error.")") | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # handle GET and HEAD methods | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | my $response = eval { | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($method eq 'GET' || $method eq 'HEAD') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$method eq "GET" or $method eq "HEAD"
MC/DC0%0/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET"
$method eq "HEAD"
Truth table0%0/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $stat = $sftp->stat($path) or die "remote file stat failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # check if-modified-since | ||||||||||||||||||||||||||||
| 86 | 0 | my $ims = $request->header('If-Modified-Since'); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (defined $ims) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | my $time = HTTP::Date::str2time($ims); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (defined $time and $time >= $stat->mtime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $time and $time >= $stat->mtime
MC/DC0%0/2
defined $time and $time >= $stat->mtime
defined $time
$time >= $stat->mtime
Truth table0%0/3
A and B
A= defined $timeB= $time >= $stat->mtime
| |||||||||||||||||||||||||||||
| 90 | 0 | return HTTP::Response->new(HTTP::Status::RC_NOT_MODIFIED, | |||||||||||||||||||||||||||
| 91 | "$method $path") | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Ok, should be an OK response by now... | ||||||||||||||||||||||||||||
| 96 | 0 | my $response = HTTP::Response->new(HTTP::Status::RC_OK); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # fill in response headers | ||||||||||||||||||||||||||||
| 99 | 0 | $response->header('Last-Modified', HTTP::Date::time2str($stat->mtime)); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if (S_ISDIR($stat->perm)) { # If the path is a directory, process it | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | # generate the HTML for directory | ||||||||||||||||||||||||||||
| 103 | my $ls = $sftp->ls($path, ordered => 1, | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | @{$DEFAULTS{ls}}) or die "remote ls failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Make directory listing | ||||||||||||||||||||||||||||
| 107 | my @lines = map { | ||||||||||||||||||||||||||||
| 108 | 0 | my $fn = $_->{filename}; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | $fn .= '/' if S_ISDIR($_->{a}->perm); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | my $furl = URI::Escape::uri_escape($fn, $dont_escape_in_paths); | |||||||||||||||||||||||||||
| 111 | 0 | my $desc = HTML::Entities::encode($fn); | |||||||||||||||||||||||||||
| 112 | 0 | qq{<li><a href="$furl">$desc</a>} | |||||||||||||||||||||||||||
| 113 | } @$ls; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | $path =~ s|/?$|/|; | |||||||||||||||||||||||||||
| 116 | 0 | my $ue_path = URI::Escape::uri_escape($path, $dont_escape_in_paths); | |||||||||||||||||||||||||||
| 117 | 0 | my $ee_path = HTML::Entities::encode($path); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # regenerate base url without password | ||||||||||||||||||||||||||||
| 120 | 0 | my $base = 'sftp://'; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | $base .= URI::Escape::uri_escape($user) . '@' if defined $user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $base .= URI::Escape::uri_escape($host); | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | $base .= ':' . URI::Escape::uri_escape($port) if defined $port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | $base .= $ue_path; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $html = join("\n", | |||||||||||||||||||||||||||
| 127 | "<HTML>\n<HEAD>", | ||||||||||||||||||||||||||||
| 128 | "<TITLE>Directory $ee_path</TITLE>", | ||||||||||||||||||||||||||||
| 129 | "<BASE HREF=\"$base\">", | ||||||||||||||||||||||||||||
| 130 | "</HEAD>\n<BODY>", | ||||||||||||||||||||||||||||
| 131 | "<H1>Directory listing of $ee_path</H1>", | ||||||||||||||||||||||||||||
| 132 | "<UL>", @lines, "</UL>", | ||||||||||||||||||||||||||||
| 133 | "</BODY>\n</HTML>\n"); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $response->header('Content-Type', 'text/html'); | |||||||||||||||||||||||||||
| 136 | 0 | $response->header('Content-Length', length $html); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | $html = "" if $method eq "HEAD"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | return $self->collect_once($arg, $response, $html); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # path is a regular file | ||||||||||||||||||||||||||||
| 143 | 0 | my $file_size = $stat->size; | |||||||||||||||||||||||||||
| 144 | 0 | $response->header('Content-Length', $file_size); | |||||||||||||||||||||||||||
| 145 | 0 | LWP::MediaTypes::guess_media_type($path, $response); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # read the file | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($method ne "HEAD") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $fh = $sftp->open($path) or die "remote file open failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $response = $self->collect($arg, $response, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 151 | 0 | my $content = $sftp->read($fh, $size); | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | defined $content ? \$content : \"" }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | $sftp->close($fh) or die "remote file read failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 0 | return $response; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # handle PUT method | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($method eq 'PUT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | my $fh = $sftp->open($path, SSH2_FXF_WRITE | SSH2_FXF_CREAT | SSH2_FXF_TRUNC) or die "remote file open failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $content = $request->content; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | while (length $content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my $bytes = $sftp->write($fh, $content) or die "remote file write failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | substr($content, 0, $bytes, ''); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $sftp->close($fh) or die "remote file write failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | return HTTP::Response->new(HTTP::Status::RC_OK); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # unsupported method | ||||||||||||||||||||||||||||
| 174 | 0 | return HTTP::Response->new(HTTP::Status::RC_BAD_REQUEST, | |||||||||||||||||||||||||||
| 175 | "Library does not allow method $method for 'sftp:' URLs"); | ||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my $error = $sftp->error; | |||||||||||||||||||||||||||
| 180 | 0 | return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR, | |||||||||||||||||||||||||||
| 181 | "SFTP error: $@ - $error"); | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | return $response; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | 1; | |||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||