DAV/Response.pm

filter: Statementstmt 13.3% 10 / 75 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 11.1% 2 / 18 Podpod 62.5% 10 / 16 totaltotal 13.4% 22 / 164 CC 27 SCAR 31.3
CC
27
Cov
8%
as_string
72.0
is_success
30.0
messages
20.0
CRAP 23.0
« DAV/Lock.pm ↑ Summary DAV/Utils.pm »
1 package HTTP::DAV::Response;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 $VERSION = '0.14';
7  
8 4 require HTTP::Response;
9 4 @ISA = qw(HTTP::Response);
10  
11 4 my %dav_status_codes = (
12 102 => "Processing. Server has accepted the request, but has not yet completed it",
13 204 => "No Content",
14 207 => "Multistatus",
15 422 => "Unprocessable Entity. Bad client XML sent?",
16 423 => "Locked. The source or destination resource is locked",
17 424 => "Failed Dependency",
18 507 => "Insufficient Storage. The server is unable to store the request",
19 );
20  
21 # PROTECTED METHODS
22  
23 0 ▶sub clone_http_resp {
Subroutine clone_http_resp: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my ($class,$http_resp) = @_;
25 0 my %clone = %{$http_resp};
26 0 my $self = \%clone;
27 0 ▶ bless $self, (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
28 }
29  
30 # This routine resets the base
31 # message in the
32 # object based on the
33 # code and the status_codes above.
34 # set_message('207');
35 0 ▶sub set_message {
Subroutine set_message: not called CC=2 SCAR=17.9
Pod undocumented
36 0 my ($self,$code) = @_;
37  
38 # Set the status code
39 0 ▶ if ( defined $dav_status_codes{$code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dav_status_codes{$code}) 0 0 0
40 0 $self->message( $dav_status_codes{$code} );
41 }
42 }
43  
44 0 ▶sub set_responsedescription {
Subroutine set_responsedescription: not called CC=2 SCAR=17.9
Pod undocumented
45 0 ▶ $_[0]->{'_dav_responsedescription'} = $_[1] if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 0 0
46 }
47  
48 0 ▶sub get_responsedescription { $_[0]->{'_dav_responsedescription'}; }
Subroutine get_responsedescription: not called CC=1 SCAR=6.9
Pod undocumented
49  
50 0 ▶sub add_status_line {
Subroutine add_status_line: not called CC=3 SCAR=24.8
Pod undocumented
51 0 my($self,$message,$responsedescription,$handle,$url) = @_;
52
53 # Parse "status-line". See section 6.1 of RFC 2068
54 # Status-Line= HTTP-Version SP Status-Code SP Reason-Phrase CRLF
55 0 ▶ if (defined $message && $message =~ /^(.*?)\s(.*?)\s(.*?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and $message =~ /^(.*?)\s(.*?)\s(.*?)$/) { } 0 0 0
Condition0%0/3
defined $message and $message =~ /^(.*?)\s(.*?)\s(.*?)$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $message and $message =~ /^(.*?)\s(.*?)\s(.*?)$/
defined $message $message =~ /^(.*?)\s(.*?)\s(.*?)$/
Truth table0%0/3
A and B
A= defined $messageB= $message =~ /^(.*?)\s(.*?)\s(.*?)$/
ABresult
0X0
100
111
56 0 my ($http_version,$status_code,$reason_phrase) = ($1,$2,$3);
57  
58 0 push ( @{$self->{_dav_multistatus}},
59 {
60 'handle' => $handle,
61 'url' => $url,
62 'HTTP Version' => $http_version,
63 'code' => $status_code,
64 'message' => $reason_phrase,
65 'description' => $responsedescription,
66 }
67 );
68 0 return 1;
69  
70 } else {
71 0 return 0;
72 }
73 }
74  
75 # PUBLIC METHODS
76  
77 0 ▶sub is_multistatus {
Subroutine is_multistatus: not called CC=2 SCAR=17.9
Pod documented
78 0 ▶ return ($_[0]->code eq "207" )? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->code eq '207' ? : 0 0 0
79 }
80  
81 0 ▶sub messages {
Subroutine messages: not called CC=4 SCAR=30.0
Pod documented
82 0 my ($self) = @_;
83  
84 0 my @messages = ();
85 0 ▶ if ($self->is_multistatus() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_multistatus) { } 0 0 0
86 0 foreach my $num ( 0 .. $self->response_count()) {
87 0 push(@messages, $self->message_bynum($num));
88 }
89 } else {
90 0 push(@messages,$self->message());
91 }
92  
93 0 ▶ return wantarray ? @messages : join("\n",@messages);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
94 }
95  
96 0 ▶sub codes {
Subroutine codes: not called CC=3 SCAR=24.8
Pod undocumented
97 0 my ($self) = @_;
98  
99 0 my @codes = ();
100 0 ▶ if ($self->is_multistatus() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_multistatus) { } 0 0 0
101 0 foreach my $num ( 0 .. $self->response_count()) {
102 0 push(@codes, $self->code_bynum($num));
103 }
104 } else {
105 0 push(@codes,$self->code());
106 }
107  
108 0 return \@codes;
109 }
110  
111 0 ▶sub response_count {
Subroutine response_count: not called CC=3 SCAR=24.8
Pod documented
112 0 ▶ return -1 unless exists $_[0]->{_dav_multistatus};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"_dav_multistatus"} 0 0 0
113 0 ▶ return -1 unless ref($_[0]->{_dav_multistatus}) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]{"_dav_multistatus"}) =~ /ARRAY/ 0 0 0
114 0 return $#{$_[0]->{_dav_multistatus}};
115 }
116  
117 0 ▶sub message_bynum { $_[0]->{_dav_multistatus}[$_[1]]{'message'}; }
Subroutine message_bynum: not called CC=1 SCAR=6.9
Pod documented
118 0 ▶sub code_bynum { $_[0]->{_dav_multistatus}[$_[1]]{'code'}; }
Subroutine code_bynum: not called CC=1 SCAR=6.9
Pod documented
119 0 ▶sub url_bynum { $_[0]->{_dav_multistatus}[$_[1]]{'url'}; }
Subroutine url_bynum: not called CC=1 SCAR=6.9
Pod documented
120 0 ▶sub description_bynum { $_[0]->{_dav_multistatus}[$_[1]]{'description'}; }
Subroutine description_bynum: not called CC=1 SCAR=6.9
Pod documented
121  
122 0 ▶sub response_bynum {
Subroutine response_bynum: not called CC=3 SCAR=24.8
Pod documented
123 0 my ($self,$number) = @_;
124  
125 0 ▶ if (defined $number && $number>=0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number and $number >= 0) 0 0 0
Condition0%0/3
defined $number and $number >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $number and $number >= 0
defined $number $number >= 0
Truth table0%0/3
A and B
A= defined $numberB= $number >= 0
ABresult
0X0
100
111
126 return (
127 0 $self->code_bynum($number),
128 $self->message_bynum($number),
129 $self->url_bynum($number),
130 $self->description_bynum($number),
131 );
132 }
133 }
134  
135 0 ▶sub is_success {
Subroutine is_success: not called CC=5 SCAR=34.0
Pod documented
136 0 my ($self) = @_;
137  
138 0 ▶ if ($self->is_multistatus() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_multistatus) { } 0 0 0
139 0 foreach my $code ( @{ $self->codes() } ) {
140 0 ▶ return 0 if ( HTTP::Status::is_error($code) );
Branch: 1 branch
BranchTrueFalseTotal
if &HTTP::Status::is_error($code) 0 0 0
141 }
142 } else {
143 0 ▶ return ($self->SUPER::is_success() || 0);
Condition0%0/2
$self->SUPER::is_success || 0
l!l
00
MC/DC0%0/1
$self->SUPER::is_success || 0
$self->SUPER::is_success
Truth table0%0/2
A
A= $self->SUPER::is_success
Aresult
11
00
144 }
145  
146 0 return 1;
147 }
148  
149 0 ▶sub as_string {
Subroutine as_string: not called CC=8 SCAR=42.8
Pod documented
150 0 my ($self) = @_;
151 0 my ($ms, $returnstr) = "";
152  
153 # use Data::Dumper;
154 # print Data::Dumper->Dump( [\$self] , [ '$self' ] );
155 0 foreach my $num ( 0 .. $self->response_count() ) {
156 0 my %h = %{$self->{_dav_multistatus}[$num]};
157 0 $ms .= "Error number $num ($h{handle}):\n";
158 0 ▶ $ms .= " Href: $h{url}\n" if defined $h{url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"url"} 0 0 0
159 0 ▶ $ms .= " Mesg(code): $h{message} ($h{code})\n" if defined $h{code};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"code"} 0 0 0
160 0 ▶ $ms .= " Desc: $h{'description'}\n" if defined $h{'description'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"description"} 0 0 0
161 0 $ms .= "\n";
162 }
163  
164 0 ▶ my $rd = $self->get_responsedescription() || "";
Condition0%0/2
$self->get_responsedescription || ''
l!l
00
MC/DC0%0/1
$self->get_responsedescription || ''
$self->get_responsedescription
Truth table0%0/2
A
A= $self->get_responsedescription
Aresult
11
00
165  
166 0 ▶ $returnstr .= "Multistatus lines:\n$ms\n" if $ms;
Branch: 1 branch
BranchTrueFalseTotal
if $ms 0 0 0
167 0 ▶ $returnstr .= "Overall responsedescription: \"$rd\"\n" if $rd;
Branch: 1 branch
BranchTrueFalseTotal
if $rd 0 0 0
168 0 $returnstr .= $self->SUPER::as_string;
169 0 $returnstr;
170 }
171  
172 =head1 NAME
173  
174 HTTP::DAV::Response - represents a WebDAV HTTP Response (ala HTTP::Response)
175  
176 =head1 SYNOPSIS
177  
178 require HTTP::DAV::Response;
179  
180 =head1 DESCRIPTION
181  
182 The HTTP::DAV::Response class encapsulates HTTP style responses. A response consists of a response line, some headers, and (potentially empty) content.
183  
184 HTTP::DAV::Response is a subclass of C<HTTP::Response> and therefore inherits its methods. (HTTP::Response in turn inherits it's methods from C<HTTP::Message>).
185  
186 Therefore, this class actually inherits a rich library of functions. You are more likely wanting to read the C<HTTP::Response> class as opposed to this class.
187  
188 Instances of this class are usually created by a C<HTTP::DAV::Resource> object after it has performed some request (such as get, lock, delete, etc). You use the object to analyse the success or otherwise of the request.
189  
190 HTTP::DAV::Response was created to handle two extra functions that normal HTTP Responses don't require:
191  
192 - WebDAV responses have 6 extra error codes: 102, 207, 422, 423, 424 and 507. Older versions of the LWP's C<HTTP::Status> class did not have these extra codes. These were added.
193  
194 - WebDAV responses can actually contain more than one response (and often DO contain more than one) in the form of a "Multistatus". These multistatus responses come in the form of an XML document. HTTP::DAV::Response can accurately parse these XML responses and emulate the normal of the C<HTTP::Response>.
195  
196 HTTP::DAV::Response transparently implements these extra features without the user having to be aware, so you really should be reading the C<HTTP::Response> documentation for most of the things you want to do (have I already said that?).
197  
198 There are only a handful of custom functions that HTTP::DAV::Response returns and those are to handle multistatus requests, C<messages()> and C<codes()>.
199  
200 The six extra status codes that DAV servers can be returned in an HTTP Response are:
201 102 => "Processing. Server has accepted the request, but has not yet completed it",
202 207 => "Multistatus",
203 422 => "Unprocessable Entity. Bad client XML sent?",
204 423 => "Locked. The source or destination resource is locked",
205 424 => "Failed Dependency",
206 507 => "Insufficient Storage. The server is unable to store the request",
207  
208 See C<HTTP::Status> for the rest.
209  
210 =head1 HANDLING A MULTISTATUS
211  
212 So, many DAV requests may return a multistatus ("207 multistatus") instead of, say, "200 OK" or "403 Forbidden".
213  
214 The HTTP::DAV::Response object stores each "response" sent back in the multistatus. You access them by array number.
215  
216 The following code snippet shows what you will normally want to do:
217  
218 ...
219 $response = $resource->lock();
220  
221 if ( $response->is_multistatus() ) {
222  
223 foreach $num ( 0 .. $response->response_count() ) {
224 ($err_code,$mesg,$url,$desc) =
225 $response->response_bynum($num);
226 print "$mesg ($err_code) for $url\n";
227 }
228 }
229  
230 Would produce something like this:
231 Failed Dependency (424) for /test/directory
232 Locked (423) for /test/directory/file3
233  
234 This says that we couldn't lock /test/directory
235 because file3 which exists inside is already locked by somebody else.
236  
237 =head1 METHODS
238  
239 =over 4
240  
241 =item B<is_multistatus>
242  
243 This function takes no arguments and returns a 1 or a 0.
244  
245 For example: if ($response->is_multistatus() ) { }
246  
247 If the HTTP reply had "207 Multistatus" in the header then that indicates that there are multiple status messages in the XML content that was returned.
248  
249 In this event, you may be interested in knowing what the individual messages were. To do this you would then use C<messages>.
250  
251 =item B<response_count>
252  
253 Takes no arguments and returns "the number of error responses -1" that we got.
254 Why -1? Because usually you will want to use this like an array operator:
255  
256 foreach $num ( 0 .. $response->response_count() ) {
257 print $response->message_bynum();
258 }
259  
260 =item B<response_bynum>
261  
262 Takes one argument, the "response number" that you're interested in. And returns an array of details:
263  
264 ($code,$message,$url,$description) = response_bynum(2);
265  
266 where
267 $code - is the HTTP error code (e.g. 403, 423, etc).
268 $message - is the associated message for that error code.
269 $url - is the url that this error applies to (recall that there can be multiple responses within one response and they all relate to one URL)
270 $description - is server's attempt at an english description of what happened.
271  
272 =item B<code_bynum>
273  
274 Takes one argument, the "response number" that you're interested in, and returns it's code. E.g:
275  
276 $code = $response->code_bynum(1);
277  
278 See C<response_bynum()>
279  
280 =item B<message_bynum>
281  
282 Takes one argument, the "response number" that you're interested in, and returns it's message. E.g:
283  
284 $code = $response->message_bynum(1);
285  
286 See C<response_bynum()>
287  
288 =item B<url_bynum>
289  
290 Takes one argument, the "response number" that you're interested in, and returns it's url. E.g:
291  
292 $code = $response->message_bynum(1);
293  
294 See C<response_bynum()>
295  
296 =item B<description_bynum>
297  
298 Takes one argument, the "response number" that you're interested in, and returns it's description. E.g:
299  
300 $code = $response->message_description(1);
301  
302 See C<response_bynum()>
303  
304 =item B<messages>
305  
306 Takes no arguments and returns all of the messages returned in a multistatus response. If called in a scalar context then all of the messages will be returned joined together by newlines. If called in an array context the messages will be returned as an array.
307  
308 $messages = $response->messages();
309 e.g. $messages eq "Forbidden\nLocked";
310  
311 @messages = $response->messages();
312 e.g. @messages eq ["Forbidden", "Locked"];
313  
314 This routine is a variant on the standard C<HTTP::Response> C<message()>.
315  
316 =back
317  
318 =cut
319  
« DAV/Lock.pm ↑ Summary DAV/Utils.pm »