| 1 | package OpenInteract2::Request::Apache2; | ||
| 2 | |||
| 3 | # $Id: Apache2.pm,v 1.3 2005/03/17 14:58:04 sjn Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract2::Request ); | ||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | use OpenInteract2::Constants qw( :log ); | ||
| 9 | use OpenInteract2::Context qw( CTX ); | ||
| 10 | use OpenInteract2::Exception qw( oi_error ); | ||
| 11 | use OpenInteract2::Upload; | ||
| 12 | |||
| 13 | $OpenInteract2::Request::Apache2::VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | my ( $log ); | ||
| 16 | |||
| 17 | my @FIELDS = qw( apache ); | ||
| 18 | OpenInteract2::Request::Apache2->mk_accessors( @FIELDS ); | ||
| 19 | |||
| 20 | my ( $done ); | ||
| 21 | |||
| 22 | sub init { | ||
| 23 | my ( $self, $params ) = @_; | ||
| 24 | $log ||= get_logger( LOG_REQUEST ); | ||
| 25 | $log->is_info && | ||
| 26 | $log->info( "Creating Apache 2.x request" ); | ||
| 27 | |||
| 28 | my $r = $params->{apache}; | ||
| 29 | unless ( ref $r ) { | ||
| 30 | $log->error( "No 'apache' object for creating request" ); | ||
| 31 | oi_error "Cannot initialize the OpenInteract2::Request object - ", | ||
| 32 | "pass in an Apache2 request object in 'apache'"; | ||
| 33 | } | ||
| 34 | |||
| 35 | $self->apache( $r ); | ||
| 36 | |||
| 37 | # Hopefully this will pull the right Apache::Request... | ||
| 38 | unless ( $done ) { | ||
| 39 | require Apache::Connection; | ||
| 40 | # require Apache::Request; | ||
| 41 | require APR::URI; | ||
| 42 | require APR::SockAddr; | ||
| 43 | $done++; | ||
| 44 | } | ||
| 45 | |||
| 46 | my $apache_uri = $r->parsed_uri; | ||
| 47 | my $full_url = $apache_uri->path; | ||
| 48 | my $query_args = $apache_uri->query; | ||
| 49 | if ( $query_args ) { | ||
| 50 | $full_url .= "?$query_args"; | ||
| 51 | } | ||
| 52 | $log->is_debug && $log->debug( "Got URL from apache2 '$full_url'" ); | ||
| 53 | $self->assign_request_url( $full_url ); | ||
| 54 | |||
| 55 | # HACK! | ||
| 56 | my $cgi = CGI->new( $r ); | ||
| 57 | require OpenInteract2::Request::CGI; | ||
| 58 | OpenInteract2::Request::CGI::_assign_params_from_cgi( $self, $cgi ); | ||
| 59 | |||
| 60 | # Then the various headers, properties, etc. | ||
| 61 | |||
| 62 | my $in = $r->headers_in(); | ||
| 63 | $self->referer( $in->{'Referer'} ); | ||
| 64 | $self->user_agent( $in->{'User-Agent'} ); | ||
| 65 | $self->cookie_header( $in->{'Cookie'} ); | ||
| 66 | $self->language_header( $in->{'Accept-Language'} ); | ||
| 67 | |||
| 68 | $self->server_name( $r->get_server_name ); | ||
| 69 | $self->remote_host( $r->connection->remote_addr->ip_get ); | ||
| 70 | $log->is_info && | ||
| 71 | $log->info( "Finished creating Apache 2.x request" ); | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | |||
| 75 | ################################################## | ||
| 76 | |||
| 77 | # TODO - set parameters here like: | ||
| 78 | # TEMP_DIR => $WEBSITE_DIR/tmp | ||
| 79 | |||
| 80 | my $apache = Apache::Request->new( $params->{apache} ); | ||
| 81 | $self->apache( $apache ); | ||
| 82 | $log->is_debug && | ||
| 83 | $log->debug( "Created Apache::Request object and set in OI request" ); | ||
| 84 | |||
| 85 | # Set the URI and parse it | ||
| 86 | |||
| 87 | my $url = $apache->parsed_uri; | ||
| 88 | my $request_path = $url->path; | ||
| 89 | $log->is_debug && | ||
| 90 | $log->debug( "Got URL from apache '$request_path'" ); | ||
| 91 | $self->assign_request_url( $request_path ); | ||
| 92 | |||
| 93 | # Setup the GET/SET params | ||
| 94 | |||
| 95 | my $num_params = 0; | ||
| 96 | foreach my $field ( $apache->param() ) { | ||
| 97 | my @values = $apache->param( $field ); | ||
| 98 | if ( scalar @values > 1 ) { | ||
| 99 | $self->param( $field, \@values ); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $self->param( $field, $values[0] ); | ||
| 103 | } | ||
| 104 | $num_params++; | ||
| 105 | } | ||
| 106 | $log->is_debug && | ||
| 107 | $log->debug( "Set all $num_params parameters ok" ); | ||
| 108 | |||
| 109 | # Next set the uploaded files | ||
| 110 | |||
| 111 | my $num_uploads = 0; | ||
| 112 | my @upload_names = $apache->upload(); | ||
| 113 | $log->is_debug && | ||
| 114 | $log->debug( "Got upload names: ", join( ", ", @upload_names ) ); | ||
| 115 | foreach my $upload_name ( @upload_names ) { | ||
| 116 | $log->is_debug && | ||
| 117 | $log->debug( "Checking for upload in upload name '$upload_name'..." ); | ||
| 118 | my $upload = $apache->upload( $upload_name ); | ||
| 119 | $log->is_debug && | ||
| 120 | $log->debug( "Upload retrieved isa: ", ref $upload ); | ||
| 121 | next unless ( ref $upload ); | ||
| 122 | my $oi_upload = OpenInteract2::Upload->new( | ||
| 123 | { name => $upload->name, | ||
| 124 | content_type => $upload->type, | ||
| 125 | size => $upload->size, # does this work yet?? | ||
| 126 | # This might not do what you think... see APR::Brigade (?!) | ||
| 127 | filehandle => $upload->bb, | ||
| 128 | filename => $upload->filename, | ||
| 129 | # This doesn't seem to be supported anymore | ||
| 130 | #tmp_name => $upload->tempname | ||
| 131 | }); | ||
| 132 | $self->_set_upload( $upload->name, $oi_upload ); | ||
| 133 | $num_uploads++; | ||
| 134 | } | ||
| 135 | $log->is_debug && | ||
| 136 | $log->debug( "Set all $num_uploads uploaded files" ); | ||
| 137 | |||
| 138 | # Then the various headers, properties, etc. | ||
| 139 | |||
| 140 | my $in = $apache->headers_in(); | ||
| 141 | $self->referer( $in->{'Referer'} ); | ||
| 142 | $self->user_agent( $in->{'User-Agent'} ); | ||
| 143 | $self->cookie_header( $in->{'Cookie'} ); | ||
| 144 | $self->language_header( $in->{'Accept-Language'} ); | ||
| 145 | |||
| 146 | $self->server_name( $apache->get_server_name ); | ||
| 147 | $self->remote_host( $apache->connection->remote_addr->ip_get ); | ||
| 148 | $log->is_info && | ||
| 149 | $log->info( "Finished creating Apache 2.x request" ); | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __END__ |