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