| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/Engine/Apache2.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::Engine::Apache2; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | use GX::HTTP::Body::File; | ||
| 10 | use GX::HTTP::Body::Scalar; | ||
| 11 | |||
| 12 | use Apache2::Connection (); | ||
| 13 | use Apache2::Const '-compile' => qw( :common ); | ||
| 14 | use Apache2::RequestIO (); | ||
| 15 | use Apache2::RequestRec (); | ||
| 16 | use Apache2::RequestUtil (); | ||
| 17 | use Apache2::Response (); | ||
| 18 | use APR::Table (); | ||
| 19 | |||
| 20 | |||
| 21 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 22 | # Class setup | ||
| 23 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 24 | |||
| 25 | use GX::Class ( extends => 'GX::Engine' ); | ||
| 26 | |||
| 27 | build; | ||
| 28 | |||
| 29 | |||
| 30 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 31 | # Export mechanism | ||
| 32 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 33 | |||
| 34 | __PACKAGE__->_install_import_method; | ||
| 35 | |||
| 36 | |||
| 37 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 38 | # Handlers | ||
| 39 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 40 | |||
| 41 | sub finalize_response_headers :Handler( FinalizeResponseHeaders ) { | ||
| 42 | |||
| 43 | my $self = shift; | ||
| 44 | my $context = shift; | ||
| 45 | |||
| 46 | my $response = $context->response; | ||
| 47 | |||
| 48 | for my $cookie ( $response->cookies->all ) { | ||
| 49 | $response->headers->add( 'Set-Cookie' => $cookie->as_string ); | ||
| 50 | } | ||
| 51 | |||
| 52 | return; | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub process_request_body :Handler( ProcessRequestBody ) { | ||
| 57 | |||
| 58 | my $self = shift; | ||
| 59 | my $context = shift; | ||
| 60 | |||
| 61 | my $request = $context->request; | ||
| 62 | my $content_length = $request->content_length; | ||
| 63 | |||
| 64 | if ( ! $content_length ) { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | if ( $content_length !~ /^\d+$/ ) { | ||
| 69 | $context->send_response( status => 400 ); | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | my $config = $self->_get_config; | ||
| 74 | |||
| 75 | if ( $content_length > $config->{'max_request_size'} ) { | ||
| 76 | $context->send_response( status => 413 ); | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | my $body; | ||
| 81 | |||
| 82 | if ( $content_length > $config->{'max_request_memory_usage'} ) { | ||
| 83 | $body = GX::HTTP::Body::File->new; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $body = GX::HTTP::Body::Scalar->new; | ||
| 87 | } | ||
| 88 | |||
| 89 | my $body_fh = $body->open( '>' ); | ||
| 90 | my $input_stream = $context->input_stream; | ||
| 91 | my $buffer_size = $config->{'buffer_size'}; | ||
| 92 | my $bytes_read = 0; | ||
| 93 | my $read_callback = $request->read_callback; | ||
| 94 | |||
| 95 | while ( ( my $bytes = $input_stream->read( my $buffer, $buffer_size ) ) > 0 ) { | ||
| 96 | |||
| 97 | $bytes_read += $bytes; | ||
| 98 | |||
| 99 | if ( ! $body_fh->print( $buffer ) ) { | ||
| 100 | throw "Cannot write request body"; | ||
| 101 | } | ||
| 102 | |||
| 103 | if ( $bytes_read > $content_length ) { | ||
| 104 | last; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ( $read_callback ) { | ||
| 108 | $read_callback->( $context, $bytes_read, $content_length ); | ||
| 109 | } | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | $body_fh->close; | ||
| 114 | |||
| 115 | if ( $bytes_read != $content_length ) { | ||
| 116 | $context->send_response( status => 400 ); | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | $request->body( $body ); | ||
| 121 | |||
| 122 | return; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub process_request_headers :Handler( ProcessRequestHeaders ) { | ||
| 127 | |||
| 128 | my $self = shift; | ||
| 129 | my $context = shift; | ||
| 130 | |||
| 131 | my $headers = $context->request->headers; | ||
| 132 | my $headers_in = $context->request_record->headers_in; | ||
| 133 | |||
| 134 | while ( my ( $field_name, $field_value ) = each %$headers_in ) { | ||
| 135 | $headers->add( $field_name, $field_value ); | ||
| 136 | } | ||
| 137 | |||
| 138 | return; | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | sub send_response :Handler( SendResponse ) { | ||
| 143 | |||
| 144 | my $self = shift; | ||
| 145 | my $context = shift; | ||
| 146 | |||
| 147 | my $output_stream = $context->output_stream; | ||
| 148 | |||
| 149 | my $response = $context->response; | ||
| 150 | my $request_record = $context->request_record; | ||
| 151 | |||
| 152 | $request_record->status( $response->status ); | ||
| 153 | |||
| 154 | my $headers = $response->headers; | ||
| 155 | my $headers_out = $request_record->err_headers_out; | ||
| 156 | |||
| 157 | for my $field_name ( $headers->field_names ) { | ||
| 158 | |||
| 159 | for my $field_value ( $headers->get( $field_name ) ) { | ||
| 160 | $headers_out->add( $field_name => $field_value ); | ||
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | my $content_type = $headers->content_type; | ||
| 166 | |||
| 167 | if ( defined $content_type ) { | ||
| 168 | $request_record->content_type( $content_type ); | ||
| 169 | } | ||
| 170 | |||
| 171 | my $body = $response->body; | ||
| 172 | |||
| 173 | if ( my $length = $body->length ) { | ||
| 174 | |||
| 175 | if ( $length > 0 ) { | ||
| 176 | $request_record->set_content_length( $length ); | ||
| 177 | } | ||
| 178 | |||
| 179 | if ( $body->isa( 'GX::HTTP::Body::Scalar' ) ) { | ||
| 180 | $output_stream->print( ${$body->content} ); | ||
| 181 | } | ||
| 182 | elsif ( $body->isa( 'GX::HTTP::Body::File' ) ) { | ||
| 183 | $output_stream->sendfile( $body->file ); | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | $body->print_to( $output_stream ); | ||
| 187 | } | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | $output_stream->flush; | ||
| 192 | |||
| 193 | return; | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 199 | # Private methods | ||
| 200 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 201 | |||
| 202 | sub _create_mod_perl_handler { | ||
| 203 | |||
| 204 | my $self = shift; | ||
| 205 | my $application = shift; | ||
| 206 | |||
| 207 | my $context_class = ref( $application ) . '::Context'; | ||
| 208 | my $request_class = ref( $application ) . '::Request'; | ||
| 209 | my $response_class = ref( $application ) . '::Response'; | ||
| 210 | |||
| 211 | my $error_stream_class = __PACKAGE__ . '::ErrorStream'; | ||
| 212 | my $input_stream_class = __PACKAGE__ . '::InputStream'; | ||
| 213 | my $output_stream_class = __PACKAGE__ . '::OutputStream'; | ||
| 214 | |||
| 215 | my $handler = sub :method { | ||
| 216 | |||
| 217 | my $application = shift->instance; | ||
| 218 | my $request_record = shift; | ||
| 219 | |||
| 220 | local $@; | ||
| 221 | |||
| 222 | eval { | ||
| 223 | |||
| 224 | my $scheme = do { | ||
| 225 | my $value = $request_record->subprocess_env( 'HTTPS' ); | ||
| 226 | ( defined $value && uc $value eq 'ON' ) ? 'https' : 'http'; | ||
| 227 | }; | ||
| 228 | |||
| 229 | my $context = $context_class->new( | ||
| 230 | |||
| 231 | error_stream => $error_stream_class->new( | ||
| 232 | request_record => $request_record | ||
| 233 | ), | ||
| 234 | |||
| 235 | input_stream => $input_stream_class->new( | ||
| 236 | request_record => $request_record | ||
| 237 | ), | ||
| 238 | |||
| 239 | output_stream => $output_stream_class->new( | ||
| 240 | request_record => $request_record | ||
| 241 | ), | ||
| 242 | |||
| 243 | request => $request_class->new( | ||
| 244 | host => $request_record->hostname, | ||
| 245 | method => $request_record->method, | ||
| 246 | path => $request_record->uri, | ||
| 247 | port => $request_record->get_server_port, | ||
| 248 | protocol => $request_record->protocol, | ||
| 249 | query => $request_record->args, | ||
| 250 | remote_address => $request_record->connection->remote_ip, | ||
| 251 | scheme => $scheme, | ||
| 252 | uri => $request_record->unparsed_uri | ||
| 253 | ), | ||
| 254 | |||
| 255 | response => $response_class->new( | ||
| 256 | status => 200 | ||
| 257 | ), | ||
| 258 | |||
| 259 | request_record => $request_record | ||
| 260 | |||
| 261 | ); | ||
| 262 | |||
| 263 | $application->process( $context ); | ||
| 264 | |||
| 265 | }; | ||
| 266 | |||
| 267 | if ( $@ ) { | ||
| 268 | return $application->engine->_handle_error( $request_record, $@ ); | ||
| 269 | } | ||
| 270 | |||
| 271 | return Apache2::Const::OK; | ||
| 272 | |||
| 273 | }; | ||
| 274 | |||
| 275 | if ( $application->mode eq 'development' ) { | ||
| 276 | |||
| 277 | my $real_handler = $handler; | ||
| 278 | |||
| 279 | $handler = sub :method { | ||
| 280 | goto &{"$_[0]\::handler"} if eval { $_[0]->reload }; | ||
| 281 | __PACKAGE__->_handle_reload_error( $_[0], $_[1], $@ ) if $@; | ||
| 282 | return $real_handler->( @_ ); | ||
| 283 | }; | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | return $handler; | ||
| 288 | |||
| 289 | } | ||
| 290 | |||
| 291 | sub _deploy { | ||
| 292 | |||
| 293 | my $self = shift; | ||
| 294 | |||
| 295 | $self->SUPER::_deploy; | ||
| 296 | |||
| 297 | my $application = $self->application; | ||
| 298 | |||
| 299 | $self->_export_method( | ||
| 300 | ref( $application ), | ||
| 301 | 'handler', | ||
| 302 | $self->_create_mod_perl_handler( $application ) | ||
| 303 | ); | ||
| 304 | |||
| 305 | $self->_export_attribute( | ||
| 306 | ref( $application ) . '::Context', | ||
| 307 | { | ||
| 308 | name => 'request_record', | ||
| 309 | type => 'Scalar', | ||
| 310 | accessor => { type => 'get' } | ||
| 311 | } | ||
| 312 | ); | ||
| 313 | |||
| 314 | return; | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | sub _handle_error { | ||
| 319 | |||
| 320 | my $self = shift; | ||
| 321 | my $request_record = shift; | ||
| 322 | my $error = shift; | ||
| 323 | |||
| 324 | local $@; | ||
| 325 | |||
| 326 | eval { | ||
| 327 | |||
| 328 | my $message; | ||
| 329 | |||
| 330 | if ( defined $error ) { | ||
| 331 | $message = "$error"; | ||
| 332 | $message =~ s/\n*$//; | ||
| 333 | $message =~ s/\n/\\n/g; | ||
| 334 | } | ||
| 335 | else { | ||
| 336 | $message = 'Unknown error'; | ||
| 337 | } | ||
| 338 | |||
| 339 | print STDERR "[" . ref( $self->application ) . "] [error] Uncaught error: $message\n"; | ||
| 340 | |||
| 341 | }; | ||
| 342 | |||
| 343 | return Apache2::Const::SERVER_ERROR; | ||
| 344 | |||
| 345 | } | ||
| 346 | |||
| 347 | sub _handle_reload_error { | ||
| 348 | |||
| 349 | |||
| 350 | my $package = shift; | ||
| 351 | my $application_class = shift; | ||
| 352 | my $request_record = shift; | ||
| 353 | my $error = shift; | ||
| 354 | |||
| 355 | eval { | ||
| 356 | |||
| 357 | my $message; | ||
| 358 | |||
| 359 | if ( defined $error ) { | ||
| 360 | $message = "$error"; | ||
| 361 | $message =~ s/\n*$//; | ||
| 362 | $message =~ s/\n/\\n/g; | ||
| 363 | } | ||
| 364 | else { | ||
| 365 | $message = 'Unknown error'; | ||
| 366 | } | ||
| 367 | |||
| 368 | print STDERR "[$application_class] [fatal] $message\n"; | ||
| 369 | print STDERR "[$application_class] [fatal] Reload failed, exiting ...\n"; | ||
| 370 | |||
| 371 | $request_record->status( 500 ); | ||
| 372 | $request_record->content_type( 'text/html; charset=UTF-8' ); | ||
| 373 | $request_record->print( $package->_render_reload_error( $error ) ); | ||
| 374 | $request_record->rflush; | ||
| 375 | |||
| 376 | }; | ||
| 377 | |||
| 378 | CORE::exit( 1 ); | ||
| 379 | |||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 384 | # I/O stream adaptors | ||
| 385 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 386 | |||
| 387 | package GX::Engine::Apache2::InputStream; | ||
| 388 | |||
| 389 | use GX::Class::Object; | ||
| 390 | |||
| 391 | has 'request_record'; | ||
| 392 | |||
| 393 | build; | ||
| 394 | |||
| 395 | sub read { | ||
| 396 | |||
| 397 | my $self = shift; | ||
| 398 | |||
| 399 | local $@; | ||
| 400 | |||
| 401 | return eval { $self->{'request_record'}->read( @_ ) }; | ||
| 402 | |||
| 403 | } | ||
| 404 | |||
| 405 | |||
| 406 | package GX::Engine::Apache2::OutputStream; | ||
| 407 | |||
| 408 | use GX::Class::Object; | ||
| 409 | |||
| 410 | has 'request_record'; | ||
| 411 | |||
| 412 | build; | ||
| 413 | |||
| 414 | sub flush { | ||
| 415 | |||
| 416 | my $self = shift; | ||
| 417 | |||
| 418 | local $@; | ||
| 419 | |||
| 420 | return eval { $self->{'request_record'}->rflush; 1 }; | ||
| 421 | |||
| 422 | } | ||
| 423 | |||
| 424 | sub print { | ||
| 425 | |||
| 426 | my $self = shift; | ||
| 427 | |||
| 428 | local $@; | ||
| 429 | |||
| 430 | return eval { $self->{'request_record'}->print( @_ ) }; | ||
| 431 | |||
| 432 | } | ||
| 433 | |||
| 434 | sub sendfile { | ||
| 435 | |||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | local $@; | ||
| 439 | |||
| 440 | return eval { $self->{'request_record'}->sendfile( @_ ) }; | ||
| 441 | |||
| 442 | } | ||
| 443 | |||
| 444 | |||
| 445 | package GX::Engine::Apache2::ErrorStream; | ||
| 446 | |||
| 447 | use GX::Class::Object; | ||
| 448 | |||
| 449 | has 'request_record'; | ||
| 450 | |||
| 451 | build; | ||
| 452 | |||
| 453 | sub print { | ||
| 454 | |||
| 455 | shift; | ||
| 456 | |||
| 457 | return print STDERR @_; | ||
| 458 | |||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | 1; | ||
| 463 | |||
| 464 | __END__ |