ASP/HTTPContext.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ASP/Test/Base.pm ↑ Summary ASP/Test/UserAgent.pm »
1  
2 package Apache2::ASP::HTTPContext;
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Apache2::ASP::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Apache2::ASP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Apache2::ASP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Apache2::ASP::Server;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Carp qw( cluck confess );
11 use Scalar::Util 'weaken';
12 use HTTP::Headers;
13  
14 use Apache2::ASP::SessionStateManager::NonPersisted;
15 use Apache2::ASP::ApplicationStateManager::NonPersisted;
16  
17 our $instance;
18 our $ClassName = __PACKAGE__;
19 our %StartedServers = ( );
20  
21 #==============================================================================
22 sub current
23 {
24 my $class = shift;
25
26 return $instance;
27 }# end current()
28  
29  
30 #==============================================================================
31 sub new
32 {
33 my ($class, %args) = @_;
34
35 my $s = bless {
36 config => Apache2::ASP::ConfigLoader->load(),
37 }, $class;
38 $s->config->_init_inc();
39
40 return $instance = $s;
41 }# end new()
42  
43  
44 #==============================================================================
45 sub setup_request
46 {
47 my ($s, $requestrec, $cgi) = @_;
48
49 $s->{_is_setup}++;
50
51 $s->{r} = $requestrec;
52 $s->{cgi} = $cgi;
53  
54 $s->_setup_headers_out();
55 $s->_setup_headers_in();
56
57 $s->{connection} = $s->r->connection;
58
59 $s->{response} = Apache2::ASP::Response->new();
60 $s->{request} = Apache2::ASP::Request->new();
61 $s->{server} = Apache2::ASP::Server->new();
62  
63 my $conns = $s->config->data_connections;
64 if( $s->do_disable_application_state )
65 {
66 $s->{application} = Apache2::ASP::ApplicationStateManager::NonPersisted->new();
67 }
68 else
69 {
70 my $app_manager = $conns->application->manager;
71 $s->_load_class( $app_manager );
72 $s->{application} = $app_manager->new();
73 }# end if()
74
75 if( $s->do_disable_session_state )
76 {
77 $s->{session} = Apache2::ASP::SessionStateManager::NonPersisted->new();
78 }
79 else
80 {
81 my $session_manager = $conns->session->manager;
82 $s->_load_class( $session_manager );
83 $s->{session} = $session_manager->new();
84 }# end if()
85
86 # Make the global Stash object:
87 $s->{stash} = { };
88
89 $s->{global_asa} = $s->resolve_global_asa_class( );
90 {
91 no warnings 'uninitialized';
92 $s->{global_asa}->init_asp_objects( $s )
93 unless $s->r->headers_in->{'content-type'} =~ m/multipart/;
94 }
95
96 $s->_load_class( $s->config->web->handler_resolver );
97 eval {
98 $s->{handler} = $s->config->web->handler_resolver->new()->resolve_request_handler( $s->r->uri );
99 };
100 if( $@ )
101 {
102 $s->server->{LastError} = $@;
103 return $s->handle_error;
104 }# end if()
105
106 return 1;
107 }# end setup_request()
108  
109  
110 #==============================================================================
111 sub _setup_headers_out
112 {
113 my ($s) = @_;
114
115 $s->{headers_out} = HTTP::Headers->new();
116 }# end _setup_headers_out()
117  
118  
119 #==============================================================================
120 sub _setup_headers_in
121 {
122 my ($s) = @_;
123
124 my $h = $s->r->headers_in;
125 if( UNIVERSAL::isa($h, 'HTTP::Headers') )
126 {
127 $s->{headers_in} = $h;
128 }
129 else
130 {
131 my $headers_in = HTTP::Headers->new();
132 while( my ($k,$v) = each(%$h) )
133 {
134 $headers_in->push_header( $k => $v );
135 }# end while()
136 $s->{headers_in} = $headers_in;
137 }# end if()
138 }# end _setup_headers_in()
139  
140  
141 #==============================================================================
142 sub do_disable_session_state
143 {
144 my ($s) = @_;
145
146 my ($uri) = split /\?/, $s->r->uri;
147 my ($yes) = grep { $_->disable_session } grep {
148 if( my $pattern = $_->uri_match )
149 {
150 $uri =~ m/$pattern/
151 }
152 else
153 {
154 $uri eq $_->uri_equals;
155 }# end if()
156 } $s->config->web->disable_persistence;
157
158 return $yes;
159 }# end do_disable_session_state()
160  
161  
162 #==============================================================================
163 sub do_disable_application_state
164 {
165 my ($s) = @_;
166
167 my ($uri) = split /\?/, $s->r->uri;
168 my ($yes) = grep { $_->disable_application } grep {
169 if( my $pattern = $_->uri_match )
170 {
171 $uri =~ m/$pattern/
172 }
173 else
174 {
175 $uri eq $_->uri_equals;
176 }# end if()
177 } $s->config->web->disable_persistence;
178
179 return $yes;
180 }# end do_disable_application_state()
181  
182  
183 #==============================================================================
184 sub execute
185 {
186 my ($s, $args) = @_;
187  
188 # local $SIG{__DIE__} = \&Carp::confess;
189  
190 return 404 unless $s->handler;
191
192 if( defined(my $preinit_res = $s->do_preinit) )
193 {
194 return $preinit_res;
195 }# end if()
196
197 # Set up and execute any matching request filters:
198 my $resolver = $s->config->web->filter_resolver;
199 $s->_load_class( $resolver );
200 foreach my $filter ( $resolver->new()->resolve_request_filters( $s->r->uri ) )
201 {
202 $s->_load_class( $filter->class );
203 $filter->class->init_asp_objects( $s );
204 my $res = $s->handle_phase(sub{ $filter->class->new()->run( $s ) });
205 if( defined($res) && $res != -1 )
206 {
207 return $res;
208 }# end if()
209 }# end foreach()
210
211 my $start_res = $s->handle_phase( $s->global_asa->can('Script_OnStart') );
212 return $start_res if defined( $start_res );
213
214 $s->_load_class( $s->config->web->handler_runner );
215 eval {
216 $s->_load_class( $s->handler );
217 $s->config->web->handler_runner->new()->run_handler( $s->handler, $args );
218 };
219 if( $@ )
220 {
221 $s->server->{LastError} = $@;
222 return $s->handle_error;
223 }# end if()
224
225 $s->response->Flush;
226 my $res = $s->end_request();
227 # if( $s->page && $s->page->directives->{OutputCache} && defined($s->{_cache_buffer}) )
228 # {
229 # if( $res == 200 || $res == 0 )
230 # {
231 # $s->page->_write_cache( \$s->{_cache_buffer} );
232 # }# end if()
233 # }# end if()
234
235 $res = 0 if $res =~ m/^200/;
236 return $res;
237 }# end execute()
238  
239  
240 #==============================================================================
241 #sub _setup_inc
242 #{
243 # my $s = shift;
244 #
245 # my $www_root = $s->config->web->www_root;
246 # push @INC, $www_root unless grep { $_ eq $www_root } @INC;
247 # my %libs = map { $_ => 1 } @INC;
248 # push @INC, grep { ! $libs{$_} } $s->config->system->libs;
249 #}# end _setup_inc()
250  
251  
252 #==============================================================================
253 sub do_preinit
254 {
255 my $s = shift;
256
257 unless( $s->_is_setup )
258 {
259 $s->setup_request( $Apache2::ASP::ModPerl::R, $Apache2::ASP::ModPerl::CGI );
260 }# end unless()
261
262 # Initialize the Server, Application and Session:
263 unless( $StartedServers{ $s->config->web->application_name } )
264 {
265 my $res = $s->handle_phase(
266 $s->global_asa->can('Server_OnStart')
267 );
268 $StartedServers{ $s->config->web->application_name }++
269 unless $@;
270 return $s->end_request if $s->{did_end};
271 }# end unless()
272
273 unless( $s->application->{__Application_Started} )
274 {
275 my $res = $s->handle_phase(
276 $s->global_asa->can('Application_OnStart')
277 );
278 $s->application->{__Application_Started}++ unless $@;
279 return $s->end_request if $s->{did_end};
280 }# end unless()
281
282 unless( $s->session->{__Started} )
283 {
284 my $res = $s->handle_phase(
285 $s->global_asa->can('Session_OnStart')
286 );
287 $s->session->{__Started}++ unless $@;
288 return $s->end_request if $s->{did_end};
289 }# end unless()
290
291 return;
292 }# end do_preinit()
293  
294  
295 #==============================================================================
296 sub handle_phase
297 {
298 my ($s, $ref) = @_;
299
300 eval { $ref->( ) };
301 if( $@ )
302 {
303 $s->handle_error;
304 }# end if()
305
306 # Undef on success:
307 return $s->response->Status =~ m/^200/ ? undef : $s->response->Status;
308 }# end handle_phase()
309  
310  
311 #==============================================================================
312 sub handle_error
313 {
314 my $s = shift;
315
316 my $error = "$@";
317 $s->response->Status( 500 );
318 no strict 'refs';
319  
320 $s->response->Clear;
321 my ($main, $title, $file, $line) = $error =~ m/^((.*?)\s(?:at|in)\s(.*?)\sline\s(\d+))/;
322 $s->stash->{error} = {
323 title => $title,
324 file => $file,
325 line => $line,
326 stacktrace => $error,
327 };
328 warn "[Error: @{[ HTTP::Date::time2iso() ]}] $main\n";
329
330 $s->_load_class( $s->config->errors->error_handler );
331 my $error_handler = $s->config->errors->error_handler->new();
332 $error_handler->init_asp_objects( $s );
333 eval { $error_handler->run( $s ) };
334 confess $@ if $@;
335
336 return $s->end_request;
337 }# end handle_error()
338  
339  
340 #==============================================================================
341 sub end_request
342 {
343 my $s = shift;
344
345 $s->handle_phase( $s->global_asa->can('Script_OnEnd') )
346 unless $s->server->GetLastError;
347
348 $s->response->End;
349 $s->session->save;
350 $s->application->save;
351 my $res = $s->response->Status =~ m/^200/ ? 0 : $s->response->Status;
352
353 return $res;
354 }# end end_request()
355  
356  
357 #==============================================================================
358 sub clone
359 {
360 my $s = shift;
361
362 return bless {%$s}, ref($s);
363 }# end clone()
364  
365  
366 #==============================================================================
367 sub get_prop
368 {
369 my ($s, $prop) = @_;
370
371 $s->{parent} ? $s->{parent}->get_prop($prop) : $s->{$prop};
372 }# end get_prop()
373  
374  
375 #==============================================================================
376 sub set_prop
377 {
378 my ($s) = shift;
379 my $prop = shift;
380
381 $s->{parent} ? $s->{parent}->set_prop($prop, @_) : $s->{$prop} = shift;
382 }# end set_prop()
383  
384 sub config { $_[0]->get_prop('config') }
385 sub session { $_[0]->get_prop('session') }
386 sub server { $_[0]->get_prop('server') }
387 sub request { $_[0]->get_prop('request') }
388 sub response { $_[0]->get_prop('response') }
389 sub application { $_[0]->get_prop('application') }
390 sub stash { $_[0]->get_prop('stash') }
391 sub global_asa { $_[0]->get_prop('global_asa') }
392 sub _is_setup { $_[0]->get_prop('_is_setup') }
393  
394 sub r { $_[0]->{r} }
395 sub cgi
396 {
397 my $s = shift;
398 $s->{cgi} ||= Apache2::ASP::SimpleCGI->new(
399 querystring => $s->r->args
400 );
401 return $s->{cgi};
402 }
403 sub handler { $_[0]->{handler} }
404 sub connection { $_[0]->{connection} }
405 sub page { $_[0]->{page} }
406  
407 sub headers_in { shift->get_prop('headers_in') }
408 sub send_headers
409 {
410 my $s = shift;
411 # return if $s->{_did_send_headers};
412 return if $s->get_prop('_did_send_headers');
413
414 my $headers = $s->get_prop('headers_out');
415 my $r = $s->get_prop('r');
416 while( my ($k,$v) = each(%$headers) )
417 {
418 $r->err_headers_out->{$k} = $v;
419 }# end while()
420  
421 $r->rflush;
422 # $s->{_did_send_headers} = 1;
423 $s->set_prop(_did_send_headers => 1);
424 }# end send_headers()
425  
426 sub headers_out { shift->get_prop('headers_out') }
427 sub content_type { shift->get_prop('r')->content_type( @_ ) }
428  
429  
430 sub print
431 {
432 my ($s, $str) = @_;
433
434 $s->send_headers unless $s->get_prop('_did_send_headers');
435 return unless defined($str);
436 eval {
437 $s->{r}->print( $str );
438 };
439 }# end print()
440  
441  
442  
443 #==============================================================================
444 sub rflush
445 {
446 my $s = shift;
447
448 $s->send_headers
449 unless $s->did_send_headers;
450 eval {
451 $s->{r}->rflush();
452 };
453 }# end rflush()
454  
455 sub did_send_headers { shift->get_prop('_did_send_headers') }
456  
457  
458 #==============================================================================
459 sub resolve_global_asa_class
460 {
461 my $s = shift;
462
463 my $file = $s->config->web->www_root . '/GlobalASA.pm';
464 my $class;
465 if( -f $file )
466 {
467 $class = $s->config->web->application_name . '::GlobalASA';
468 eval { require $file };
469 confess $@ if $@;
470 }
471 else
472 {
473 $class = 'Apache2::ASP::GlobalASA';
474 $s->_load_class( $class );
475 }# end if()
476
477 return $class;
478 }# end resolve_global_asa_class()
479  
480  
481 #==============================================================================
482 sub _load_class
483 {
484 my ($s, $class) = @_;
485
486 (my $file = "$class.pm") =~ s/::/\//g;
487 eval { require $file; 1 }
488 or confess "Cannot load $class: $@";
489 }# end _load_class()
490  
491  
492 #==============================================================================
493 sub AUTOLOAD
494 {
495 my $s = shift;
496
497 our $AUTOLOAD;
498 my ($key) = $AUTOLOAD =~ m/([^:]+)$/;
499 @_ ? $s->set_prop( $key, shift ) : $s->get_prop( $key );
500 }# end AUTOLOAD()
501  
502  
503 #==============================================================================
504 sub DESTROY
505 {
506 my $s = shift;
507
508 undef(%$s);
509 }# end DESTROY()
510  
511 1;# return true:
512  
513  
514 =pod
515  
516 =head1 NAME
517  
518 Apache2::ASP::HTTPContext - Contextual execution harness for ASP scripts.
519  
520 =head1 SYNOPSIS
521  
522 # Get the original mod_perl '$r' object:
523 my Apache2::RequestRec $r = $context->r;
524
525 # Get the other traditional ASP objects:
526 my $Config = $context->config;
527 my $Request = $context->request;
528 my $Response = $context->response;
529 my $Server = $context->server;
530 my $Session = $context->session;
531 my $Application = $context->application;
532
533 # Get the current context object from anywhere within your application:
534 my $context = Apache2::ASP::HTTPContext->current;
535  
536 =head1 DESCRIPTION
537  
538 =head1 STATIC PROPERTIES
539  
540 =head2 current
541  
542 Returns the "current" HTTPContext instance.
543  
544 =head1 PUBLIC PROPERTIES
545  
546 =head2 r
547  
548 Returns the current Apache2::RequestRec object.
549  
550 B<NOTE>: while in "API" or "Testing" mode, C<r> returns the current
551 L<Apache2::ASP::Mock::RequestRec> object.
552  
553 =head2 config
554  
555 Returns the current L<Apache2::ASP::Config> object.
556  
557 =head2 request
558  
559 Returns the current L<Apache2::ASP::Request> object.
560  
561 =head2 response
562  
563 Returns the current L<Apache2::ASP::Response> object.
564  
565 =head2 server
566  
567 Returns the current L<Apache2::ASP::Server> object.
568  
569 =head2 session
570  
571 Returns the current L<Apache2::ASP::Session> object.
572  
573 =head2 application
574  
575 Returns the current L<Apache2::ASP::Application> object.
576
577 =head1 BUGS
578
579 It's possible that some bugs have found their way into this release.
580
581 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports.
582
583 =head1 HOMEPAGE
584
585 Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples
586 of Apache2::ASP in action.
587  
588 =head1 AUTHOR
589  
590 John Drago <jdrago_999@yahoo.com>
591  
592 =head1 COPYRIGHT
593  
594 Copyright 2008 John Drago. All rights reserved.
595  
596 =head1 LICENSE
597  
598 This software is Free software and is licensed under the same terms as perl itself.
599  
600 =cut
601  
« ASP/Test/Base.pm ↑ Summary ASP/Test/UserAgent.pm »