The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Mojolicious::Plugin::ReverseProxy

 package ProxyFun;
 use Mojo::Base 'Mojolicious';

 sub startup {
    my $app = shift;

    $app->plugin('Mojolicious::Plugin::ReverseProxy',{
        # mandatory
        destination_url => 'http://www.oetiker.ch',
        # optional
        routes => $app->routes, # default 
        mount_point => '/', # default
        req_processor => sub { 
            my ($c,$req) = @_; 
            # do something to the request object prior
            # to passing it on to the destination_url
            # maybe fix the Origin or Referer headers
            for (qw(Origin Referer)){
                my $value = $req->headers->header($_) or next;
                if ( $value =~ s{http://www.oetiker.ch}{http://localhost:3000} ){
                    $req->headers->header($_,$value);   
                }
            }                
        },
        res_processor => sub {
            my ($c,$res) = @_;
            # do something to the response object prior
            # to passing it on to the client
            # maybe fixing the location header
            # or absolute URLs in the body
            if (my $location = $res->headers->location){
                if ( $location =~ s{http://www.oetiker.ch}{http://localhost:3000} ){
                    $res->headers->location($location); 
                }
            }
            if ($res->headers->content_type =~ m{text/html} and my $body = $res->body){
                if ( $body =~ s{http://www.oetiker.ch}{http://localhost:3000}g){
                    $res->body($body);
                    $res->headers->content_length(length($body));
                }
            }
        },
    }
 }

DESCRIPTION

The Mojolicious::Plugin::ReverseProxy lets your register a proxy route. The module is rather mindless in the sense that it does not try to help you with fixing headers or content to actually work with the proxy, apart from the Host header.

What makes this Plugin really useful, is that you can supply a req_processor and a res_processor callback which will act on the request prior to passing it on to the destination and on the response prior to returning it to the client respectively.

The plugin takes the following options:

destination_url

Where should the proxy connect to

  destination_url => 'http://www.oetiker.ch'
routes (defaults to app->routes)

the routes object to use for adding the proxy route

mount_point (defaults to /)

under which path should the proxy appear.

req_processor

Can be pointed to an anonymous subroutine which is called prior to handing control over to the user agent.

If you render the page in the req_processor callback, the page will be returned immediately without calling the destination_url

res_processor

Can be pointed to an anonymous subroutine which is called prior to rendering the response.

AUTHOR

Tobias Oetiker, <tobi@oetiker.ch>

COPYRIGHT

Copyright OETIKER+PARTNER AG 2014

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 162:

You forgot a '=back' before '=head1'