UserAgent/Role/Resume.pm

filter: Statementstmt 20.8% 20 / 96 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 1 totaltotal 9.0% 25 / 276 CC 53 SCAR 61.0
CC
53
Cov
9%
__ANON__
930.0
__ANON__
72.0
_total_size
42.0
CRAP 447.4
↑ Summary Transaction/HTTP/Role/Resume.pm »
1 package Mojo::UserAgent::Role::Resume;
2 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = "v0.1.0";
11  
12 1 has max_attempts => 1;
13  
14 1 my $TX_ROLE = "Mojo::Transaction::HTTP::Role::Resume";
15  
16 # only add the role to HTTP requests, and not to websockets
17 0 ▶around build_tx => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 my ($orig, $self, @args) = @_;
19  
20 0 my $tx = $self->$orig(@args)->with_roles($TX_ROLE);
21 0 return $tx->_RESUME_original_arguments(\@args);
22 1 };
23  
24 0 ▶around start => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
25 0 my ($orig, $self, @args) = @_;
26  
27 0 my ($orig_tx, @orig_cb) = @args;
28 0 my $orig_cb = $orig_cb[0];
29 0 my $blocking = !defined $orig_cb;
30  
31 # don't wrap this 'start' invocation, if websocket, redirect, or retry
32 return $self->$orig(@args)
33 0 ▶ if !eval{$orig_tx->does($TX_ROLE)} or $orig_tx->previous or $orig_tx->RESUME_previous_attempt;
Branch: 1 branch
BranchTrueFalseTotal
if not eval { do { $orig_tx->does($TX_ROLE) } } or $orig_tx->previous or $orig_tx->RESUME_previous_attempt 0 0 0
Condition0%0/6
not eval { do { $orig_tx->does($TX_ROLE) } } or $orig_tx->previous or $orig_tx->RESUME_previous_attempt
l!l&&r!l&&!r
000
not eval { do { $orig_tx->does($TX_ROLE) } } or $orig_tx->previous
l!l&&r!l&&!r
000
MC/DC0%0/3
not eval { do { $orig_tx->does($TX_ROLE) } } or $orig_tx->previous or $orig_tx->RESUME_previous_attempt
not eval { do { $orig_tx->does($TX_ROLE) } } $orig_tx->previous $orig_tx->RESUME_previous_attempt
Truth table0%0/4
not(A) or B or C
A= not eval { do { $orig_tx->does($TX_ROLE) } }B= $orig_tx->previousC= $orig_tx->RESUME_previous_attempt
ABCresult
1XX1
01X1
0011
0000
34  
35 0 my $tx = $orig_tx; # $tx will always equal the first tx (ie before any redirections) of every attempt
36 0 my @original_build_args = @{ $tx->_RESUME_original_arguments() };
37 0 my $server_is_crappy_for_ranges = 0; # has server been proven to be unreliable for ranged requests?
38 0 my $latest_good_200_tx; # the last tx with code 200 & full headers (holds the asset that 206 tx's should append to)
39 0 my $remaining_attempts = $self->max_attempts;
40  
41 # prevent ref cycles (which could lead to memory leaks)
42 0 ▶ my $_clean = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
43 0 undef $_ foreach ($tx, $latest_good_200_tx);
44 0 undef @original_build_args;
45 0 };
46  
47 # set special my_progress event handler to $tx, and also to the retries:
48 0 ▶ my $progress_handler = sub {
Subroutine __ANON__: not called CC=30 SCAR=68.4
49 0 my ($msg) = @_;
50  
51 # my_progress handlers are by default only executed on 200|206 responses
52 0 ▶ return unless $msg->code and $msg->code =~ /^20(0|6)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->code and $msg->code =~ /^20(0|6)$/u 0 0 0
Condition0%0/3
$msg->code and $msg->code =~ /^20(0|6)$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->code and $msg->code =~ /^20(0|6)$/u
$msg->code $msg->code =~ /^20(0|6)$/u
Truth table0%0/3
A and B
A= $msg->codeB= $msg->code =~ /^20(0|6)$/u
ABresult
0X0
100
111
53 0 ▶ return unless $msg->headers->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->headers->is_finished 0 0 0
54  
55 # only run once
56 0 $msg->unsubscribe(progress => __SUB__);
57  
58 0 my $total_size = _total_size($msg);
59  
60 0 ▶ !$server_is_crappy_for_ranges or return; # only interested if server was not labelled crappy
Branch: 1 branch
BranchTrueFalseTotal
$server_is_crappy_for_ranges and return 0 0 0
61  
62 0 my $headers = $msg->headers;
63 0 my $old_headers = eval {$latest_good_200_tx->res->headers};
64 0 ▶ my ($h_start_byte) = ($headers->content_range // '') =~ /^bytes (\d+)\-/;
Condition0%0/2
$headers->content_range // ""
l!l
00
MC/DC0%0/1
$headers->content_range // ""
$headers->content_range
Truth table0%0/2
A
A= $headers->content_range
Aresult
11
00
65  
66 0 ▶ $server_is_crappy_for_ranges ||= do {
Condition0%0/3
$server_is_crappy_for_ranges ||= do { not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u or defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res)) or $msg->code == 206 and !(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size }
l!l&&r!l&&!r
000
MC/DC0%0/2
$server_is_crappy_for_ranges ||= do { not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u or defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res)) or $msg->code == 206 and !(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size }
$server_is_crappy_for_ranges do { not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u or defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res)) or $msg->code == 206 and !(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size }
Truth table0%0/3
A ||= B
A= $server_is_crappy_for_rangesB= do { not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u or defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res)) or $msg->code == 206 and !(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size }
ABresult
1X1
011
000
67 # 1. missing all important headers
68 (!$headers->etag and !$headers->last_modified and !defined $total_size) or
69  
70 # 2. wrong HTTP status code:
71 ($tx->req->headers->range xor $msg->code == 206) or
72  
73 # 3. missing, malformed, or extra Content-Range header:
74 ($msg->code == 200 and $headers->content_range) or
75 ($msg->code == 206 and ($headers->content_range // '') !~ /^bytes \d+\-\d+\/(\*|\d+)$/) or
76  
77 # 4. an important header is different than that of the older tx that we are resuming on:
78 (defined $old_headers and (
79 !eqq($headers->etag, $old_headers->etag) or
80 !eqq($headers->last_modified, $old_headers->last_modified) or
81 !eqq($total_size, _total_size($latest_good_200_tx->res))
82 )) or
83  
84 # 5. the numbers in the headers don't add-up correctly
85 ($msg->code == 206 and (
86 !defined $h_start_byte or
87 0 ▶ !eqq($h_start_byte, scalar(eval {$latest_good_200_tx->res->content->asset->size})) or
Condition0%0/57
!($headers->etag || $headers->last_modified || defined $total_size) || ($tx->req->headers->range ^^ $msg->code == 206) || $msg->code == 200 && $headers->content_range || $msg->code == 206 && !(($headers->content_range // '') =~ m[^bytes \d+\-\d+/(\*|\d+)$]u) || defined $old_headers && !(eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res))) || $msg->code == 206 && (!(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size)
l!l&&r!l&&!r
000
not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u or defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res))
l!l&&r!l&&!r
000
not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range or $msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u
l!l&&r!l&&!r
000
not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206) or $msg->code == 200 and $headers->content_range
l!l&&r!l&&!r
000
not $headers->etag || $headers->last_modified || defined $total_size or ($tx->req->headers->range xor $msg->code == 206)
l!l&&r!l&&!r
000
$headers->etag || $headers->last_modified || defined $total_size
l!l&&r!l&&!r
000
$headers->etag or $headers->last_modified
l!l&&r!l&&!r
000
$tx->req->headers->range xor $msg->code == 206
l&&rl&&!r!l&&r!l&&!r
0000
$msg->code == 200 and $headers->content_range
!ll&&!rl&&r
000
$msg->code == 206 and not +($headers->content_range // "") =~ m[^bytes \d+\-\d+/(\*|\d+)$]u
!ll&&!rl&&r
000
$headers->content_range // ""
l!l
00
defined $old_headers and not eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res))
!ll&&!rl&&r
000
eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res))
!ll&&!rl&&r
000
eqq($headers->etag, $old_headers->etag) and eqq($headers->last_modified, $old_headers->last_modified)
!ll&&!rl&&r
000
$msg->code == 206 and !(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size
!ll&&!rl&&r
000
not defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } }) or defined $total_size and defined $headers->content_length and $h_start_byte + $headers->content_length != $total_size
l!l&&r!l&&!r
000
defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })
!ll&&!rl&&r
000
defined $total_size and defined $headers->content_length and $h_start_byte + $headers->content_length != $total_size
!ll&&!rl&&r
000
defined $total_size and defined $headers->content_length
!ll&&!rl&&r
000
MC/DC0%0/19
!($headers->etag || $headers->last_modified || defined $total_size) || ($tx->req->headers->range ^^ $msg->code == 206) || $msg->code == 200 && $headers->content_range || $msg->code == 206 && !(($headers->content_range // '') =~ m[^bytes \d+\-\d+/(\*|\d+)$]u) || defined $old_headers && !(eqq($headers->etag, $old_headers->etag) && eqq($headers->last_modified, $old_headers->last_modified) && eqq($total_size, _total_size($latest_good_200_tx->res))) || $msg->code == 206 && (!(defined $h_start_byte && eqq($h_start_byte, scalar eval { do { $latest_good_200_tx->res->content->asset->size } })) || defined $total_size && defined $headers->content_length && $h_start_byte + $headers->content_length != $total_size)
too many conditions
88 (defined $total_size and defined $headers->content_length
89 and $h_start_byte + $headers->content_length != $total_size)
90 ));
91 };
92  
93 0 ▶ if ($msg->code == 206) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg->code == 206) { } 0 0 0
elsif ($msg->code == 200) { } 0 0 0
94 0 ▶ if ($server_is_crappy_for_ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_is_crappy_for_ranges) { } 0 0 0
95 # abort and retry
96 0 $remaining_attempts++; # just this one attempt should be "for free", since it was inexpensive
97 0 $msg->error({ message => "web server can't handle ranged requests, aborting" }); # abort
98 0 return;
99 } else {
100 # switch asset
101 0 my $big_asset = $latest_good_200_tx->res->content->asset->add_chunk($msg->content->asset->slurp);
102 0 $msg->content->asset($big_asset);
103 }
104 } elsif ($msg->code == 200) {
105 0 ▶ if (!$server_is_crappy_for_ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $server_is_crappy_for_ranges) 0 0 0
106 # mark as latest good tx:
107 0 $latest_good_200_tx = $tx;
108 }
109 }
110 0 };
111 0 unshift @{ $tx->res->subscribers('progress') }, $progress_handler;
112  
113 0 ▶ my $build_retry_tx = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
114 0 my ($old_tx) = @_;
115  
116 # don't continue if $old_tx isn't a failure
117 0 ▶ return undef unless $old_tx->error and !$old_tx->res->is_client_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tx->error and not $old_tx->res->is_client_error 0 0 0
Condition0%0/3
$old_tx->error and not $old_tx->res->is_client_error
!ll&&!rl&&r
000
MC/DC0%0/2
$old_tx->error and not $old_tx->res->is_client_error
$old_tx->error not $old_tx->res->is_client_error
Truth table0%0/3
A and not(B)
A= $old_tx->errorB= not $old_tx->res->is_client_error
ABresult
0X0
100
111
118  
119 # TODO: Check whether we should also check for "content received < total_size_based_on_headers"
120  
121 0 my $retry_tx = $self->build_tx(@original_build_args);
122 0 unshift @{ $retry_tx->res->subscribers('progress') }, $progress_handler;
123  
124 0 ▶ if ($latest_good_200_tx and !$server_is_crappy_for_ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if ($latest_good_200_tx and not $server_is_crappy_for_ranges) 0 0 0
Condition0%0/3
$latest_good_200_tx and not $server_is_crappy_for_ranges
!ll&&!rl&&r
000
MC/DC0%0/2
$latest_good_200_tx and not $server_is_crappy_for_ranges
$latest_good_200_tx not $server_is_crappy_for_ranges
Truth table0%0/3
A and not(B)
A= $latest_good_200_txB= not $server_is_crappy_for_ranges
ABresult
0X0
100
111
125 # set Range: header
126 0 $retry_tx->req->headers->range('bytes='.$latest_good_200_tx->res->content->asset->size.'-');
127 }
128  
129 # store old_tx in a field of new_tx, similar to $tx->previous for redirects
130 0 $retry_tx->RESUME_previous_attempt($old_tx); # store old tx in $retry_tx
131  
132 0 return $retry_tx;
133 0 };
134  
135 # don't start any transactions if $remaining_attempts is not high enough
136 0 ▶ $remaining_attempts >= 1 or return $tx;
Branch: 1 branch
BranchTrueFalseTotal
$remaining_attempts >= 1 or return $tx 0 0 0
137  
138 0 ▶ if ($blocking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocking) { } 0 0 0
139 0 my $tx_after_redirects;
140 0 while (1) {
141 0 $tx_after_redirects = $self->$orig($tx, @orig_cb);
142 0 ▶ --$remaining_attempts >= 1 and $tx = $build_retry_tx->($tx_after_redirects) or last;
Branch: 1 branch
BranchTrueFalseTotal
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects) or last 0 0 0
Condition0%0/3
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects)
!ll&&!rl&&r
000
MC/DC0%0/2
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects)
--$remaining_attempts >= 1 $tx = &$build_retry_tx($tx_after_redirects)
Truth table0%0/3
A and B
A= --$remaining_attempts >= 1B= $tx = &$build_retry_tx($tx_after_redirects)
ABresult
0X0
100
111
143 }
144 0 $_clean->();
145 0 return $tx_after_redirects;
146 } else {
147 0 ▶ return $self->$orig($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
148 0 my ($ua, $tx_after_redirects) = @_;
149 0 ▶ --$remaining_attempts >= 1 and $tx = $build_retry_tx->($tx_after_redirects)
Branch: 1 branch
BranchTrueFalseTotal
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects) or &$_clean(), &$orig_cb($ua, $tx_after_redirects), return 0 0 0
Condition0%0/3
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects)
!ll&&!rl&&r
000
MC/DC0%0/2
--$remaining_attempts >= 1 and $tx = &$build_retry_tx($tx_after_redirects)
--$remaining_attempts >= 1 $tx = &$build_retry_tx($tx_after_redirects)
Truth table0%0/3
A and B
A= --$remaining_attempts >= 1B= $tx = &$build_retry_tx($tx_after_redirects)
ABresult
0X0
100
111
150 or $_clean->(), $orig_cb->($ua, $tx_after_redirects), return;
151 0 $ua->$orig($tx, __SUB__);
152 0 });
153 }
154 1 };
155  
156 0 ▶sub eqq {
Subroutine eqq: not called CC=5 SCAR=34.0
Pod undocumented
157 0 my ($x, $y) = @_;
158  
159 0 ▶ defined $x or return !defined $y;
Branch: 1 branch
BranchTrueFalseTotal
defined $x or return !defined($y) 0 0 0
160 0 ▶ defined $y or return !!0;
Branch: 1 branch
BranchTrueFalseTotal
defined $y or 0 0 0
161 0 ▶ ref $x eq ref $y or return !!0;
Branch: 1 branch
BranchTrueFalseTotal
ref $x eq ref $y or 0 0 0
162 0 ▶ return length(ref $x) ? refaddr $x == refaddr $y : $x eq $y;
Branch: 1 branch
BranchTrueFalseTotal
length ref $x ? : 0 0 0
163 }
164  
165 0 ▶sub _total_size {
Subroutine _total_size: not called CC=6 SCAR=37.4
166 0 my ($msg) = @_;
167  
168 0 ▶ return undef unless $msg->headers->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->headers->is_finished 0 0 0
169  
170 0 ▶ if ($msg->code == 206) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->code == 206) { } 0 0 0
171 0 ▶ ($msg->headers->content_range // '') =~ /^bytes \d+\-(\d+)\/(?:\*|(\d+))\z/;
Condition0%0/2
$msg->headers->content_range // ""
l!l
00
MC/DC0%0/1
$msg->headers->content_range // ""
$msg->headers->content_range
Truth table0%0/2
A
A= $msg->headers->content_range
Aresult
11
00
172 0 ▶ return $2 // (defined $1 ? $1 + 1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
Condition0%0/2
$2 // (defined $1 ? $1 + 1 : undef)
l!l
00
MC/DC0%0/1
$2 // (defined $1 ? $1 + 1 : undef)
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
173 } else {
174 0 return $msg->headers->content_length;
175 }
176 }
177  
178 1 1;
179 __END__
↑ Summary Transaction/HTTP/Role/Resume.pm »