10 use Maypole::Constants;
12 __PACKAGE__->mk_accessors( qw( ar ) );
17 # pjs -- fixed to use standard way from perl.apache.org
19 #eval 'use Apache;'; # could fuck shit up if you have some file na
20 # named Apache.pm in your path forex CGI/Apache.pm
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;
30 require Apache2::RequestUtil;
31 eval 'use Apache2::Const -compile => qw/REDIRECT/;'; # -compile 4 no import
35 eval ' use mod_perl; ';
37 require Apache::Request;
38 eval 'use Apache::Constants -compile => qw/REDIRECT/;';
48 Apache::MVC - Apache front-end to Maypole
53 use Maypole::Application;
57 A mod_perl platform driver for Maypole. Your application can inherit from
58 Apache::MVC directly, but it is recommended that you use
59 L<Maypole::Application>.
63 Create a driver module like the one illustrated in L<Maypole::Application>.
65 Put the following in your Apache config:
68 SetHandler perl-script
72 Copy the templates found in F<templates/factory> into the F<beer/factory>
73 directory off the web root. When the designers get back to you with custom
74 templates, they are to go in F<beer/custom>. If you need to override templates
75 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
77 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
78 commands; for instance, to see a list of breweries, go to
80 http://your.site/beer/brewery/list
82 For more information about how the system works and how to extend it,
87 This class overrides a set of methods in the base Maypole class to provide its
88 functionality. See L<Maypole> for these:
98 my $ar = ($MODPERL2) ? $r : Apache::Request->instance($r);
109 # Reconstruct the request headers
110 $self->headers_in(Maypole::Headers->new);
112 if ($MODPERL2) { %headers = %{$self->ar->headers_in};
113 } else { %headers = $self->ar->headers_in; }
114 for (keys %headers) {
115 $self->headers_in->set($_, $headers{$_});
117 my $path = $self->ar->uri;
118 my $loc = $self->ar->location;
120 no warnings 'uninitialized';
121 $path .= '/' if $path eq $loc;
122 $path =~ s/^($loc)?\///;
136 $self->params( { $self->_mod_perl_args( $self->ar ) } );
137 $self->query( $self->params );
140 =item redirect_request
144 # FIXME: use headers_in to gather host and other information?
145 # pjs 4-7-06 fixed so it works but did not fix headers_in issue
149 my $redirect_url = $_[0];
150 my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' :
151 eval 'Apache::Constants::REDIRECT;'; # why have to eval this?
155 $redirect_url = $args{url};
157 my $path = $args{path} || $r->path;
158 my $host = $args{domain} || $r->ar->hostname;
159 my $protocol = $args{protocol} || $r->get_protocol;
160 $redirect_url = "${protocol}://${host}/${path}";
162 $status = $args{status} if ($args{status});
165 $r->ar->status($status);
166 $r->ar->headers_out->set('Location' => $redirect_url);
177 my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
187 $r->ar->content_type(
188 $r->content_type =~ m/^text/
189 ? $r->content_type . "; charset=" . $r->document_encoding
192 $r->ar->headers_out->set(
193 "Content-Length" => do { use bytes; length $r->output }
196 foreach ($r->headers_out->field_names) {
197 next if /^Content-(Type|Length)/;
198 $r->ar->headers_out->set($_ => $r->headers_out->get($_));
201 $MODPERL2 || $r->ar->send_http_header;
202 $r->ar->print( $r->output );
205 =item get_template_root
209 sub get_template_root {
211 $r->ar->document_root . "/" . $r->ar->location;
218 #########################################################
219 # private / internal methods and subs
223 my ( $self, $apr ) = @_;
225 if ($apr->isa('Apache::Request')) {
226 foreach my $key ( $apr->param ) {
227 my @values = $apr->param($key);
228 $args{$key} = @values == 1 ? $values[0] : \@values;
231 my $body = $self->_prepare_body($apr);
232 %args = %{$body->param};
238 my ( $self, $r ) = @_;
240 unless ($self->{__http_body}) {
241 my $content_type = $r->headers_in->get('Content-Type');
242 my $content_length = $r->headers_in->get('Content-Length');
243 my $body = HTTP::Body->new( $content_type, $content_length );
244 my $length = $content_length;
246 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
247 $length -= length($buffer);
250 $self->{__http_body} = $body;
252 return $self->{__http_body};
259 Simon Cozens, C<simon@cpan.org>
264 Marcus Ramberg, C<marcus@thefeed.no>
265 Sebastian Riedel, C<sri@oook.de>
269 You may distribute this code under the same terms as Perl itself.