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