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 23 ▶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 if( defined(my $preinit_res = $s->do_preinit) )
191 {
192 return $preinit_res;
193 }# end if()
194
195 # Set up and execute any matching request filters:
196 my $resolver = $s->config->web->filter_resolver;
197 $s->_load_class( $resolver );
198 foreach my $filter ( $resolver->new()->resolve_request_filters( $s->r->uri ) )
199 {
200 $s->_load_class( $filter->class );
201 $filter->class->init_asp_objects( $s );
202 my $res = $s->handle_phase(sub{ $filter->class->new()->run( $s ) });
203 if( defined($res) && $res != -1 )
204 {
205 return $res;
206 }# end if()
207 }# end foreach()
208
209 my $start_res = $s->handle_phase( $s->global_asa->can('Script_OnStart') );
210 return $start_res if defined( $start_res );
211
212 $s->_load_class( $s->config->web->handler_runner );
213 eval {
214 $s->_load_class( $s->handler );
215 $s->config->web->handler_runner->new()->run_handler( $s->handler, $args );
216 };
217 if( $@ )
218 {
219 $s->server->{LastError} = $@;
220 return $s->handle_error;
221 }# end if()
222
223 $s->response->Flush;
224 my $res = $s->end_request();
225 # if( $s->page && $s->page->directives->{OutputCache} && defined($s->{_cache_buffer}) )
226 # {
227 # if( $res == 200 || $res == 0 )
228 # {
229 # $s->page->_write_cache( \$s->{_cache_buffer} );
230 # }# end if()
231 # }# end if()
232
233 $res = 0 if $res =~ m/^200/;
234 return $res;
235 }# end execute()
236  
237  
238 #==============================================================================
239 #sub _setup_inc
240 #{
241 # my $s = shift;
242 #
243 # my $www_root = $s->config->web->www_root;
244 # push @INC, $www_root unless grep { $_ eq $www_root } @INC;
245 # my %libs = map { $_ => 1 } @INC;
246 # push @INC, grep { ! $libs{$_} } $s->config->system->libs;
247 #}# end _setup_inc()
248  
249  
250 #==============================================================================
251 sub do_preinit
252 {
253 my $s = shift;
254
255 unless( $s->_is_setup )
256 {
257 $s->setup_request( $Apache2::ASP::ModPerl::R, $Apache2::ASP::ModPerl::CGI );
258 }# end unless()
259
260 # Initialize the Server, Application and Session:
261 unless( $StartedServers{ $s->config->web->application_name } )
262 {
263 my $res = $s->handle_phase(
264 $s->global_asa->can('Server_OnStart')
265 );
266 $StartedServers{ $s->config->web->application_name }++
267 unless $@;
268 return $s->end_request if $s->{did_end};
269 }# end unless()
270
271 unless( $s->application->{__Application_Started} )
272 {
273 my $res = $s->handle_phase(
274 $s->global_asa->can('Application_OnStart')
275 );
276 $s->application->{__Application_Started}++ unless $@;
277 return $s->end_request if $s->{did_end};
278 }# end unless()
279
280 unless( $s->session->{__Started} )
281 {
282 my $res = $s->handle_phase(
283 $s->global_asa->can('Session_OnStart')
284 );
285 $s->session->{__Started}++ unless $@;
286 return $s->end_request if $s->{did_end};
287 }# end unless()
288
289 return;
290 }# end do_preinit()
291  
292  
293 #==============================================================================
294 sub handle_phase
295 {
296 my ($s, $ref) = @_;
297
298 eval { $ref->( ) };
299 if( $@ )
300 {
301 $s->handle_error;
302 }# end if()
303
304 # Undef on success:
305 return $s->response->Status =~ m/^200/ ? undef : $s->response->Status;
306 }# end handle_phase()
307  
308  
309 #==============================================================================
310 sub handle_error
311 {
312 my $s = shift;
313
314 my $error = "$@";
315 $s->response->Status( 500 );
316 no strict 'refs';
317  
318 $s->response->Clear;
319 my ($main, $title, $file, $line) = $error =~ m/^((.*?)\s(?:at|in)\s(.*?)\sline\s(\d+))/;
320 $s->stash->{error} = {
321 title => $title,
322 file => $file,
323 line => $line,
324 stacktrace => $error,
325 };
326 warn "[Error: @{[ HTTP::Date::time2iso() ]}] $main\n";
327
328 $s->_load_class( $s->config->errors->error_handler );
329 my $error_handler = $s->config->errors->error_handler->new();
330 $error_handler->init_asp_objects( $s );
331 eval { $error_handler->run( $s ) };
332 confess $@ if $@;
333
334 return $s->end_request;
335 }# end handle_error()
336  
337  
338 #==============================================================================
339 sub end_request
340 {
341 my $s = shift;
342
343 $s->handle_phase( $s->global_asa->can('Script_OnEnd') )
344 unless $s->server->GetLastError;
345
346 $s->response->End;
347 $s->session->save;
348 $s->application->save;
349 my $res = $s->response->Status =~ m/^200/ ? 0 : $s->response->Status;
350
351 return $res;
352 }# end end_request()
353  
354  
355 #==============================================================================
356 sub clone
357 {
358 my $s = shift;
359
360 return bless {%$s}, ref($s);
361 }# end clone()
362  
363  
364 #==============================================================================
365 sub get_prop
366 {
367 my ($s, $prop) = @_;
368
369 $s->{parent} ? $s->{parent}->get_prop($prop) : $s->{$prop};
370 }# end get_prop()
371  
372  
373 #==============================================================================
374 sub set_prop
375 {
376 my ($s) = shift;
377 my $prop = shift;
378
379 $s->{parent} ? $s->{parent}->set_prop($prop, @_) : $s->{$prop} = shift;
380 }# end set_prop()
381  
382 sub config { $_[0]->get_prop('config') }
383 sub session { $_[0]->get_prop('session') }
384 sub server { $_[0]->get_prop('server') }
385 sub request { $_[0]->get_prop('request') }
386 sub response { $_[0]->get_prop('response') }
387 sub application { $_[0]->get_prop('application') }
388 sub stash { $_[0]->get_prop('stash') }
389 sub global_asa { $_[0]->get_prop('global_asa') }
390 sub _is_setup { $_[0]->get_prop('_is_setup') }
391  
392 sub r { $_[0]->{r} }
393 sub cgi
394 {
395 my $s = shift;
396 $s->{cgi} ||= Apache2::ASP::SimpleCGI->new(
397 querystring => $s->r->args
398 );
399 return $s->{cgi};
400 }
401 sub handler { $_[0]->{handler} }
402 sub connection { $_[0]->{connection} }
403 sub page { $_[0]->{page} }
404  
405 sub headers_in { shift->get_prop('headers_in') }
406 sub send_headers
407 {
408 my $s = shift;
409 return if $s->{_did_send_headers};
410
411 my $headers = $s->get_prop('headers_out');
412 my $r = $s->get_prop('r');
413 while( my ($k,$v) = each(%$headers) )
414 {
415 $r->err_headers_out->{$k} = $v;
416 }# end while()
417
418 $r->rflush();
419 $s->{_did_send_headers}++;
420 }# end send_headers()
421  
422 sub headers_out { shift->get_prop('headers_out') }
423 sub content_type { shift->get_prop('r')->content_type( @_ ) }
424  
425  
426 sub print
427 {
428 my ($s, $str) = @_;
429
430 return unless defined($str);
431 $s->{r}->print( $str );
432 }# end print()
433  
434  
435  
436 #==============================================================================
437 sub rflush
438 {
439 my $s = shift;
440
441 $s->send_headers
442 unless $s->did_send_headers;
443
444 $s->{r}->rflush();
445 }# end rflush()
446  
447 sub did_send_headers { shift->get_prop('_did_send_headers') }
448  
449  
450 #==============================================================================
451 sub resolve_global_asa_class
452 {
453 my $s = shift;
454
455 my $file = $s->config->web->www_root . '/GlobalASA.pm';
456 my $class;
457 if( -f $file )
458 {
459 $class = $s->config->web->application_name . '::GlobalASA';
460 eval { require $file };
461 confess $@ if $@;
462 }
463 else
464 {
465 $class = 'Apache2::ASP::GlobalASA';
466 $s->_load_class( $class );
467 }# end if()
468
469 return $class;
470 }# end resolve_global_asa_class()
471  
472  
473 #==============================================================================
474 sub _load_class
475 {
476 my ($s, $class) = @_;
477
478 (my $file = "$class.pm") =~ s/::/\//g;
479 eval { require $file; 1 }
480 or confess "Cannot load $class: $@";
481 }# end _load_class()
482  
483  
484 #==============================================================================
485 sub AUTOLOAD
486 {
487 my $s = shift;
488
489 our $AUTOLOAD;
490 my ($key) = $AUTOLOAD =~ m/([^:]+)$/;
491 @_ ? $s->set_prop( $key, shift ) : $s->get_prop( $key );
492 }# end AUTOLOAD()
493  
494  
495 #==============================================================================
496 sub DESTROY
497 {
498 my $s = shift;
499
500 undef(%$s);
501 }# end DESTROY()
502  
503 1;# return true:
504  
505  
506 =pod
507  
508 =head1 NAME
509  
510 Apache2::ASP::HTTPContext - Contextual execution harness for ASP scripts.
511  
512 =head1 SYNOPSIS
513  
514 # Get the original mod_perl '$r' object:
515 my Apache2::RequestRec $r = $context->r;
516
517 # Get the other traditional ASP objects:
518 my $Config = $context->config;
519 my $Request = $context->request;
520 my $Response = $context->response;
521 my $Server = $context->server;
522 my $Session = $context->session;
523 my $Application = $context->application;
524
525 # Get the current context object from anywhere within your application:
526 my $context = Apache2::ASP::HTTPContext->current;
527  
528 =head1 DESCRIPTION
529  
530 =head1 STATIC PROPERTIES
531  
532 =head2 current
533  
534 Returns the "current" HTTPContext instance.
535  
536 =head1 PUBLIC PROPERTIES
537  
538 =head2 r
539  
540 Returns the current Apache2::RequestRec object.
541  
542 B<NOTE>: while in "API" or "Testing" mode, C<r> returns the current
543 L<Apache2::ASP::Mock::RequestRec> object.
544  
545 =head2 config
546  
547 Returns the current L<Apache2::ASP::Config> object.
548  
549 =head2 request
550  
551 Returns the current L<Apache2::ASP::Request> object.
552  
553 =head2 response
554  
555 Returns the current L<Apache2::ASP::Response> object.
556  
557 =head2 server
558  
559 Returns the current L<Apache2::ASP::Server> object.
560  
561 =head2 session
562  
563 Returns the current L<Apache2::ASP::Session> object.
564  
565 =head2 application
566  
567 Returns the current L<Apache2::ASP::Application> object.
568
569 =head1 BUGS
570
571 It's possible that some bugs have found their way into this release.
572
573 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports.
574
575 =head1 HOMEPAGE
576
577 Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples
578 of Apache2::ASP in action.
579  
580 =head1 AUTHOR
581  
582 John Drago <jdrago_999@yahoo.com>
583  
584 =head1 COPYRIGHT
585  
586 Copyright 2008 John Drago. All rights reserved.
587  
588 =head1 LICENSE
589  
590 This software is Free software and is licensed under the same terms as perl itself.
591  
592 =cut
593  
« ASP/Test/Base.pm ↑ Summary ASP/Test/UserAgent.pm »