OpenInteract2/Response/CGI.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 156 CC 11 SCAR 24.8
CC
11
Cov
0%
generate_cgi_header_fields
30.0
out
20.0
send
12.0
CRAP 12.0
1 package OpenInteract2::Response::CGI;
2  
3 # $Id: CGI.pm,v 1.20 2005/03/17 14:58:05 sjn Exp $
4  
5 use strict;
6 use base qw( OpenInteract2::Response );
7 use Log::Log4perl qw( get_logger );
8 use Data::Dumper qw( Dumper );
9 use IO::File;
10 use HTTP::Status qw( RC_OK RC_FOUND );
11 use OpenInteract2::Constants qw( :log );
12 use OpenInteract2::Context qw( CTX );
13 use OpenInteract2::Exception qw( oi_error );
14  
15 $OpenInteract2::Response::CGI::VERSION = sprintf("%d.%02d", q$Revision: 1.20 $ =~ /(\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 CGI 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 unless ( $self->is_redirect ) {
66 $self->out( $self->generate_cgi_header_fields, "\r\n\r\n" );
67 $self->out( $self->content );
68 }
69 }
70  
71  
72 sub generate_cgi_header_fields {
73 my ( $self ) = @_;
74 $self->content_type( 'text/html' ) unless ( $self->content_type );
75 $self->status( RC_OK ) unless ( $self->status );
76 my $header_entries = $self->header;
77  
78 my @header = ();
79 push @header, "Status: " . $self->status;
80 push @header, $self->_generate_cookie_lines;
81 while ( my ( $key, $value ) = each %{ $header_entries } ) {
82 next if ( $key eq 'Content-Type' );
83 push @header, "$key: $value";
84 }
85 push @header, "Content-Type: " . $self->content_type_header;
86 if ( CTX->server_config->{promote_oi} eq 'yes' ) {
87 push @header, join( '', 'X-Powered-By: ', 'OpenInteract ', CTX->version );
88 }
89 return join( "\r\n", @header );
90 }
91  
92  
93 sub redirect {
94 my ( $self, $url ) = @_;
95 $log ||= get_logger( LOG_RESPONSE );
96  
97 $self->save_session;
98  
99 $url ||= $self->return_url;
100 $log->is_info &&
101 $log->info( "Assigning redirect status and redirect ",
102 "'Location' header to '$url'" );
103 $self->status( RC_FOUND );
104 my @header = ();
105 push @header, "Status: 302 Moved";
106 push @header, "Location: $url";
107 push @header, $self->_generate_cookie_lines;
108 $self->out( join( "\r\n", @header ), "\r\n\r\n" );
109 }
110  
111  
112 sub _generate_cookie_lines {
113 my ( $self ) = @_;
114 my $cookies = $self->cookie;
115 return () unless ( ref $cookies eq 'ARRAY' );
116 return map { "Set-Cookie: " . $_->as_string } @{ $cookies };
117 }
118  
119 1;
120  
121 __END__