File Coverage

lib/Mojolicious/Plugin/ReverseProxy.pm
Criterion Covered Total %
statement 27 39 69.2
branch 2 10 20.0
condition 2 5 40.0
subroutine 6 7 85.7
pod 1 1 100.0
total 38 62 61.2


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ReverseProxy;
2 1     1   764 use Mojo::Base 'Mojolicious::Plugin';
  1         3  
  1         11  
3 1     1   1030 use Mojo::Transaction::HTTP;
  1         2  
  1         25  
4 1     1   90 use Mojo::UserAgent;
  1         3  
  1         14  
5 1     1   33 use Mojo::URL;
  1         3  
  1         9  
6 1     1   27 use Carp qw(croak);
  1         2  
  1         651  
7              
8             # let's have our own private unadulterated useragent
9             # instead of using the shared one from app. Who knows
10             # what all the others are doing to the poor thing.
11              
12             my $ua = Mojo::UserAgent->new(cookie_jar => Mojo::UserAgent::CookieJar->new(ignore => sub { 1 }));
13              
14             our $VERSION = '0.706';
15              
16             my $make_req = sub {
17             my $c = shift;
18             my $dest_url = Mojo::URL->new(shift);
19             my $mount_point = shift;
20             my $tx = Mojo::Transaction::HTTP->new( req=> $c->req->clone );
21             my $url = $tx->req->url;
22             my $req_path = $url->path;
23             $url->scheme($dest_url->scheme);
24             $url->host($dest_url->host);
25             $url->port($dest_url->port);
26             $url->path($dest_url->path);
27             $url->path->trailing_slash(1);
28             if ($mount_point){
29             $req_path =~ s[^\Q${mount_point}\E/*][];
30             $url->path($req_path);
31             }
32             $tx->req->headers->header('Host',$url->host_port);
33             return $tx;
34             };
35              
36             sub register {
37 1     1 1 470 my $self = shift;
38 1         3 my $app = shift;
39 1         2 my $conf = shift;
40 1 50       7 if ($conf->{helper_name}){
41 0         0 die "helper_name is no more. In Mojolicious::Plugin::ReverseProxy 0.6 the API changed radically. Please check the docs.";
42             }
43 1 50       5 my $dest_url = $conf->{destination_url} or die "the destination_url parameter is mandatory";
44 1         4 my $req_processor = $conf->{req_processor};
45 1         2 my $res_processor = $conf->{res_processor};
46 1   33     8 my $routes = $conf->{routes} || $app->routes;
47 1   50     15 my $mount_point = $conf->{mount_point} || '';
48 1         3 $mount_point =~ s{/$}{};
49 1         7 my $log = $app->log;
50              
51             $routes->any($mount_point.'/*catchall' => { catchall => '' })->to(cb => sub {
52 0     0     my $c = shift;
53 0           $c->render_later;
54 0           my $tx = $c->$make_req($dest_url,$mount_point);
55 0 0         $req_processor->($c,$tx->req) if ref $req_processor eq 'CODE';
56             # if we call $c->rendered in the preprocessor,
57             # we are done ...
58 0 0         return if $c->stash('mojo.finished');
59             $ua->start($tx, sub {
60 0           my ($ua,$tx) = @_;
61 0           my $res = $tx->res;
62 0 0         $res_processor->($c,$res) if ref $res_processor eq 'CODE';
63 0           $c->tx->res($res);
64 0           $c->rendered;
65 0           });
66 1         162 });
67             }
68              
69             1;
70              
71             __END__