| 1 | package OpenInteract2::Response::Apache2; | ||
| 2 | |||
| 3 | # $Id: Apache2.pm,v 1.7 2005/03/17 14:58:05 sjn Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Response ); | ||
| 7 | use Apache::Const -compile => qw( OK REDIRECT ); | ||
| 8 | use HTTP::Status qw( RC_OK RC_FOUND ); | ||
| 9 | use Log::Log4perl qw( get_logger ); | ||
| 10 | use OpenInteract2::Constants qw( :log ); | ||
| 11 | use OpenInteract2::Context qw( CTX ); | ||
| 12 | use OpenInteract2::Exception qw( oi_error ); | ||
| 13 | |||
| 14 | $OpenInteract2::Response::Apache2::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/); | ||
| 15 | |||
| 16 | my ( $log ); | ||
| 17 | |||
| 18 | my @FIELDS = qw( apache ); | ||
| 19 | OpenInteract2::Response::Apache2->mk_accessors( @FIELDS ); | ||
| 20 | |||
| 21 | my ( $done ); | ||
| 22 | |||
| 23 | sub init { | ||
| 24 | my ( $self, $params ) = @_; | ||
| 25 | unless ( $done ) { | ||
| 26 | require Apache::RequestIO; | ||
| 27 | $done++; | ||
| 28 | } | ||
| 29 | $self->apache( $params->{apache} ); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub send { | ||
| 35 | my ( $self ) = @_; | ||
| 36 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 37 | |||
| 38 | $log->info( "Sending Apache 2.x response" ); | ||
| 39 | |||
| 40 | my $apache = $self->apache; | ||
| 41 | |||
| 42 | $self->save_session; | ||
| 43 | |||
| 44 | my $headers_out = $apache->headers_out; | ||
| 45 | foreach my $cookie ( @{ $self->cookie } ) { | ||
| 46 | $headers_out->add( 'Set-Cookie', $cookie->as_string ); | ||
| 47 | } | ||
| 48 | |||
| 49 | while ( my ( $name, $value ) = each %{ $self->header } ) { | ||
| 50 | $headers_out->add( $name, $value ); | ||
| 51 | } | ||
| 52 | |||
| 53 | if ( my $filename = $self->send_file ) { | ||
| 54 | $self->set_file_info; | ||
| 55 | open( my $fh, "< $filename" ) | ||
| 56 | || oi_error "Cannot read from '$filename': $!"; | ||
| 57 | $self->_send_header; | ||
| 58 | $apache->send_fd( $fh ); | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->_send_header; | ||
| 63 | |||
| 64 | # 2.x needs to dereference the content | ||
| 65 | my $content = $self->content; | ||
| 66 | $apache->print( $$content ); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub _send_header { | ||
| 71 | my ( $self ) = @_; | ||
| 72 | unless ( $self->content_type ) { | ||
| 73 | $self->content_type( 'text/html' ); | ||
| 74 | } | ||
| 75 | unless ( $self->status ) { | ||
| 76 | $self->status( Apache::OK ); | ||
| 77 | } | ||
| 78 | |||
| 79 | my $apache = $self->apache; | ||
| 80 | if ( CTX->server_config->{promote_oi} eq 'yes' ) { | ||
| 81 | $apache->headers_out->add( | ||
| 82 | 'X-Powered-By', "OpenInteract " . CTX->version ); | ||
| 83 | } | ||
| 84 | $apache->content_type( $self->content_type_header ); | ||
| 85 | # From 1.x... | ||
| 86 | #$apache->send_http_header( $self->content_type ); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub redirect { | ||
| 91 | my ( $self, $url ) = @_; | ||
| 92 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 93 | |||
| 94 | $self->save_session; | ||
| 95 | |||
| 96 | $url ||= $self->return_url; | ||
| 97 | $log->is_info && | ||
| 98 | $log->info( "Assigning redirect status and redirect ", | ||
| 99 | "'Location' header to '$url'" ); | ||
| 100 | $self->status( RC_FOUND ); | ||
| 101 | $self->header_out( Location => $url ); | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | __END__ |