blib/lib/LWP/Protocol/sftp.pm

filter: Statementstmt 31.1% 33 / 106 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 22.0% 41 / 186 CC 30 SCAR 65.0
CC
30
Cov
20%
request
870.0
__ANON__
6.0
BEGIN
1.0
CRAP 664.4
↑ Summary
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
BranchTrueFalseTotal
unless defined $size and $size > 0 0 0 0
Condition0%0/3
defined $size and $size > 0
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
41  
42 # check proxy
43 0 ▶ defined $proxy and
Branch: 1 branch
BranchTrueFalseTotal
defined $proxy and return "HTTP::Response"->new(400, "You can not proxy through the sftp subsystem") 0 0 0
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
BranchTrueFalseTotal
if ($scheme ne "sftp") 0 0 0
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
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($sftp->error) 0 0 0
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
BranchTrueFalseTotal
if ($method eq "GET" or $method eq "HEAD") 0 0 0
Condition0%0/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
000
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"
ABresult
1X1
011
000
82  
83 0 ▶ my $stat = $sftp->stat($path) or die "remote file stat failed";
Branch: 1 branch
BranchTrueFalseTotal
my $stat = $sftp->stat($path) or die "remote file stat failed" 0 0 0
84  
85 # check if-modified-since
86 0 my $ims = $request->header('If-Modified-Since');
87 0 ▶ if (defined $ims) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ims) 0 0 0
88 0 my $time = HTTP::Date::str2time($ims);
89 0 ▶ if (defined $time and $time >= $stat->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time and $time >= $stat->mtime) 0 0 0
Condition0%0/3
defined $time and $time >= $stat->mtime
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (S_ISDIR($stat->perm)) 0 0 0
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
BranchTrueFalseTotal
my $ls = $sftp->ls($path, "ordered", 1, @{$LWP::Protocol::sftp::DEFAULTS{"ls"};}) or die "remote ls failed" 0 0 0
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
BranchTrueFalseTotal
if S_ISDIR($_->{"a"}->perm) 0 0 0
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
BranchTrueFalseTotal
if defined $user 0 0 0
122 0 $base .= URI::Escape::uri_escape($host);
123 0 ▶ $base .= ':' . URI::Escape::uri_escape($port) if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 0 0 0
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
BranchTrueFalseTotal
if $method eq "HEAD" 0 0 0
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
BranchTrueFalseTotal
if ($method ne "HEAD") 0 0 0
149 0 ▶ my $fh = $sftp->open($path) or die "remote file open failed";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $sftp->open($path) or die "remote file open failed" 0 0 0
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
BranchTrueFalseTotal
defined $content ? : 0 0 0
153 0 ▶ $sftp->close($fh) or die "remote file read failed";
Branch: 1 branch
BranchTrueFalseTotal
$sftp->close($fh) or die "remote file read failed" 0 0 0
154 }
155 0 return $response;
156 }
157  
158 # handle PUT method
159 0 ▶ if ($method eq 'PUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "PUT") 0 0 0
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
BranchTrueFalseTotal
my $fh = $sftp->open($path, 26) or die "remote file open failed" 0 0 0
161  
162 0 my $content = $request->content;
163 0 ▶ while (length $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) 0 0 0
164 0 ▶ my $bytes = $sftp->write($fh, $content) or die "remote file write failed";
Branch: 1 branch
BranchTrueFalseTotal
my $bytes = $sftp->write($fh, $content) or die "remote file write failed" 0 0 0
165 0 substr($content, 0, $bytes, '');
166 }
167  
168 0 ▶ $sftp->close($fh) or die "remote file write failed";
Branch: 1 branch
BranchTrueFalseTotal
$sftp->close($fh) or die "remote file write failed" 0 0 0
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
BranchTrueFalseTotal
if ($@) 0 0 0
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__
↑ Summary