LWP/UserAgent.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 139 CC 12 SCAR 33.1
CC
12
Cov
0%
mirror
56.0
prepare_cache_related_headers
20.0
sigauth_credentials
12.0
CRAP 27.3
1 package Net::OBS::LWP::UserAgent;
2  
3 use strict;
4 use warnings;
5 use Const::Fast;
6 use Carp;
7  
8 use base 'LWP::UserAgent';
9  
10 const my $MTIME_POS => 9;
11 const my $FILE_LENGTH_POS => 7;
12 const my $FILE_MODE_RW_ALL => '0777';
13  
14 sub sigauth_credentials {
15 my ($self, $creds) = @_;
16 die "Credentials a not a HASH Ref!" if $creds && ref($creds) ne 'HASH';
17 $self->{sigauth_credentials} = $creds if $creds;
18 return $self->{sigauth_credentials};
19 }
20  
21 sub mirror
22 {
23 my($self, %opt) = @_;
24 my $url = $opt{url};
25 my $file = $opt{file};
26 my $etag = $opt{etag};
27  
28 my $request = $opt{request} || HTTP::Request->new('GET', $url);
29  
30 $self->prepare_cache_related_headers($request, %opt);
31  
32 my $tmpfile = "$file-$$";
33  
34 my $response = $self->request($request, $tmpfile);
35 croak($response->header('X-Died')) if $response->header('X-Died');
36  
37 # Only fetching a fresh copy of the would be considered success.
38 # If the file was not modified, "304" would returned, which
39 # is considered by HTTP::Status to be a "redirect", /not/ "success"
40 if ( $response->is_success ) {
41 my @stat = stat $tmpfile or croak("Could not stat tmpfile '$tmpfile': $!");
42 my $file_length = $stat[$FILE_LENGTH_POS];
43 my ($content_length) = $response->header('Content-length');
44  
45 if ( defined $content_length and $file_length < $content_length ) {
46 unlink $tmpfile || croak("Could not unlink $tmpfile: $!\n");
47 croak("Transfer truncated: only $file_length out of $content_length bytes received\n");
48 }
49 elsif ( defined $content_length and $file_length > $content_length ) {
50 unlink $tmpfile || croak("Could not unlink $tmpfile: $!\n");
51 croak("Content-length mismatch: expected $content_length bytes, got $file_length\n");
52 }
53 # The file was the expected length.
54 else {
55 # Replace the stale file with a fresh copy
56 if ( -e $file ) {
57 # Some DOSish systems fail to rename if the target exists
58 chmod $FILE_MODE_RW_ALL, $file
59 || croak("Cannot change mode for '$file': $!\n");
60 unlink $file || croak("Could not unlink $file: $!\n");
61 }
62 rename $tmpfile, $file
63 or croak("Cannot rename '$tmpfile' to '$file': $!\n");
64  
65 # make sure the file has the same last modification time
66 if ( my $lm = $response->last_modified ) {
67 utime $lm, $lm, $file || croak("Cannot set utime for '$file': $!\n");
68 }
69 }
70 }
71 # The local copy is fresh enough, so just delete the temp file
72 else {
73 unlink $tmpfile || croak("Could not unlink $tmpfile: $!\n");
74 }
75 return $response;
76 }
77  
78 sub prepare_cache_related_headers {
79 my ($self, $request, %opt) = @_;
80 my $file = $opt{file};
81 my $etag = $opt{etag};
82  
83 # If the file exists, add a cache-related header
84 if ( -e $file ) {
85 my ($mtime) = ( stat $file )[$MTIME_POS];
86 if ($etag) {
87 $request->header('If-None-Match' => $etag);
88 $self->default_header('If-None-Match' => $etag);
89 } elsif ($mtime) {
90 my $http_date = HTTP::Date::time2str($mtime);
91 $request->header('If-Modified-Since' => $http_date);
92 $self->default_header('If-Modified-Since' => $http_date);
93 }
94 }
95  
96 return;
97 }
98  
99 1;