1 package Catalyst::Engine::Apache;
5 use constant MP2 => $mod_perl::VERSION >= 1.99;
6 use base 'Catalyst::Engine';
12 require Apache::RequestIO;
13 require Apache::RequestRec;
14 require Apache::SubRequest;
15 require Apache::RequestUtil;
19 else { require Apache }
22 require Apache::Request;
23 require Apache::Cookie;
24 require Apache::Upload if MP2;
26 __PACKAGE__->mk_accessors(qw/apache_request original_request/);
30 Catalyst::Engine::Apache - Catalyst Apache Engine
38 This is the Catalyst engine specialized for Apache (i.e. for mod_perl).
44 =item $c->apache_request
46 Returns an C<Apache::Request> object.
48 =item $c->original_request
50 Returns the original Apache request object.
54 =head1 OVERLOADED METHODS
56 This class overloads some methods from C<Catalyst::Engine>.
60 =item $c->finalize_headers
64 sub finalize_headers {
66 for my $name ( $c->response->headers->header_field_names ) {
67 next if $name =~ /Content-Type/i;
68 $c->original_request->headers_out->set(
69 $name => $c->response->headers->header($name) );
71 while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
72 my %cookie = ( -name => $name, -value => $cookie->{value} );
73 $cookie->{-expires} = $cookie->{expires} if $cookie->{expires};
74 $cookie->{-domain} = $cookie->{domain} if $cookie->{domain};
75 $cookie->{-path} = $cookie->{path} if $cookie->{path};
76 $cookie->{-secure} = $cookie->{secure} if $cookie->{secure};
77 my $cookie = Apache::Cookie->new( $c->original_request, %cookie );
79 ? $c->apache_request->err_headers_out->add(
80 'Set-Cookie' => $cookie->as_string )
83 $c->original_request->status( $c->response->status );
84 $c->original_request->content_type( $c->response->headers->content_type
86 MP2 || $c->apache_request->send_http_header;
90 =item $c->finalize_output
96 $c->original_request->print( $c->response->{output} );
99 =item $c->prepare_cookies
103 sub prepare_cookies {
106 ? $c->req->cookies( { Apache::Cookie->fetch } )
108 { Apache::Cookie->new( $c->apache_request )->fetch } );
111 =item $c->prepare_headers
115 sub prepare_headers {
117 $c->req->method( $c->apache_request->method );
118 $c->req->headers->header( %{ $c->apache_request->headers_in } );
121 =item $c->prepare_parameters
125 sub prepare_parameters {
128 foreach my $key ( $c->apache_request->param ) {
129 my @values = $c->apache_request->param($key);
130 $args{$key} = @values == 1 ? $values[0] : \@values;
132 $c->req->parameters( \%args );
135 =item $c->prepare_path
141 $c->req->path( $c->apache_request->uri );
142 my $loc = $c->apache_request->location;
143 no warnings 'uninitialized';
144 $c->req->{path} =~ s/^($loc)?\///;
146 $base->scheme( $ENV{HTTPS} ? 'https' : 'http' );
147 $base->host( $c->apache_request->hostname );
148 $base->port( $c->apache_request->get_server_port );
149 my $path = $c->apache_request->location;
150 $base->path( $path =~ /\/$/ ? $path : "$path/" );
151 $c->req->base( $base->as_string );
154 =item $c->prepare_request($r)
158 sub prepare_request {
160 $c->apache_request( Apache::Request->new($r) );
161 $c->original_request($r);
164 =item $c->prepare_uploads
168 sub prepare_uploads {
170 for my $upload ( $c->apache_request->upload ) {
171 $upload = $c->apache_request->upload($upload) if MP2;
172 $c->req->uploads->{ $upload->filename } = {
174 size => $upload->size,
175 type => $upload->type
188 Sebastian Riedel, C<sri@cpan.org>
192 This program is free software, you can redistribute it and/or modify it under
193 the same terms as Perl itself.