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   526 use Mojo::Base 'Mojolicious::Plugin';
  1         1  
  1         10  
3 1     1   802 use Mojo::Transaction::HTTP;
  1         1  
  1         11  
4 1     1   25 use Mojo::UserAgent;
  1         1  
  1         7  
5 1     1   17 use Mojo::URL;
  1         1  
  1         6  
6 1     1   19 use Carp qw(croak);
  1         1  
  1         472  
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.705';
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             $url->scheme($dest_url->scheme);
23             $url->host($dest_url->host);
24             $url->port($dest_url->port);
25             if ($mount_point){
26             my $req_path = $url->path;
27             $req_path =~ s[^\Q${mount_point}\E/*][];
28             $url->path($req_path);
29             }
30             $tx->req->headers->header('Host',$url->host_port);
31             return $tx;
32             };
33              
34             sub register {
35 1     1 1 711 my $self = shift;
36 1         2 my $app = shift;
37 1         2 my $conf = shift;
38 1 50       6 if ($conf->{helper_name}){
39 0         0 die "helper_name is no more. In Mojolicious::Plugin::ReverseProxy 0.6 the API changed radically. Please check the docs.";
40             }
41 1 50       6 my $dest_url = $conf->{destination_url} or die "the destination_url parameter is mandatory";
42 1         2 my $req_processor = $conf->{req_processor};
43 1         4 my $res_processor = $conf->{res_processor};
44 1   33     10 my $routes = $conf->{routes} || $app->routes;
45 1   50     17 my $mount_point = $conf->{mount_point} || '';
46 1         6 $mount_point =~ s{/$}{};
47 1         12 my $log = $app->log;
48              
49             $routes->any($mount_point.'/*catchall' => { catchall => '' })->to(cb => sub {
50 0     0     my $c = shift;
51 0           $c->render_later;
52 0           my $tx = $c->$make_req($dest_url,$mount_point);
53 0 0         $req_processor->($c,$tx->req) if ref $req_processor eq 'CODE';
54             # if we call $c->rendered in the preprocessor,
55             # we are done ...
56 0 0         return if $c->stash('mojo.finished');
57             $ua->start($tx, sub {
58 0           my ($ua,$tx) = @_;
59 0           my $res = $tx->res;
60 0 0         $res_processor->($c,$res) if ref $res_processor eq 'CODE';
61 0           $c->tx->res($res);
62 0           $c->rendered;
63 0           });
64 1         68 });
65             }
66              
67             1;
68              
69             __END__