7d11ea42c5f070503857724500dd224a06bc62a5
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Apache.pm
1 package Catalyst::Engine::Apache;
2
3 use strict;
4 use base 'Catalyst::Engine';
5
6 use URI;
7 use URI::http;
8
9 __PACKAGE__->mk_accessors(qw/apache/);
10
11 =head1 NAME
12
13 Catalyst::Engine::Apache - Catalyst Apache Engine
14
15 =head1 SYNOPSIS
16
17 See L<Catalyst>.
18
19 =head1 DESCRIPTION
20
21 This is the Catalyst engine specialized for Apache (i.e. for mod_perl).
22
23 =head1 METHODS
24
25 =over 4
26
27 =item $c->apache
28
29 Returns an C<Apache::Request> object.
30
31 =back
32
33 =head1 OVERLOADED METHODS
34
35 This class overloads some methods from C<Catalyst::Engine>.
36
37 =over 4
38
39 =item $c->finalize_body
40
41 =cut
42
43 sub finalize_body {
44     my $c = shift;
45     $c->apache->print( $c->response->output );
46 }
47
48 =item $c->prepare_body
49
50 =cut
51
52 sub prepare_body {
53     my $c = shift;
54
55     my $length = $c->request->content_length;
56     my ( $buffer, $content );
57
58     while ($length) {
59
60         $c->apache->read( $buffer, ( $length < 8192 ) ? $length : 8192 );
61
62         $length  -= length($buffer);
63         $content .= $buffer;
64     }
65     
66     $c->request->input($content);
67 }
68
69 =item $c->prepare_connection
70
71 =cut
72
73 sub prepare_connection {
74     my $c = shift;
75     $c->request->hostname( $c->apache->connection->remote_host );
76     $c->request->address( $c->apache->connection->remote_ip );
77 }
78
79 =item $c->prepare_headers
80
81 =cut
82
83 sub prepare_headers {
84     my $c = shift;
85     $c->request->method( $c->apache->method );
86     $c->request->header( %{ $c->apache->headers_in } );
87 }
88
89 =item $c->prepare_parameters
90
91 =cut
92
93 sub prepare_parameters {
94     my $c = shift;
95
96     foreach my $key ( $c->apache->param ) {
97         my @values = $c->apache->param($key);
98         $c->req->parameters->{$key} = ( @values == 1 ) ? $values[0] : \@values;
99     }
100 }
101
102 =item $c->prepare_path
103
104 =cut
105
106 # XXX needs fixing, only work with <Location> directive,
107 # not <Directory> directive
108 sub prepare_path {
109     my $c = shift;
110     $c->request->path( $c->apache->uri );
111     my $loc = $c->apache->location;
112     no warnings 'uninitialized';
113     $c->req->{path} =~ s/^($loc)?\///;
114     my $base = URI->new;
115     $base->scheme( $ENV{HTTPS} ? 'https' : 'http' );
116     $base->host( $c->apache->hostname );
117     $base->port( $c->apache->get_server_port );
118     my $path = $c->apache->location;
119     $base->path( $path =~ /\/$/ ? $path : "$path/" );
120     $c->request->base( $base->as_string );
121 }
122
123 =item $c->run
124
125 =cut
126
127 sub run { }
128
129 =back
130
131 =head1 SEE ALSO
132
133 L<Catalyst>.
134
135 =head1 AUTHOR
136
137 Sebastian Riedel, C<sri@cpan.org>
138
139 =head1 COPYRIGHT
140
141 This program is free software, you can redistribute it and/or modify it under
142 the same terms as Perl itself.
143
144 =cut
145
146 1;