| 1 | package OpenInteract2::Request::CGI; | ||
| 2 | |||
| 3 | # $Id: CGI.pm,v 1.23 2005/03/18 04:09:51 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Request ); | ||
| 7 | use CGI; | ||
| 8 | use Log::Log4perl qw( get_logger ); | ||
| 9 | use OpenInteract2::Constants qw( :log ); | ||
| 10 | use OpenInteract2::Context qw( CTX ); | ||
| 11 | use OpenInteract2::Upload; | ||
| 12 | use OpenInteract2::URL; | ||
| 13 | |||
| 14 | $OpenInteract2::Request::CGI::VERSION = sprintf("%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/); | ||
| 15 | |||
| 16 | my ( $log ); | ||
| 17 | |||
| 18 | my @FIELDS = qw( cgi ); | ||
| 19 | OpenInteract2::Request::CGI->mk_accessors( @FIELDS ); | ||
| 20 | |||
| 21 | sub init { | ||
| 22 | my ( $self, $params ) = @_; | ||
| 23 | $log ||= get_logger( LOG_REQUEST ); | ||
| 24 | $log->is_info && $log->info( "Creating CGI request" ); | ||
| 25 | if ( $params->{cgi} ) { | ||
| 26 | $self->cgi( $params->{cgi} ); | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | binmode STDIN; | ||
| 30 | $self->cgi( CGI->new() ); | ||
| 31 | } | ||
| 32 | my $cgi = $self->cgi; | ||
| 33 | my $req_type = $cgi->request_method || 'GET'; | ||
| 34 | |||
| 35 | # Assign URL info from CGI... | ||
| 36 | |||
| 37 | my $base_url = $cgi->script_name || ''; | ||
| 38 | |||
| 39 | $log->is_info && $log->info( "Deployed as $req_type to $base_url" ); | ||
| 40 | CTX->assign_deploy_url( $base_url ); | ||
| 41 | |||
| 42 | my $full_url = join( '', $base_url, $cgi->path_info ); | ||
| 43 | my $query_args = $cgi->query_string; | ||
| 44 | if ( $query_args ) { | ||
| 45 | $full_url .= "?$query_args"; | ||
| 46 | } | ||
| 47 | $log->is_debug && $log->debug( "Full OI URL from CGI: $full_url" ); | ||
| 48 | $self->assign_request_url( $full_url ); | ||
| 49 | |||
| 50 | # Then the various headers, properties, etc. | ||
| 51 | |||
| 52 | $self->referer( $cgi->referer ); | ||
| 53 | $self->user_agent( $cgi->user_agent ); | ||
| 54 | $self->cookie_header( $cgi->raw_cookie ); | ||
| 55 | $self->language_header( $cgi->http( 'Accept-Language' ) ); | ||
| 56 | |||
| 57 | $self->server_name( $cgi->server_name ); | ||
| 58 | $self->remote_host( $cgi->remote_host ); | ||
| 59 | |||
| 60 | # Then the rest of the parameters/uploads (works with other | ||
| 61 | # environments too...) | ||
| 62 | |||
| 63 | $self->_assign_params_from_cgi( $cgi ); | ||
| 64 | |||
| 65 | $log->is_info && $log->info( "Finished creating CGI request" ); | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _assign_params_from_cgi { | ||
| 70 | my ( $self, $cgi ) = @_; | ||
| 71 | |||
| 72 | $log ||= get_logger( LOG_REQUEST ); | ||
| 73 | |||
| 74 | # See if there are any uploads among the parameters. (Note: only | ||
| 75 | # supporting a single upload per fieldname right now...) | ||
| 76 | |||
| 77 | my @fields = $cgi->param; | ||
| 78 | |||
| 79 | my $num_param = 0; | ||
| 80 | my $num_upload = 0; | ||
| 81 | foreach my $field ( @fields ) { | ||
| 82 | my @items = $cgi->param( $field ); | ||
| 83 | next unless ( scalar @items ); | ||
| 84 | |||
| 85 | # ISA upload | ||
| 86 | if ( ref( $items[0] ) ) { | ||
| 87 | foreach my $upload ( @items ) { | ||
| 88 | my $upload_info = $cgi->uploadInfo( $upload ); | ||
| 89 | my $oi_upload = OpenInteract2::Upload->new({ | ||
| 90 | name => $field, | ||
| 91 | content_type => $upload_info->{'Content-Type'}, | ||
| 92 | size => (stat $upload)[7], | ||
| 93 | filehandle => $upload, | ||
| 94 | filename => $cgi->tmpFileName( $upload ) | ||
| 95 | }); | ||
| 96 | $self->_set_upload( $field, $oi_upload ); | ||
| 97 | $num_upload++; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # ISNOTA upload | ||
| 102 | else { | ||
| 103 | if ( scalar @items > 1 ) { | ||
| 104 | $log->is_debug && | ||
| 105 | $log->debug( "Param: $field = (multiple) ", | ||
| 106 | join( ', ', @items ) ); | ||
| 107 | $self->param( $field, \@items ); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | $log->is_debug && | ||
| 111 | $log->debug( "Param: $field = (single) $items[0]" ); | ||
| 112 | $self->param( $field, $items[0] ); | ||
| 113 | } | ||
| 114 | $num_param++; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | $log->is_info && | ||
| 118 | $log->info( "Set $num_param params, $num_upload file uploads" ); | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | |||
| 123 | __END__ |