blib/lib/Unblock/HTTP1/Transaction.pm

filter: Statementstmt 98.6% 150 / 152 Branchbran 75.0% 69 / 92 Conditioncond 62.5% 30 / 48 MC/DCmcdc 33.3% 9 / 27 Subroutinesub 100% 32 / 32 Podpod 86.6% 13 / 15 totaltotal 82.7% 303 / 366 CC 65 SCAR 18.7
CC
65
Cov
85%
respond
12.9
on_data
11.4
send_informational
11.0
CRAP 6.5
« blib/lib/Unblock/HTTP1/_Engine.pm ↑ Summary blib/lib/Unblock/HTTP1/NativeABI.pm »
1 package Unblock::HTTP1::Transaction;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Uniform::HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 35 our $VERSION = '0.13';
10  
11 171 ▶sub _new {
Subroutine _new: called CC=7 SCAR=20.8
12 171 my ($class, $owner, $request, %args) = @_;
13 my $self = bless {
14 owner => $owner,
15 request => $request,
16 response => undef,
17 state => 'active',
18 error => undef,
19 callbacks => $args{callbacks} || {},
20 local_done => $args{local_done} ? 1 : 0,
21 remote_done => $args{remote_done} ? 1 : 0,
22 send_plan => $args{send_plan},
23 171 ▶ blocked => 0,
Branch: 2 branches
BranchTrueFalseTotal
$args{'local_done'} ? : 0 171 171
$args{'remote_done'} ? : 0 171 171
Condition50%1/2
$args{'callbacks'} || {}
l!l
1710
MC/DC0%0/1
$args{'callbacks'} || {}
$args{'callbacks'}
Truth table50%1/2
A
A= $args{'callbacks'}
Aresult
11
00
24 }, $class;
25 171 $self->{receive_body} = $owner->{receive_body};
26 171 $self->{max_buffered_body_bytes} = $owner->{max_buffered_body_bytes};
27 171 my $on_data = delete $self->{callbacks}{on_data};
28 171 ▶ $self->on_data($on_data) if $on_data;
Branch: 1 branch
BranchTrueFalseTotal
if $on_data 0 171 171
29 croak 'buffered reception cannot be combined with on_body'
30 171 ▶ if $self->{receive_body} eq 'buffered' && $self->{callbacks}{on_body};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"receive_body"} eq "buffered" and $self->{"callbacks"}{"on_body"} 0 171 171
Condition66%2/3
$self->{"receive_body"} eq "buffered" and $self->{"callbacks"}{"on_body"}
!ll&&!rl&&r
143280
MC/DC0%0/2
$self->{"receive_body"} eq "buffered" and $self->{"callbacks"}{"on_body"}
$self->{"receive_body"} eq "buffered" $self->{"callbacks"}{"on_body"}
Truth table66%2/3
A and B
A= $self->{"receive_body"} eq "buffered"B= $self->{"callbacks"}{"on_body"}
ABresult
0X0
100
111
31 171 weaken($self->{owner});
32 171 return $self;
33 }
34  
35 238 ▶sub request { $_[0]{request} }
Subroutine request: called CC=1 SCAR=0.0
Pod documented
36 122 ▶sub response { $_[0]{response} }
Subroutine response: called CC=1 SCAR=0.0
Pod documented
37 61 ▶sub state { $_[0]{state} }
Subroutine state: called CC=1 SCAR=0.0
Pod documented
38 8 ▶sub error { $_[0]{error} }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
39 43 ▶sub is_complete { $_[0]{state} eq 'complete' ? 1 : 0 }
Subroutine is_complete: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'state'} eq 'complete' ? : 40 3 43
40 5 ▶sub is_cancelled { $_[0]{state} eq 'cancelled' ? 1 : 0 }
Subroutine is_cancelled: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'state'} eq 'cancelled' ? : 3 2 5
41 4 ▶sub is_error { $_[0]{state} eq 'error' ? 1 : 0 }
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'state'} eq 'error' ? : 2 2 4
42 824 ▶sub is_terminal { $_[0]{state} ne 'active' ? 1 : 0 }
Subroutine is_terminal: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'state'} ne 'active' ? : 212 612 824
43  
44 11 ▶sub on_data {
Subroutine on_data: called CC=9 SCAR=24.4
Pod undocumented
45 11 my ($self, $cb) = @_;
46 11 ▶ croak 'on_data(): callback must be a coderef' unless ref($cb) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 11 11
47 croak 'on_data(): receive body has already started or finished'
48 11 ▶ if $self->{receive_started} || $self->{receive_done} || $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"receive_started"} or $self->{"receive_done"} or $self->is_terminal 2 9 11
Condition66%4/6
$self->{"receive_started"} or $self->{"receive_done"} or $self->is_terminal
l!l&&r!l&&!r
209
$self->{"receive_started"} or $self->{"receive_done"}
l!l&&r!l&&!r
209
MC/DC33%1/3
$self->{"receive_started"} or $self->{"receive_done"} or $self->is_terminal
$self->{"receive_started"} $self->{"receive_done"} $self->is_terminal
Truth table50%2/4
A or B or C
A= $self->{"receive_started"}B= $self->{"receive_done"}C= $self->is_terminal
ABCresult
1XX1
01X1
0011
0000
49 9 my $owner = $self->{owner};
50 croak 'on_data(): cannot be combined with on_body'
51 9 ▶ if $self->{callbacks}{on_body} || ($owner && $owner->{callbacks}{on_body});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"}{"on_body"} or $owner and $owner->{"callbacks"}{"on_body"} 0 9 9
Condition33%2/6
$self->{"callbacks"}{"on_body"} or $owner and $owner->{"callbacks"}{"on_body"}
l!l&&r!l&&!r
009
$owner and $owner->{"callbacks"}{"on_body"}
!ll&&!rl&&r
090
MC/DC0%0/3
$self->{"callbacks"}{"on_body"} or $owner and $owner->{"callbacks"}{"on_body"}
$self->{"callbacks"}{"on_body"} $owner $owner->{"callbacks"}{"on_body"}
Truth table25%1/4
A or B and C
A= $self->{"callbacks"}{"on_body"}B= $ownerC= $owner->{"callbacks"}{"on_body"}
ABCresult
1XX1
0111
00X0
0100
52 9 ▶ croak 'on_data(): callback is already registered' if $self->{callbacks}{on_data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"}{"on_data"} 0 9 9
53 9 $self->{callbacks}{on_data} = $cb;
54 9 $self->{receive_body} = 'stream';
55 9 return $self;
56 }
57  
58 53 ▶sub on_finish {
Subroutine on_finish: called CC=4 SCAR=14.0
Pod undocumented
59 53 my ($self, $cb) = @_;
60 53 ▶ croak 'on_finish(): callback must be a coderef' unless ref($cb) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 53 53
61 53 ▶ croak 'on_finish(): Transaction is terminal' if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 16 37 53
62 37 ▶ croak 'on_finish(): callback is already registered' if $self->{callbacks}{on_finish};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"}{"on_finish"} 0 37 37
63 37 $self->{callbacks}{on_finish} = $cb;
64 37 return $self;
65 }
66  
67 79 ▶sub _receive_data {
Subroutine _receive_data: called CC=8 SCAR=20.8
68 79 my ($self, $message, $bytes) = @_;
69 79 ▶ return 1 if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 79 79
70 79 $self->{receive_started} = 1;
71 79 ▶ if ($self->{receive_body} eq 'buffered') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"receive_body"} eq "buffered") 19 60 79
72 19 ▶ my $size = ($self->{received_bytes} || 0) + length($bytes);
Condition100%2/2
$self->{'received_bytes'} || 0
l!l
712
MC/DC100%1/1
$self->{'received_bytes'} || 0
$self->{'received_bytes'}
Truth table100%2/2
A
A= $self->{'received_bytes'}
Aresult
11
00
73 19 ▶ if ($size > $self->{max_buffered_body_bytes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $self->{"max_buffered_body_bytes"}) 1 18 19
74 1 delete $self->{receive_chunks};
75 1 delete $self->{received_bytes};
76 1 return 'received body exceeds max_buffered_body_bytes';
77 }
78 18 push @{ $self->{receive_chunks} }, $bytes;
79 18 $self->{received_bytes} = $size;
80 18 return 1;
81 }
82 60 ▶ return $self->_invoke('on_data', $bytes) if $self->{callbacks}{on_data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"}{"on_data"} 5 55 60
83 55 my $owner = $self->{owner};
84 55 ▶ return $owner->_invoke_server('on_body', $self, $message, $bytes)
Branch: 1 branch
BranchTrueFalseTotal
if $owner and $owner->can("_invoke_server") 19 36 55
Condition66%2/3
$owner and $owner->can("_invoke_server")
!ll&&!rl&&r
03619
MC/DC50%1/2
$owner and $owner->can("_invoke_server")
$owner $owner->can("_invoke_server")
Truth table66%2/3
A and B
A= $ownerB= $owner->can("_invoke_server")
ABresult
0X0
100
111
85 if $owner && $owner->can('_invoke_server');
86 36 return $self->_invoke('on_body', $message, $bytes);
87 }
88  
89 71 ▶sub _finish_receive {
Subroutine _finish_receive: called CC=4 SCAR=13.9
90 71 my ($self, $message) = @_;
91 71 $self->{receive_done} = 1;
92 71 ▶ if ($self->{receive_body} eq 'buffered' && $message->body_is_mutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"receive_body"} eq "buffered" and $message->body_is_mutable) 12 59 71
Condition66%2/3
$self->{"receive_body"} eq "buffered" and $message->body_is_mutable
!ll&&!rl&&r
59012
MC/DC50%1/2
$self->{"receive_body"} eq "buffered" and $message->body_is_mutable
$self->{"receive_body"} eq "buffered" $message->body_is_mutable
Truth table66%2/3
A and B
A= $self->{"receive_body"} eq "buffered"B= $message->body_is_mutable
ABresult
0X0
100
111
93 12 ▶ $message->body(join '', @{ delete($self->{receive_chunks}) || [] });
Condition100%2/2
delete $self->{'receive_chunks'} || []
l!l
102
MC/DC100%1/1
delete $self->{'receive_chunks'} || []
delete $self->{'receive_chunks'}
Truth table100%2/2
A
A= delete $self->{'receive_chunks'}
Aresult
11
00
94 }
95 71 delete $self->{receive_chunks};
96 71 delete $self->{received_bytes};
97 71 return;
98 }
99  
100 171 ▶sub _notify_finish {
Subroutine _notify_finish: called CC=4 SCAR=14.2
101 171 my ($self) = @_;
102 171 ▶ return unless $self->is_terminal && !$self->{finish_called};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_terminal and not $self->{"finish_called"} 0 171 171
Condition33%1/3
$self->is_terminal and not $self->{"finish_called"}
!ll&&!rl&&r
00171
MC/DC0%0/2
$self->is_terminal and not $self->{"finish_called"}
$self->is_terminal not $self->{"finish_called"}
Truth table33%1/3
A and not(B)
A= $self->is_terminalB= not $self->{"finish_called"}
ABresult
0X0
100
111
103 171 $self->{finish_pending} = 1;
104 171 ▶ return if $self->{callback_depth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callback_depth"} 5 166 171
105 166 $self->{finish_called} = 1;
106 166 delete $self->{finish_pending};
107 # Terminal observers cannot undo cleanup or recursively report an error.
108 166 $self->_invoke('on_finish');
109 166 return;
110 }
111  
112 14 ▶sub write {
Subroutine write: called CC=3 SCAR=11.0
Pod documented
113 14 my ($self, $bytes) = @_;
114 14 ▶ croak 'write(): Transaction is terminal' if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 1 13 14
115 13 ▶ my $owner = $self->{owner} or croak 'write(): HTTP/1 connection is gone';
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->{"owner"} or croak("write(): HTTP/1 connection is gone") 13 0 13
116 13 return $owner->_transaction_write($self, $bytes, 0);
117 }
118  
119 10 ▶sub end {
Subroutine end: called CC=4 SCAR=14.2
Pod documented
120 10 my ($self, $bytes) = @_;
121 10 ▶ croak 'end(): Transaction is terminal' if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 10 10
122 10 ▶ my $owner = $self->{owner} or croak 'end(): HTTP/1 connection is gone';
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->{"owner"} or croak("end(): HTTP/1 connection is gone") 10 0 10
123 10 ▶ return $owner->_transaction_write($self, defined($bytes) ? $bytes : '', 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 8 2 10
124 }
125  
126 89 ▶sub respond {
Subroutine respond: called CC=12 SCAR=25.6
Pod documented
127 89 my ($self, @arg) = @_;
128 89 ▶ croak 'respond(): Transaction is terminal' if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 89 89
129 89 ▶ my $owner = $self->{owner} or croak 'respond(): HTTP/1 connection is gone';
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->{"owner"} or croak("respond(): HTTP/1 connection is gone") 89 0 89
130  
131 89 my ($response, %option);
132 89 ▶ if (@arg && ref($arg[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg and ref $arg[0]) { } 41 48 89
Condition66%2/3
@arg and ref $arg[0]
!ll&&!rl&&r
04841
MC/DC50%1/2
@arg and ref $arg[0]
@arg ref $arg[0]
Truth table66%2/3
A and B
A= @argB= ref $arg[0]
ABresult
0X0
100
111
133 41 $response = shift @arg;
134 41 ▶ croak 'respond(): requires a Uniform HTTP response object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($response) and $response->can("status") and $response->can("header_count") 1 40 41
Condition66%4/6
builtin::blessed($response) and $response->can("status") and $response->can("header_count")
!ll&&!rl&&r
1040
builtin::blessed($response) and $response->can("status")
!ll&&!rl&&r
1040
MC/DC33%1/3
builtin::blessed($response) and $response->can("status") and $response->can("header_count")
builtin::blessed($response) $response->can("status") $response->can("header_count")
Truth table50%2/4
A and B and C
A= builtin::blessed($response)B= $response->can("status")C= $response->can("header_count")
ABCresult
0XX0
10X0
1100
1111
135 unless blessed($response) && $response->can('status')
136 && $response->can('header_count');
137 40 ▶ croak 'respond(): options must be key/value pairs' if @arg % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @arg % 2 0 40 40
138 40 %option = @arg;
139 } else {
140 48 ▶ croak 'respond(): response fields must be key/value pairs' if @arg % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @arg % 2 0 48 48
141 48 my %field = @arg;
142 48 for my $name (qw(stream_body on_drain)) {
143 96 ▶ $option{$name} = delete $field{$name} if exists $field{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $field{$name} 5 91 96
144 }
145 48 $response = Uniform::HTTP::Response->new(%field);
146 }
147  
148 88 return $owner->_transaction_respond($self, $response, %option);
149 }
150  
151 5 ▶sub send_informational {
Subroutine send_informational: called CC=10 SCAR=24.0
Pod documented
152 5 my ($self, @arg) = @_;
153 5 ▶ croak 'send_informational(): Transaction is terminal' if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 5 5
154 5 ▶ my $owner = $self->{owner} or croak 'send_informational(): HTTP/1 connection is gone';
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->{"owner"} or croak("send_informational(): HTTP/1 connection is gone") 5 0 5
155  
156 5 my $response;
157 5 ▶ if (@arg && ref($arg[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg and ref $arg[0]) { } 3 2 5
Condition66%2/3
@arg and ref $arg[0]
!ll&&!rl&&r
023
MC/DC50%1/2
@arg and ref $arg[0]
@arg ref $arg[0]
Truth table66%2/3
A and B
A= @argB= ref $arg[0]
ABresult
0X0
100
111
158 3 ▶ croak 'send_informational(): response object does not take options'
Branch: 1 branch
BranchTrueFalseTotal
unless @arg == 1 0 3 3
159 unless @arg == 1;
160 3 $response = $arg[0];
161 3 ▶ croak 'send_informational(): requires a Uniform HTTP response object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($response) and $response->can("status") and $response->can("header_count") 1 2 3
Condition66%4/6
builtin::blessed($response) and $response->can("status") and $response->can("header_count")
!ll&&!rl&&r
102
builtin::blessed($response) and $response->can("status")
!ll&&!rl&&r
102
MC/DC33%1/3
builtin::blessed($response) and $response->can("status") and $response->can("header_count")
builtin::blessed($response) $response->can("status") $response->can("header_count")
Truth table50%2/4
A and B and C
A= builtin::blessed($response)B= $response->can("status")C= $response->can("header_count")
ABCresult
0XX0
10X0
1100
1111
162 unless blessed($response) && $response->can('status')
163 && $response->can('header_count');
164 } else {
165 2 ▶ croak 'send_informational(): response fields must be key/value pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @arg % 2 0 2 2
166 if @arg % 2;
167 2 $response = Uniform::HTTP::Response->new(@arg);
168 }
169  
170 4 return $owner->_transaction_informational($self, $response);
171 }
172  
173 28 ▶sub cancel {
Subroutine cancel: called CC=3 SCAR=11.4
Pod documented
174 28 my ($self) = @_;
175 28 ▶ return $self if $self->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 19 9 28
176 9 my $owner = $self->{owner};
177 9 ▶ if ($owner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($owner) { } 9 0 9
178 9 $owner->_transaction_cancel($self);
179 } else {
180 0 $self->_mark_cancelled;
181 0 $self->_notify_finish;
182 }
183 9 return $self;
184 }
185  
186 137 ▶sub _set_response { $_[0]{response} = $_[1]; return $_[0] }
Subroutine _set_response: called CC=1 SCAR=0.0
187 92 ▶sub _mark_local_done { $_[0]{local_done} = 1; return }
Subroutine _mark_local_done: called CC=1 SCAR=0.0
188 127 ▶sub _mark_remote_done { $_[0]{remote_done} = 1; return }
Subroutine _mark_remote_done: called CC=1 SCAR=0.0
189 121 ▶sub _mark_complete { $_[0]{state} = 'complete'; return }
Subroutine _mark_complete: called CC=1 SCAR=0.0
190 9 ▶sub _mark_cancelled { $_[0]->_discard_receive; $_[0]{state} = 'cancelled'; return }
Subroutine _mark_cancelled: called CC=1 SCAR=0.0
191 36 ▶sub _fail { $_[0]->_discard_receive; $_[0]{state} = 'error'; $_[0]{error} = $_[1]; return }
Subroutine _fail: called CC=1 SCAR=0.0
192  
193 45 ▶sub _discard_receive {
Subroutine _discard_receive: called CC=1 SCAR=0.0
194 45 delete $_[0]{receive_chunks};
195 45 delete $_[0]{received_bytes};
196 45 return;
197 }
198  
199 327 ▶sub _invoke {
Subroutine _invoke: called CC=4 SCAR=13.9
200 327 my ($self, $name, @args) = @_;
201 327 ▶ my $cb = $self->{callbacks}{$name} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"callbacks"}{$name} or return 1 148 179 327
202 148 $self->{callback_depth}++;
203 148 my $ok = eval { $cb->($self, @args); 1 };
204 148 my $error = "$@";
205 148 $self->{callback_depth}--;
206 148 ▶ $self->_notify_finish if $self->{finish_pending};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finish_pending"} 2 146 148
207 148 ▶ return $ok ? 1 : $error;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 146 2 148
208 }
209  
210 35 1;
211  
212 __END__
« blib/lib/Unblock/HTTP1/_Engine.pm ↑ Summary blib/lib/Unblock/HTTP1/NativeABI.pm »