File Coverage

lib/Slaughter/Transport/mojo.pm
Criterion Covered Total %
statement 14 44 31.8
branch 0 16 0.0
condition 1 6 16.6
subroutine 4 7 57.1
pod 5 5 100.0
total 24 78 30.7


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3            
4             Slaughter::Transport::mojo - HTTP transport class.
5            
6             =head1 SYNOPSIS
7            
8             This transport copes with fetching files and policies from a remote server
9             using HTTP or HTTPS as a transport.
10            
11             =cut
12              
13             =head1 DESCRIPTION
14            
15             This transport is slightly different to the others, as each file is fetched
16             on-demand, with no local filesystem access and no caching.
17            
18             If HTTP Basic-Auth is required the appropriate details should be passed to
19             slaughter with the "C<--username>" & "C<--password>" flags.
20            
21             =cut
22              
23              
24              
25 1     1   858 use strict;
  1         2  
  1         172  
26 1     1   3 use warnings;
  1         1  
  1         385  
27              
28              
29              
30             package Slaughter::Transport::mojo;
31              
32              
33             #
34             # The version of our release.
35             #
36             our $VERSION = "3.0.5";
37              
38              
39              
40             =head2 new
41            
42             Create a new instance of this object.
43            
44             =cut
45              
46             sub new
47             {
48 1     1 1 431     my ( $proto, %supplied ) = (@_);
49 1   33     5     my $class = ref($proto) || $proto;
50              
51 1         1     my $self = {};
52              
53             #
54             # Allow user supplied values to override our defaults
55             #
56 1         3     foreach my $key ( keys %supplied )
57                 {
58 0         0         $self->{ lc $key } = $supplied{ $key };
59                 }
60              
61             #
62             # Explicitly ensure we have no error.
63             #
64 1         1     $self->{ 'error' } = undef;
65              
66 1         2     bless( $self, $class );
67 1         2     return $self;
68             }
69              
70              
71              
72             =head2 name
73            
74             Return the name of this transport.
75            
76             =cut
77              
78             sub name
79             {
80 1     1 1 1107     return ("mojo");
81             }
82              
83              
84              
85             =head2 isAvailable
86            
87             Return whether this transport is available.
88            
89             As we're pure-perl it should be always available if the C<Mojo::UserAgent> module is
90             present.
91            
92             =cut
93              
94             sub isAvailable
95             {
96 0     0 1       my ($self) = (@_);
97              
98 0               my $mojo = "use Mojo::UserAgent;";
99              
100             ## no critic (Eval)
101 0               eval($mojo);
102             ## use critic
103              
104 0 0             if ($@)
105                 {
106 0                   $self->{ 'error' } = "Mojo::UserAgent module not available.";
107 0                   return 0;
108                 }
109              
110             #
111             # Module loading succeeded; we're available.
112             #
113 0               return 1;
114             }
115              
116              
117              
118             =head2 error
119            
120             Return the last error from the transport.
121            
122             This is only set in L</isAvailable>.
123            
124             =cut
125              
126             sub error
127             {
128 0     0 1       my ($self) = (@_);
129 0               return ( $self->{ 'error' } );
130             }
131              
132              
133              
134             =head2 fetchContents
135            
136             Fetch the contents of a remote URL, using HTTP basic-auth if we should
137            
138             =cut
139              
140             sub fetchContents
141             {
142 0     0 1       my ( $self, %args ) = (@_);
143              
144              
145             #
146             # The file to fetch, and the prefix from which to load it.
147             #
148 0               my $pref = $args{ 'prefix' };
149 0               my $url = $args{ 'file' };
150              
151             #
152             # Is this fully-qualified?
153             #
154 0 0             if ( $url !~ /^http/i )
155                 {
156 0                   $url = "$self->{'prefix'}/$pref/$url";
157              
158 0 0                 $self->{ 'verbose' } &&
159                       print "\tExpanded to: $url \n";
160                 }
161              
162 0               my $ua = Mojo::UserAgent->new;
163              
164             #
165             # Use a proxy, if we should.
166             #
167 0               $ua = $ua->detect_proxy;
168              
169             #
170             # Make a request, do it in this fashion so we can use Basic-Auth if we need to.
171             #
172 0 0 0           if ( $self->{ 'username' } && $self->{ 'password' } )
173                 {
174 0                   my ( $schema, $hostpath ) = split( '//', $url, 2 );
175 0                   $url =
176                       $schema . '//' . $self->{ 'username' } . ':' . $self->{ 'password' } .
177                       '@' . $hostpath;
178                 }
179              
180             #
181             # Send the request
182             #
183             #print "url: " . $url . "\n";
184 0               my $tx = $ua->build_tx( GET => $url );
185 0               $ua->start($tx);
186 0 0             if ( my $res = $tx->success )
187                 {
188 0 0                 $self->{ 'verbose' } &&
189                       print "\tOK\n";
190 0                   return ( $res->body );
191                 }
192              
193             #
194             # Failed?
195             #
196 0               my ( $err, $code ) = $tx->error;
197 0 0             $self->{ 'verbose' } &&
    0          
198                   print $code ? "\tFailed to fetch: $url - $code response: $err\n" :
199                                 "Connection error: $err\n";
200              
201             #
202             # Return undef, but hide this from perlcritic.
203             #
204 0               my $res = undef;
205 0               return ($res);
206              
207             }
208              
209              
210             1;
211              
212              
213             =head1 AUTHOR
214            
215             Steve Kemp <steve@steve.org.uk>
216            
217             =cut
218              
219             =head1 LICENSE
220            
221             Copyright (c) 2010-2015 by Steve Kemp. All rights reserved.
222            
223             This module is free software;
224             you can redistribute it and/or modify it under
225             the same terms as Perl itself.
226             The LICENSE file contains the full text of the license.
227            
228             =cut
229