Repositorio/Server/Helper/Proxy.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 79 CC 9 SCAR 35.8
CC
9
Cov
0%
register
56.0
_proxy_tx
12.0
BEGIN
2.0
CRAP 36.0
1 use strict;
2  
3 package Rex::Repositorio::Server::Helper::Proxy;
4  
5 use base 'Mojolicious::Plugin';
6  
7 our $VERSION = '1.2.1'; # VERSION
8  
9 sub register {
10 my ( $self, $app ) = @_;
11  
12 $app->helper(
13 proxy_to => sub {
14 my $c = shift;
15 my $url = Mojo::URL->new(shift);
16 my $cb = shift || sub { };
17 my $filter = shift;
18 my $headers = shift || {};
19  
20 if ( $c->repo->{proxy} ne "true" ) {
21 $c->app->log->debug("No proxy support for this repository.");
22 $c->render( text => 'no proxy support', status => 500 );
23 return;
24 }
25  
26 $c->inactivity_timeout(900);
27  
28 my $ua = $c->ua;
29 $ua->max_redirects(5);
30 $ua->proxy->detect;
31  
32 if ( $c->repo->{ca} ) {
33 $ua->ca( $c->repo->{ca} );
34 }
35 if ( $c->repo->{key} ) {
36 $ua->key( $c->repo->{key} );
37 }
38 if ( $c->repo->{cert} ) {
39 $ua->cert( $c->repo->{cert} );
40 }
41  
42 my %args = @_;
43 $url->query( $c->req->params ) if ( $args{with_query_params} );
44  
45 if ( Mojo::IOLoop->is_running ) {
46 $c->render_later;
47 $ua->get(
48 $url, $headers,
49 sub {
50 my ( $self, $tx ) = @_;
51 _proxy_tx( $c, $tx, $cb, $filter );
52 }
53 );
54 }
55 else {
56 my $tx = $ua->get( $url, $headers );
57 _proxy_tx( $c, $tx, $cb, $filter );
58 }
59 }
60 );
61 }
62  
63 sub _proxy_tx {
64 my ( $self, $tx, $cb, $filter ) = @_;
65 if ( my $res = $tx->success ) {
66 if ( ref $filter eq "CODE" ) {
67 $filter->( $self, $tx );
68 }
69  
70 $self->tx->res($res);
71 $self->rendered;
72 $cb->( $self, $tx );
73 }
74 else {
75 my $error = $tx->error;
76 $self->tx->res->headers->add( 'X-Remote-Status',
77 $error->{code} . ': ' . $error->{message} );
78 $self->app->log->error( "Error during proxy request: ("
79 . $error->{code} . ") "
80 . $error->{message}
81 . " [url: "
82 . $tx->req->url
83 . "]" );
84 $self->render( status => 500, text => 'Failed to fetch data from backend' );
85 }
86 }
87  
88 1;