| 1 | package OpenInteract2::Response::Standalone; | ||
| 2 | |||
| 3 | # $Id: Standalone.pm,v 1.15 2005/03/17 14:58:05 sjn Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Response ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | use IO::File; | ||
| 9 | use HTTP::Status qw( RC_OK RC_FOUND ); | ||
| 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::Standalone::VERSION = sprintf("%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/); | ||
| 16 | |||
| 17 | my ( $log ); | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my ( $self, @params ) = @_; | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub out { | ||
| 25 | my $self = shift; | ||
| 26 | for ( @_ ) { | ||
| 27 | if ( ! ref( $_ ) ) { | ||
| 28 | print $_; | ||
| 29 | } | ||
| 30 | elsif ( ref( $_ ) eq 'SCALAR' ) { | ||
| 31 | print ${ $_ }; | ||
| 32 | } | ||
| 33 | elsif ( UNIVERSAL::isa( $_, 'OpenInteract2::Exception' ) ) { | ||
| 34 | print "<h1>Exception!</h1>\n", | ||
| 35 | "<p>Error: ", $_->message, "<br>", | ||
| 36 | $_->creation_location, "</p>\n"; | ||
| 37 | } | ||
| 38 | else { | ||
| 39 | $Data::Dumper::Indent = 1; | ||
| 40 | print '<pre><font size="-1">', Dumper( $_ ), '</font></pre>'; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub send { | ||
| 46 | my ( $self ) = @_; | ||
| 47 | $log ||= get_logger( LOG_RESPONSE ); | ||
| 48 | |||
| 49 | $log->info( "Sending Standalone response" ); | ||
| 50 | |||
| 51 | $self->save_session; | ||
| 52 | |||
| 53 | if ( my $filename = $self->send_file ) { | ||
| 54 | $self->set_file_info; | ||
| 55 | my $fh = IO::File->new( "< $filename" ) | ||
| 56 | || oi_error "Cannot open file [$filename]: $!"; | ||
| 57 | $self->out( $self->generate_cgi_header_fields, "\r\n\r\n" ); | ||
| 58 | my ( $data ); | ||
| 59 | while ( $fh->read( $data, 1024 ) ) { | ||
| 60 | $self->out( $data ); | ||
| 61 | } | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->out( $self->generate_cgi_header_fields, "\r\n\r\n" ); | ||
| 66 | $self->out( $self->content ); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub generate_cgi_header_fields { | ||
| 71 | my ( $self ) = @_; | ||
| 72 | $self->content_type( 'text/html' ) unless ( $self->content_type ); | ||
| 73 | $self->status( RC_OK ) unless ( $self->status ); | ||
| 74 | my $header_entries = $self->header; | ||
| 75 | |||
| 76 | my @header = (); | ||
| 77 | push @header, "Status: " . $self->status; | ||
| 78 | push @header, $self->_generate_cookie_lines; | ||
| 79 | while ( my ( $key, $value ) = each %{ $header_entries } ) { | ||
| 80 | next if ( $key eq 'Content-Type' ); | ||
| 81 | push @header, "$key: $value"; | ||
| 82 | } | ||
| 83 | push @header, "Content-Type: " . $self->content_type_header; | ||
| 84 | if ( CTX->server_config->{promote_oi} eq 'yes' ) { | ||
| 85 | push @header, join( '', 'X-Powered-By: ', 'OpenInteract ', CTX->version ); | ||
| 86 | } | ||
| 87 | return join( "\r\n", @header ); | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub redirect { | ||
| 92 | my ( $self, $url ) = @_; | ||
| 93 | $self->save_session; | ||
| 94 | my @header = (); | ||
| 95 | push @header, "Status: 302 Moved"; | ||
| 96 | push @header, "Location: $url"; | ||
| 97 | push @header, $self->_generate_cookie_lines; | ||
| 98 | $self->out( join( "\r\n", @header ), "\r\n\r\n" ); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub _generate_cookie_lines { | ||
| 103 | my ( $self ) = @_; | ||
| 104 | my $cookies = $self->cookie; | ||
| 105 | return () unless ( ref $cookies eq 'ARRAY' ); | ||
| 106 | return map { "Set-Cookie: " . $_->as_string } @{ $cookies }; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |