]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC.pm
hopefully fixed Apache::MVC with HTTP::Body fallback to handle url args
[maypole.git] / lib / Apache / MVC.pm
1 package Apache::MVC;
2
3 our $VERSION = '2.11';
4
5 use strict;
6 use warnings;
7
8 use URI;
9 use URI::QueryParam;
10
11 use base 'Maypole';
12 use Maypole::Headers;
13 use Maypole::Constants;
14
15 __PACKAGE__->mk_accessors( qw( ar ) );
16
17 our $MODPERL2;
18 our $modperl_version;
19
20 BEGIN {
21     $MODPERL2  = ( exists $ENV{MOD_PERL_API_VERSION} and
22                         $ENV{MOD_PERL_API_VERSION} >= 2 );
23     if ($MODPERL2) {
24      eval 'use mod_perl2; $modperl_version = $mod_perl2::VERSION;';
25      if ($@) {
26       $modperl_version = $Apache2::RequestRec::VERSION;
27      }
28      require Apache2::RequestIO;
29      require Apache2::RequestRec;
30      require Apache2::RequestUtil;
31      eval 'use Apache2::Const -compile => qw/REDIRECT/;'; # -compile 4 no import
32      require APR::URI;
33      require HTTP::Body;
34     } else {
35      eval ' use mod_perl; ';
36      require Apache;
37      require Apache::Request;
38      eval 'use Apache::Constants -compile => qw/REDIRECT/;';
39      $modperl_version = 1;
40     }
41
42 }
43
44 =head1 NAME
45
46 Apache::MVC - Apache front-end to Maypole
47
48 =head1 SYNOPSIS
49
50     package BeerDB;
51     use Maypole::Application;
52
53 =head1 DESCRIPTION
54
55 A mod_perl platform driver for Maypole. Your application can inherit from
56 Apache::MVC directly, but it is recommended that you use
57 L<Maypole::Application>.
58
59 =head1 INSTALLATION
60
61 Create a driver module like the one illustrated in L<Maypole::Application>.
62
63 Put the following in your Apache config:
64
65     <Location /beer>
66         SetHandler perl-script
67         PerlHandler BeerDB
68     </Location>
69
70 Copy the templates found in F<templates/factory> into the F<beer/factory>
71 directory off the web root. When the designers get back to you with custom
72 templates, they are to go in F<beer/custom>. If you need to override templates
73 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
74
75 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
76 commands; for instance, to see a list of breweries, go to
77
78     http://your.site/beer/brewery/list
79
80 For more information about how the system works and how to extend it,
81 see L<Maypole>.
82
83 =head1 Implementation
84
85 This class overrides a set of methods in the base Maypole class to provide its
86 functionality. See L<Maypole> for these:
87
88 =over
89
90 =item get_request
91
92 =cut
93
94 sub get_request {
95     my ($self, $r) = @_;
96     my $ar = ($MODPERL2) ? $r : Apache::Request->instance($r);
97     $self->ar($ar);
98 }
99
100 =item parse_location
101
102 =cut
103
104 sub parse_location {
105     my $self = shift;
106
107     # Reconstruct the request headers
108     $self->headers_in(Maypole::Headers->new);
109     my %headers;
110     if ($MODPERL2) { %headers = %{$self->ar->headers_in};
111     } else { %headers = $self->ar->headers_in; }
112     for (keys %headers) {
113         $self->headers_in->set($_, $headers{$_});
114     }
115     my $path = $self->ar->uri;
116     my $loc  = $self->ar->location;
117     {
118         no warnings 'uninitialized';
119         $path .= '/' if $path eq $loc;
120         $path =~ s/^($loc)?\///;
121     }
122     $self->path($path);
123     $self->parse_path;
124     $self->parse_args;
125 }
126
127 =item parse_args
128
129 =cut
130
131 sub parse_args {
132     my $self = shift;
133     $self->params( { $self->_mod_perl_args( $self->ar ) } );
134     $self->query( $self->params );
135 }
136
137 =item redirect_request
138
139 =cut
140
141 sub redirect_request
142 {
143   my $r = shift;
144   my $redirect_url = $_[0];
145   my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' :
146           eval 'Apache::Constants::REDIRECT;'; # why have to eval this?
147   if ($_[1]) {
148     my %args = @_;
149     if ($args{url}) {
150       $redirect_url = $args{url};
151     } else {
152       my $path = $args{path} || $r->path;
153       my $host = $args{domain} || $r->ar->hostname;
154       my $protocol = $args{protocol} || $r->get_protocol;
155       $redirect_url = "${protocol}://${host}/${path}";
156     }
157     $status = $args{status} if ($args{status});
158   }
159
160   $r->ar->status($status);
161   $r->ar->headers_out->set('Location' => $redirect_url);
162   return OK;
163 }
164
165 =item get_protocol
166
167 =cut
168
169 sub get_protocol {
170   my $self = shift;
171   my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
172   return $protocol;
173 }
174
175 =item send_output
176
177 =cut
178
179 sub send_output {
180     my $r = shift;
181     $r->ar->content_type(
182           $r->content_type =~ m/^text/
183         ? $r->content_type . "; charset=" . $r->document_encoding
184         : $r->content_type
185     );
186     $r->ar->headers_out->set(
187         "Content-Length" => do { use bytes; length $r->output }
188     );
189
190     foreach ($r->headers_out->field_names) {
191         next if /^Content-(Type|Length)/;
192         $r->ar->headers_out->set($_ => $r->headers_out->get($_));
193     }
194
195     $MODPERL2 || $r->ar->send_http_header;
196     $r->ar->print( $r->output );
197 }
198
199 =item get_template_root
200
201 =cut
202
203 sub get_template_root {
204     my $r = shift;
205     $r->ar->document_root . "/" . $r->ar->location;
206 }
207
208 =back
209
210 =cut
211
212 #########################################################
213 # private / internal methods and subs
214
215
216 sub _mod_perl_args {
217     my ( $self, $apr ) = @_;
218     my %args;
219     if ($apr->isa('Apache::Request')) {
220       foreach my $key ( $apr->param ) {
221         my @values = $apr->param($key);
222         $args{$key} = @values == 1 ? $values[0] : \@values;
223       }
224     } else {
225       my $body = $self->_prepare_body($apr);
226       %args = %{$body->param};
227       my $uri = URI->new($self->ar->uri);
228       foreach my $key ($uri->query_param) {
229         if (ref $args{$key}) {
230           push (@{$args{$key}}, $uri->query_param($key));
231         } else {
232           if ($args{$key}) {
233             $args{$key} = [ $args{$key}, $uri->query_param($key) ];
234           } else {
235             my @args = $uri->query_param($key);
236             if (scalar @args > 1) {
237               $args{$key} = [ $uri->query_param($key) ];
238             } else {
239               $args{$key} = $uri->query_param($key);
240             }
241           }
242         }
243       }
244     }
245     return %args;
246 }
247
248 sub _prepare_body {
249     my ( $self, $r ) = @_;
250
251     unless ($self->{__http_body}) {
252         my $content_type   = $r->headers_in->get('Content-Type');
253         my $content_length = $r->headers_in->get('Content-Length');
254         my $body   = HTTP::Body->new( $content_type, $content_length );
255         my $length = $content_length;
256         while ( $length ) {
257             $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
258             $length -= length($buffer);
259             $body->add($buffer);
260         }
261         $self->{__http_body} = $body;
262     }
263     return $self->{__http_body};
264 }
265
266
267
268 =head1 AUTHOR
269
270 Simon Cozens, C<simon@cpan.org>
271
272 =head1 CREDITS
273
274 Aaron Trevena
275 Marcus Ramberg, C<marcus@thefeed.no>
276 Sebastian Riedel, C<sri@oook.de>
277
278 =head1 LICENSE
279
280 You may distribute this code under the same terms as Perl itself.
281
282 =cut
283
284 1;