DAV.pm

filter: Statementstmt 20.1% 26 / 129 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 23.5% 8 / 34 Podpod 84.6% 11 / 13 totaltotal 15.9% 45 / 282 CC 41 SCAR 33.4
CC
41
Cov
12%
put
90.0
propfind
42.0
getinfo
42.0
CRAP 28.1
↑ Summary DAV/XML.pm »
1 package Net::Async::HTTP::DAV;
2 # ABSTRACT: WebDAV using Net::Async::HTTP
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.001';
9  
10 =head1 NAME
11  
12 Net::Async::HTTP::DAV - support for WebDAV over L<Net::Async::HTTP>
13  
14 =head1 VERSION
15  
16 Version 0.001
17  
18 =head1 SYNOPSIS
19  
20 use IO::Async::Loop;
21 use Net::Async::HTTP;
22 use Net::Async::HTTP::DAV;
23 use POSIX qw(strftime);
24 my $loop = IO::Async::Loop->new;
25 $loop->add(my $dav = Net::Async::HTTP::DAV->new(
26 host => 'cpan.perlsite.co.uk',
27 ));
28 $dav->propfind(
29 path => '/authors/id/T/TE/TEAM/',
30 on_item => sub {
31 my ($item) = @_;
32 printf "%-32.32s %-64.64s %12d\n", strftime("%Y-%m-%d %H:%M:%S", localtime $item->{modified}), $item->{displayname}, $item->{size};
33 },
34 )->get;
35  
36 =head1 DESCRIPTION
37  
38 Does some very basic WebDAV stuff.
39  
40 See L<http://www.webdav.org/specs/rfc2518.html>.
41  
42 Highly experimental, no documentation, see examples/ in source distribution.
43 API is likely to change.
44  
45 =cut
46  
47 2 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Net::Async::HTTP::DAV::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 =head1 METHODS
55  
56 =cut
57  
58 =head2 configure
59  
60 Accepts configuration parameters (can also be passed to L</new>).
61  
62 =over 4
63  
64 =item * host - which host we're connecting to
65  
66 =item * path - base path for requests
67  
68 =item * user - optional username
69  
70 =item * pass - optional password, Basic auth
71  
72 =item * http - a pre-existing L<Net::Async::HTTP> instance
73  
74 =back
75  
76 =cut
77  
78 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
79 0 my ($self, %args) = @_;
80 0 foreach (qw(user pass path host http)) {
81 0 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 0 0
82 }
83 0 return $self;
84 }
85  
86 =head2 http
87  
88 Accessor for the internal L<Net::Async::HTTP> instance.
89  
90 =cut
91  
92 0 ▶sub http {
Subroutine http: not called CC=3 SCAR=24.8
Pod documented
93 0 my $self = shift;
94 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
95 0 shift->{http} = shift;
96 0 return $self
97 }
98 0 ▶ unless($self->{http}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"http"}) 0 0 0
99 0 my $ua = $self->ua_factory;
100 0 $self->add_child($ua);
101 0 Scalar::Util::weaken($self->{http} = $ua);
102 }
103 0 return $self->{http};
104 }
105  
106 =head2 ua_factory
107  
108 Populates the L<Net::Async::HTTP> instance via factory or default settings.
109  
110 =cut
111  
112 0 ▶sub ua_factory {
Subroutine ua_factory: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($self) = @_;
114 0 ▶ $self->{ua_factory}->() if $self->{ua_factory};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua_factory"} 0 0 0
115 0 Net::Async::HTTP->new(
116 decode_content => 0,
117 fail_on_error => 1,
118 max_connections_per_host => 4,
119 stall_timeout => 60,
120 )
121 }
122  
123 =head2 path
124  
125 Base path for requests.
126  
127 =cut
128  
129 0 ▶sub path { shift->{path} }
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
130  
131 =head2 propfind
132  
133 Does a propfind request.
134  
135 Parameters are basically 'path' and on_item for a per-item callback.
136  
137 =cut
138  
139 0 ▶sub propfind {
Subroutine propfind: not called CC=6 SCAR=37.4
Pod documented
140 0 my $self = shift;
141 0 my %args = @_;
142 # want a trailing /
143 0 ▶ my $uri = $self->uri_from_path(File::Spec->catdir(($self->path // ()), $args{path}) . '/') or die "Invalid URL?";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->uri_from_path("File::Spec"->catdir($self->path // (), $args{"path"}) . '/') or die "Invalid URL?" 0 0 0
Condition0%0/3
$self->path // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->path // ()
$self->path ()
Truth table0%0/3
A // B
A= $self->pathB= ()
ABresult
1X1
011
000
144 0 my $body = <<"EOF";
145 <?xml version="1.0" encoding="utf-8" ?>
146 <D:propfind xmlns:D="DAV:">
147 <D:allprop/>
148 </D:propfind>
149 EOF
150  
151 0 my $req = HTTP::Request->new(
152 PROPFIND => $uri->path, [
153 'Host' => $uri->host,
154 'Depth' => 1,
155 'Content-Type' => 'text/xml'
156 ], encode_utf8($body)
157 );
158 0 $req->protocol('HTTP/1.1');
159 0 ▶ $req->authorization_basic($self->user, $self->pass) if defined($self->user);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user 0 0 0
160 $self->http->do_request(
161 request => $req,
162 host => $uri->host,
163 port => $uri->scheme || 80,
164 SSL => $uri->scheme eq 'https' ? 1 : 0,
165 0 ▶ on_header => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 my $response = shift;
167 0 my $result = Net::Async::HTTP::DAV::Response->new(
168 %args,
169 path => $uri->path
170 );
171 # Seems we'll need to return the response?
172 0 weaken $response;
173 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 ▶ $result->parse_chunk($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
175 0 $response
176 0 };
177 },
178 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 0 0
Condition0%0/2
$uri->scheme || 80
l!l
00
MC/DC0%0/1
$uri->scheme || 80
$uri->scheme
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
179 }
180  
181 0 ▶sub getinfo {
Subroutine getinfo: not called CC=6 SCAR=37.4
Pod undocumented
182 0 my $self = shift;
183 0 my %args = @_;
184 0 ▶ my $uri = $self->uri_from_path($args{path} // $self->{path}) or die "Invalid URL?";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->uri_from_path($args{"path"} // $self->{"path"}) or die "Invalid URL?" 0 0 0
Condition0%0/3
$args{"path"} // $self->{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"path"} // $self->{"path"}
$args{"path"} $self->{"path"}
Truth table0%0/3
A // B
A= $args{"path"}B= $self->{"path"}
ABresult
1X1
011
000
185 0 my $body = <<"EOF";
186 <?xml version="1.0" encoding="utf-8" ?>
187 <D:propfind xmlns:D="DAV:">
188 <D:allprop/>
189 </D:propfind>
190 EOF
191  
192 0 my $req = HTTP::Request->new(
193 PROPFIND => $uri->path, [
194 'Host' => $uri->host,
195 'Depth' => 0,
196 'Content-Type' => 'text/xml'
197 ], encode_utf8($body)
198 );
199 0 $req->protocol('HTTP/1.1');
200 0 ▶ $req->authorization_basic($self->user, $self->pass) if $self->user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->user 0 0 0
201 $self->http->do_request(
202 request => $req,
203 host => $uri->host,
204 port => $uri->scheme || 80,
205 SSL => $uri->scheme eq 'https' ? 1 : 0,
206 0 ▶ on_header => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 my $response = shift;
208 my $result = Net::Async::HTTP::DAV::Response->new(
209 %args,
210 path => $uri->path,
211 0 ▶ on_item => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 my $item = shift;
213 0 $args{on_size}->($item->{size});
214 }
215 0 );
216 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
217 0 ▶ $result->parse_chunk($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
218 0 };
219 },
220 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 my ( $message ) = @_;
222 0 die "Failed - $message\n";
223 }
224 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 0 0
Condition0%0/2
$uri->scheme || 80
l!l
00
MC/DC0%0/1
$uri->scheme || 80
$uri->scheme
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
225 0 return $self;
226 }
227  
228 =head2 head
229  
230 Perform HEAD request on given path.
231  
232 =cut
233  
234 0 ▶sub head {
Subroutine head: not called CC=6 SCAR=37.4
Pod documented
235 0 my $self = shift;
236 0 my %args = @_;
237 0 ▶ my $uri = $self->uri_from_path($args{path} // $self->{path}) or die "Invalid URL?";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->uri_from_path($args{"path"} // $self->{"path"}) or die "Invalid URL?" 0 0 0
Condition0%0/3
$args{"path"} // $self->{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"path"} // $self->{"path"}
$args{"path"} $self->{"path"}
Truth table0%0/3
A // B
A= $args{"path"}B= $self->{"path"}
ABresult
1X1
011
000
238 0 my $req = HTTP::Request->new(
239 HEAD => $uri->path, [
240 'Host' => $uri->host,
241 ]
242 );
243 0 $req->protocol('HTTP/1.1');
244 0 ▶ $req->authorization_basic($self->user, $self->pass) if $self->user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->user 0 0 0
245 $self->http->do_request(
246 request => $req,
247 host => $uri->host,
248 port => $uri->scheme || 80,
249 SSL => $uri->scheme eq 'https' ? 1 : 0,
250 0 ▶ on_response => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 my $response = shift;
252 # $args{on_size}->($response->content_length);
253 },
254 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 0 my ( $message ) = @_;
256 0 die "Failed - $message\n";
257 }
258 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 0 0
Condition0%0/2
$uri->scheme || 80
l!l
00
MC/DC0%0/1
$uri->scheme || 80
$uri->scheme
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
259 0 return $self;
260 }
261  
262 =head2 get
263  
264 GET the given resource
265  
266 =cut
267  
268 0 ▶sub get {
Subroutine get: not called CC=6 SCAR=37.4
Pod documented
269 0 my $self = shift;
270 0 my %args = @_;
271 0 ▶ my $uri = $self->uri_from_path($args{path} // $self->{path}) or die "Invalid URL?";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->uri_from_path($args{"path"} // $self->{"path"}) or die "Invalid URL?" 0 0 0
Condition0%0/3
$args{"path"} // $self->{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"path"} // $self->{"path"}
$args{"path"} $self->{"path"}
Truth table0%0/3
A // B
A= $args{"path"}B= $self->{"path"}
ABresult
1X1
011
000
272 0 my $req = HTTP::Request->new(
273 GET => $uri->path, [
274 'Host' => $uri->host,
275 ]
276 );
277 0 $req->protocol('HTTP/1.1');
278 0 ▶ $req->authorization_basic($self->user, $self->pass) if $self->user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->user 0 0 0
279 $self->http->do_request(
280 request => $req,
281 host => $uri->host,
282 port => $uri->scheme || 80,
283 SSL => $uri->scheme eq 'https' ? 1 : 0,
284 0 ▶ on_header => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 my $response = shift;
286 0 return $args{on_header}->($response);
287 },
288 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 my ( $message ) = @_;
290 0 die "Failed - $message\n";
291 }
292 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 0 0
Condition0%0/2
$uri->scheme || 80
l!l
00
MC/DC0%0/1
$uri->scheme || 80
$uri->scheme
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
293 0 return $self;
294 }
295  
296 =head2 put
297  
298 Write data directly to the given resource.
299  
300 =cut
301  
302 0 ▶sub put {
Subroutine put: not called CC=9 SCAR=45.0
Pod documented
303 0 my $self = shift;
304 0 my %args = @_;
305 0 my $handler = delete $args{response_body};
306 0 ▶ my $uri = $self->uri_from_path($args{path} // $self->{path});
Condition0%0/3
$args{"path"} // $self->{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"path"} // $self->{"path"}
$args{"path"} $self->{"path"}
Truth table0%0/3
A // B
A= $args{"path"}B= $self->{"path"}
ABresult
1X1
011
000
307  
308 my $req = HTTP::Request->new(
309 PUT => $uri->path, [
310 'Host' => $uri->host,
311 'Content-Type' => 'application/octetstream',
312 ], defined $args{content} ? $args{content} : ()
313 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'content'} ? : 0 0 0
314 0 $req->protocol('HTTP/1.1');
315 0 $req->authorization_basic($self->{user}, $self->{pass});
316 0 ▶ $req->content_length($args{size}) unless defined $args{content};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"content"} 0 0 0
317  
318 0 my $fh;
319 $self->http->do_request(
320 request => $req,
321 host => $uri->host,
322 port => $uri->scheme || 80,
323 SSL => $uri->scheme eq 'https' ? 1 : 0,
324 (defined $args{content})
325 ? ()
326 0 ▶ : (request_body => $handler || sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
327 0 my ($stream) = @_;
328 0 warn $stream;
329 0 return '';
330 0 my $read = sysread $fh, my $buffer, 32768;
331 0 ▶ warn $! unless defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 0 0
332 0 ▶ return $buffer if $read;
Branch: 1 branch
BranchTrueFalseTotal
if $read 0 0 0
333 0 return;
334 }),
335 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 my ( $message ) = @_;
337 0 die "Failed - $message\n";
338 },
339 0 ▶ on_response => $args{on_response} || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 my ($response) = @_;
341 0 my $msg = $response->message;
342 0 $msg =~ s/\s+/ /ig;
343 0 $msg =~ s/(?:^\s+)|(?:\s+$)//g; # trim
344 0 warn $response->code . " - $msg\n";
345 }
346 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 0 0
defined $args{'content'} ? : 0 0 0
Condition0%0/8
$uri->scheme || 80
l!l
00
$handler or sub { my($stream) = @_; warn $stream; return ""; my $read = sysread($fh, my $buffer, 32768); warn $! unless defined $read; return $buffer if $read; return; }
l!l&&r!l&&!r
000
$args{'on_response'} || sub { my($response) = @_; my $msg = $response->message; $msg =~ s/\s+/ /gi; $msg =~ s/(?:^\s+)|(?:\s+$)//g; warn $response->code . " - $msg\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/1
$uri->scheme || 80
$uri->scheme
MC/DC0%0/2
$handler or sub { my($stream) = @_; warn $stream; return ""; my $read = sysread($fh, my $buffer, 32768); warn $! unless defined $read; return $buffer if $read; return; }
$handler sub { my($stream) = @_; warn $stream; return ""; my $read = sysread($fh, my $buffer, 32768); warn $! unless defined $read; return $buffer if $read; return; }
MC/DC0%0/2
$args{'on_response'} || sub { my($response) = @_; my $msg = $response->message; $msg =~ s/\s+/ /gi; $msg =~ s/(?:^\s+)|(?:\s+$)//g; warn $response->code . " - $msg\n"; }
$args{'on_response'} sub { my($response) = @_; my $msg = $response->message; $msg =~ s/\s+/ /gi; $msg =~ s/(?:^\s+)|(?:\s+$)//g; warn $response->code . " - $msg\n"; }
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
Truth table0%0/3
A or B
A= $handlerB= sub { my($stream) = @_; warn $stream; return ""; my $read = sysread($fh, my $buffer, 32768); warn $! unless defined $read; return $buffer if $read; return; }
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $args{'on_response'}B= sub { my($response) = @_; my $msg = $response->message; $msg =~ s/\s+/ /gi; $msg =~ s/(?:^\s+)|(?:\s+$)//g; warn $response->code . " - $msg\n"; }
ABresult
1X1
011
000
347 }
348  
349 0 ▶sub host { shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
350 0 ▶sub user { shift->{user} }
Subroutine user: not called CC=1 SCAR=6.9
Pod documented
351 0 ▶sub pass { shift->{pass} }
Subroutine pass: not called CC=1 SCAR=6.9
Pod documented
352  
353 0 ▶sub uri_from_path {
Subroutine uri_from_path: not called CC=4 SCAR=30.0
Pod undocumented
354 0 my $self = shift;
355 0 ▶ my $path = shift // '/';
Condition0%0/2
shift() // "/"
l!l
00
MC/DC0%0/1
shift() // "/"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
356 0 ▶ $path = "/$path" unless substr($path, 0, 1) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($path, 0, 1) eq "/" 0 0 0
357 0 $path =~ s{/+}{/}g;
358 0 ▶ return URI->new('http://' . $self->host . $path) || die "Invalid URL?";
Condition0%0/2
'URI'->new('http://' . $self->host . $path) || die('Invalid URL?')
l!l
00
MC/DC0%0/1
'URI'->new('http://' . $self->host . $path) || die('Invalid URL?')
'URI'->new('http://' . $self->host . $path)
Truth table0%0/2
A
A= 'URI'->new('http://' . $self->host . $path)
Aresult
11
00
359 }
360  
361 2 1;
362  
363 __END__
↑ Summary DAV/XML.pm »