blib/lib/ASP4/HTTPContext.pm

filter: Statementstmt 81.4% 132 / 162 Branchbran 62.5% 25 / 40 Conditioncond 33.3% 11 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 86.3% 38 / 44 Podpod 30.0% 9 / 30 totaltotal 65.1% 215 / 330 CC 33 SCAR 18.0
CC
33
Cov
71%
handle_phase
22.3
execute
13.0
setup_request
9.2
CRAP 6.0
« blib/lib/ASP4/PageParser.pm ↑ Summary blib/lib/ASP4/Response.pm »
1  
2 package ASP4::HTTPContext;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use ASP4::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use ASP4::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use ASP4::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use ASP4::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use ASP4::OutBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use ASP4::SessionStateManager::NonPersisted;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 ▶use vars '$_instance';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub new
19 5145 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
20 5145 my ($class, %args) = @_;
21
22 my $s = bless {
23 config => ASP4::ConfigLoader->load,
24 buffer => [ ASP4::OutBuffer->new ],
25 stash => { },
26 headers_out => HTTP::Headers->new(),
27 is_subrequest => $args{is_subrequest},
28 5145 }, $class;
29 5145 $s->config->_init_inc();
30
31 5145 my $web = $s->config->web;
32 5145 $s->config->load_class( $web->handler_resolver );
33 5145 $s->config->load_class( $web->handler_runner );
34 5145 $s->config->load_class( $s->config->data_connections->session->manager );
35 5145 $s->config->load_class( $web->filter_resolver );
36
37 5145 ▶ return $s->is_subrequest ? $s : $_instance = $s;
Branch: 1 branch
BranchTrueFalseTotal
$s->is_subrequest ? : 14 5131 5145
38 }# end new()
39  
40  
41 sub setup_request
42 5145 ▶{
Subroutine setup_request: called CC=7 SCAR=22.2
Pod undocumented
43 5145 my ($s, $r, $cgi) = @_;
44
45 5145 $ENV{DOCUMENT_ROOT} = $r->document_root;
46 5145 $s->{r} = $r;
47 5145 $s->{cgi} = $cgi;
48
49 # Must instantiate $_instance before creating the other objects:
50 5145 ▶ $s->{request} ||= ASP4::Request->new();
Condition33%1/3
$s->{"request"} ||= "ASP4::Request"->new
l!l&&r!l&&!r
051450
MC/DC0%0/2
$s->{"request"} ||= "ASP4::Request"->new
$s->{"request"} "ASP4::Request"->new
Truth table33%1/3
A ||= B
A= $s->{"request"}B= "ASP4::Request"->new
ABresult
1X1
011
000
51 5145 ▶ $s->{response} ||= ASP4::Response->new();
Condition33%1/3
$s->{"response"} ||= "ASP4::Response"->new
l!l&&r!l&&!r
051450
MC/DC0%0/2
$s->{"response"} ||= "ASP4::Response"->new
$s->{"response"} "ASP4::Response"->new
Truth table33%1/3
A ||= B
A= $s->{"response"}B= "ASP4::Response"->new
ABresult
1X1
011
000
52 5145 ▶ $s->{server} ||= ASP4::Server->new();
Condition33%1/3
$s->{"server"} ||= "ASP4::Server"->new
l!l&&r!l&&!r
051450
MC/DC0%0/2
$s->{"server"} ||= "ASP4::Server"->new
$s->{"server"} "ASP4::Server"->new
Truth table33%1/3
A ||= B
A= $s->{"server"}B= "ASP4::Server"->new
ABresult
1X1
011
000
53
54 5145 ▶ if( $s->do_disable_session_state )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->do_disable_session_state) { } 2001 3144 5145
55 {
56 2001 ▶ $s->{session} ||= ASP4::SessionStateManager::NonPersisted->new( $s->r );
Condition33%1/3
$s->{"session"} ||= "ASP4::SessionStateManager::NonPersisted"->new($s->r)
l!l&&r!l&&!r
020010
MC/DC0%0/2
$s->{"session"} ||= "ASP4::SessionStateManager::NonPersisted"->new($s->r)
$s->{"session"} "ASP4::SessionStateManager::NonPersisted"->new($s->r)
Truth table33%1/3
A ||= B
A= $s->{"session"}B= "ASP4::SessionStateManager::NonPersisted"->new($s->r)
ABresult
1X1
011
000
57 }
58 else
59 {
60 3144 ▶ $s->{session} ||= $s->config->data_connections->session->manager->new( $s->r );
Condition33%1/3
$s->{"session"} ||= $s->config->data_connections->session->manager->new($s->r)
l!l&&r!l&&!r
031440
MC/DC0%0/2
$s->{"session"} ||= $s->config->data_connections->session->manager->new($s->r)
$s->{"session"} $s->config->data_connections->session->manager->new($s->r)
Truth table33%1/3
A ||= B
A= $s->{"session"}B= $s->config->data_connections->session->manager->new($s->r)
ABresult
1X1
011
000
61 }# end if()
62
63 5145 return $_instance;
64 }# end setup_request()
65  
66  
67 # Intrinsics:
68 109977 ▶sub current { $_instance || shift->new }
Subroutine current: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$ASP4::HTTPContext::_instance or (shift())->new 109977 0 109977
69 10296 ▶sub request { shift->{request} }
Subroutine request: called CC=1 SCAR=0.0
Pod documented
70 30845 ▶sub response { shift->{response} }
Subroutine response: called CC=1 SCAR=0.0
Pod documented
71 5153 ▶sub server { shift->{server} }
Subroutine server: called CC=1 SCAR=0.0
Pod documented
72 20546 ▶sub session { shift->{session} }
Subroutine session: called CC=1 SCAR=0.0
Pod documented
73 75303 ▶sub config { shift->{config} }
Subroutine config: called CC=1 SCAR=0.0
Pod documented
74 5147 ▶sub stash { shift->{stash} }
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
75  
76 # More advanced:
77 5145 ▶sub is_subrequest { shift->{is_subrequest} }
Subroutine is_subrequest: called CC=1 SCAR=0.0
Pod undocumented
78 5170 ▶sub cgi { shift->{cgi} }
Subroutine cgi: called CC=1 SCAR=0.0
Pod documented
79 74187 ▶sub r { shift->{r} }
Subroutine r: called CC=1 SCAR=0.0
Pod documented
80 10290 ▶sub handler { shift->{handler} }
Subroutine handler: called CC=1 SCAR=0.0
Pod undocumented
81 16470 ▶sub headers_out { shift->{headers_out} }
Subroutine headers_out: called CC=1 SCAR=0.0
Pod undocumented
82 0 ▶sub content_type { my $s = shift; $s->r->content_type( @_ ) }
Subroutine content_type: not called CC=1 SCAR=6.9
Pod undocumented
83 0 ▶sub status { my $s = shift; $s->r->status( @_ ) }
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
84 0 ▶sub did_send_headers { shift->{did_send_headers} }
Subroutine did_send_headers: not called CC=1 SCAR=6.9
Pod undocumented
85 33182 ▶sub did_end {
Subroutine did_end: called CC=2 SCAR=6.9
Pod undocumented
86 33182 my $s = shift;
87 33182 ▶ @_ ? $s->{did_end} = shift : $s->{did_end};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5148 28034 33182
88 }
89  
90 40654 ▶sub rprint {
Subroutine rprint: called CC=1 SCAR=0.0
Pod undocumented
91 40654 my ($s,$str) = @_;
92 40654 $s->buffer->add( $str );
93 }
94  
95 10288 ▶sub rflush {
Subroutine rflush: called CC=1 SCAR=0.0
Pod undocumented
96 10288 my $s = shift;
97 10288 $s->send_headers;
98 10288 $s->r->print( $s->buffer->data );
99 10288 $s->r->rflush;
100 10288 $s->rclear;
101 }
102  
103 10289 ▶sub rclear {
Subroutine rclear: called CC=1 SCAR=0.0
Pod undocumented
104 10289 my $s = shift;
105 10289 $s->buffer->clear;
106 }
107  
108 sub send_headers
109 10288 ▶{
Subroutine send_headers: called CC=3 SCAR=11.0
Pod undocumented
110 10288 my $s = shift;
111 10288 ▶ return if $s->{did_send_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"did_send_headers"} 5144 5144 10288
112
113 5144 my $headers = $s->headers_out;
114 5144 ▶ while( my ($k,$v) = each(%$headers) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 6205 5144 11349
115 {
116 6205 $s->r->err_headers_out->{$k} = $v;
117 }# end while()
118  
119 5144 $s->r->rflush;
120 5144 $s->{did_send_headers} = 1;
121 }# end send_headers()
122  
123 # Here be dragons:
124 61231 ▶sub buffer { shift->{buffer}->[-1] }
Subroutine buffer: called CC=1 SCAR=0.0
Pod undocumented
125 0 ▶sub add_buffer {
Subroutine add_buffer: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my $s = shift;
127 0 $s->rflush;
128 0 push @{$s->{buffer}}, ASP4::OutBuffer->new;
129 }
130 0 ▶sub purge_buffer { shift( @{shift->{buffer}} ) }
Subroutine purge_buffer: not called CC=1 SCAR=6.9
Pod undocumented
131  
132  
133 sub execute
134 5145 ▶{
Subroutine execute: called CC=10 SCAR=25.7
Pod undocumented
135 5145 my ($s, $args, $is_include) = @_;
136
137 5145 ▶ unless( $is_include )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_include) 5135 10 5145
138 {
139 # Set up and execute any matching request filters:
140 5135 my $resolver = $s->config->web->filter_resolver;
141 5135 foreach my $filter ( $resolver->new()->resolve_request_filters( $s->r->uri ) )
142 {
143 2 $s->config->load_class( $filter->class );
144 2 $filter->class->init_asp_objects( $s );
145 2 my $IS_FILTER = 1;
146 2 ▶ my $res = $s->handle_phase(sub{ $filter->class->new()->run( $s ) }, $IS_FILTER);
Subroutine __ANON__: called CC=1 SCAR=0.0
147 2 ▶ if( $s->did_end || ( defined($res) && $res != -1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->did_end or defined $res and $res != -1) 0 2 2
Condition33%2/6
$s->did_end or defined $res and $res != -1
l!l&&r!l&&!r
002
defined $res and $res != -1
!ll&&!rl&&r
200
MC/DC0%0/3
$s->did_end or defined $res and $res != -1
$s->did_end defined $res $res != -1
Truth table25%1/4
A or B and C
A= $s->did_endB= defined $resC= $res != -1
ABCresult
1XX1
0111
00X0
0100
148 {
149 0 return $res;
150 }# end if()
151 }# end foreach()
152 }# end unless()
153
154 5145 eval {
155 5145 $s->{handler} = $s->config->web->handler_resolver->new()->resolve_request_handler( $s->r->uri );
156 };
157
158 5145 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5145 5145
159 {
160 0 $s->server->{LastError} = $@;
161 0 return $s->handle_error;
162 }# end if()
163  
164 5145 ▶ return $s->response->Status( 404 ) unless $s->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"handler"} 0 5145 5145
165
166 5145 eval {
167 5145 $s->config->load_class( $s->handler );
168 5145 $s->config->web->handler_runner->new()->run_handler( $s->handler, $args );
169 };
170
171 5145 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5145 5145
172 {
173 0 $s->server->{LastError} = $@;
174 0 return $s->handle_error;
175 }# end if()
176
177 5145 $s->response->Flush;
178 5145 my $res = $s->end_request();
179
180 5145 ▶ $res = 0 if $res =~ m/^200/;
Branch: 1 branch
BranchTrueFalseTotal
if $res =~ /^200/ 0 5145 5145
181 5145 return $res;
182 }# end execute()
183  
184  
185 sub handle_phase
186 2 ▶{
Subroutine handle_phase: called CC=8 SCAR=31.1
Pod undocumented
187 2 my ($s, $ref, $is_filter) = @_;
188
189 2 my $res = eval { $ref->( ) };
190 2 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
191 {
192 0 $s->handle_error;
193 }# end if()
194
195 # Undef on success:
196 2 ▶ if( $is_filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_filter) { } 2 0 2
197 {
198 2 ▶ if( defined($res) && $res > -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res > -1) { } 0 2 2
Condition33%1/3
defined $res and $res > -1
!ll&&!rl&&r
020
MC/DC0%0/2
defined $res and $res > -1
defined $res $res > -1
Truth table33%1/3
A and B
A= defined $resB= $res > -1
ABresult
0X0
100
111
199 {
200 0 $s->response->Status( $res );
201 0 return $res;
202 }
203 else
204 {
205 2 return;
206 }# end if()
207 }
208 else
209 {
210 0 ▶ return if (! defined($res)) || $res == -1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $res or $res == -1 0 0 0
Condition0%0/3
not defined $res or $res == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $res or $res == -1
not defined $res $res == -1
Truth table0%0/3
not(A) or B
A= not defined $resB= $res == -1
ABresult
1X1
011
000
211 0 ▶ return $s->response->Status =~ m/^200/ ? undef : $s->response->Status;
Branch: 1 branch
BranchTrueFalseTotal
$s->response->Status =~ /^200/ ? : 0 0 0
212 }# end if()
213 }# end handle_phase()
214  
215  
216 sub handle_error
217 0 ▶{
Subroutine handle_error: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my $s = shift;
219
220 0 $s->response->Status( 500 );
221 0 $s->response->Clear();
222 0 my $err_str = $@;
223 0 my $error = $s->server->Error( $@ );
224 0 warn "[Error: @{[ HTTP::Date::time2iso() ]}] $err_str\n";
225
226 0 return $s->end_request;
227 }# end handle_error()
228  
229  
230 sub end_request
231 5145 ▶{
Subroutine end_request: called CC=2 SCAR=6.9
Pod undocumented
232 5145 my $s = shift;
233
234 5145 $s->response->End;
235 5145 ▶ my $res = $s->response->Status =~ m/^200/ ? 0 : $s->response->Status;
Branch: 1 branch
BranchTrueFalseTotal
$s->response->Status =~ /^200/ ? : 5142 3 5145
236
237 5145 return $res;
238 }# end end_request()
239  
240  
241 sub do_disable_session_state
242 5145 ▶{
Subroutine do_disable_session_state: called CC=3 SCAR=11.3
Pod undocumented
243 5145 my ($s) = @_;
244
245 # my ($uri) = split /\?/, $s->r->uri;
246 5145 ▶ my ($uri) = split /\?/, $ENV{REQUEST_URI} || $s->r->uri;
Condition66%2/3
$ENV{'REQUEST_URI'} || $s->r->uri
l!l&&r!l&&!r
5131140
MC/DC0%0/2
$ENV{'REQUEST_URI'} || $s->r->uri
$ENV{'REQUEST_URI'} $s->r->uri
Truth table66%2/3
A || B
A= $ENV{'REQUEST_URI'}B= $s->r->uri
ABresult
1X1
011
000
247 2001 my ($yes) = grep { $_->disable_session } grep {
248 5145 ▶ if( my $pattern = $_->uri_match )
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = $_->uri_match) { } 20580 0 20580
249 {
250 20580 $uri =~ m/^$pattern$/
251 }
252 else
253 {
254 0 $uri eq $_->uri_equals;
255 }# end if()
256 } $s->config->web->disable_persistence;
257
258 5145 return $yes;
259 }# end do_disable_session_state()
260  
261  
262 sub DESTROY
263 5133 ▶{
Subroutine DESTROY: called CC=3 SCAR=12.0
264 5133 my $s = shift;
265 5133 ▶ $s->session->save if $s->session && ! $s->session->is_read_only;
Branch: 1 branch
BranchTrueFalseTotal
if $s->session and not $s->session->is_read_only 5133 0 5133
Condition33%1/3
$s->session and not $s->session->is_read_only
!ll&&!rl&&r
005133
MC/DC0%0/2
$s->session and not $s->session->is_read_only
$s->session not $s->session->is_read_only
Truth table33%1/3
A and not(B)
A= $s->sessionB= not $s->session->is_read_only
ABresult
0X0
100
111
266 5133 $s = { };
267 5133 undef(%$s);
268 }# end DESTROY()
269  
270 15 1;# return true:
271  
272 =pod
273  
274 =head1 NAME
275  
276 ASP4::HTTPContext - Provides access to the intrinsic objects for an HTTP request.
277  
278 =head1 SYNOPSIS
279  
280 use ASP4::HTTPContext;
281
282 my $context = ASP4::HTTPContext->current;
283
284 # Intrinsics:
285 my $request = $context->request;
286 my $response = $context->response;
287 my $session = $context->session;
288 my $server = $context->server;
289 my $config = $context->config;
290 my $stash = $context->stash;
291
292 # Advanced:
293 my $cgi = $context->cgi;
294 my $r = $context->r;
295  
296 =head1 DESCRIPTION
297  
298 The HTTPContext itself is the root of all request-processing in an ASP4 web application.
299  
300 There is only one ASP4::HTTPContext instance throughout the lifetime of a request.
301  
302 =head1 PROPERTIES
303  
304 =head2 current
305  
306 Returns the C<ASP4::HTTPContext> object in use for the current HTTP request.
307  
308 =head2 request
309  
310 Returns the L<ASP4::Request> for the HTTP request.
311  
312 =head2 response
313  
314 Returns the L<ASP4::Response> for the HTTP request.
315  
316 =head2 server
317  
318 Returns the L<ASP4::Server> for the HTTP request.
319  
320 =head2 session
321  
322 Returns the L<ASP4::SessionStateManager> for the HTTP request.
323  
324 =head2 stash
325  
326 Returns the current stash hash in use for the HTTP request.
327  
328 =head2 config
329  
330 Returns the current C<ASP4::Config> for the HTTP request.
331  
332 =head2 cgi
333  
334 Provided B<Just In Case> - returns the L<CGI> object for the HTTP request.
335  
336 =head2 r
337  
338 Provided B<Just In Case> - returns the L<Apache2::RequestRec> for the HTTP request.
339  
340 B<NOTE:> Under L<ASP4::API> (eg: in a unit test) C<$r> will be an instance of L<ASP4::Mock::RequestRec> instead.
341  
342 =head1 BUGS
343  
344 It's possible that some bugs have found their way into this release.
345  
346 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
347  
348 =head1 HOMEPAGE
349  
350 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
351 of ASP4 in action.
352  
353 =cut
354  
« blib/lib/ASP4/PageParser.pm ↑ Summary blib/lib/ASP4/Response.pm »