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 29.4% 5 / 17 Podpod 0.0% 0 / 15 totaltotal 25.2% 58 / 230 CC 34 SCAR 30.2
CC
34
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 sub rearrange {
15 1 ▶ my($order,@param) = @_;
Subroutine rearrange: called CC=12 SCAR=27.9
Pod undocumented
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 sub use_named_parameters {
63 0 ▶ my($use_named) = shift;
Subroutine use_named_parameters: not called CC=2 SCAR=17.9
Pod undocumented
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 sub make_attributes {
72 0 ▶ my($attr) = @_;
Subroutine make_attributes: not called CC=6 SCAR=37.4
Pod undocumented
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 sub bad {
86 0 ▶ my($str) = @_;
Subroutine bad: not called CC=1 SCAR=6.9
Pod undocumented
87 0 print STDERR "Error: $str\n";
88 0 exit;
89 }
90  
91 sub bad_node {
92 0 ▶ my($node,$str) = @_;
Subroutine bad_node: not called CC=2 SCAR=17.9
Pod undocumented
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 sub get_only_cdata {
105 0 ▶ my($node) = @_;
Subroutine get_only_cdata: not called CC=3 SCAR=24.8
Pod undocumented
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 sub get_elements_by_tag_name {
129 0 ▶ my ($node, $elemname ) = @_;
Subroutine get_elements_by_tag_name: not called CC=4 SCAR=30.0
Pod undocumented
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 sub get_only_element {
153 0 ▶ my($node,$elemname) = @_;
Subroutine get_only_element: not called CC=7 SCAR=40.3
Pod undocumented
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 sub XML_remove_namespace {
195 #print "XML: $_[0] -> ";
196 0 ▶ $_[0] =~ s/.*?:(.*)/$1/g;
Subroutine XML_remove_namespace: not called CC=1 SCAR=6.9
Pod undocumented
197 #$_[0] =~ s/(.*?)\s.*/$1/g;
198 #print "$_[0]\n";
199 0 return $_[0];
200 }
201  
202 ###########################################################################
203 sub make_uri {
204 8 ▶ my $uri = shift;
Subroutine make_uri: called CC=2 SCAR=7.1
Pod undocumented
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 sub make_trail_slash {
219 0 ▶ my ($uri) = @_;
Subroutine make_trail_slash: not called CC=1 SCAR=6.9
Pod undocumented
220 0 $uri =~ s{/*$}{}g;
221 0 $uri .= '/';
222 0 return $uri;
223 }
224  
225 sub compare_uris {
226 4 ▶ my ($uri1,$uri2) = @_;
Subroutine compare_uris: called CC=2 SCAR=6.9
Pod undocumented
227  
228 4 for ($uri1, $uri2) {
229 8 $_ = make_uri($_);
230 8 s{/$}{};
231 8 s{(%[0-9a-fA-F][0-9a-fA-F])}{lc $1}eg;
232 }
233  
234 4 return $uri1 eq $uri2;
235 }
236  
237 # This subroutine takes a URI and gets the last portion
238 # of it: the filename.
239 # e.g. /dir1/dir2/file.txt => file.txt
240 # /dir1/dir2/ => dir2
241 # / => undef
242 sub get_leafname {
243 0 ▶ my($url) = shift;
Subroutine get_leafname: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my $leaf;
245 0 ($url,$leaf) = &split_leaf($url);
246 0 return $leaf;
247 }
248  
249 # This subroutine takes a URI and splits the leaf from the path.
250 # It returns both.
251 # of it: the filename.
252 # e.g. /dir1/dir2/file.txt => file.txt
253 # /dir1/dir2/ => dir2
254 # / => undef
255 sub split_leaf {
256 0 ▶ my($url) = shift;
Subroutine split_leaf: not called CC=2 SCAR=17.9
Pod undocumented
257 0 $url =~ s#[\/\\]$##; #Remove trailing slashes.
258 0 $url = HTTP::DAV::Utils::make_uri($url);
259  
260 # Remove the leaf from the path.
261 0 my $path = $url->path_query();
262 0 my @path = split(/[\/\\]+/,$path);
263 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
264 0 $path = join('/',@path);
265  
266 #Now put the path back into the URL.
267 0 $url->path_query($path);
268  
269 0 return ($url,$leaf);
270 }
271  
272 # Turns a file-oriented glob
273 # into a regular expression.
274 # BTW, I recommend you eval any regex command you use on
275 # this outputted regex value.
276 # If somebody types uses an incorrect glob and you try to /$regex/ it
277 # then perl will bomb with a fatal regex error.
278 # For instance, /file[ab.txt/ would bomb.
279 sub glob2regex {
280 0 ▶ my($f) = @_;
Subroutine glob2regex: not called CC=2 SCAR=17.9
Pod undocumented
281 # Turn the leafname glob into a regex.
282 # Substitute \ for \\
283 # Substitute . for \.
284 # Substitute * for .*
285 # Substitute ? for .
286 # No need to substitute [...]
287 0 $f =~ s/\\/\\\\/g;
288 0 $f =~ s/\./\\./g;
289 0 $f =~ s/\*/.*/g;
290 0 $f =~ s/\?/./g;
291 0 ▶ print "Glob regex becomes $f\n" if $HTTP::DAV::DEBUG>1;
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::DAV::DEBUG > 1 0 0 0
292 0 return $f;
293 }
294  
295 5 1;
« DAV/Response.pm ↑ Summary DAV/ResourceList.pm »