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