« OpenInteract2/Setup/CheckDatasources.pm
↑ Summary
OpenInteract2/Setup/InitializeContentGenerators.pm »
| 1 | package OpenInteract2::Response::LWP; | ||
| 2 | |||
| 3 | # $Id: LWP.pm,v 1.22 2005/03/17 14:58:05 sjn Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Response ); | ||
| 7 | use HTTP::Response; | ||
| 8 | use HTTP::Status qw( RC_OK RC_FOUND ); | ||
| 9 | use IO::File; | ||
| 10 | use Log::Log4perl qw( get_logger ); | ||
| 11 | use OpenInteract2::Constants qw( :log ); | ||
| 12 | use OpenInteract2::Context qw( CTX ); | ||
| 13 | use OpenInteract2::Exception qw( oi_error ); | ||
| 14 | |||
| 15 | $OpenInteract2::Response::LWP::VERSION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/); | ||
| 16 | |||
| 17 | my ( $log ); | ||
| 18 | |||
| 19 | my @FIELDS = qw( lwp_response client ); | ||
| 20 | OpenInteract2::Response::LWP->mk_accessors( @FIELDS ); | ||
| 21 | |||
| 22 | sub init { | ||
| 23 | my ( $self, $params ) = @_; | ||
| 24 | $self->client( $params->{client} ); | ||
| 25 | $self->lwp_response( $params->{response} ); | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub send { | ||
| 30 | my ( $self ) = @_; | ||
| 31 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 32 | |||
| 33 | $log->is_info && $log->info( "Sending LWP response" ); | ||
| 34 | if ( $self->is_redirect ) { | ||
| 35 | $log->is_info | ||
| 36 | && $log->info( "Response is redirect, already sent" ); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | $self->content_type( 'text/html' ) unless ( $self->content_type ); | ||
| 40 | $self->status( RC_OK ) unless ( $self->status ); | ||
| 41 | |||
| 42 | $self->save_session; | ||
| 43 | $log->is_info && $log->info( "Saved session ok" ); | ||
| 44 | |||
| 45 | if ( $self->lwp_response ) { | ||
| 46 | $self->lwp_response->code( $self->status ); | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $self->lwp_response( HTTP::Response->new( $self->status ) ); | ||
| 50 | } | ||
| 51 | if ( my $filename = $self->send_file ) { | ||
| 52 | $self->set_file_info; | ||
| 53 | $self->_set_lwp_headers; | ||
| 54 | my $fh = IO::File->new( "< $filename" ) | ||
| 55 | || oi_error "Cannot open file '$filename': $!"; | ||
| 56 | $self->client->send_file( $fh ); | ||
| 57 | $log->is_info && | ||
| 58 | $log->info( "Sent file '$filename' directly to client" ); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $self->_set_lwp_headers; | ||
| 62 | $self->lwp_response->content( | ||
| 63 | ( ref $self->content ) ? ${ $self->content } : $self->content | ||
| 64 | ); | ||
| 65 | if ( my $client = $self->client ) { | ||
| 66 | $client->send_response( $self->lwp_response ); | ||
| 67 | $log->is_info && $log->info( "Sent response ok" ); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $log->is_info && | ||
| 71 | $log->info( "Set content/headers but did not send content" ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub redirect { | ||
| 78 | my ( $self, $url ) = @_; | ||
| 79 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 80 | |||
| 81 | $self->save_session; | ||
| 82 | |||
| 83 | $url ||= $self->return_url; | ||
| 84 | $log->is_info && | ||
| 85 | $log->info( "Got request for redirect to '$url'" ); | ||
| 86 | |||
| 87 | my $lwp_response = $self->lwp_response; | ||
| 88 | unless ( $lwp_response ) { | ||
| 89 | $self->lwp_response( HTTP::Response->new( RC_FOUND ) ); | ||
| 90 | $lwp_response = $self->lwp_response; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $lwp_response->code( RC_FOUND ); | ||
| 94 | } | ||
| 95 | $self->status( RC_FOUND ); | ||
| 96 | $lwp_response->header( Location => $url ); | ||
| 97 | $log->is_info && | ||
| 98 | $log->info( "Set 'Location' header to ", $lwp_response->header( 'Location' ) ); | ||
| 99 | $self->_set_lwp_cookies; | ||
| 100 | $log->debug( "Getting ready to send response: ", CTX->dump( $lwp_response ) ); | ||
| 101 | if ( my $client = $self->client ) { | ||
| 102 | $client->send_response( $lwp_response ); | ||
| 103 | } | ||
| 104 | $log->is_info && $log->info( "Sent redirect ok" ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _set_lwp_headers { | ||
| 108 | my ( $self ) = @_; | ||
| 109 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 110 | |||
| 111 | my $lwp_response = $self->lwp_response; | ||
| 112 | $lwp_response->code( $self->status ); | ||
| 113 | |||
| 114 | $self->content_type( 'text/html' ) unless ( $self->content_type ); | ||
| 115 | |||
| 116 | while ( my ( $name, $value ) = each %{ $self->header } ) { | ||
| 117 | if ( ref $value eq 'ARRAY' ) { | ||
| 118 | $lwp_response->push_header( $name => $_ ) for ( @{ $value } ); | ||
| 119 | } | ||
| 120 | elsif ( $name eq 'Content-Type' ) { | ||
| 121 | $lwp_response->header( $name => $self->content_type_header ); | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | $lwp_response->header( $name => $value ); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | if ( CTX->server_config->{promote_oi} eq 'yes' ) { | ||
| 128 | $lwp_response->header( 'X-Powered-By' => 'OpenInteract ' . CTX->version ); | ||
| 129 | } | ||
| 130 | $log->is_debug && $log->debug( "Set response headers ok" ); | ||
| 131 | $self->_set_lwp_cookies; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _set_lwp_cookies { | ||
| 135 | my ( $self ) = @_; | ||
| 136 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 137 | |||
| 138 | for ( @{ $self->cookie } ) { | ||
| 139 | $self->lwp_response->push_header( 'Set-Cookie' => $_->as_string ); | ||
| 140 | } | ||
| 141 | $log->is_debug && $log->debug( "Set response cookies ok" ); | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | |||
| 146 | __END__ |