3 our $VERSION = '2.121';
13 use Maypole::Constants;
15 __PACKAGE__->mk_accessors( qw( ar ) );
21 $MODPERL2 = ( exists $ENV{MOD_PERL_API_VERSION} and
22 $ENV{MOD_PERL_API_VERSION} >= 2 );
24 eval 'use mod_perl2; $modperl_version = $mod_perl2::VERSION;';
26 $modperl_version = $Apache2::RequestRec::VERSION;
28 require Apache2::RequestIO;
29 require Apache2::RequestRec;
31 require Apache2::RequestUtil;
32 eval 'use Apache2::Const -compile => qw/REDIRECT/;'; # -compile 4 no import
36 eval ' use mod_perl; ';
38 require Apache::Request;
39 eval 'use Apache::Constants -compile => qw/REDIRECT/;';
47 Apache::MVC - Apache front-end to Maypole
52 use Maypole::Application;
56 A mod_perl platform driver for Maypole. Your application can inherit from
57 Apache::MVC directly, but it is recommended that you use
58 L<Maypole::Application>.
62 Create a driver module like the one illustrated in L<Maypole::Application>.
64 Put the following in your Apache config:
67 SetHandler perl-script
71 Copy the templates found in F<templates/factory> into the F<beer/factory>
72 directory off the web root. When the designers get back to you with custom
73 templates, they are to go in F<beer/custom>. If you need to override templates
74 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
76 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
77 commands; for instance, to see a list of breweries, go to
79 http://your.site/beer/brewery/list
81 For more information about how the system works and how to extend it,
86 This class overrides a set of methods in the base Maypole class to provide its
87 functionality. See L<Maypole> for these:
97 my $request_options = $self->config->request_options || {};
100 $ar = eval {require Apache2::Request} ? Apache2::Request->new($r,%{$request_options}) : $r;
102 if (keys %$request_options) {
103 $ar = Apache::Request->new($r,%{$request_options});
105 $ar = Apache::Request->instance($r);
116 my ($self,@args) = @_;
117 my ($package, $line) = (caller)[0,2];
118 my $ar = $self->parent ? $self->parent->{ar} : $self->{ar};
119 if ( $args[0] and ref $self ) {
120 $ar->warn("[$package line $line] ", @args) ;
122 print "warn called by ", caller, " with ", @_, "\n";
135 # Reconstruct the request headers
136 $self->headers_in(Maypole::Headers->new);
139 if ($MODPERL2) { %headers = %{$self->ar->headers_in};
140 } else { %headers = $self->ar->headers_in; }
141 for (keys %headers) {
142 $self->headers_in->set($_, $headers{$_});
145 $self->preprocess_location();
147 my $path = $self->ar->uri;
148 my $base = URI->new($self->config->uri_base);
149 my $loc = $base->path;
152 no warnings 'uninitialized';
153 $path .= '/' if $path eq $loc;
155 $path =~ s/^($loc)?//;
157 $path =~ s/^($loc)?\///;
172 $self->params( { $self->_mod_perl_args( $self->ar ) } );
173 $self->query( $self->params );
176 =item redirect_request
178 Sets output headers to redirect based on the arguments provided
180 Accepts either a single argument of the full url to redirect to, or a hash of
183 $r->redirect_request('http://www.example.com/path');
187 $r->redirect_request(protocol=>'https', domain=>'www.example.com', path=>'/path/file?arguments', status=>'302', url=>'..');
189 The named parameters are protocol, domain, path, status and url
191 Only 1 named parameter is required but other than url, they can be combined as
192 required and current values (from the request) will be used in place of any
193 missing arguments. The url argument must be a full url including protocol and
194 can only be combined with status.
198 sub redirect_request {
200 my $redirect_url = $_[0];
201 my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' : eval 'Apache::Constants::REDIRECT;';
205 $redirect_url = $args{url};
207 my $path = $args{path} || $r->path;
208 my $host = $args{domain} || $r->ar->hostname;
209 my $protocol = $args{protocol} || $r->get_protocol;
211 $redirect_url = URI->new;
212 $redirect_url->scheme($protocol);
213 $redirect_url->host($host);
214 $redirect_url->path($path);
216 $status = $args{status} if ($args{status});
219 $r->ar->status($status);
220 $r->ar->headers_out->set('Location' => $redirect_url);
221 $r->output('<html><head><title>redirecting...</title></head><body><h2>redirecting..</h2></body></html>') unless ($r->output);
232 my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
242 $r->ar->content_type(
243 $r->content_type =~ m/^text/
244 ? $r->content_type . "; charset=" . $r->document_encoding
247 $r->ar->headers_out->set(
248 "Content-Length" => do { use bytes; length $r->output }
251 foreach ($r->headers_out->field_names) {
252 next if /^Content-(Type|Length)/;
253 $r->ar->headers_out->set($_ => $r->headers_out->get($_));
256 $MODPERL2 || $r->ar->send_http_header;
257 $r->ar->print( $r->output );
260 =item get_template_root
264 sub get_template_root {
266 $r->ar->document_root . "/" . $r->ar->location;
273 #########################################################
274 # private / internal methods and subs
278 my ( $self, $apr ) = @_;
280 if ($apr->isa('Apache::Request')) {
281 foreach my $key ( $apr->param ) {
282 my @values = $apr->param($key);
283 $args{$key} = @values == 1 ? $values[0] : \@values;
286 my $body = $self->_prepare_body($apr);
287 %args = %{$body->param};
288 my $uri = URI->new($self->ar->unparsed_uri);
289 foreach my $key ($uri->query_param) {
290 if (ref $args{$key}) {
291 push (@{$args{$key}}, $uri->query_param($key));
294 $args{$key} = [ $args{$key}, $uri->query_param($key) ];
296 my @args = $uri->query_param($key);
297 if (scalar @args > 1) {
298 $args{$key} = [ $uri->query_param($key) ];
300 $args{$key} = $uri->query_param($key);
310 my ( $self, $r ) = @_;
312 unless ($self->{__http_body}) {
313 my $content_type = $r->headers_in->get('Content-Type');
314 my $content_length = $r->headers_in->get('Content-Length');
315 my $body = HTTP::Body->new( $content_type, $content_length );
316 my $length = $content_length;
318 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
319 $length -= length($buffer);
322 $self->{__http_body} = $body;
324 return $self->{__http_body};
331 Simon Cozens, C<simon@cpan.org>
336 Marcus Ramberg, C<marcus@thefeed.no>
337 Sebastian Riedel, C<sri@oook.de>
341 You may distribute this code under the same terms as Perl itself.