Apache2-CondProxy

 view release on metacpan or  search on metacpan

lib/Apache2/CondProxy.pm  view on Meta::CPAN

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

lib/Apache2/CondProxy.pm  view on Meta::CPAN

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 {

lib/Apache2/CondProxy.pm  view on Meta::CPAN

            # 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;

t/htdocs/env.cgi  view on Meta::CPAN

#!/bin/sh

echo Status: 404 Not Found
echo Content-Type: text/plain
echo
env | sort
echo
cat -



( run in 0.883 second using v1.01-cache-2.11-cpan-39bf76dae61 )