line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Proxy; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
693
|
use base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
391
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.7'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub register { |
8
|
1
|
|
|
1
|
1
|
43
|
my ($self, $app) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$app->helper( |
12
|
|
|
|
|
|
|
proxy_to => sub { |
13
|
2
|
|
|
2
|
|
23561
|
my $c = shift; |
14
|
2
|
|
|
|
|
9
|
my $url = Mojo::URL->new(shift); |
15
|
2
|
|
|
|
|
1019
|
my %args = @_; |
16
|
2
|
50
|
|
|
|
9
|
$url->query($c->req->params) if ($args{with_query_params}); |
17
|
|
|
|
|
|
|
|
18
|
2
|
50
|
|
|
|
12
|
if (Mojo::IOLoop->is_running) { |
19
|
2
|
|
|
|
|
35
|
$c->render_later; |
20
|
|
|
|
|
|
|
$c->ua->get( |
21
|
|
|
|
|
|
|
$url, |
22
|
|
|
|
|
|
|
sub { |
23
|
2
|
|
|
|
|
82588
|
my ($self, $tx) = @_; |
24
|
2
|
|
|
|
|
7
|
_proxy_tx($c, $tx); |
25
|
|
|
|
|
|
|
} |
26
|
2
|
|
|
|
|
50
|
); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else { |
29
|
0
|
|
|
|
|
0
|
my $tx = $c->ua->get($url); |
30
|
0
|
|
|
|
|
0
|
_proxy_tx($c, $tx); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
1
|
|
|
|
|
10
|
); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _proxy_tx { |
37
|
2
|
|
|
2
|
|
5
|
my ($self, $tx) = @_; |
38
|
2
|
100
|
|
|
|
7
|
if (!$tx->error) { |
39
|
1
|
|
|
|
|
19
|
my $res = $tx->res; |
40
|
1
|
|
|
|
|
6
|
$self->tx->res($res); |
41
|
1
|
|
|
|
|
26
|
$self->rendered; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
1
|
|
|
|
|
18
|
my $error = $tx->error; |
45
|
|
|
|
|
|
|
$self->tx->res->headers->add('X-Remote-Status', |
46
|
1
|
|
|
|
|
16
|
$error->{code} . ': ' . $error->{message}); |
47
|
1
|
|
|
|
|
30
|
$self->render(status => 500, text => 'Failed to fetch data from backend'); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
__END__ |