blib/lib/WebDAO/Response.pm

filter: Statementstmt 76.4% 133 / 174 Branchbran 50.0% 20 / 40 Conditioncond 43.7% 7 / 16 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 72.9% 27 / 37 Podpod 76.9% 20 / 26 totaltotal 69.3% 208 / 300 CC 27 SCAR 14.0
CC
27
Cov
70%
send_file
12.3
_print_dep_on_context
6.0
error404
6.0
CRAP 4.1
« Build ↑ Summary blib/lib/WebDAO/Sessionco.pm »
1 package WebDAO::Response;
2  
3 =head1 NAME
4  
5 WebDAO::Response - Response class
6  
7 =head1 SYNOPSYS
8  
9 new WebDAO::Response:: cv => $cv
10  
11 =head1 DESCRIPTION
12  
13 Class for make HTTP response
14  
15 =cut
16  
17 5 our $VERSION = '0.01';
18 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use WebDAO::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use DateTime::Format::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use base qw( WebDAO::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 __PACKAGE__->mk_attr(
26 _headers => undef,
27 _is_headers_printed =>0,
28 _cv_obj => undef,
29 _is_file_send => 0,
30 _is_need_close_fh => 0,
31 __fh => undef,
32 _is_flushed => 0,
33 _call_backs => undef,
34 _is_modal => 0,
35 _forced_want_format => undef,
36 _is_empty=>0,
37 status => 200 #default HTTP status
38 );
39  
40 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =head1 METHODS
44  
45 =cut
46  
47 sub new {
48 12 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 12 my $self = {};
50 12 my $stat;
51 12 bless( $self, $class );
52 12 $self->_init(@_);
53 12 return $self;
54 }
55  
56 sub _init {
57 12 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
58 12 return $self->init(@_);
59 }
60  
61 sub init {
62 12 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
63 12 my %par = @_;
64 12 $self->_headers( {} );
65 12 $self->_call_backs( [] );
66 12 $self->_cv_obj( $par{cv} );
67 12 return 1;
68 }
69  
70 =head2 get_request
71  
72 Return ref to request object (WebDAO::CV)
73  
74 =cut
75  
76 sub get_request {
77 7 ▶ my $self = shift;
Subroutine get_request: called CC=1 SCAR=0.0
Pod documented
78 7 return $self->_cv_obj;
79 }
80  
81 =head2 set_status INT
82  
83 set response HTTP status
84  
85 $r->set_status(200)
86  
87 return C<$self>
88  
89 =cut
90  
91 sub set_status {
92 2 ▶ my $self = shift;
Subroutine set_status: called CC=1 SCAR=0.0
Pod documented
93 2 $self->status(@_);
94 2 $self
95 }
96  
97  
98 =head2 set_header NAME, VALUE
99  
100 Set out header:
101  
102 $response->set_header('Location', $redirect_url);
103 $response->set_header( 'Content-Type' => 'text/html; charset=utf-8' );
104  
105 return $self reference
106  
107 =cut
108  
109 sub set_header {
110 10 ▶ my ( $self, $name, $par ) = @_;
Subroutine set_header: called CC=4 SCAR=17.4
Pod documented
111 #translate CGI headers
112 10 ▶ if ( $name =~ /^-/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^-/) { } 0 10 10
elsif ($name eq 'Set-Cookie') { } 4 6 10
113 0 my $UKey = uc $name;
114
115 0 ▶ if ( $UKey eq '-STATUS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UKey eq "-STATUS") 0 0 0
116 0 my ($status) = $par =~ m/(\d+)/;
117 0 $self->status($status);
118 0 return $self;
119 }
120 0 warn "Deprecated header name $name !";
121 } elsif ( $name eq 'Set-Cookie') {
122 4 push @{ $self->_headers->{ $name } }, $par;
123 4 return $self
124 }
125
126 6 $self->_headers->{ $name } = $par;
127 6 $self;
128 }
129  
130  
131  
132 =head2 get_header NAME
133  
134 return value for header NAME:
135  
136 =cut
137  
138 sub get_header {
139 6 ▶ my ( $self, $name ) = @_;
Subroutine get_header: called CC=1 SCAR=0.0
Pod documented
140 6 return $self->_headers->{ $name };
141 }
142  
143 =head2 aliases for headers
144  
145 =head3 content_type
146  
147 $r->content_type('text/html; charset=utf-8');
148  
149 =cut
150  
151 sub content_type {
152 2 ▶ my $self = shift;
Subroutine content_type: called CC=2 SCAR=7.0
Pod documented
153 2 ▶ unless ($#_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#_ > 0) 2 0 2
154 2 $self->set_header('Content-Type', @_)
155 }
156 2 $self->get_header('Content-Type');
157 }
158  
159 =head3 content_length
160  
161 A decimal number indicating the size in bytes of the message content.
162  
163 =cut
164  
165 sub content_length {
166 2 ▶ my $self = shift;
Subroutine content_length: called CC=2 SCAR=7.0
Pod documented
167 2 ▶ unless ($#_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#_ > 0) 2 0 2
168 2 $self->set_header('Content-Length' , @_)
169 }
170 2 $self->get_header('Content-Length');
171 }
172  
173 =head3
174  
175 =head2 get_mime_for_filename <filename>
176  
177 Determine mime type for filename (Simple by ext);
178 return str
179  
180 =cut
181  
182 sub get_mime_for_filename {
183 1 ▶ my $self = shift;
Subroutine get_mime_for_filename: called CC=3 SCAR=12.0
Pod documented
184 1 my $filename = shift;
185 1 my $no_default_flag = shift;
186 1 my %types_for_ext = (
187 avi => 'video/x-msvideo',
188 bmp => 'image/bmp',
189 css => 'text/css',
190 gif => 'image/gif',
191 gz => 'application/gzip',
192 html => 'text/html',
193 htm => 'text/html',
194 jpg => 'image/jpeg',
195 jpeg => 'image/jpeg',
196 js => 'application/javascript',
197 midi => 'audio/midi',
198 mp3 => 'audio/mpeg',
199 mpeg => 'video/mpeg',
200 mpg => 'video/mpeg',
201 mov => 'video/quicktime',
202 pdf => 'application/pdf',
203 png => 'image/png',
204 ppt => 'application/vnd.ms-powerpoint',
205 rtf => 'text/rtf',
206 tif => 'image/tif',
207 tiff => 'image/tif',
208 txt => 'text/plain',
209 xls => 'application/vnd.ms-excel',
210 xml => 'appliction/xml',
211 wav => 'audio/x-wav',
212 zip => 'application/zip',
213 );
214 1 my ($ext) = $filename =~ /\.(\w+)$/;
215 1 ▶ if ( my $type = $types_for_ext{ lc $ext } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $types_for_ext{lc $ext}) 1 0 1
216 1 return $type;
217 }
218 0 ▶ return $no_default_flag ? undef : 'application/octet-stream';
Branch: 1 branch
BranchTrueFalseTotal
$no_default_flag ? : 0 0 0
219 }
220  
221 =head2 print_header
222  
223 print header.return $self reference
224  
225 =cut
226  
227 sub print_header {
228 3 ▶ my $self = shift;
Subroutine print_header: called CC=2 SCAR=6.9
Pod documented
229 3 my $pnted = $self->_is_headers_printed;
230 3 ▶ return $self if $pnted;
Branch: 1 branch
BranchTrueFalseTotal
if $pnted 0 3 3
231 3 my $cv = $self->get_request;
232 3 $cv->status($self->status);
233 3 $cv->print_headers(%{ $self->_headers });
234 3 $self->_is_headers_printed(1);
235 3 $self;
236 }
237  
238 =head2 redirect2url <url for redirect to> [, $code]
239  
240 Set headers for redirect to url.return $self reference
241  
242 =cut
243  
244 sub redirect2url {
245 1 ▶ my ( $self, $redirect_url, $code ) = @_;
Subroutine redirect2url: called CC=2 SCAR=7.1
Pod documented
246 1 ▶ $self->set_modal->set_status( $code || 302 );
Condition50%1/2
$code || 302
l!l
01
MC/DC0%0/1
$code || 302
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
247 1 $self->set_header( 'Location', $redirect_url );
248 }
249  
250 =head2 set_cookie ( name => <cookie_name>, value=><cookie_value> ...)
251  
252 Set cookie.
253 return $self reference
254  
255 =cut
256  
257 sub set_cookie {
258 4 ▶ my $self = shift;
Subroutine set_cookie: called CC=1 SCAR=0.0
Pod documented
259 4 $self->set_header("Set-Cookie", { @_ });
260 4 $self;
261 }
262  
263  
264 =head2 set_callback(sub1{}[, sub2{} ..])
265  
266 Set callbacks for call after flush
267  
268 =cut
269  
270 sub set_callback {
271 2 ▶ my $self = shift;
Subroutine set_callback: called CC=1 SCAR=0.0
Pod documented
272 2 push @{ $self->_call_backs }, @_;
273 2 return $self;
274 }
275  
276 =head2 send_file <filename>|<file_handle>|<reference to GLOB> [, -type=><MIME type string>]
277  
278 Prepare headers and save
279  
280 $respose->send_file($filename, -type=>'image/jpeg');
281  
282 =cut
283  
284 sub send_file {
285 1 ▶ my $self = shift;
Subroutine send_file: called CC=8 SCAR=25.1
Pod documented
286 1 my $file = shift;
287 1 my %args = @_;
288 1 my $file_handle;
289 my $file_name;
290 1 ▶ if ( ref $file
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and UNIVERSAL::isa($file, 'IO::Handle') || ref $file eq 'GLOB' or UNIVERSAL::isa($file, 'Tie::Handle')) { } 0 1 1
Condition22%2/9
ref $file and UNIVERSAL::isa($file, "IO::Handle") || ref $file eq "GLOB" or UNIVERSAL::isa($file, "Tie::Handle")
l!l&&r!l&&!r
001
ref $file and UNIVERSAL::isa($file, "IO::Handle") || ref $file eq "GLOB"
!ll&&!rl&&r
100
UNIVERSAL::isa($file, "IO::Handle") or ref $file eq "GLOB"
l!l&&r!l&&!r
000
MC/DC0%0/4
ref $file and UNIVERSAL::isa($file, "IO::Handle") || ref $file eq "GLOB" or UNIVERSAL::isa($file, "Tie::Handle")
ref $file UNIVERSAL::isa($file, "IO::Handle") ref $file eq "GLOB" UNIVERSAL::isa($file, "Tie::Handle")
Truth table16%1/6
A and B or C or D
A= ref $fileB= UNIVERSAL::isa($file, "IO::Handle")C= ref $file eq "GLOB"D= UNIVERSAL::isa($file, "Tie::Handle")
ABCDresult
11XX1
101X1
0XX11
10011
0XX00
10000
291 and ( UNIVERSAL::isa( $file, 'IO::Handle' ) or ( ref $file ) eq 'GLOB' )
292 or UNIVERSAL::isa( $file, 'Tie::Handle' ) )
293 {
294 0 $file_handle = $file;
295 }
296 else {
297 1 $file_name = $file;
298 1 ▶ $file_handle = new IO::File::("< $file")
Branch: 1 branch
BranchTrueFalseTotal
$file_handle = "IO::File"->new("< $file") or die "can't open file: $file" . $! 0 1 1
299 or die "can't open file: $file" . $!;
300 1 $self->_is_need_close_fh(1);
301 1 $self->__fh($file_handle);
302 }
303  
304 #set file headers
305 1 my ( $size, $mtime ) = ( stat $file_handle )[ 7, 9 ];
306 1 $self->content_length( $size );
307 1 my $formated =
308 DateTime::Format::HTTP->format_datetime(
309 DateTime->from_epoch( epoch => $mtime ) );
310 1 $self->set_header( 'Last-Modified', $formated );
311  
312 #Determine mime tape of file
313 1 ▶ if ( my $predefined = $args{-type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $predefined = $args{'-type'}) { } 1 0 1
314 1 $self->content_type( $predefined );
315 }
316 else {
317 ##
318 0 ▶ if ($file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name) 0 0 0
319 0 $self->content_type(
320 $self->get_mime_for_filename($file_name) );
321 }
322 }
323  
324 #set modal mode and flag for send file
325 1 $self->set_modal->_is_file_send(1);
326 1 $self;
327 }
328  
329 sub print {
330 0 ▶ my $self = shift;
Subroutine print: not called CC=1 SCAR=6.9
Pod undocumented
331 0 my $cv = $self->get_request;
332 0 $self->print_header;
333 0 $cv->print(@_);
334 0 return $self;
335 }
336  
337 sub _print_dep_on_context {
338 0 ▶ my ( $self, $session ) = @_;
Subroutine _print_dep_on_context: not called CC=2 SCAR=17.9
339 0 my $res = $self->html;
340 0 ▶ $self->print( ref($res) eq 'CODE' ? $res->() : $res );
Branch: 1 branch
BranchTrueFalseTotal
ref $res eq 'CODE' ? : 0 0 0
341 }
342  
343 =head2 flush
344  
345 Flush current state of response.
346  
347 =cut
348  
349 sub flush {
350 2 ▶ my $self = shift;
Subroutine flush: called CC=5 SCAR=16.1
Pod documented
351 2 ▶ return $self if $self->_is_flushed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_flushed 0 2 2
352 2 $self->print_header;
353  
354 #do self print file
355 2 ▶ if ( $self->_is_file_send ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_file_send) 1 1 2
356 1 my $fd = $self->__fh;
357 1 $self->get_request->print(<$fd>);
358 1 ▶ close($fd) if $self->_is_need_close_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_need_close_fh 1 0 1
359 }
360 2 $self->_is_flushed(1);
361  
362 #do callbacks
363 2 my $ref_calls = $self->_call_backs;
364 2 ▶ while ( my $code = pop @$ref_calls ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = pop @$ref_calls) 2 2 4
365 2 $code->();
366 }
367  
368 #clear callbacks
369 2 @{ $self->_call_backs } = ();
370 2 $self;
371 }
372  
373 =head2 set_modal
374  
375 Set modal mode for answer
376  
377 =cut
378  
379 sub set_modal {
380 2 ▶ my $self = shift;
Subroutine set_modal: called CC=1 SCAR=0.0
Pod documented
381 2 $self->_is_modal(1);
382 2 $self;
383 }
384  
385 =head2 error404
386  
387 Set HTTP 404 headers
388  
389 =cut
390  
391 sub error404 {
392 0 ▶ my $self = shift;
Subroutine error404: not called CC=2 SCAR=17.9
Pod documented
393 0 $self->set_modal->set_status(404);
394 0 ▶ $self->print(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
395 0 return $self;
396 }
397  
398 sub html : lvalue {
399 0 ▶ my $self = shift;
Subroutine html: not called CC=1 SCAR=6.9
Pod undocumented
400 0 $self->{__html};
401 }
402  
403 sub set_html {
404 0 ▶ my $self = shift;
Subroutine set_html: not called CC=1 SCAR=6.9
Pod undocumented
405 0 my $data = shift;
406 0 $self->html = $data;
407 0 return $self;
408 }
409  
410 sub json : lvalue {
411 0 ▶ my $self = shift;
Subroutine json: not called CC=1 SCAR=6.9
Pod undocumented
412 0 $self->{__json};
413  
414 }
415  
416 sub set_json {
417 0 ▶ my $self = shift;
Subroutine set_json: not called CC=1 SCAR=6.9
Pod undocumented
418 0 my $data = shift;
419 0 $self->json = $data;
420 0 return $self;
421 }
422  
423 sub _destroy {
424 0 ▶ my $self = shift;
Subroutine _destroy: not called CC=1 SCAR=6.9
425 0 $self->{__html} = undef;
426 #destroy called from Engine::execute2
427 # destroy tests by cleared _cv_obj
428 # $self->_cv_obj( undef );
429 0 $self->_headers( {} );
430 0 $self->_call_backs( [] );
431 }
432  
433 =head2 wantformat ['format',['forse_set_format']]
434  
435 Return expected output format: defauilt html
436
437 # return string for format
438 $r->wantformat()
439  
440 Check if desired format is expected
441  
442 #$r->wantformat('html') return boolean
443 if ($r->wantformat('html')) {
444 #
445 }
446  
447 Force set desired format:
448  
449 $r->wantformat('html'=>1); #return $response object ref
450  
451 =cut
452  
453 sub wantformat {
454 6 ▶ my $self = shift;
Subroutine wantformat: called CC=5 SCAR=16.1
Pod documented
455 6 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 5 6
456 1 $self->_forced_want_format(shift);
457 1 return $self;
458 }
459 5 my $desired = $self->_forced_want_format();
460 5 ▶ my $default =
Condition80%4/5
$desired || $self->detect_wantformat($self->get_request) || 'html'
l!l
23
$desired or $self->detect_wantformat($self->get_request)
l!l&&r!l&&!r
203
MC/DC50%1/2
$desired || $self->detect_wantformat($self->get_request) || 'html'
$desired $self->detect_wantformat($self->get_request)
Truth table66%2/3
A or B
A= $desiredB= $self->detect_wantformat($self->get_request)
ABresult
1X1
011
000
461 $desired
462 || $self->detect_wantformat( $self->get_request ) #call with CV object
463 || 'html';
464 5 ▶ if ( scalar(@_) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 3 2 5
465 3 return $default eq shift;
466 }
467 2 return $default;
468 }
469  
470 =head2 detect_wantformat ($cv)
471  
472 Method for detect output format when C<wantformat()> called
473  
474 Must return :
475
476 string - output format, i.e. 'html', 'xml'
477 undef - unknown ( use defaults )
478  
479 =cut
480  
481 sub detect_wantformat {
482 return undef #unknown by default
483 3 ▶}
Subroutine detect_wantformat: called CC=1 SCAR=0.0
Pod documented
484  
485  
486 =head2 set_empty
487  
488 Set flag for empty response. Headers are not printed.
489 return $self
490 =cut
491  
492 0 ▶sub set_empty { $_[0]->_is_empty(1); $_[0]}
Subroutine set_empty: not called CC=1 SCAR=6.9
Pod documented
493  
494 =head2 is_empty
495  
496 Check is response cleared.
497 Return 1|0
498  
499 =cut
500  
501 0 ▶sub is_empty { return $_[0]->_is_empty() }
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod documented
502  
503 5 1;
504 __DATA__
505  
506 =head1 SEE ALSO
507  
508 http://webdao.sourceforge.net
509  
510 =head1 AUTHOR
511  
512 Zahatski Aliaksandr, E<lt>zag@cpan.orgE<gt>
513  
514 =head1 COPYRIGHT AND LICENSE
515  
516 Copyright 2002-2012 by Zahatski Aliaksandr
517  
518 This library is free software; you can redistribute it and/or modify
519 it under the same terms as Perl itself.
520  
521 =cut
522  
« Build ↑ Summary blib/lib/WebDAO/Sessionco.pm »