1 package Catalyst::Engine::Apache;
5 use constant MP2 => $mod_perl::VERSION >= 1.99;
6 use base 'Catalyst::Engine';
12 require Apache::Connection;
13 require Apache::RequestIO;
14 require Apache::RequestRec;
15 require Apache::SubRequest;
16 require Apache::RequestUtil;
20 else { require Apache }
23 require Apache::Request;
24 require Apache::Cookie;
25 require Apache::Upload if MP2;
27 __PACKAGE__->mk_accessors(qw/apache_request original_request/);
31 Catalyst::Engine::Apache - Catalyst Apache Engine
39 This is the Catalyst engine specialized for Apache (i.e. for mod_perl).
45 =item $c->apache_request
47 Returns an C<Apache::Request> object.
49 =item $c->original_request
51 Returns the original Apache request object.
55 =head1 OVERLOADED METHODS
57 This class overloads some methods from C<Catalyst::Engine>.
61 =item $c->finalize_headers
65 sub finalize_headers {
67 for my $name ( $c->response->headers->header_field_names ) {
68 next if $name =~ /Content-Type/i;
69 $c->original_request->headers_out->set(
70 $name => $c->response->headers->header($name) );
72 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
73 my %cookie = ( -name => $name, -value => $cookie->{value} );
74 $cookie->{-expires} = $cookie->{expires} if $cookie->{expires};
75 $cookie->{-domain} = $cookie->{domain} if $cookie->{domain};
76 $cookie->{-path} = $cookie->{path} if $cookie->{path};
77 $cookie->{-secure} = $cookie->{secure} if $cookie->{secure};
78 my $cookie = Apache::Cookie->new( $c->original_request, %cookie );
80 ? $c->apache_request->err_headers_out->add(
81 'Set-Cookie' => $cookie->as_string )
84 $c->original_request->status( $c->response->status );
85 $c->original_request->content_type( $c->response->headers->content_type
87 MP2 || $c->apache_request->send_http_header;
91 =item $c->finalize_output
97 $c->original_request->print( $c->response->{output} );
100 =item $c->prepare_connection
104 sub prepare_connection {
106 $c->req->hostname( $c->apache_request->connection->remote_host );
107 $c->req->address( $c->apache_request->connection->remote_ip );
110 =item $c->prepare_cookies
114 sub prepare_cookies {
117 ? $c->req->cookies( { Apache::Cookie->fetch } )
119 { Apache::Cookie->new( $c->apache_request )->fetch } );
122 =item $c->prepare_headers
126 sub prepare_headers {
128 $c->req->method( $c->apache_request->method );
129 $c->req->headers->header( %{ $c->apache_request->headers_in } );
132 =item $c->prepare_parameters
136 sub prepare_parameters {
139 foreach my $key ( $c->apache_request->param ) {
140 my @values = $c->apache_request->param($key);
141 $args{$key} = @values == 1 ? $values[0] : \@values;
143 $c->req->parameters( \%args );
146 =item $c->prepare_path
152 $c->req->path( $c->apache_request->uri );
153 my $loc = $c->apache_request->location;
154 no warnings 'uninitialized';
155 $c->req->{path} =~ s/^($loc)?\///;
157 $base->scheme( $ENV{HTTPS} ? 'https' : 'http' );
158 $base->host( $c->apache_request->hostname );
159 $base->port( $c->apache_request->get_server_port );
160 my $path = $c->apache_request->location;
161 $base->path( $path =~ /\/$/ ? $path : "$path/" );
162 $c->req->base( $base->as_string );
165 =item $c->prepare_request($r)
169 sub prepare_request {
171 $c->apache_request( Apache::Request->new($r) );
172 $c->original_request($r);
175 =item $c->prepare_uploads
179 sub prepare_uploads {
181 for my $upload ( $c->apache_request->upload ) {
182 $upload = $c->apache_request->upload($upload) if MP2;
183 $c->req->uploads->{ $upload->filename } = {
185 size => $upload->size,
186 type => $upload->type
205 Sebastian Riedel, C<sri@cpan.org>
209 This program is free software, you can redistribute it and/or modify it under
210 the same terms as Perl itself.