Apache2-CondProxy
view release on metacpan or search on metacpan
lib/Apache2/CondProxy.pm view on Meta::CPAN
# check for some other kind of error
if ($rv != APR::Const::SUCCESS) {
$ret = $rv;
return $rv if $rv != APR::Const::ENOTIMPL;
}
}
# now concatenate the brigade to the target
$saveto->concat($bb);
return $ret;
};
}
}
# if i recall correctly, mod_perl doesn't like 'use base'.
our @ISA = qw(Apache2::RequestRec);
=head1 NAME
Apache2::CondProxy - Intelligent reverse proxy for missing resources
=head1 VERSION
Version 0.17
=cut
our $VERSION = '0.17';
=head1 SYNOPSIS
# httpd.conf
PerlFixupHandler Apache2::CondProxy
PerlSetVar ProxyTarget http://another.host/
PerlSetVar RequestBodyCache /tmp
PerlSetVar MatchScheme on
=head1 DESCRIPTION
This module performs the logic required to achieve what is implied by
the following Apache configuration:
# httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_URI} !-U
RewriteRule (.*) http://another.host$1 [P,NS]
Which says I<if I can't respond to a given request, try another.host>.
Unfortunately, the architecture of mod_rewrite, as well as the design
of Apache's handler model itself, prohibits this. In the first case,
all C<RewriteCond> directives are evaluated I<after> the associated
C<RewriteRule>. In the second, the response code is initialized to
C<200> and remains that way until it is changed, most likely by a
response handler which never gets run. This confluence of behaviour
makes the above configuration not do what we imagine it would.
This module works by running the request all the way through in a
subrequest. Before doing so, a filter is installed to trap the
subrequest's response. If the response is I<unsuccessful>,
specifically if it is a C<403> or C<404>, the filter disposes of the
error body, and the request is forwarded to the proxy target. The
proxy URI scheme is matched to the original request URI scheme, so
make sure you have C<SSLProxyEngine on>.
If a proxy response contains a C<Location> header, and its host is the
same as the proxy target, that header will be rewritten to point to
the source host.
=head1 DIRECTIVES
Configuration directives are currently carried out using
C<PerlSetVar>. Yep, I know. Consider them I<provisional>. You will
almost certainly need to reconfigure this module with proper Apache
directives by the time I get to 0.20.
=head2 ProxyTarget
ProxyTarget http://some.other.site/
This is the prefix of the location where requests go when they can't
be served by the site where the request was originated. Note the path
of the original request is appended I<relative> to the path of this
URI, as if its initial C</> was pruned off, so craft this URI
accordingly.
=head2 RequestBodyCache
RequestBodyCache /tmp/cond-proxy
In order to work with request content (e.g. C<POST>, C<PUT>), we have
to stash it somewhere so we can replay it into the pipe. This means
the contents of this directory are potentially sensitive. So if you're
going to put it in C</tmp>, make sure to at least make it only
readable to the server. Or you can have this module do that
automatically, just make sure it can write to the parent.
=head2 MatchScheme
MatchScheme on
This will cause the URI scheme in proxy requests (and C<Location>
headers from proxied responses) to match that of the originating
request, be it C<http> or C<https>.
=head2 RemoteFirst
RemoteFirst on
This will try to serve the resource at C<ProxyTarget> first and
I<then> the local resource in case the remote resource responds with a
404. Note: Under the hood, this still checks the local resource first,
due to a limitation of C<mod_proxy>'s handling of subrequests.
=cut
# XXX this probably doesn't need to be a method handler
sub new {
bless {}, __PACKAGE__;
}
sub handler : method {
my $r = ref $_[0] ? $_[0] : bless { r => $_[1] }, $_[0];
if ($r->is_initial_req) {
# make the temp directory
# apparently umask has no effect on mkpath
# umask 077;
my $dir = Path::Class::Dir->new
($r->dir_config('RequestBodyCache') || File::Spec->tmpdir);
eval { $dir->mkpath(0, 0700) };
if ($@) {
$r->log->crit("Cannot make directory $dir: $@");
return Apache2::Const::SERVER_ERROR;
}
$r->pnotes(CACHE, $dir);
my $rf = $r->dir_config('RemoteFirst') || '';
if ($rf =~ $TRUE) {
# this juggling is because mod_proxy eats non-main
# requests, which is why we have to subrequest the local
# resource in an output filter, then pipe that out as our
# response.
return _do_proxy($r, 1);
}
else {
my $uri = $r->unparsed_uri;
$r->log->debug("Attempting lookup on $uri");
my $subr = _make_subreq($r, $uri);
# set the content-type and content-length in the subrequest
my $ct = $r->headers_in->get('Content-Type');
$subr->headers_in->set('Content-Type', $ct) if $ct;
my $cl = $r->headers_in->get('Content-Length');
$subr->headers_in->set('Content-Length', $cl) if $cl;
# remove Accept-Encoding headers for proxy
my $ae = $r->headers_in->get('Accept-Encoding');
$r->headers_in->unset('Accept-Encoding');
if ($subr->status == 404) {
$r->log->debug('Proxying before subrequest is run');
return _do_proxy($r);
}
$r->log->debug(
sprintf 'Results inconclusive: %d; running subrequest',
$subr->status);
$subr->add_input_filter(\&_input_filter_tee);
$subr->add_output_filter(\&_output_filter_hold);
my $rv = $subr->run;
# we only care about 404
my $st = $subr->status;
if (grep { $rv == $_ || $st == $_ } (403, 404)) {
$r->log->debug("Proxying $uri after subrequest is run");
return _do_proxy($r);
}
else {
# override the subrequest status
$subr->status($rv) if $subr->status != $rv && $rv != 0;
$r->status($subr->status);
# replace Accept-Encoding header
$r->headers_in->set('Accept-Encoding', $ae) if $ae;
$r->log->debug(
sprintf 'Subrequest returned %d; serving content for %s',
$subr->status, $uri);
# copy headers from subreq
$r->headers_out->overlap
($subr->headers_out, APR::Const::OVERLAP_TABLES_SET);
$r->err_headers_out->overlap
($subr->err_headers_out, APR::Const::OVERLAP_TABLES_SET);
# apparently content_type has to be done separately
#$r->log->debug($subr->content_type);
$r->content_type($subr->content_type) if $subr->content_type;
$r->content_encoding($subr->content_encoding)
if $subr->content_encoding;
$r->set_last_modified($subr->mtime) if $subr->mtime;
$r->SUPER::handler('modperl');
$r->set_handlers(PerlResponseHandler => \&_response_handler);
$r->push_handlers(PerlCleanupHandler => \&_cleanup_handler);
$r->add_output_filter(\&_output_filter_release);
return Apache2::Const::OK;
}
}
}
Apache2::Const::OK;
}
sub _make_subreq {
my ($r, $uri) = @_;
$uri = $r->unparsed_uri unless defined $uri;
my $subr = $r->lookup_method_uri($r->method, $uri);
# set the content-type and content-length in the subrequest
my $ct = $r->headers_in->get('Content-Type');
$subr->headers_in->set('Content-Type', $ct) if $ct;
my $cl = $r->headers_in->get('Content-Length');
$subr->headers_in->set('Content-Length', $cl) if $cl;
# remove this so no gzip filter etc happens
$subr->headers_in->unset('Accept-Encoding');
$subr;
}
( run in 2.150 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )