| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package AnyEvent::MultiDownload; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use AE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Asset::File; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use AnyEvent::Digest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use List::Util qw/shuffle/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use AnyEvent::HTTP qw/http_get/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '1.13'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | has path => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | die "文件存在" if -e $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | required => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | has url => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | has mirror => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return 1 if ref $_[0] eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | has digest => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | return 1 if $_[0] =~ /Digest::(SHA|MD5)/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | has on_finish => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | ▶ | return 2 if ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | has on_error => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | return 2 if ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | default => sub { sub { 1 } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | has on_block_finish => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | isa => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | ▶ | return 2 if ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | default => sub { return sub {1} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | has cv => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | default => sub { AE::cv }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | has fh => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | return Asset::File->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | has retry_interval => is => 'rw', default => sub { 10 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | has max_retries => is => 'rw', default => sub { 3 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | has block_size => is => 'rw', default => sub { 1 * 1024 * 1024 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | has timeout => is => 'rw', default => sub { 10 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | has recurse => is => 'rw', default => sub { 6 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | has headers => is => 'rw', default => sub {{}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | has tasks => is => 'rw', default => sub { [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | has error => is => 'rw', default => sub {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | has task_lists => is => 'rw', default => sub {[]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | has max_per_host => is => 'rw', default => sub { 8 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | has url_status => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my %hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | if ($self->has_mirror) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | %hash = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $_ => 0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | } @{ $self->mirror }, $self->url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | $hash{$self->url} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | return \%hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | my $cb = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | $self->cv->cb(sub{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | my $cv = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | my ($info, $hdr) = $cv->recv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # 出错的回调 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | if ($info) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | AE::log debug => $info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | return $self->on_error->($info, $hdr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # 移文件 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | $self->fh->move_to($self->path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | AE::log debug => $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return $self->on_error->("$@", []); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $self->on_finish->($self->fh->size); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | $self->cv->begin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | $self->first_request(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | sub first_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
first_request: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | my ($self, $retry) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 4 | my $url = $self->shuffle_url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | my $first_task; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | my $ev; $ev = http_get $url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | headers => $self->headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | timeout => $self->timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | recurse => $self->recurse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | ▶ | on_header => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | my ($hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | if ( $hdr->{Status} == 200 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | my $len = $hdr->{'content-length'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | return $self->cv->send(("Cannot find a content-length header.", $hdr)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $len or $len <= 0
MC/DC0%0/2
not defined $len or $len <= 0
not defined $len
$len <= 0
Truth table33%1/3
not(A) or B
A= not defined $lenB= $len <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | if !defined($len) or $len <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # 准备开始下载的信息 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | my $ranges = $self->split_range($len); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # 除了第一个块, 其它块现在开始下载 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # 事件开始, 但这个回调会在最后才调用. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | $first_task = shift @{ $self->tasks }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | $first_task->{block} = $first_task->{block} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$first_task->{'block'} || 0
MC/DC0%0/1
$first_task->{'block'} || 0
$first_task->{'block'}
Truth table50%1/2
A
A= $first_task->{'block'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | $first_task->{ofs} = $first_task->{ofs} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$first_task->{'ofs'} || 0
MC/DC0%0/1
$first_task->{'ofs'} || 0
$first_task->{'ofs'}
Truth table50%1/2
A
A= $first_task->{'ofs'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | ▶ | return 1 if $len <= $self->block_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | for ( 1 .. $self->max_per_host ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 8 | my $block_task = shift @{ $self->tasks }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | ▶ | last unless defined $block_task; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 7 | $self->cv->begin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 7 | $self->fetch_block($block_task) ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | ▶ | on_body => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | my ($partial_body, $hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 2 | ▶ | if ( $self->on_body($first_task)->($partial_body, $hdr) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # 如果是第一个块的话, 下载到指定的大小就需要断开 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | ▶ | if ( ( $hdr->{'content-length'} <= $self->block_size and $first_task->{size} == $hdr->{'content-length'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$hdr->{"content-length"} <= $self->block_size and $first_task->{"size"} == $hdr->{"content-length"} or $first_task->{"size"} >= $self->block_size
$hdr->{"content-length"} <= $self->block_size and $first_task->{"size"} == $hdr->{"content-length"}
MC/DC0%0/3
$hdr->{"content-length"} <= $self->block_size and $first_task->{"size"} == $hdr->{"content-length"} or $first_task->{"size"} >= $self->block_size
$hdr->{"content-length"} <= $self->block_size
$first_task->{"size"} == $hdr->{"content-length"}
$first_task->{"size"} >= $self->block_size
Truth table20%1/5
A and B or C
A= $hdr->{"content-length"} <= $self->block_sizeB= $first_task->{"size"} == $hdr->{"content-length"}C= $first_task->{"size"} >= $self->block_size
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | $first_task->{size} >= $self->block_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | $self->cv->send(("The 0 block the compared failure", $hdr)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | if !$self->on_block_finish->( $hdr, $first_task, $self->digest ? $first_task->{ctx}->hexdigest : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | $self->cv->end; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | return 0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 4 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | my (undef, $hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | undef $ev; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 4 | my $status = $hdr->{Status}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # on_body 正常的下载 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 4 | ▶ | return if ( $hdr->{OrigStatus} and $hdr->{OrigStatus} == 200 ) or $hdr->{Status} == 200 or $hdr->{Status} == 416; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$hdr->{"OrigStatus"} and $hdr->{"OrigStatus"} == 200 or $hdr->{"Status"} == 200 or $hdr->{"Status"} == 416
$hdr->{"OrigStatus"} and $hdr->{"OrigStatus"} == 200 or $hdr->{"Status"} == 200
$hdr->{"OrigStatus"} and $hdr->{"OrigStatus"} == 200
MC/DC25%1/4
$hdr->{"OrigStatus"} and $hdr->{"OrigStatus"} == 200 or $hdr->{"Status"} == 200 or $hdr->{"Status"} == 416
$hdr->{"OrigStatus"}
$hdr->{"OrigStatus"} == 200
$hdr->{"Status"} == 200
$hdr->{"Status"} == 416
Truth table28%2/7
A and B or C or D
A= $hdr->{"OrigStatus"}B= $hdr->{"OrigStatus"} == 200C= $hdr->{"Status"} == 200D= $hdr->{"Status"} == 416
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | if ( ($status == 500 or $status == 503 or $status =~ /^59/) and $retry < $self->max_retries ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$status == 500 || $status == 503 || $status =~ /^59/ and $retry < $self->max_retries
$status == 500 or $status == 503 or $status =~ /^59/
$status == 500 or $status == 503
MC/DC50%2/4
$status == 500 || $status == 503 || $status =~ /^59/ and $retry < $self->max_retries
$status == 500
$status == 503
$status =~ /^59/
$retry < $self->max_retries
Truth table42%3/7
A or B or C and D
A= $status == 500B= $status == 503C= $status =~ /^59/D= $retry < $self->max_retries
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | my $w; $w = AE::timer( $self->retry_interval, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | $first_task->{pos} = $first_task->{ofs}; # 重下本块时要 seek 回零 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | $first_task->{size} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | $first_task->{ctx} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | $self->first_request(++$retry); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | undef $w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | AE::log debug => "地址 $url 的块 0 下载出错, 重试"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | return $self->cv->send(( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | sprintf("Status: %s, Reason: %s.", $status ? $status : '500', $hdr->{Reason} ? $hdr->{Reason} : ' '), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | $hdr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 4 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 11 | ▶ | sub shuffle_url { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shuffle_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 11 | my $urls = $self->url_status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 11 | return (shuffle keys %$urls)[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 9 | ▶ | sub on_body { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 9 | my ($self, $task) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 9 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 9 | my ($partial_body, $hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 9 | ▶ | return 0 unless ($hdr->{Status} == 206 || $hdr->{Status} == 200); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$hdr->{"Status"} == 206 or $hdr->{"Status"} == 200
MC/DC100%2/2
$hdr->{"Status"} == 206 or $hdr->{"Status"} == 200
$hdr->{"Status"} == 206
$hdr->{"Status"} == 200
Truth table100%3/3
A or B
A= $hdr->{"Status"} == 206B= $hdr->{"Status"} == 200
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | my $len = length($partial_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # 主要是用于解决第一个块会超过写的位置 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 8 | ▶ | if ( $task->{size} + $len > $self->block_size ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 8 | my $spsize = $len - ( $task->{size} + $len - $self->block_size ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 8 | $partial_body = substr($partial_body, 0, $spsize); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 8 | $len = $spsize; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | $self->fh->start_range($task->{pos}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | $self->fh->add_chunk($partial_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 8 | ▶ | if ( $self->digest ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | ▶ | $task->{ctx} ||= AnyEvent::Digest->new($self->digest); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$task->{"ctx"} ||= "AnyEvent::Digest"->new($self->digest)
MC/DC0%0/2
$task->{"ctx"} ||= "AnyEvent::Digest"->new($self->digest)
$task->{"ctx"}
"AnyEvent::Digest"->new($self->digest)
Truth table33%1/3
A ||= B
A= $task->{"ctx"}B= "AnyEvent::Digest"->new($self->digest)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 8 | $task->{ctx}->add_async($partial_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 8 | $task->{pos} += $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 8 | $task->{size} += $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 8 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 9 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 7 | ▶ | sub fetch_block { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch_block: called CC=2 SCAR=9.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 7 | my ($self, $task, $retry) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 7 | ▶ | $retry ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$retry ||= 0
MC/DC0%0/1
$retry ||= 0
$retry
Truth table50%1/2
A
A= $retry
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 7 | my $url = $self->shuffle_url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | my $ev; $ev = http_get $url, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | timeout => $self->timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | recurse => $self->recurse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | persistent => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | keepalive => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | headers => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | %{ $self->headers }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | Range => $task->{range} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | on_body => $self->on_body($task), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 7 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=35.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 7 | my ($hdl, $hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 7 | my $status = $hdr->{Status}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 7 | undef $ev; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | # 成功下载到的流程 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # 1. 需要对比大小是否一致, 接着对比块较检 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # 2. 开始下一个任务的下载 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # 3. 当前块就退出, 不然下面会重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 7 | ▶ | if ( $status == 200 || $status == 206 ) { # 第一个块, 这二个都有可能 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$status == 200 or $status == 206
MC/DC0%0/2
$status == 200 or $status == 206
$status == 200
$status == 206
Truth table33%1/3
A or B
A= $status == 200B= $status == 206
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # not ok 块较检不相等 | 直接失败 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | return $self->cv->send(("The $task->{block} block the compared failure", $hdr)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | if ($task->{size} != ( $task->{tail} -$task->{ofs} + 1 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 7 | ▶ | or !$self->on_block_finish->($hdl, $task, $self->digest ? $task->{ctx}->hexdigest : '')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$task->{"size"} != $task->{"tail"} - $task->{"ofs"} + 1 or not $self->on_block_finish->($hdl, $task, $self->digest ? $task->{"ctx"}->hexdigest : "")
MC/DC0%0/2
$task->{"size"} != $task->{"tail"} - $task->{"ofs"} + 1 or not $self->on_block_finish->($hdl, $task, $self->digest ? $task->{"ctx"}->hexdigest : "")
$task->{"size"} != $task->{"tail"} - $task->{"ofs"} + 1
not $self->on_block_finish->($hdl, $task, $self->digest ? $task->{"ctx"}->hexdigest : "")
Truth table33%1/3
A or not(B)
A= $task->{"size"} != $task->{"tail"} - $task->{"ofs"} + 1B= not $self->on_block_finish->($hdl, $task, $self->digest ? $task->{"ctx"}->hexdigest : "")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 7 | my $block_task = shift @{ $self->tasks }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # 完成, 标记结束本次请求 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # ok 大小相等, 块较检相等, 当前块下载完成, 开始下载新的 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 7 | AE::log debug => "地址 $url 的块 $task->{block} 下载完成 $$"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # 处理接下来的一个请求 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 7 | ▶ | $block_task ? $self->fetch_block($block_task) : $self->cv->end; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # 是否重试的流程 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | my $error = sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | "Block %s the size is wrong, expect the size: %s actual size: %s, The %s try again, Status: %s, Reason: %s.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | $task->{block}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | $self->block_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | $task->{size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | $retry, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | $status ? $status : '500', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | $hdr->{Reason} ? $hdr->{Reason} : ' ', ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | AE::log warn => $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # 失败 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # 如果有可能还连接上的响应, 就需要重试, 直到达到重试, 如果不可能连接的响应, 就直接快速的退出 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | return $self->cv->send(($error, $hdr)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $status =~ /^(59.|503|500|502|200|206|)$/ or $retry > $self->max_retries
MC/DC0%0/2
not $status =~ /^(59.|503|500|502|200|206|)$/ or $retry > $self->max_retries
not $status =~ /^(59.|503|500|502|200|206|)$/
$retry > $self->max_retries
Truth table0%0/3
not(A) or B
A= not $status =~ /^(59.|503|500|502|200|206|)$/B= $retry > $self->max_retries
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | if $status !~ /^(59.|503|500|502|200|206|)$/ or $retry > $self->max_retries; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | $self->retry($task, $retry); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | sub retry { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retry: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | my ($self, $task, $retry) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | my $w;$w = AE::timer( $self->retry_interval, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | $task->{pos} = $task->{ofs}; # 重下本块时要 seek 回零 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $task->{size} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | $task->{ctx} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | $self->fetch_block( $task, ++$retry ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | undef $w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | ▶ | sub split_range { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_range: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | my $length = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # 每个请求的段大小的范围,字节 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 1 | my $block_size = $self->block_size; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | my $segments = int($length / $block_size); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # 要处理的字节的总数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 1 | my $len_remain = $length; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | my @ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | my $block = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 1 | ▶ | while ( $len_remain > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # 每个 segment 的大小 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 8 | my $seg_len = $block_size; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # 偏移长度 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 8 | my $ofs = $length - $len_remain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # 剩余字节 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 8 | $len_remain -= $seg_len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 8 | my $tail = $ofs + $seg_len - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 8 | ▶ | if ( $length-1 < $tail) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $tail = $length-1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 8 | my $task = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | block => $block, # 当前块编号 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | ofs => $ofs, # 当前的偏移量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | pos => $ofs, # 本块的起点 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | tail => $tail, # 本块的结束 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | range => 'bytes=' . $ofs . '-' . $tail, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | size => 0, # 总共下载的长度 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 8 | $self->tasks->[$block] = $task; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 8 | $block++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||