| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2017,2018 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||
| 7 | # This package is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | package Doit::Lwp; # Convention: all commands here should be prefixed with 'lwp_' | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.012'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Doit::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub functions { qw(lwp_mirror) } | ||||||||||||||||||||||||||
|
Subroutine
functions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 2 | my $ua; # XXX cache in object? | |||||||||||||||||||||||||||
| 27 | sub _get_cached_ua { | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_cached_ua: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | return $ua if $ua; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 1 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 31 | 1 | $ua = LWP::UserAgent->new; # XXX options? | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | sub lwp_mirror { | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | my($self, $url, $filename, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
lwp_mirror: called CC=19 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 11 | ▶ | if (!defined $url) { error "url is mandatory" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | if (!defined $filename) { error "filename is mandatory" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 9 | ▶ | my $refresh = delete $opts{refresh} || 'always'; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{'refresh'} || 'always'
MC/DC100%1/1
delete $opts{'refresh'} || 'always'
delete $opts{'refresh'}
Truth table100%2/2
A
A= delete $opts{'refresh'}
| |||||||||||||||||||||||||||||
| 40 | 9 | ▶ | if ($refresh !~ m{^(always|never)$}) { error "refresh may be 'always' or 'never'" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 8 | my $debug = delete $opts{debug}; | |||||||||||||||||||||||||||
| 42 | 8 | my $ua = delete $opts{ua}; | |||||||||||||||||||||||||||
| 43 | 8 | ▶ | error "Unhandled options: " . join(" ", %opts) if %opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | ▶ | if (-e $filename && $refresh eq 'never') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-e $filename and $refresh eq "never"
MC/DC100%2/2
-e $filename and $refresh eq "never"
-e $filename
$refresh eq "never"
Truth table100%3/3
A and B
A= -e $filenameB= $refresh eq "never"
| |||||||||||||||||||||||||||||
| 46 | 1 | info "$url -> $filename already exists, do not refresh"; | |||||||||||||||||||||||||||
| 47 | 1 | return 0; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | $ua ||= _get_cached_ua; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ua ||= _get_cached_ua()
MC/DC0%0/2
$ua ||= _get_cached_ua()
$ua
_get_cached_ua()
Truth table66%2/3
A ||= B
A= $uaB= _get_cached_ua()
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if ($self->is_dry_run) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | info "mirror $url -> $filename (dry-run)"; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 6 | info "mirror $url -> $filename"; | |||||||||||||||||||||||||||
| 56 | 6 | my $resp = $ua->mirror($url, $filename); | |||||||||||||||||||||||||||
| 57 | 6 | ▶ | if (ref $ua eq 'HTTP::Tiny') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||
| 60 | 0 | info "Response: " . Data::Dumper->new([$resp],[qw()])->Indent(1)->Useqq(1)->Sortkeys(1)->Terse(1)->Dump; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | if (!$resp->{success}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 1 | my $msg = "mirroring failed: $resp->{status} $resp->{reason}"; | |||||||||||||||||||||||||||
| 64 | 1 | ▶ | if ($resp->{status} == 599) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | $msg .= ": $resp->{content}"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 1 | error $msg; | |||||||||||||||||||||||||||
| 68 | } elsif ($resp->{status} == 304) { | ||||||||||||||||||||||||||||
| 69 | 0 | return 0; | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 1 | return 1; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 4 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | info "Response: " . $resp->as_string; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 4 | ▶ | if ($resp->code == 304) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 78 | 1 | return 0; | |||||||||||||||||||||||||||
| 79 | } elsif (!$resp->is_success) { | ||||||||||||||||||||||||||||
| 80 | 1 | error "mirroring failed: " . $resp->status_line; | |||||||||||||||||||||||||||
| 81 | } elsif ($resp->header('X-Died')) { | ||||||||||||||||||||||||||||
| 82 | 0 | error "mirroring failed: " . $resp->header('X-Died'); | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 2 | return 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||