| 1 | |||
| 2 | package ASP4::HTTPContext; | ||
| 3 | |||
| 4 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use HTTP::Date (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use HTTP::Headers (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 9 | ▶ | use ASP4::ConfigLoader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 9 | ▶ | use ASP4::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 9 | ▶ | use ASP4::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 9 | ▶ | use ASP4::Server; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use ASP4::OutBuffer; | ||
| 13 | use ASP4::SessionStateManager::NonPersisted; | ||
| 14 | |||
| 15 | use vars '$_instance'; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my ($class) = @_; | ||
| 20 | |||
| 21 | my $s = bless { | ||
| 22 | config => ASP4::ConfigLoader->load, | ||
| 23 | buffer => [ ASP4::OutBuffer->new ], | ||
| 24 | stash => { }, | ||
| 25 | headers_out => HTTP::Headers->new(), | ||
| 26 | }, $class; | ||
| 27 | $s->config->_init_inc(); | ||
| 28 | |||
| 29 | $s->config->load_class( $s->config->web->handler_resolver ); | ||
| 30 | $s->config->load_class( $s->config->web->handler_runner ); | ||
| 31 | $s->config->load_class( $s->{config}->data_connections->session->manager ); | ||
| 32 | $s->config->load_class( $s->config->web->filter_resolver ); | ||
| 33 | |||
| 34 | return $s; | ||
| 35 | }# end new() | ||
| 36 | |||
| 37 | |||
| 38 | sub setup_request | ||
| 39 | { | ||
| 40 | my ($s, $r, $cgi) = @_; | ||
| 41 | |||
| 42 | $s->{r} = $r; | ||
| 43 | $s->{cgi} = $cgi; | ||
| 44 | |||
| 45 | # Must instantiate $_instance before creating the other objects: | ||
| 46 | $_instance = $s; | ||
| 47 | $s->{request} ||= ASP4::Request->new(); | ||
| 48 | $s->{response} ||= ASP4::Response->new(); | ||
| 49 | $s->{server} ||= ASP4::Server->new(); | ||
| 50 | |||
| 51 | my $do_session_onstart; | ||
| 52 | if( $s->do_disable_session_state ) | ||
| 53 | { | ||
| 54 | $s->{session} ||= ASP4::SessionStateManager::NonPersisted->new( $s->r ); | ||
| 55 | } | ||
| 56 | else | ||
| 57 | { | ||
| 58 | $s->{session} ||= $s->config->data_connections->session->manager->new( $s->r ); | ||
| 59 | $do_session_onstart++; | ||
| 60 | }# end if() | ||
| 61 | |||
| 62 | $s->{global_asa} = $s->resolve_global_asa_class( ); | ||
| 63 | { | ||
| 64 | no warnings 'uninitialized'; | ||
| 65 | $s->{global_asa}->init_asp_objects( $s ); | ||
| 66 | if( $do_session_onstart ) | ||
| 67 | { | ||
| 68 | unless( $s->session->{__started} ) | ||
| 69 | { | ||
| 70 | $s->handle_phase( $s->global_asa->can('Session_OnStart') ); | ||
| 71 | $s->session->{__started} = 1; | ||
| 72 | }# end unless() | ||
| 73 | }# end if() | ||
| 74 | } | ||
| 75 | |||
| 76 | eval { | ||
| 77 | $s->{handler} = $s->config->web->handler_resolver->new()->resolve_request_handler( $r->uri ); | ||
| 78 | }; | ||
| 79 | if( $@ ) | ||
| 80 | { | ||
| 81 | $s->server->{LastError} = $@; | ||
| 82 | return $s->handle_error; | ||
| 83 | }# end if() | ||
| 84 | |||
| 85 | return $_instance; | ||
| 86 | }# end setup_request() | ||
| 87 | |||
| 88 | |||
| 89 | # Intrinsics: | ||
| 90 | sub current { $_instance || shift->new } | ||
| 91 | sub request { shift->{request} } | ||
| 92 | sub response { shift->{response} } | ||
| 93 | sub server { shift->{server} } | ||
| 94 | sub session { shift->{session} } | ||
| 95 | sub config { shift->{config} } | ||
| 96 | sub stash { shift->{stash} } | ||
| 97 | |||
| 98 | # More advanced: | ||
| 99 | sub cgi { shift->{cgi} } | ||
| 100 | sub r { shift->{r} } | ||
| 101 | sub global_asa { shift->{global_asa} } | ||
| 102 | sub handler { shift->{handler} } | ||
| 103 | sub headers_out { shift->{headers_out} } | ||
| 104 | sub content_type { my $s = shift; $s->r->content_type( @_ ) } | ||
| 105 | sub status { my $s = shift; $s->r->status( @_ ) } | ||
| 106 | sub did_send_headers { shift->{did_send_headers} } | ||
| 107 | sub did_end { | ||
| 108 | my $s = shift; | ||
| 109 | @_ ? $s->{did_end} = shift : $s->{did_end}; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub rprint { | ||
| 113 | my ($s,$str) = @_; | ||
| 114 | $s->buffer->add( $str ) | ||
| 115 | } | ||
| 116 | |||
| 117 | sub rflush { | ||
| 118 | my $s = shift; | ||
| 119 | $s->send_headers; | ||
| 120 | $s->r->print( $s->buffer->data ); | ||
| 121 | $s->r->rflush; | ||
| 122 | $s->rclear; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub rclear { | ||
| 126 | my $s = shift; | ||
| 127 | $s->buffer->clear; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub send_headers | ||
| 131 | { | ||
| 132 | my $s = shift; | ||
| 133 | return if $s->{did_send_headers}; | ||
| 134 | |||
| 135 | my $headers = $s->headers_out; | ||
| 136 | while( my ($k,$v) = each(%$headers) ) | ||
| 137 | { | ||
| 138 | $s->r->err_headers_out->{$k} = $v; | ||
| 139 | }# end while() | ||
| 140 | |||
| 141 | $s->r->rflush; | ||
| 142 | $s->{did_send_headers} = 1; | ||
| 143 | }# end send_headers() | ||
| 144 | |||
| 145 | # Here be dragons: | ||
| 146 | sub buffer { shift->{buffer}->[-1] } | ||
| 147 | sub add_buffer { | ||
| 148 | my $s = shift; | ||
| 149 | $s->rflush; | ||
| 150 | push @{$s->{buffer}}, ASP4::OutBuffer->new; | ||
| 151 | } | ||
| 152 | sub purge_buffer { shift( @{shift->{buffer}} ) } | ||
| 153 | |||
| 154 | |||
| 155 | sub execute | ||
| 156 | { | ||
| 157 | my ($s, $args, $is_include) = @_; | ||
| 158 | |||
| 159 | return $s->response->Status( 404 ) unless $s->{handler}; | ||
| 160 | |||
| 161 | unless( $is_include ) | ||
| 162 | { | ||
| 163 | # Set up and execute any matching request filters: | ||
| 164 | my $resolver = $s->config->web->filter_resolver; | ||
| 165 | foreach my $filter ( $resolver->new()->resolve_request_filters( $s->r->uri ) ) | ||
| 166 | { | ||
| 167 | $s->config->load_class( $filter->class ); | ||
| 168 | $filter->class->init_asp_objects( $s ); | ||
| 169 | my $res = $s->handle_phase(sub{ $filter->class->new()->run( $s ) }); | ||
| 170 | if( defined($res) && $res != -1 ) | ||
| 171 | { | ||
| 172 | return $res; | ||
| 173 | }# end if() | ||
| 174 | }# end foreach() | ||
| 175 | |||
| 176 | my $start_res = $s->handle_phase( $s->global_asa->can('Script_OnStart') ); | ||
| 177 | return $start_res if defined( $start_res ); | ||
| 178 | }# end unless() | ||
| 179 | |||
| 180 | eval { | ||
| 181 | $s->config->load_class( $s->handler ); | ||
| 182 | $s->config->web->handler_runner->new()->run_handler( $s->handler, $args ); | ||
| 183 | }; | ||
| 184 | if( $@ ) | ||
| 185 | { | ||
| 186 | $s->server->{LastError} = $@; | ||
| 187 | return $s->handle_error; | ||
| 188 | }# end if() | ||
| 189 | |||
| 190 | $s->response->Flush; | ||
| 191 | my $res = $s->end_request(); | ||
| 192 | |||
| 193 | $res = 0 if $res =~ m/^200/; | ||
| 194 | return $res; | ||
| 195 | }# end execute() | ||
| 196 | |||
| 197 | |||
| 198 | sub handle_phase | ||
| 199 | { | ||
| 200 | my ($s, $ref) = @_; | ||
| 201 | |||
| 202 | eval { $ref->( ) }; | ||
| 203 | if( $@ ) | ||
| 204 | { | ||
| 205 | $s->handle_error; | ||
| 206 | }# end if() | ||
| 207 | |||
| 208 | # Undef on success: | ||
| 209 | return $s->response->Status =~ m/^200/ ? undef : $s->response->Status; | ||
| 210 | }# end handle_phase() | ||
| 211 | |||
| 212 | |||
| 213 | sub handle_error | ||
| 214 | { | ||
| 215 | my $s = shift; | ||
| 216 | |||
| 217 | my $error = "$@"; | ||
| 218 | $s->response->Status( 500 ); | ||
| 219 | no strict 'refs'; | ||
| 220 | |||
| 221 | $s->response->Clear; | ||
| 222 | my ($main, $title, $file, $line) = $error =~ m/^((.*?)\s(?:at|in)\s(.*?)\sline\s(\d+))/; | ||
| 223 | $s->stash->{error} = { | ||
| 224 | title => $title, | ||
| 225 | file => $file, | ||
| 226 | line => $line, | ||
| 227 | stacktrace => $error, | ||
| 228 | }; | ||
| 229 | warn "[Error: @{[ HTTP::Date::time2iso() ]}] $main\n"; | ||
| 230 | |||
| 231 | $s->config->load_class( $s->config->errors->error_handler ); | ||
| 232 | my $error_handler = $s->config->errors->error_handler->new(); | ||
| 233 | $error_handler->init_asp_objects( $s ); | ||
| 234 | eval { $error_handler->run( $s ) }; | ||
| 235 | confess $@ if $@; | ||
| 236 | |||
| 237 | return $s->end_request; | ||
| 238 | }# end handle_error() | ||
| 239 | |||
| 240 | |||
| 241 | sub end_request | ||
| 242 | { | ||
| 243 | my $s = shift; | ||
| 244 | |||
| 245 | $s->handle_phase( $s->global_asa->can('Script_OnEnd') ) | ||
| 246 | unless $s->{did_end}; | ||
| 247 | |||
| 248 | $s->response->End; | ||
| 249 | $s->session->save; | ||
| 250 | my $res = $s->response->Status =~ m/^200/ ? 0 : $s->response->Status; | ||
| 251 | |||
| 252 | return $res; | ||
| 253 | }# end end_request() | ||
| 254 | |||
| 255 | |||
| 256 | sub resolve_global_asa_class | ||
| 257 | { | ||
| 258 | my $s = shift; | ||
| 259 | |||
| 260 | my $file = $s->config->web->www_root . '/GlobalASA.pm'; | ||
| 261 | my $class; | ||
| 262 | if( -f $file ) | ||
| 263 | { | ||
| 264 | $class = $s->config->web->application_name . '::GlobalASA'; | ||
| 265 | eval { require $file }; | ||
| 266 | confess $@ if $@; | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | $class = 'ASP4::GlobalASA'; | ||
| 271 | $s->config->load_class( $class ); | ||
| 272 | }# end if() | ||
| 273 | |||
| 274 | return $class; | ||
| 275 | }# end resolve_global_asa_class() | ||
| 276 | |||
| 277 | |||
| 278 | sub do_disable_session_state | ||
| 279 | { | ||
| 280 | my ($s) = @_; | ||
| 281 | |||
| 282 | my ($uri) = split /\?/, $s->r->uri; | ||
| 283 | my ($yes) = grep { $_->disable_session } grep { | ||
| 284 | if( my $pattern = $_->uri_match ) | ||
| 285 | { | ||
| 286 | $uri =~ m/$pattern/ | ||
| 287 | } | ||
| 288 | else | ||
| 289 | { | ||
| 290 | $uri eq $_->uri_equals; | ||
| 291 | }# end if() | ||
| 292 | } $s->config->web->disable_persistence; | ||
| 293 | |||
| 294 | return $yes; | ||
| 295 | }# end do_disable_session_state() | ||
| 296 | |||
| 297 | |||
| 298 | sub DESTROY | ||
| 299 | { | ||
| 300 | my $s = shift; | ||
| 301 | undef(%$s); | ||
| 302 | }# end DESTROY() | ||
| 303 | |||
| 304 | 1;# return true: | ||
| 305 | |||
| 306 | =pod | ||
| 307 | |||
| 308 | =head1 NAME | ||
| 309 | |||
| 310 | ASP4::HTTPContext - Provides access to the intrinsic objects for an HTTP request. | ||
| 311 | |||
| 312 | =head1 SYNOPSIS | ||
| 313 | |||
| 314 | use ASP4::HTTPContext; | ||
| 315 | |||
| 316 | my $context = ASP4::HTTPContext->current; | ||
| 317 | |||
| 318 | # Intrinsics: | ||
| 319 | my $request = $context->request; | ||
| 320 | my $response = $context->response; | ||
| 321 | my $session = $context->session; | ||
| 322 | my $server = $context->server; | ||
| 323 | my $config = $context->config; | ||
| 324 | my $stash = $context->stash; | ||
| 325 | |||
| 326 | # Advanced: | ||
| 327 | my $cgi = $context->cgi; | ||
| 328 | my $r = $context->r; | ||
| 329 | |||
| 330 | =head1 DESCRIPTION | ||
| 331 | |||
| 332 | The HTTPContext itself is the root of all request-processing in an ASP4 web application. | ||
| 333 | |||
| 334 | There is only one ASP4::HTTPContext instance throughout the lifetime of a request. | ||
| 335 | |||
| 336 | =head1 PROPERTIES | ||
| 337 | |||
| 338 | =head2 current | ||
| 339 | |||
| 340 | Returns the C<ASP4::HTTPContext> object in use for the current HTTP request. | ||
| 341 | |||
| 342 | =head2 request | ||
| 343 | |||
| 344 | Returns the L<ASP4::Request> for the HTTP request. | ||
| 345 | |||
| 346 | =head2 response | ||
| 347 | |||
| 348 | Returns the L<ASP4::Response> for the HTTP request. | ||
| 349 | |||
| 350 | =head2 server | ||
| 351 | |||
| 352 | Returns the L<ASP4::Server> for the HTTP request. | ||
| 353 | |||
| 354 | =head2 session | ||
| 355 | |||
| 356 | Returns the L<ASP4::SessionStateManager> for the HTTP request. | ||
| 357 | |||
| 358 | =head2 stash | ||
| 359 | |||
| 360 | Returns the current stash hash in use for the HTTP request. | ||
| 361 | |||
| 362 | =head2 config | ||
| 363 | |||
| 364 | Returns the current C<ASP4::Config> for the HTTP request. | ||
| 365 | |||
| 366 | =head2 cgi | ||
| 367 | |||
| 368 | Provided B<Just In Case> - returns the L<CGI> object for the HTTP request. | ||
| 369 | |||
| 370 | =head2 r | ||
| 371 | |||
| 372 | Provided B<Just In Case> - returns the L<Apache2::RequestRec> for the HTTP request. | ||
| 373 | |||
| 374 | B<NOTE:> Under L<ASP4::API> (eg: in a unit test) C<$r> will be an instance of L<ASP4::Mock::RequestRec> instead. | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||