DAVTalk.pm

filter: Statementstmt 17.9% 41 / 228 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 43.3% 13 / 30 Podpod 100% 17 / 17 totaltotal 16.4% 71 / 432 CC 69 SCAR 54.5
CC
69
Cov
11%
SafeRequest
702.0
GetPropsArray
240.0
SetURL
42.0
CRAP 232.4
↑ Summary DAVTalk/XMLParser.pm »
1 package Net::DAVTalk;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Tie::DataUUID qw{$uuid};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use XML::Spice;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::DAVTalk::XMLParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URI::Escape qw(uri_escape uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Net::DAVTalk - Interface to talk to DAV servers
21  
22 =head1 VERSION
23  
24 Version 0.24
25  
26 =cut
27  
28 2 our $VERSION = '0.25';
29  
30 =head1 SYNOPSIS
31  
32 Net::DAVTalk is was originally designed as a service module for Net::CalDAVTalk
33 and Net::CardDAVTalk, abstracting the process of connecting to a DAV server and
34 parsing the XML responses.
35  
36 Example:
37  
38 use Net::DAVTalk;
39 use XML::Spice;
40  
41 my $davtalk = Net::DAVTalk->new(
42 url => "https://dav.example.com/",
43 user => "foo\@example.com",
44 password => "letmein",
45 headers => { Cookie => "123", Referer => "456" },
46 );
47  
48 $davtalk->Request(
49 'MKCALENDAR',
50 "$calendarId/",
51 x('C:mkcalendar', $Self->NS(),
52 x('D:set',
53 x('D:prop', @Properties),
54 ),
55 ),
56 );
57  
58 $davtalk->Request(
59 'DELETE',
60 "$calendarId/",
61 );
62  
63 =head1 SUBROUTINES/METHODS
64  
65 =head2 $class->new(%Options)
66  
67 Options:
68  
69 url: either full https?:// url, or relative base path on the
70 server to the DAV endpoint
71  
72 host, scheme and port: alternative to using full URL.
73 If URL doesn't start with https?:// then these will be used to
74 construct the endpoint URI.
75  
76 expandurl and wellknown: if these are set, then the wellknown
77 name (caldav and carddav are both defined) will be used to
78 resolve /.well-known/$wellknown to find the current-user-principal
79 URI, and then THAT will be resovlved to find the $wellknown-home-set
80 URI, which will be used as the URL for all further actions on
81 this object.
82  
83 user and password: if these are set, perform basic authentication.
84 user and access_token: if these are set, perform Bearer (OAUTH2)
85 authentication.
86  
87 headers: a hashref of additional headers to add to every request
88  
89 SSL_options: a hashref of SSL options to pass down to the default
90 user agent
91 =cut
92  
93 # General methods
94  
95 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
96 0 my ($Class, %Params) = @_;
97  
98 0 ▶ unless ($Params{url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Params{"url"}) 0 0 0
99 0 confess "URL not supplied";
100 }
101  
102 # Assume url points to xyz-home-set, otherwise expand the url
103 0 ▶ if (delete $Params{expandurl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $Params{"expandurl"}) 0 0 0
104 # Locating Services for CalDAV and CardDAV (RFC6764)
105 0 my $PrincipalURL = $Class->GetCurrentUserPrincipal(%Params);
106 0 $Params{principal} = $PrincipalURL;
107  
108 0 my $HomeSet = $Class->GetHomeSet(
109 %Params,
110 url => $PrincipalURL,
111 );
112  
113 0 $Params{url} = $HomeSet;
114 }
115  
116 0 ▶ my $Self = bless \%Params, ref($Class) || $Class;
Condition0%0/3
ref $Class || $Class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $Class || $Class
ref $Class $Class
Truth table0%0/3
A || B
A= ref $ClassB= $Class
ABresult
1X1
011
000
117 0 $Self->SetURL($Params{url});
118 0 $Self->SetPrincipalURL($Params{principal});
119 0 $Self->ns(D => 'DAV:');
120  
121 0 return $Self;
122 }
123  
124 =head2 my $ua = $Self->ua();
125 =head2 $Self->ua($setua);
126  
127 Get or set the useragent (HTTP::Tiny or compatible) that will be used to make
128 the requests:
129  
130 e.g.
131  
132 my $ua = $Self->ua();
133  
134 $Self->ua(HTTP::Tiny->new(agent => "MyAgent/1.0", timeout => 5));
135  
136 =cut
137  
138 0 ▶sub ua {
Subroutine ua: not called CC=3 SCAR=24.8
Pod documented
139 0 my $Self = shift;
140 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
141 0 $Self->{ua} = shift;
142 }
143 else {
144 $Self->{ua} ||= HTTP::Tiny->new(
145 agent => "Net-DAVTalk/$VERSION",
146 SSL_options => $Self->{SSL_options},
147 0 ▶ );
Condition0%0/3
$Self->{"ua"} ||= "HTTP::Tiny"->new("agent", "Net-DAVTalk/$Net::DAVTalk::VERSION", "SSL_options", $Self->{"SSL_options"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$Self->{"ua"} ||= "HTTP::Tiny"->new("agent", "Net-DAVTalk/$Net::DAVTalk::VERSION", "SSL_options", $Self->{"SSL_options"})
$Self->{"ua"} "HTTP::Tiny"->new("agent", "Net-DAVTalk/$Net::DAVTalk::VERSION", "SSL_options", $Self->{"SSL_options"})
Truth table0%0/3
A ||= B
A= $Self->{"ua"}B= "HTTP::Tiny"->new("agent", "Net-DAVTalk/$Net::DAVTalk::VERSION", "SSL_options", $Self->{"SSL_options"})
ABresult
1X1
011
000
148 }
149 0 return $Self->{ua};
150 }
151  
152 =head2 $Self->SetURL($url)
153  
154 Change the endpoint URL for an existing connection.
155  
156 =cut
157  
158 0 ▶sub SetURL {
Subroutine SetURL: not called CC=6 SCAR=37.4
Pod documented
159 0 my ($Self, $URL) = @_;
160  
161 0 $URL =~ s{/$}{}; # remove any trailing slash
162  
163 0 ▶ if ($URL =~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URL =~ m[^https?://]) { } 0 0 0
164 0 my ($HTTPS, $Hostname, $Port, $BasePath)
165 = $URL =~ m{^http(s)?://([^/:]+)(?::(\d+))?(.*)?};
166  
167 0 ▶ unless ($Hostname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Hostname) 0 0 0
168 0 confess "Invalid hostname in '$URL'";
169 }
170  
171 0 ▶ $Self->{scheme} = $HTTPS ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$HTTPS ? : 0 0 0
172 0 $Self->{host} = $Hostname;
173 0 ▶ $Self->{port} = ($Port || ($HTTPS ? 443 : 80));
Branch: 1 branch
BranchTrueFalseTotal
$HTTPS ? : 0 0 0
Condition0%0/3
$Port || ($HTTPS ? 443 : 80)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Port || ($HTTPS ? 443 : 80)
$Port ($HTTPS ? 443 : 80)
Truth table0%0/3
A || B
A= $PortB= ($HTTPS ? 443 : 80)
ABresult
1X1
011
000
174 0 $Self->{basepath} = $BasePath;
175 }
176 else {
177 0 $Self->{basepath} = $URL;
178 }
179  
180 0 $Self->{url} = "$Self->{scheme}://$Self->{host}:$Self->{port}$Self->{basepath}";
181  
182 0 return $Self->{url};
183 }
184  
185 =head2 $Self->SetPrincipalURL($url)
186  
187 Set the URL to the DAV Principal
188  
189 =cut
190  
191 0 ▶sub SetPrincipalURL {
Subroutine SetPrincipalURL: not called CC=1 SCAR=6.9
Pod documented
192 0 my ($Self, $PrincipalURL) = @_;
193  
194 0 return $Self->{principal} = $PrincipalURL;
195 }
196  
197 =head2 $Self->fullpath($shortpath)
198  
199 Convert from a relative path to a full path:
200  
201 e.g
202 my $path = $Dav->fullpath('Default');
203 ## /dav/calendars/user/foo/Default
204  
205 NOTE: a you can pass a non-relative full path (leading /)
206 to this function and it will be returned unchanged.
207  
208 =cut
209  
210 0 ▶sub fullpath {
Subroutine fullpath: not called CC=2 SCAR=17.9
Pod documented
211 0 my $Self = shift;
212 0 my $path = shift;
213 0 my $basepath = $Self->{basepath};
214 0 ▶ return $path if $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 0 0 0
215 0 return "$basepath/$path";
216 }
217  
218 =head2 $Self->shortpath($fullpath)
219  
220 Convert from a full path to a relative path
221  
222 e.g
223 my $path = $Dav->fullpath('/dav/calendars/user/foo/Default');
224 ## Default
225  
226 NOTE: if the full path is outside the basepath of the object, it
227 will be unchanged.
228  
229 my $path = $Dav->fullpath('/dav/calendars/user/bar/Default');
230 ## /dav/calendars/user/bar/Default
231  
232 =cut
233  
234 0 ▶sub shortpath {
Subroutine shortpath: not called CC=2 SCAR=17.9
Pod documented
235 0 my $Self = shift;
236 0 my $origpath = shift;
237 0 my $basepath = $Self->{basepath};
238 0 my $path = $origpath;
239 0 $path =~ s{^$basepath/?}{};
240 0 ▶ return ($path eq '' ? $origpath : $path);
Branch: 1 branch
BranchTrueFalseTotal
$path eq '' ? : 0 0 0
241 }
242  
243 =head2 $Self->Request($method, $path, $content, %headers)
244  
245 The whole point of the module! Perform a DAV request against the
246 endpoint, returning the response as a parsed hash.
247  
248 method: http method, i.e. GET, PROPFIND, MKCOL, DELETE, etc
249  
250 path: relative to base url. With a leading slash, relative to
251 server root, i.e. "Default/", "/dav/calendars/user/foo/Default".
252  
253 content: if the method takes a body, raw bytes to send
254  
255 headers: additional headers to add to request, i.e (Depth => 1)
256  
257 In the event of timeout or most HTTP errors, an exception will be raised.
258  
259 =head2 $Self->SafeRequest($method, $path, $content, %headers)
260  
261 my $Result = $DAVTalk->SafeRequest($method, $path, $content, %headers)
262  
263 This is mostly Request, but it won't throw an exception on most errors.
264  
265 Instead, it always returns a reference to a hash with these entries:
266  
267 payload - a structured representation of the DAV response, if possible;
268 this is the same thing that the ->Request method returns
269 http_response - the underlying HTTP response from HTTP::Tiny
270 error - a string describing the error condition, if any
271  
272 The C<Request> method uses C<SafeRequest> under the hood, converting error
273 results into exceptions.
274  
275 =cut
276  
277 0 ▶sub SafeRequest {
Subroutine SafeRequest: not called CC=26 SCAR=65.5
Pod documented
278 0 my ($Self, $Method, $Path, $Content, %Headers) = @_;
279  
280 # setup request {{{
281  
282 0 ▶ $Content = '' unless defined $Content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Content 0 0 0
283 0 my $Bytes = encode('UTF-8', $Content);
284  
285 0 my $ua = $Self->ua();
286  
287 0 ▶ $Headers{'Content-Type'} //= 'application/xml; charset=utf-8';
Condition0%0/2
$Headers{"Content-Type"} //= "application/xml; charset=utf-8"
l!l
00
MC/DC0%0/1
$Headers{"Content-Type"} //= "application/xml; charset=utf-8"
$Headers{"Content-Type"}
Truth table0%0/2
A
A= $Headers{"Content-Type"}
Aresult
11
00
288  
289 0 ▶ if ($Self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"user"}) 0 0 0
290 0 $Headers{'Authorization'} = $Self->auth_header();
291 }
292  
293 0 ▶ %Headers = (%Headers, %{$Self->{headers}}) if $Self->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if $Self->{"headers"} 0 0 0
294  
295 # XXX - Accept-Encoding for gzip, etc?
296  
297 # }}}
298  
299 # send request {{{
300  
301 0 my $URI = $Self->request_url($Path);
302  
303 0 my $Response = $ua->request($Method, $URI, {
304 headers => \%Headers,
305 content => $Bytes,
306 });
307  
308 0 ▶ if ($Response->{status} == '599' and $Response->{content} =~ m/timed out/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i) 0 0 0
Condition0%0/3
$Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i
!ll&&!rl&&r
000
MC/DC0%0/2
$Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i
$Response->{"status"} == 599 $Response->{"content"} =~ /timed out/i
Truth table0%0/3
A and B
A= $Response->{"status"} == 599B= $Response->{"content"} =~ /timed out/i
ABresult
0X0
100
111
309 return {
310 0 http_response => $Response,
311 error => "Error with $Method for $URI (504, Gateway Timeout)",
312 };
313 }
314  
315 0 my $count = 0;
316 0 ▶ while ($Response->{status} =~ m{^30[1278]} and (++$count < 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->{"status"} =~ /^30[1278]/ and ++$count < 10) 0 0 0
Condition0%0/3
$Response->{"status"} =~ /^30[1278]/ and ++$count < 10
!ll&&!rl&&r
000
MC/DC0%0/2
$Response->{"status"} =~ /^30[1278]/ and ++$count < 10
$Response->{"status"} =~ /^30[1278]/ ++$count < 10
Truth table0%0/3
A and B
A= $Response->{"status"} =~ /^30[1278]/B= ++$count < 10
ABresult
0X0
100
111
317 0 my $location = URI->new_abs($Response->{headers}{location}, $URI);
318 0 ▶ if ($ENV{DEBUGDAV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUGDAV"}) 0 0 0
319 0 warn "******** REDIRECT ($count) $Response->{status} to $location\n";
320 }
321  
322 0 $Response = $ua->request($Method, $location, {
323 headers => \%Headers,
324 content => $Bytes,
325 });
326  
327 0 ▶ if ($Response->{status} == '599' and $Response->{content} =~ m/timed out/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i) 0 0 0
Condition0%0/3
$Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i
!ll&&!rl&&r
000
MC/DC0%0/2
$Response->{"status"} == 599 and $Response->{"content"} =~ /timed out/i
$Response->{"status"} == 599 $Response->{"content"} =~ /timed out/i
Truth table0%0/3
A and B
A= $Response->{"status"} == 599B= $Response->{"content"} =~ /timed out/i
ABresult
0X0
100
111
328 return {
329 0 http_response => $Response,
330 error => "Error with $Method for $location (504, Gateway Timeout)",
331 };
332 }
333 }
334  
335 # one is enough
336  
337 0 ▶ my $ResponseContent = $Response->{content} || '';
Condition0%0/2
$Response->{'content'} || ''
l!l
00
MC/DC0%0/1
$Response->{'content'} || ''
$Response->{'content'}
Truth table0%0/2
A
A= $Response->{'content'}
Aresult
11
00
338  
339 0 ▶ if ($ENV{DEBUGDAV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUGDAV"}) 0 0 0
340 0 warn "<<<<<<<< $Method $URI HTTP/1.1\n$Bytes\n" .
341 ">>>>>>>> $Response->{protocol} $Response->{status} $Response->{reason}\n$ResponseContent\n" .
342 "========\n\n";
343 }
344  
345 0 ▶ if ($Method eq 'REPORT' && $Response->{status} == 403) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Method eq "REPORT" and $Response->{"status"} == 403) 0 0 0
Condition0%0/3
$Method eq "REPORT" and $Response->{"status"} == 403
!ll&&!rl&&r
000
MC/DC0%0/2
$Method eq "REPORT" and $Response->{"status"} == 403
$Method eq "REPORT" $Response->{"status"} == 403
Truth table0%0/3
A and B
A= $Method eq "REPORT"B= $Response->{"status"} == 403
ABresult
0X0
100
111
346 # maybe invalid sync token, need to return that fact
347 0 my $Xml = xmlToHash($ResponseContent);
348 0 ▶ if (exists $Xml->{"{DAV:}valid-sync-token"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Xml->{"{DAV:}valid-sync-token"}) 0 0 0
349 # This weird "return error in payload" behavior is legacy behavior, and
350 # quite possibly should be changed. It's the only place we returned an
351 # "error" in the hashref from Request before writing SafeRequest.
352 # -- rjbs, 2026-02-11
353 return {
354 0 http_response => $Response,
355 payload => { error => "valid-sync-token" },
356 };
357 }
358 }
359  
360 0 ▶ unless ($Response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Response->{"success"}) 0 0 0
361 return {
362 0 http_response => $Response,
363 error => "ERROR WITH REQUEST\n"
364 . "<<<<<<<< $Method $URI HTTP/1.1\n$Bytes\n"
365 . ">>>>>>>> $Response->{protocol} $Response->{status} $Response->{reason}\n$ResponseContent\n"
366 . "========\n\n",
367 };
368 }
369  
370 0 ▶ if ((grep { $Method eq $_ } qw{GET DELETE}) or ($Response->{status} != 207) or (not $ResponseContent)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$Method eq $_;} "GET", "DELETE" or $Response->{"status"} != 207 or not $ResponseContent) 0 0 0
Condition0%0/6
grep {$Method eq $_;} "GET", "DELETE" or $Response->{"status"} != 207 or not $ResponseContent
l!l&&r!l&&!r
000
grep {$Method eq $_;} "GET", "DELETE" or $Response->{"status"} != 207
l!l&&r!l&&!r
000
MC/DC0%0/3
grep {$Method eq $_;} "GET", "DELETE" or $Response->{"status"} != 207 or not $ResponseContent
grep {$Method eq $_;} "GET", "DELETE" $Response->{"status"} != 207 not $ResponseContent
Truth table0%0/4
A or B or not(C)
A= grep {$Method eq $_;} "GET", "DELETE"B= $Response->{"status"} != 207C= not $ResponseContent
ABCresult
1XX1
01X1
0011
0000
371 return {
372 0 http_response => $Response,
373 payload => { content => $ResponseContent },
374 };
375 }
376  
377 # }}}
378  
379 # parse XML response {{{
380 0 my $Xml = xmlToHash($ResponseContent);
381  
382 # Normalise XML
383  
384 0 ▶ if (exists($Xml->{"{DAV:}response"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Xml->{"{DAV:}response"}) 0 0 0
385 0 ▶ if (ref($Xml->{"{DAV:}response"}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Xml->{"{DAV:}response"} ne "ARRAY") 0 0 0
386 0 $Xml->{"{DAV:}response"} = [ $Xml->{"{DAV:}response"} ];
387 }
388  
389 0 foreach my $Response (@{$Xml->{"{DAV:}response"}}) {
390 0 ▶ if (exists($Response->{"{DAV:}propstat"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Response->{"{DAV:}propstat"}) 0 0 0
391 0 ▶ unless (ref($Response->{"{DAV:}propstat"}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Response->{"{DAV:}propstat"} eq "ARRAY") 0 0 0
392 0 $Response->{"{DAV:}propstat"} = [$Response->{"{DAV:}propstat"}];
393 }
394 }
395 }
396 }
397  
398 return {
399 0 http_response => $Response,
400 payload => $Xml,
401 };
402  
403 # }}}
404 }
405  
406 0 ▶sub Request {
Subroutine Request: not called CC=2 SCAR=17.9
Pod documented
407 0 my ($Self, @Rest) = @_;
408 0 my ($Struct) = $Self->SafeRequest(@Rest);
409  
410 0 ▶ if ($Struct->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Struct->{"error"}) 0 0 0
411 0 confess $Struct->{error};
412 }
413  
414 0 return $Struct->{payload};
415 }
416  
417 =head2 $Self->GetProps($Path, @Props)
418  
419 perform a propfind on a particular path and get the properties back
420  
421 =cut
422  
423 0 ▶sub GetProps {
Subroutine GetProps: not called CC=2 SCAR=17.9
Pod documented
424 0 my ($Self, $Path, @Props) = @_;
425 0 my @res = $Self->GetPropsArray($Path, @Props);
426 0 ▶ return wantarray ? map { $_->[0] } @res : $res[0][0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
427 }
428  
429 =head2 $Self->GetPropsArray($Path, @Props)
430  
431 perform a propfind on a particular path and get the properties back
432 as an array of one or more items
433  
434 =cut
435  
436 0 ▶sub GetPropsArray {
Subroutine GetPropsArray: not called CC=15 SCAR=54.8
Pod documented
437 0 my ($Self, $Path, @Props) = @_;
438  
439 # Fetch one or more properties.
440 # Use [ 'prop', 'sub', 'item' ] to dig into result structure
441  
442 0 my $NS_D = $Self->ns('D');
443  
444 my $Response = $Self->Request(
445 'PROPFIND',
446 $Path,
447 x('D:propfind', $Self->NS(),
448 x('D:prop',
449 0 ▶ map { ref $_ ? x($_->[0]): x($_) } @Props,
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
450 ),
451 ),
452 Depth => 0,
453 );
454  
455 0 my @Results;
456 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
457 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}propstat"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
458 0 ▶ my $PropData = $Propstat->{"{$NS_D}prop"} || next;
Condition0%0/2
$$Propstat{"{$NS_D}prop"} || (next)
l!l
00
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"} || (next)
$$Propstat{"{$NS_D}prop"}
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}
Aresult
11
00
459 0 for my $Prop (@Props) {
460 0 my @Values = ($PropData);
461  
462 # Array ref means we need to git through structure
463 0 ▶ foreach my $Key (ref $Prop ? @$Prop : $Prop) {
Branch: 1 branch
BranchTrueFalseTotal
ref $Prop ? : 0 0 0
464 0 my @New;
465 0 foreach my $Result (@Values) {
466 0 ▶ if ($Key =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Key =~ /:/) { } 0 0 0
467 0 my ($N, $P) = split /:/, $Key;
468 0 my $NS = $Self->ns($N);
469 0 $Result = $Result->{"{$NS}$P"};
470 } else {
471 0 $Result = $Result->{$Key};
472 }
473 0 ▶ if (ref($Result) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $Result eq 'ARRAY') { } 0 0 0
elsif (defined $Result) { } 0 0 0
474 0 push @New, @$Result;
475 }
476 elsif (defined $Result) {
477 0 push @New, $Result;
478 }
479 }
480 0 @Values = @New;
481 }
482  
483 0 push @Results, [ map { $_->{content} } @Values ];
484 }
485 }
486 }
487  
488 0 ▶ return wantarray ? @Results : $Results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
489 }
490  
491 =head2 $Self->GetCurrentUserPrincipal()
492 =head2 $class->GetCurrentUserPrincipal(%Args)
493  
494 Can be called with the same args as new() as a class method, or
495 on an existing object. Either way it will use the .well-known
496 URI to find the path to the current-user-principal.
497  
498 Returns a string with the path.
499  
500 =cut
501  
502 0 ▶sub GetCurrentUserPrincipal {
Subroutine GetCurrentUserPrincipal: not called CC=6 SCAR=37.4
Pod documented
503 0 my ($Class, %Args) = @_;
504  
505 0 ▶ if (ref $Class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Class) 0 0 0
506 0 %Args = %{$Class};
507 0 $Class = ref $Class;
508 }
509  
510 0 ▶ my $OriginalURL = $Args{url} || '';
Condition0%0/2
$Args{'url'} || ''
l!l
00
MC/DC0%0/1
$Args{'url'} || ''
$Args{'url'}
Truth table0%0/2
A
A= $Args{'url'}
Aresult
11
00
511 0 my $Self = $Class->new(%Args);
512 0 my $NS_D = $Self->ns('D');
513 0 my @BasePath = split '/', $Self->{basepath};
514  
515 0 ▶ @BasePath = ('', ".well-known/$Args{wellknown}") unless @BasePath;
Branch: 1 branch
BranchTrueFalseTotal
unless @BasePath 0 0 0
516  
517 0 PRINCIPAL: while(1) {
518 0 $Self->SetURL(join '/', @BasePath);
519  
520 0 ▶ if (my $Principal = $Self->GetProps('', [ 'D:current-user-principal', 'D:href' ])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Principal = $Self->GetProps("", ["D:current-user-principal", "D:href"])) 0 0 0
521 0 $Self->SetURL(uri_unescape($Principal));
522 0 return $Self->{url};
523 }
524  
525 0 pop @BasePath;
526 0 ▶ last unless @BasePath;
Branch: 1 branch
BranchTrueFalseTotal
unless @BasePath 0 0 0
527 }
528  
529 0 croak "Error finding current user principal at '$OriginalURL'";
530 }
531  
532 =head2 $Self->GetHomeSet
533 =head2 $class->GetHomeSet(%Args)
534  
535 Can be called with the same args as new() as a class method, or
536 on an existing object. Either way it assumes that the created
537 object has a 'url' parameter pointing at the current user principal
538 URL (see GetCurrentUserPrincipal above)
539  
540 Returns a string with the path to the home set.
541  
542 =cut
543  
544 0 ▶sub GetHomeSet {
Subroutine GetHomeSet: not called CC=4 SCAR=30.0
Pod documented
545 0 my ($Class, %Args) = @_;
546  
547 0 ▶ if (ref $Class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Class) 0 0 0
548 0 %Args = %{$Class};
549 0 $Class = ref $Class;
550 }
551  
552 0 ▶ my $OriginalURL = $Args{url} || '';
Condition0%0/2
$Args{'url'} || ''
l!l
00
MC/DC0%0/1
$Args{'url'} || ''
$Args{'url'}
Truth table0%0/2
A
A= $Args{'url'}
Aresult
11
00
553 0 my $Self = $Class->new(%Args);
554 0 my $NS_D = $Self->ns('D');
555 0 my $NS_HS = $Self->ns($Args{homesetns});
556 0 my $HomeSet = $Args{homeset};
557  
558 0 ▶ if (my $Homeset = $Self->GetProps('', [ "$Args{homesetns}:$HomeSet", 'D:href' ])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Homeset = $Self->GetProps("", [qq[$Args{"homesetns"}:$HomeSet], "D:href"])) 0 0 0
559 0 $Self->SetURL(uri_unescape($Homeset));
560 0 return $Self->{url};
561 }
562  
563 0 croak "Error finding $HomeSet home set at '$OriginalURL'";
564 }
565  
566 =head2 $Self->genuuid()
567  
568 Helper to generate a uuid string. Returns a UUID, e.g.
569  
570 my $uuid = $DAVTalk->genuuid(); # 9b9d68af-ad13-46b8-b7ab-30ab70da14ac
571  
572 =cut
573  
574 0 ▶sub genuuid {
Subroutine genuuid: not called CC=1 SCAR=6.9
Pod documented
575 0 my $Self = shift;
576 0 return "$uuid";
577 }
578  
579 =head2 $Self->auth_header()
580  
581 Generate the authentication header to use on requests:
582  
583 e.g:
584  
585 $Headers{'Authorization'} = $Self->auth_header();
586  
587 =cut
588  
589 0 ▶sub auth_header {
Subroutine auth_header: not called CC=3 SCAR=24.8
Pod documented
590 0 my $Self = shift;
591  
592 0 ▶ if ($Self->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"password"}) 0 0 0
593 0 return 'Basic ' . encode_base64("$Self->{user}:$Self->{password}", '');
594 }
595  
596 0 ▶ if ($Self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"access_token"}) 0 0 0
597 0 return "Bearer $Self->{access_token}";
598 }
599  
600 0 croak "Need a method to authenticate user (password or access_token)";
601 }
602  
603 =head2 $Self->request_url()
604  
605 Generate the authentication header to use on requests:
606  
607 e.g:
608  
609 $Headers{'Authorization'} = $Self->auth_header();
610  
611 =cut
612  
613 0 ▶sub request_url {
Subroutine request_url: not called CC=4 SCAR=30.0
Pod documented
614 0 my $Self = shift;
615 0 my $Path = shift;
616  
617 0 my $URL = $Self->{url};
618  
619 # If a reference, assume absolute
620 0 ▶ if (ref $Path) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Path) 0 0 0
621 0 ($URL, $Path) = $$Path =~ m{(^https?://[^/]+)(.*)$};
622 }
623  
624 0 ▶ if ($Path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Path) 0 0 0
625 0 $Path = join "/", map { uri_escape $_ } split m{/}, $Path, -1;
626 0 ▶ if ($Path =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Path =~ m[^/]) { } 0 0 0
627 0 $URL =~ s{(^https?://[^/]+)(.*)}{$1$Path};
628 }
629 else {
630 0 $URL =~ s{/$}{};
631 0 $URL .= "/$Path";
632 }
633 }
634  
635 0 return $URL;
636 }
637  
638 =head2 $Self->NS()
639  
640 Returns a hashref of the 'xmlns:shortname' => 'full namespace' items for use in XML::Spice body generation, e.g.
641  
642 $DAVTalk->Request(
643 'MKCALENDAR',
644 "$calendarId/",
645 x('C:mkcalendar', $Self->NS(),
646 x('D:set',
647 x('D:prop', @Properties),
648 ),
649 ),
650 );
651  
652 # { 'xmlns:C' => 'urn:ietf:params:xml:ns:caldav', 'xmlns:D' => 'DAV:' }
653  
654 =cut
655  
656 0 ▶sub NS {
Subroutine NS: not called CC=1 SCAR=6.9
Pod documented
657 0 my $Self = shift;
658  
659 return {
660 0 map { ( "xmlns:$_" => $Self->ns($_) ) }
661 $Self->ns(),
662 };
663 }
664  
665  
666 =head2 $Self->ns($key, $value)
667  
668 Get or set namespace aliases, e.g
669  
670 $Self->ns(C => 'urn:ietf:params:xml:ns:caldav');
671 my $NS_C = $Self->ns('C'); # urn:ietf:params:xml:ns:caldav
672  
673 =cut
674  
675 0 ▶sub ns {
Subroutine ns: not called CC=3 SCAR=24.8
Pod documented
676 0 my $Self = shift;
677  
678 # case: keys
679 0 ▶ return keys %{$Self->{ns}} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
680  
681 0 my $key = shift;
682 # case read one
683 0 ▶ return $Self->{ns}{$key} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
684  
685 # case write
686 0 my $prev = $Self->{ns}{$key};
687 0 $Self->{ns}{$key} = shift;
688 0 return $prev;
689 }
690  
691 =head2 function2
692  
693 =cut
694  
695 =head1 AUTHOR
696  
697 Bron Gondwana, C<< <brong at cpan.org> >>
698  
699 =head1 BUGS
700  
701 Please report any bugs or feature requests to C<bug-net-davtalk at rt.cpan.org>, or through
702 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DAVTalk>. I will be notified, and then you'll
703 automatically be notified of progress on your bug as I make changes.
704  
705  
706  
707  
708 =head1 SUPPORT
709  
710 You can find documentation for this module with the perldoc command.
711  
712 perldoc Net::DAVTalk
713  
714  
715 You can also look for information at:
716  
717 =over 4
718  
719 =item * RT: CPAN's request tracker (report bugs here)
720  
721 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DAVTalk>
722  
723 =item * AnnoCPAN: Annotated CPAN documentation
724  
725 L<http://annocpan.org/dist/Net-DAVTalk>
726  
727 =item * CPAN Ratings
728  
729 L<http://cpanratings.perl.org/d/Net-DAVTalk>
730  
731 =item * Search CPAN
732  
733 L<http://search.cpan.org/dist/Net-DAVTalk/>
734  
735 =back
736  
737  
738 =head1 ACKNOWLEDGEMENTS
739  
740  
741 =head1 LICENSE AND COPYRIGHT
742  
743 Copyright 2015-2026 Fastmail Pty Ltd.
744  
745 This program is free software; you can redistribute it and/or modify it
746 under the same terms as Perl itself; that is, either the GNU General
747 Public License as published by the Free Software Foundation (version 1,
748 or at your option any later version), or the Artistic License.
749  
750 See L<http://dev.perl.org/licenses/> and the LICENSE file included with
751 this distribution for more information.
752  
753 =cut
754  
755 2 1; # End of Net::DAVTalk
↑ Summary DAVTalk/XMLParser.pm »