DAV/Lock.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 286 CC 25 SCAR 31.4
CC
25
Cov
0%
XML_lockdiscovery_parse
72.0
timeout
42.0
get_supportedlock_details
30.0
CRAP 23.2
1 package HTTP::DAV::Lock;
2  
3 use strict;
4 use vars qw($VERSION);
5 use HTTP::DAV::Utils;
6  
7 $VERSION = '0.09';
8  
9 ###########################################################################
10  
11 =head1 NAME
12  
13 HTTP::DAV::Lock - Represents a WebDAV Lock.
14  
15 =head1 SYNOPSIS
16  
17 Need example
18  
19 =head1 DESCRIPTION
20  
21 =cut
22  
23 sub new {
24 my $self = {};
25 bless $self, shift;
26 $self->_init(@_);
27 return $self;
28 }
29  
30 sub _init {
31 my ($self,@p) = @_;
32 my($owned) = HTTP::DAV::Utils::rearrange(['OWNED'],@p);
33 $self->{_owned} = $owned || 0;
34 }
35  
36 ###########################################################################
37 # ACCESSOR METHODS
38  
39 # GET
40 sub get_owner { $_[0]->{_owner}; }
41 sub get_token { $_[0]->{_token}; }
42 sub get_depth { $_[0]->{_depth}; }
43 sub get_timeout { $_[0]->{_timeout}; }
44 sub get_locktoken { $_[0]->{_locktokens}[0]; }
45 sub get_locktokens{ $_[0]->{_locktokens}; }
46  
47 sub set_scope { $_[0]->{_scope} = $_[1]; }
48 sub set_owned { $_[0]->{_owned} = $_[1]; }
49 sub set_type { $_[0]->{_type} = $_[1]; }
50 sub set_owner { $_[0]->{_owner} = $_[1]; }
51 sub set_depth { $_[0]->{_depth} = $_[1]; }
52 sub set_timeout { $_[0]->{_timeout} = $_[1]; }
53 sub set_locktoken {
54 my ($self,$href) = @_;
55 # Remove leading and trailing space from " http://.../..."
56 $href =~ s/^\s*//g; $href =~ s/\s*$//g;
57 # Remove < > from around it available
58 $href =~ s/^<(.*)>$/$1/g;
59  
60 push (@{$self->{_locktokens}}, $href);
61 }
62  
63 # IS
64 sub is_owned { $_[0]->{_owned}; }
65  
66 ###########################################################################
67 # Synopsis:
68 # Full parameters
69 # make_lock_xml (
70 # -owner => (owner|http://mysite/~mypage/)
71 # -timeout => num_of_seconds (e.g. 134123432)
72 # -scope => (exclusive|shared)
73 # -type => (write)
74 # )
75 sub make_lock_xml {
76 my ($self,@p) = @_;
77 my($owner,$timeout,$scope,$type,@other) =
78 HTTP::DAV::Utils::rearrange(['OWNER','TIMEOUT','SCOPE','TYPE'],@p);
79  
80 ####
81 # Create a new XML document
82 # It may look something like this
83 # <?xml version=1.0 encoding="utf-8"?>
84 # <D:lockinfo xmlns:D="DAV:">
85 # <D:lockscope><D:exclusive/></D:lockscope>
86 # <D:locktype><D:write/></D:locktype>
87 # <D:owner>
88 # <D:href>http://mysite/~mypage.html</D:href>
89 # </D:owner>
90 # </D:lockinfo>
91 my $xml_request = qq{<?xml version="1.0" encoding="utf-8"?>\n};
92  
93 $xml_request .= "<D:lockinfo xmlns:D='DAV:'>\n";
94 $xml_request .= "<D:lockscope><D:$scope/></D:lockscope>\n";
95 $xml_request .= "<D:locktype><D:$type/></D:locktype>\n";
96 #$xml_request = <<END;
97 #<?xml version="1.0" encoding="utf-8"?>
98 #<lockinfo xmlns='DAV:'>
99 #<lockscope><$scope/></lockscope>
100 #<locktype><$type/></locktype>
101 ##</lockinfo>
102 #END
103  
104  
105 # If the owner is an HREF then set it into an <D:href> tag
106 # else just enter it as text.
107 my $o = URI->new($owner);
108 if ($o->scheme) {
109 $xml_request .= "<D:owner><D:href>$owner</D:href></D:owner>\n";
110 #$xml_request .= "<owner><href>$owner</href></owner>\n";
111 } elsif ( $owner ) {
112 $xml_request .= "<D:owner>$owner</D:owner>\n";
113 #$xml_request .= "<owner>$owner</owner>\n";
114 }
115  
116 $xml_request .= "</D:lockinfo>\n";
117 #$xml_request .= "</lockinfo>\n";
118
119 return ($xml_request);
120 }
121  
122 ###########################################################################
123 # Synopsis: @locks = XML_lockdiscovery_parse($node);
124 # With this XML node:
125 #<D:lockdiscovery>
126 # <D:activelock>
127 # <D:locktype><D:write/></D:locktype>
128 # <D:lockscope><D:exclusive/></D:lockscope>
129 # <D:depth>0</D:depth>
130 # <D:timeout>Infinite</D:timeout>
131 # <D:owner>pcollins</D:owner>
132 # <D:locktoken>
133 # <D:href>opaquelocktoken:d3ae67b0-1dd1-a5f7-f067587e98e1</D:href>
134 # <D:href>...</D:href>
135 # </D:locktoken>
136 # </D:activelock>
137 #</D:lockdiscovery>
138 #
139 # returns an array of locks (will be more than one in shared locks scenarios)
140  
141 sub XML_lockdiscovery_parse {
142 my ($self,$node_lockdiscovery) = @_;
143 my @found_locks = ();
144  
145 # <!ELEMENT lockdiscovery (activelock)* >
146 my @nodes_activelock= HTTP::DAV::Utils::get_elements_by_tag_name($node_lockdiscovery,"D:activelock");
147  
148 # <!ELEMENT activelock (lockscope, locktype, depth, owner?, timeout?, locktoken?) >
149 foreach my $node_activelock ( @nodes_activelock ) {
150  
151 my $lock = HTTP::DAV::Lock->new();
152 push(@found_locks,$lock);
153
154 my $nodes_lock_params = $node_activelock->getChildNodes();
155 next unless $nodes_lock_params;
156 my $prop_count = $nodes_lock_params->getLength;
157  
158 for (my $prop_num = 0; $prop_num < $prop_count; $prop_num++) {
159 my $node_lock_param = $nodes_lock_params->item($prop_num);
160  
161 # $node_lock_param is one of the following
162 # 1. <!ELEMENT lockscope (exclusive | shared) >
163 # 2. <!ELEMENT locktype (write) >
164 # 3. <!ELEMENT depth (#PCDATA) >
165 # 4. <!ELEMENT owner ANY >
166 # 5. <!ELEMENT timeout (#PCDATA) >
167 # 6. <!ELEMENT locktoken (href+) >
168  
169 my $lock_prop_name = $node_lock_param->getNodeName();
170 $lock_prop_name =~ s/.*:(.*)/$1/g;
171
172 # 1. RFC2518 currently only allows locktype of exclusive or shared
173 if ( $lock_prop_name eq "lockscope" ) {
174 my $node_lock_scope = HTTP::DAV::Utils::get_only_element($node_lock_param);
175 my $lock_scope = $node_lock_scope->getNodeName;
176 $lock_scope =~ s/.*:(.*)/$1/g;
177 $lock->set_scope($lock_scope);
178 }
179
180 # 2. RFC2518 currently only allows locktype of "write"
181 elsif ( $lock_prop_name eq "locktype" ) {
182 my $node_lock_type = HTTP::DAV::Utils::get_only_element($node_lock_param);
183 my $lock_type = $node_lock_type->getNodeName;
184 $lock_type =~ s/.*:(.*)/$1/g;
185 $lock->set_type($lock_type);
186 }
187
188 # 3. RFC2518 allows only depth of 0,1,infinity
189 elsif ( $lock_prop_name eq "depth" ) {
190 my $lock_depth = HTTP::DAV::Utils::get_only_cdata($node_lock_param);
191 $lock->set_depth($lock_depth);
192 }
193
194 # 4. RFC2518 allows anything here.
195 # Patrick: I'm just going to convert the XML to a string
196 elsif ( $lock_prop_name eq "owner" ) {
197 $lock->set_owner( $node_lock_param->getFirstChild->toString );
198 }
199
200 # 5. RFC2518 (Section 9.8) e.g. Timeout: Second-234234 or Timeout: infinity
201 elsif ( $lock_prop_name eq "timeout" ) {
202 my $lock_timeout = HTTP::DAV::Utils::get_only_cdata($node_lock_param);
203 my $timeout = HTTP::DAV::Lock->interpret_timeout($lock_timeout);
204 $lock->set_timeout( $timeout );
205 #if ( $HTTP::DAV::DEBUG ) {
206 # $lock->{ "_timeout_val" } = HTTP::Date::time2str($timeout)
207 #}
208 }
209
210 # 6. RFC2518 allows one or more <href>'s
211 # Push them all into the lock object.
212 elsif ( $lock_prop_name eq "locktoken" ) {
213 my @nodelist_hrefs = HTTP::DAV::Utils::get_elements_by_tag_name($node_lock_param,"D:href");
214 foreach my $node ( @nodelist_hrefs) {
215 my $href_cdata = HTTP::DAV::Utils::get_only_cdata( $node );
216 $lock->set_locktoken( $href_cdata );
217 }
218 }
219  
220 } # Foreach property
221 } # Foreach ActiveLock
222  
223 return @found_locks;
224 }
225  
226 ###########################################################################
227 # Synopsis: $hashref = get_supportedlock_details($node);
228 #<D:supportedlock>
229 # <D:lockentry>
230 # <D:lockscope> <D:exclusive/> </D:lockscope>
231 # <D:locktype> <D:write/> </D:locktype>
232 # </D:lockentry>
233 # <D:lockentry>
234 # <D:lockscope> <D:shared/> </D:lockscope>
235 # <D:locktype> <D:write/> </D:locktype>
236 # </D:lockentry>
237 #</D:supportedlock>
238 #
239 # Returns something similar to:
240 # @supportedlocks' = (
241 # { 'type' => 'write', 'scope' => 'exclusive' },
242 # { 'type' => 'write', 'scope' => 'shared' }
243 # );
244  
245 sub get_supportedlock_details {
246 my ($node_supportedlock) = @_;
247  
248 return unless $node_supportedlock;
249  
250 # Return values
251 my @supportedlocks=();
252  
253 my @nodelist_lockentries = HTTP::DAV::Utils::get_elements_by_tag_name($node_supportedlock,"D:lockentry");
254 foreach my $i ( 0 .. $#nodelist_lockentries ) {
255 my $node_lockentry = $nodelist_lockentries[$i];
256  
257 my $lock_prop_name = $node_lockentry->getNodeName();
258 next unless $lock_prop_name;
259  
260 # RFC2518 currently only allows lockscope of exclusive or shared
261 # <D:lockscope> <D:shared|exclusive/> </D:lockscope>
262 my $node_lockscope=HTTP::DAV::Utils::get_only_element($node_lockentry,"D:lockscope");
263 if ( $node_lockscope ) {
264 my $node_lockscope_param =HTTP::DAV::Utils::get_only_element($node_lockscope);
265 my $lockscope = $node_lockscope_param->getNodeName;
266 $lockscope =~ s/.*:(.*)/$1/g;
267 $supportedlocks[$i]{ "scope" } = $lockscope;
268 }
269  
270 # RFC2518 currently only allows locktype of "write"
271 # <D:locktype> <D:write/> </D:locktype>
272 my $node_locktype = HTTP::DAV::Utils::get_only_element($node_lockentry,"D:locktype");
273 if ( $node_locktype ) {
274 my $node_locktype_param =HTTP::DAV::Utils::get_only_element($node_locktype);
275 my $locktype = $node_locktype_param->getNodeName;
276 $locktype =~ s/.*:(.*)/$1/g;
277 $supportedlocks[$i]{ "type" } = $locktype;
278 }
279 }
280  
281 return \@supportedlocks;
282 }
283  
284  
285 ###########################################################################
286 =item Timeout
287 This parameter can take an absolute or relative timeout.
288 The following forms are all valid for the -timeout field:
289  
290 Timeouts in:
291 300
292 30s 30 seconds from now
293 10m ten minutes from now
294 1h one hour from now
295 1d tomorrow
296 3M in three months
297 10y in ten years time
298 Timeout at:
299 2000-02-31 00:40:33 at the indicated time & date
300 For more time and date formats that are handled see HTTP::Date
301  
302 RFC2518 states that the timeout value MUST NOT be greater
303 than 2^32-1. If this occurs it will simply set the timeout to infinity
304 =cut
305  
306 sub timeout {
307 my ($self,$timeout) = @_;
308 my $timeoutret;
309  
310 return 0 unless $timeout;
311  
312 if ($timeout =~ /^\d+[a-zA-Z]$/ ) {
313 $timeoutret = _timeout_calc($timeout);
314 }
315 elsif ($timeout =~ /infinity/i || $timeout =~ /^\d+$/ ) {
316 $timeoutret = $timeout;
317 }
318 else {
319 my ($epochgmt) = HTTP::Date::str2time($timeout);
320 $timeoutret = $epochgmt - time;
321 }
322  
323 # Timeout value cannot be greater than 2^32-1 as per RFC2518
324 if ( $timeoutret =~ /infinity/i || $timeoutret >= 4294967295 ) {
325 return "Infinite, Second-4294967295 ";
326 }
327 elsif ( $timeoutret <= 0 ) {
328 return 0;
329 } else {
330 return "Second-$timeoutret ";
331 }
332 }
333  
334 ###########################################################################
335 sub interpret_timeout {
336 my ($self,$timeout) = @_;
337  
338 return "Infinite" if $timeout =~ /Infinite/i;
339 return "Infinite" if !defined $timeout || $timeout eq "";
340  
341 if ($timeout =~ /Second\-(\d+)/ ) {
342 return time + $1;
343 } else {
344 HTTP::DAV::Utils::bad("Ugh... can't interpret Timeout value \"timeout: $timeout\"\n");
345 }
346 }
347  
348 ###########################################################################
349 # This internal routine creates an expires time exactly some number of
350 # hours from the current time. It incorporates modifications from
351 # Mark Fisher.
352 # Borrowed from Lincoln Stein's CGI.pm
353  
354 sub _timeout_calc {
355 my($time) = @_;
356 my(%mult) = ('s'=>1,
357 'm'=>60,
358 'h'=>60*60,
359 'd'=>60*60*24,
360 'M'=>60*60*24*30,
361 'y'=>60*60*24*365);
362 # format for time can be in any of the forms...
363 # "180s" -- in 180 seconds
364 # "2m" -- in 2 minutes
365 # "12h" -- in 12 hours
366 # "1d" -- in 1 day
367 # "3M" -- in 3 months
368 # "2y" -- in 2 years
369 # "3m" -- 3 minutes
370 # If you don't supply one of these forms, we assume you are
371 # specifying the date yourself
372 my($offset);
373 if (!$time || (lc($time) eq 'now')) {
374 $offset = 0;
375 } elsif ($time=~/^(\d+|\d*\.\d*)([mhdMy]?)/) {
376 $offset = ($mult{$2} || 1)*$1;
377 } else {
378 return $time;
379 }
380 return $offset;
381 }
382  
383  
384 ###########################################################################
385 =item $r->as_string()
386  
387 Method returning a textual representation of the request.
388 Mainly useful for debugging purposes. It takes no arguments.
389  
390 =cut
391  
392 sub as_string
393 {
394 my ($self,$space,$debug) = @_;
395 my ($str) = "";
396 $space = " " if !defined $space;
397 $str .= "${space}Lock Object ($self)\n";
398 $space .= " ";
399 $str .= "${space}'_owned': " . ($self->{_owned}||"") . "\n";
400 $str .= "${space}'_scope': " . ($self->{_scope}||"") . "\n";
401 $str .= "${space}'_type': " . ($self->{_type} ||"") . "\n";
402 $str .= "${space}'_owner': " . ($self->{_owner}||"") . "\n";
403 $str .= "${space}'_depth': " . ($self->{_depth}||"") . "\n";
404 $str .= "${space}'_timeout': " . ($self->{_timeout}||"") . "\n";
405 $str .= "${space}'_locktokens': " . join(", ", @{$self->get_locktokens()} ) . "\n";
406  
407 $str;
408 }
409  
410 sub pretty_print
411 {
412 my ($self,$space) = @_;
413 my ($str) = "";
414 $str .= "${space}Owner: $self->{_owner}\n";
415 $str .= "${space}Scope: $self->{_scope}\n";
416 $str .= "${space}Type: $self->{_type}\n";
417 $str .= "${space}Depth: $self->{_depth}\n";
418 $str .= "${space}Timeout: $self->{_timeout}\n";
419 $str .= "${space}LockTokens: " . join(", ", @{$self->get_locktokens()} ) . "\n";
420  
421 $str;
422 }
423  
424  
425 ###########################################################################
426 =back
427  
428 =head1 SEE ALSO
429  
430 L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>
431  
432 =head1 COPYRIGHT
433  
434 Copyright 2000 Patrick Collins.
435  
436 This library is free software; you can redistribute it and/or
437 modify it under the same terms as Perl itself.
438  
439 =cut
440  
441 1;