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