DAV/Utils.pm

filter: Statementstmt 32.5% 42 / 129 Branchbran 18.7% 9 / 48 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 33.3% 6 / 18 Podpod 0.0% 0 / 16 totaltotal 25.4% 59 / 232 CC 33 SCAR 30.1
CC
33
Cov
28%
get_only_element
56.0
make_attributes
42.0
get_elements_by_tag_name
20.0
CRAP 20.4
« DAV/Response.pm ↑ Summary DAV/ResourceList.pm »
1 package HTTP::DAV::Utils;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 $VERSION = '0.11';
7  
8 ###########################################################################
9 # Borrowed from Lincoln Stein's CGI.pm
10 # Smart rearrangement of parameters to allow named parameter
11 # calling. We do the rearangement if:
12 # 1. The first parameter begins with a -
13 # 2. The use_named_parameters() method returns true
14 1 ▶sub rearrange {
Subroutine rearrange: called CC=12 SCAR=27.9
Pod undocumented
15 1 my($order,@param) = @_;
16 1 ▶ return () unless @param;
Branch: 1 branch
BranchTrueFalseTotal
unless @param 0 1 1
17  
18 # IF the user has passed a hashref instead of a hash then flatten it out.
19 1 ▶ if (ref($param[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param[0] eq 'HASH') { } 0 1 1
20 0 @param = %{$param[0]};
21 } else {
22 # If the user has specified that they will be explicitly
23 # using named_parameters (by setting &use_named_parameters(1))
24 # or the first parameter starts with a -, then continue.
25 # Otherwise just return the parameters as they were given to us.
26 return @param
27 1 ▶ unless (defined($param[0]) && substr($param[0],0,1) eq '-')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param[0] and substr($param[0], 0, 1) eq "-" or &use_named_parameters() 0 1 1
Condition33%2/6
defined $param[0] and substr($param[0], 0, 1) eq "-" or &use_named_parameters()
l!l&&r!l&&!r
100
defined $param[0] and substr($param[0], 0, 1) eq "-"
!ll&&!rl&&r
001
MC/DC0%0/3
defined $param[0] and substr($param[0], 0, 1) eq "-" or &use_named_parameters()
defined $param[0] substr($param[0], 0, 1) eq "-" &use_named_parameters()
Truth table20%1/5
A and B or C
A= defined $param[0]B= substr($param[0], 0, 1) eq "-"C= &use_named_parameters()
ABCresult
11X1
0X11
1011
0X00
1000
28 || &use_named_parameters();
29 }
30  
31 # map parameters into positional indices
32 1 my ($i,%pos);
33 1 $i = 0;
34 1 foreach (@$order) {
35 2 ▶ foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{$_} = $i; }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 2 2
36 2 $i++;
37 }
38  
39 1 my (@result,%leftover);
40 1 $#result = $#$order; # preextend
41 1 ▶ while (@param) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param) 1 1 2
42 1 my $key = uc(shift(@param));
43 1 $key =~ s/^\-//;
44 1 ▶ if (exists $pos{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pos{$key}) { } 1 0 1
45 1 $result[$pos{$key}] = shift(@param);
46 } else {
47 0 $leftover{$key} = shift(@param);
48 }
49 }
50  
51 1 ▶ push (@result,&make_attributes(\%leftover)) if %leftover;
Branch: 1 branch
BranchTrueFalseTotal
if %leftover 0 1 1
52 1 @result;
53 }
54  
55 #### Method: use_named_parameters
56 # Borrowed from Lincoln Stein's CGI.pm
57 # Force DAV.pm to use named parameter-style method calls
58 # rather than positional parameters. The same effect
59 # will happen automatically if the first parameter
60 # begins with a -.
61 5 my $named=0;
62 0 ▶sub use_named_parameters {
Subroutine use_named_parameters: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my($use_named) = shift;
64 0 ▶ return $named unless defined ($use_named);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $use_named 0 0 0
65  
66 # stupidity to avoid annoying warnings
67 0 return $named = $use_named;
68 }
69  
70 # Borrowed from Lincoln Stein's CGI.pm
71 0 ▶sub make_attributes {
Subroutine make_attributes: not called CC=6 SCAR=37.4
Pod undocumented
72 0 my($attr) = @_;
73 0 ▶ return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $attr and ref $attr and ref $attr eq "HASH" 0 0 0
Condition0%0/6
$attr and ref $attr and ref $attr eq "HASH"
!ll&&!rl&&r
000
$attr and ref $attr
!ll&&!rl&&r
000
MC/DC0%0/3
$attr and ref $attr and ref $attr eq "HASH"
$attr ref $attr ref $attr eq "HASH"
Truth table0%0/4
A and B and C
A= $attrB= ref $attrC= ref $attr eq "HASH"
ABCresult
0XX0
10X0
1100
1111
74 0 my(@att);
75 0 foreach (keys %{$attr}) {
76 0 my($key) = $_;
77 0 $key=~s/^\-//; # get rid of initial - if present
78 0 $key=~tr/a-z_/A-Z-/; # parameters are upper case, use dashes
79 0 ▶ push(@att,defined($attr->{$_}) ? qq/$key="$attr->{$_}"/ : qq/$key/);
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{$_} ? : 0 0 0
80 }
81 0 return @att;
82 }
83  
84 ###########################################################################
85 0 ▶sub bad {
Subroutine bad: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my($str) = @_;
87 0 print STDERR "Error: $str\n";
88 0 exit;
89 }
90  
91 0 ▶sub bad_node {
Subroutine bad_node: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my($node,$str) = @_;
93 0 print STDERR "XML error in " . $node->getNodeName . ": $str";
94 0 print STDERR "\n";
95 0 print STDERR "DUMP:\n";
96 0 ▶ print STDERR $node->toString if $node;
Branch: 1 branch
BranchTrueFalseTotal
if $node 0 0 0
97 0 exit;
98 }
99  
100 ###########################################################################
101 # This method searches for any text-based data in the children of
102 # the node supplied. It will croak if the node has anything other
103 # than text values (such as Elements or Comments).
104 0 ▶sub get_only_cdata {
Subroutine get_only_cdata: not called CC=3 SCAR=24.8
Pod undocumented
105 0 my($node) = @_;
106 0 my $return_cdata = "";
107 0 my $nodes = $node->getChildNodes();
108 0 my $n = $nodes->getLength;
109 0 ▶ for (my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
110 0 my $node = $nodes->item($i);
111 0 ▶ if ( $node->getNodeTypeName eq "TEXT_NODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->getNodeTypeName eq 'TEXT_NODE') { } 0 0 0
112 0 $return_cdata .= $node->getNodeValue;
113 } else {
114 #bad_node($node, "node has non TEXT children");
115 }
116 }
117  
118 0 return $return_cdata;
119 }
120  
121  
122 # This is a sibling to the XML::DOM's getElementsByTagName().
123 # The main difference here is that it ignores the namespace
124 # component of the element. This was done because it
125 # Takes a node and returns a list of nodes.
126 # Note that the real getElementsByTagName allows you to
127 # specify recurse or not. This routine doesn't allow recurse.
128 0 ▶sub get_elements_by_tag_name {
Subroutine get_elements_by_tag_name: not called CC=4 SCAR=30.0
Pod undocumented
129 0 my ($node, $elemname ) = @_;
130  
131 0 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
132  
133 0 my @return_nodes;
134  
135 # This is gruesome. Because we don't yet support namespaces, it
136 # just lops off the first half of the Element name
137 0 $elemname =~ s/.*?:(.*)$/$1/g;
138  
139 0 my $nodelist = $node->getChildNodes();
140 0 my $length = $nodelist->getLength();
141 0 ▶ for ( my $i=0; $i < $length; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
142 0 my $node = $nodelist->item($i);
143 # Debian change?
144 0 ▶ if ( $node->getNodeName() =~ /(?:^|:)$elemname$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->getNodeName =~ /(?:^|:)$elemname$/) 0 0 0
145 0 push(@return_nodes,$node);
146 }
147 }
148  
149 0 return @return_nodes;
150 }
151  
152 0 ▶sub get_only_element {
Subroutine get_only_element: not called CC=7 SCAR=40.3
Pod undocumented
153 0 my($node,$elemname) = @_;
154  
155 0 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
156  
157 # Find the one child element of a specific name
158 0 ▶ if ( $elemname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elemname) { } 0 0 0
159  
160 # This is gruesome. Because we don't yet support namespaces, it
161 # just lops off the first half of the Element name.
162 0 $elemname =~ s/.*?:(.*)$/$1/g;
163  
164 #my $nodes = $node->getElementsByTagName($elemname,0);
165 0 my $nodelist = $node->getChildNodes();
166 0 my $length = $nodelist->getLength();
167 0 ▶ for ( my $i=0; $i < $length; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
168 0 my $node = $nodelist->item($i);
169 0 ▶ return $node if $node->getNodeName() =~ /$elemname/;
Branch: 1 branch
BranchTrueFalseTotal
if $node->getNodeName =~ /$elemname/ 0 0 0
170 }
171  
172 # if ( $nodes->getLength > 1 ) {
173 # bad_node($node, "Too many \"$elemname\" in node");
174 # } elsif ( $nodes->getLength < 1 ) {
175 # return;
176 # #bad_node($node, "No node found matching \"$elemname\" in node");
177 # }
178 # return $nodes->item(0);
179  
180 # Just get the first child element.
181 } else {
182 0 my $nodelist = $node->getChildNodes();
183 0 my $length = $nodelist->getLength();
184 0 ▶ for ( my $i=0; $i < $length; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
185 0 my $node = $nodelist->item($i);
186 0 ▶ if ($node->getNodeTypeName eq "ELEMENT_NODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->getNodeTypeName eq "ELEMENT_NODE") 0 0 0
187 0 return $nodelist->item($i);
188 }
189 }
190 }
191 }
192  
193 ###########################################################################
194 0 ▶sub XML_remove_namespace {
Subroutine XML_remove_namespace: not called CC=1 SCAR=6.9
Pod undocumented
195 #print "XML: $_[0] -> ";
196 0 $_[0] =~ s/.*?:(.*)/$1/g;
197 #$_[0] =~ s/(.*?)\s.*/$1/g;
198 #print "$_[0]\n";
199 0 return $_[0];
200 }
201  
202 ###########################################################################
203 8 ▶sub make_uri {
Subroutine make_uri: called CC=2 SCAR=7.1
Pod undocumented
204 8 my $uri = shift;
205 8 ▶ if (ref($uri) =~ /URI/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($uri) =~ /URI/) 0 8 8
206 0 $uri = $uri->as_string;
207 }
208 # Remove double slashes from the url
209 8 $uri = URI->new($uri);
210 8 my $path = $uri->path;
211 8 $path =~ s{//}{/}g;
212 #print "make_uri: $uri->$path\n";
213 8 $uri->path($path);
214 #print "make_uri: $uri\n";
215 8 return $uri;
216 }
217  
218 8 ▶sub make_uri_canonical {
Subroutine make_uri_canonical: called CC=1 SCAR=0.0
Pod undocumented
219 8 $_ = make_uri(shift);
220 8 s{/$}{};
221 8 s{(%[0-9a-fA-F][0-9a-fA-F])}{lc $1}eg;
222 8 return $_;
223 }
224  
225 0 ▶sub make_trail_slash {
Subroutine make_trail_slash: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my ($uri) = @_;
227 0 $uri =~ s{/*$}{}g;
228 0 $uri .= '/';
229 0 return $uri;
230 }
231  
232 4 ▶sub compare_uris {
Subroutine compare_uris: called CC=1 SCAR=0.0
Pod undocumented
233 4 my ($uri1,$uri2) = @_;
234  
235 4 return make_uri_canonical($uri1) eq make_uri_canonical($uri2);
236 }
237  
238 # This subroutine takes a URI and gets the last portion
239 # of it: the filename.
240 # e.g. /dir1/dir2/file.txt => file.txt
241 # /dir1/dir2/ => dir2
242 # / => undef
243 0 ▶sub get_leafname {
Subroutine get_leafname: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my($url) = shift;
245 0 my $leaf;
246 0 ($url,$leaf) = &split_leaf($url);
247 0 return $leaf;
248 }
249  
250 # This subroutine takes a URI and splits the leaf from the path.
251 # It returns both.
252 # of it: the filename.
253 # e.g. /dir1/dir2/file.txt => file.txt
254 # /dir1/dir2/ => dir2
255 # / => undef
256 0 ▶sub split_leaf {
Subroutine split_leaf: not called CC=2 SCAR=17.9
Pod undocumented
257 0 my($url) = shift;
258 0 $url =~ s#[\/\\]$##; #Remove trailing slashes.
259 0 $url = HTTP::DAV::Utils::make_uri($url);
260  
261 # Remove the leaf from the path.
262 0 my $path = $url->path_query();
263 0 my @path = split(/[\/\\]+/,$path);
264 0 ▶ my $leaf = pop @path || "";
Condition0%0/2
pop @path || ''
l!l
00
MC/DC0%0/1
pop @path || ''
pop @path
Truth table0%0/2
A
A= pop @path
Aresult
11
00
265 0 $path = join('/',@path);
266  
267 #Now put the path back into the URL.
268 0 $url->path_query($path);
269  
270 0 return ($url,$leaf);
271 }
272  
273 # Turns a file-oriented glob
274 # into a regular expression.
275 # BTW, I recommend you eval any regex command you use on
276 # this outputted regex value.
277 # If somebody types uses an incorrect glob and you try to /$regex/ it
278 # then perl will bomb with a fatal regex error.
279 # For instance, /file[ab.txt/ would bomb.
280 0 ▶sub glob2regex {
Subroutine glob2regex: not called CC=2 SCAR=17.9
Pod undocumented
281 0 my($f) = @_;
282 # Turn the leafname glob into a regex.
283 # Substitute \ for \\
284 # Substitute . for \.
285 # Substitute * for .*
286 # Substitute ? for .
287 # No need to substitute [...]
288 0 $f =~ s/\\/\\\\/g;
289 0 $f =~ s/\./\\./g;
290 0 $f =~ s/\*/.*/g;
291 0 $f =~ s/\?/./g;
292 0 ▶ print "Glob regex becomes $f\n" if $HTTP::DAV::DEBUG>1;
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::DAV::DEBUG > 1 0 0 0
293 0 return $f;
294 }
295  
296 5 1;
« DAV/Response.pm ↑ Summary DAV/ResourceList.pm »