lib/HTML/Mason/CGIHandler.pm

filter: Statementstmt 83.8% 104 / 124 Branchbran 47.0% 16 / 34 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.7% 24 / 28 Podpod 100% 5 / 5 totaltotal 69.6% 154 / 221 CC 25 SCAR 26.7
CC
25
Cov
70%
_handler
43.4
exec
13.0
__ANON__
12.0
CRAP 14.4
« lib/HTML/Mason/Resolver/File.pm ↑ Summary lib/HTML/Mason/Request.pm »
1 package HTML::Mason::CGIHandler;
2 2 $HTML::Mason::CGIHandler::VERSION = '1.60';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use HTML::Mason;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTML::Mason::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use CGI 2.46;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::Mason::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTML::Mason::FakeApache;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use base qw(Class::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 use HTML::Mason::MethodMaker
18 2 ▶ ( read_write => [ qw( interp ) ] );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 __PACKAGE__->valid_params
21 (
22 interp => { isa => 'HTML::Mason::Interp' },
23 );
24  
25 2 __PACKAGE__->contained_objects
26 (
27 interp => 'HTML::Mason::Interp',
28 cgi_request => { class => 'HTML::Mason::FakeApache', # $r
29 delayed => 1 },
30 );
31  
32  
33 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
34 7 my $package = shift;
35  
36 7 my %p = @_;
37 my $self = $package->SUPER::new(comp_root => $ENV{DOCUMENT_ROOT},
38 7 request_class => 'HTML::Mason::Request::CGI',
39 error_mode => 'output',
40 error_format => 'html',
41 %p);
42  
43 7 ▶ $self->{has_custom_out_method} = $p{out_method} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$p{'out_method'} ? : 0 7 7
44  
45 7 $self->interp->compiler->add_allowed_globals('$r');
46
47 7 return $self;
48 }
49  
50 7 ▶sub handle_request {
Subroutine handle_request: called CC=1 SCAR=0.0
Pod documented
51 7 my $self = shift;
52 7 $self->_handler( { comp => $ENV{PATH_INFO} }, @_ );
53 }
54  
55 0 ▶sub handle_comp {
Subroutine handle_comp: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($self, $comp) = (shift, shift);
57 0 $self->_handler( { comp => $comp }, @_ );
58 }
59  
60 0 ▶sub handle_cgi_object {
Subroutine handle_cgi_object: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($self, $cgi) = (shift, shift);
62 0 $self->_handler( { comp => $cgi->path_info,
63 cgi => $cgi },
64 @_);
65 }
66  
67 7 ▶sub _handler {
Subroutine _handler: called CC=13 SCAR=37.7
68 7 my ($self, $p) = (shift, shift);
69  
70 7 my $r = $self->create_delayed_object('cgi_request', cgi => $p->{cgi});
71 7 $self->interp->set_global('$r', $r);
72  
73 # hack for testing
74 7 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 7 0 7
elsif (not $self->{'has_custom_out_method'}) { } 0 0 0
75 7 $self->{output} = '';
76 7 $self->interp->out_method( \$self->{output} );
77 } elsif (! $self->{has_custom_out_method}) {
78 0 my $sent_headers = 0;
79  
80 0 ▶ my $out_method = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
81 # Send headers if they have not been sent by us or by user.
82 # We use instance here because if we store $request we get a
83 # circular reference and a big memory leak.
84 0 ▶ if (!$sent_headers and HTML::Mason::Request->instance->auto_send_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sent_headers and "HTML::Mason::Request"->instance->auto_send_headers) 0 0 0
Condition0%0/3
not $sent_headers and "HTML::Mason::Request"->instance->auto_send_headers
!ll&&!rl&&r
000
MC/DC0%0/2
not $sent_headers and "HTML::Mason::Request"->instance->auto_send_headers
not $sent_headers "HTML::Mason::Request"->instance->auto_send_headers
Truth table0%0/3
not(A) and B
A= not $sent_headersB= "HTML::Mason::Request"->instance->auto_send_headers
ABresult
0X0
100
111
85 0 $r->send_http_header();
86 0 $sent_headers = 1;
87 }
88  
89 # We could perhaps install a new, faster out_method here that
90 # wouldn't have to keep checking whether headers have been
91 # sent and what the $r->method is. That would require
92 # additions to the Request interface, though.
93  
94 0 print STDOUT grep {defined} @_;
95 0 };
96  
97 0 $self->interp->out_method($out_method);
98 }
99  
100 7 $self->interp->delayed_object_params('request', cgi_request => $r);
101  
102 7 my %args = $self->request_args($r);
103  
104 7 my @result;
105 7 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 7 7
elsif (defined wantarray) { } 0 7 7
106 0 @result = eval { $self->interp->exec($p->{comp}, %args) };
107 } elsif ( defined wantarray ) {
108 0 $result[0] = eval { $self->interp->exec($p->{comp}, %args) };
109 } else {
110 7 eval { $self->interp->exec($p->{comp}, %args) };
111 }
112  
113 7 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 6 7
114 1 ▶ my $retval = isa_mason_exception($err, 'Abort') ? $err->aborted_value :
Branch: 2 branches
BranchTrueFalseTotal
isa_mason_exception($err, 'Abort') ? : 0 1 1
isa_mason_exception($err, 'Decline') ? : 0 1 1
115 isa_mason_exception($err, 'Decline') ? $err->declined_value :
116 rethrow_exception $err;
117  
118 # Unlike under mod_perl, we cannot simply return a 301 or 302
119 # status and let Apache send headers, we need to explicitly
120 # send this header ourself.
121 0 ▶ $r->send_http_header if $retval && grep { $retval eq $_ } ( 200, 301, 302 );
Branch: 1 branch
BranchTrueFalseTotal
if $retval and grep {$retval eq $_;} 200, 301, 302 0 0 0
Condition0%0/3
$retval and grep {$retval eq $_;} 200, 301, 302
!ll&&!rl&&r
000
MC/DC0%0/2
$retval and grep {$retval eq $_;} 200, 301, 302
$retval grep {$retval eq $_;} 200, 301, 302
Truth table0%0/3
A and B
A= $retvalB= grep {$retval eq $_;} 200, 301, 302
ABresult
0X0
100
111
122  
123 0 return $retval;
124 }
125  
126 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 0 6
127 # This is a secret feature, and should stay secret (or go
128 # away) because it's just a hack for the test suite.
129 6 $_[0] .= $r->http_header . $self->{output};
130 }
131  
132 6 ▶ return wantarray ? @result : defined wantarray ? $result[0] : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 6 6
defined wantarray ? : 0 6 6
133 }
134  
135 # This is broken out in order to make subclassing easier.
136 7 ▶sub request_args {
Subroutine request_args: called CC=1 SCAR=0.0
Pod documented
137 7 my ($self, $r) = @_;
138  
139 7 return $r->params;
140 }
141  
142  
143 ###########################################################
144 package HTML::Mason::Request::CGI;
145 # Subclass for HTML::Mason::Request object $m
146 2 $HTML::Mason::Request::CGI::VERSION = '1.60';
147 2 ▶use HTML::Mason::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 2 ▶use HTML::Mason::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 2 ▶use base qw(HTML::Mason::Request);
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 2 ▶use Params::Validate qw(BOOLEAN);
Subroutine BEGIN: called CC=1 SCAR=0.0
152 0 ▶Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
153  
154 2 __PACKAGE__->valid_params
155 ( cgi_request => { isa => 'HTML::Mason::FakeApache' },
156  
157 auto_send_headers => { parse => 'boolean', type => BOOLEAN, default => 1,
158 descr => "Whether HTTP headers should be auto-generated" },
159 );
160  
161 use HTML::Mason::MethodMaker
162 2 ▶ ( read_only => [ 'cgi_request' ],
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 read_write => [ 'auto_send_headers' ] );
164  
165 1 ▶sub cgi_object {
Subroutine cgi_object: called CC=1 SCAR=0.0
166 1 my $self = shift;
167 1 return $self->{cgi_request}->query(@_);
168 }
169  
170 #
171 # Override this method to send HTTP headers if necessary.
172 #
173 sub exec
174 7 ▶{
Subroutine exec: called CC=9 SCAR=25.6
175 7 my $self = shift;
176 7 my $r = $self->cgi_request;
177 7 my $retval;
178  
179 7 eval { $retval = $self->SUPER::exec(@_) };
180  
181 7 ▶ if (my $err = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 6 7
182 {
183 1 ▶ $retval = isa_mason_exception($err, 'Abort') ? $err->aborted_value :
Branch: 2 branches
BranchTrueFalseTotal
isa_mason_exception($err, 'Abort') ? : 0 1 1
isa_mason_exception($err, 'Decline') ? : 0 1 1
184 isa_mason_exception($err, 'Decline') ? $err->declined_value :
185 rethrow_exception $err;
186 }
187  
188 # On a success code, send headers if they have not been sent and
189 # if we are the top-level request. Since the out_method sends
190 # headers, this will typically only apply after $m->abort.
191 6 ▶ if (!$self->is_subrequest
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_subrequest and $self->auto_send_headers and not $r->http_header_sent and !$retval || $retval == 200) 6 0 6
Condition33%4/12
not $self->is_subrequest and $self->auto_send_headers and not $r->http_header_sent and !$retval || $retval == 200
!ll&&!rl&&r
006
not $self->is_subrequest and $self->auto_send_headers and not $r->http_header_sent
!ll&&!rl&&r
006
not $self->is_subrequest and $self->auto_send_headers
!ll&&!rl&&r
006
not $retval or $retval == 200
l!l&&r!l&&!r
600
MC/DC0%0/5
not $self->is_subrequest and $self->auto_send_headers and not $r->http_header_sent and !$retval || $retval == 200
not $self->is_subrequest $self->auto_send_headers not $r->http_header_sent not $retval $retval == 200
Truth table16%1/6
not(A) and B and not(C) and not(D) or E
A= not $self->is_subrequestB= $self->auto_send_headersC= not $r->http_header_sentD= not $retvalE= $retval == 200
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
192 and $self->auto_send_headers
193 and !$r->http_header_sent
194 and (!$retval or $retval==200)) {
195 6 $r->send_http_header();
196 }
197  
198 6 return $retval;
199 }
200  
201 1 ▶sub redirect {
Subroutine redirect: called CC=2 SCAR=7.0
202 1 my $self = shift;
203 1 my $url = shift;
204 1 ▶ my $status = shift || 302;
Condition50%1/2
shift() || 302
l!l
01
MC/DC0%0/1
shift() || 302
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
205  
206 1 $self->clear_buffer;
207  
208 1 $self->{cgi_request}->header_out( Location => $url );
209 1 $self->{cgi_request}->header_out( Status => $status );
210  
211 1 $self->abort;
212 }
213  
214 2 1;
215 __END__
« lib/HTML/Mason/Resolver/File.pm ↑ Summary lib/HTML/Mason/Request.pm »