| 1 | package Unblock::HTTP3::Body::Stream; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Unblock::HTTP3 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Unblock::HTTP3::_Bytes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 22 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=10 SCAR=25.3
| |||||||||||||||||||||||||||||
| 15 | 22 | my ($class, $transaction, $kind, %option) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 22 | ▶ | croak 'body stream kind must be request or response' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$kind ne "request" and $kind ne "response"
MC/DC0%0/2
$kind ne "request" and $kind ne "response"
$kind ne "request"
$kind ne "response"
Truth table66%2/3
A and B
A= $kind ne "request"B= $kind ne "response"
| |||||||||||||||||||||||||||||
| 18 | if $kind ne 'request' && $kind ne 'response'; | ||||||||||||||||||||||||||||
| 19 | 22 | ▶ | croak 'body stream requires a Unblock::HTTP3::Transaction' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
MC/DC0%0/2
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
builtin::blessed($transaction)
$transaction->isa("Unblock::HTTP3::Transaction")
Truth table33%1/3
A and B
A= builtin::blessed($transaction)B= $transaction->isa("Unblock::HTTP3::Transaction")
| |||||||||||||||||||||||||||||
| 20 | unless blessed($transaction) | ||||||||||||||||||||||||||||
| 21 | && $transaction->isa('Unblock::HTTP3::Transaction'); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 22 | my $on_drain = delete $option{on_drain}; | |||||||||||||||||||||||||||
| 24 | 22 | my $on_cancel = delete $option{on_cancel}; | |||||||||||||||||||||||||||
| 25 | 22 | my $operation = $kind . '_body'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 22 | ▶ | croak "$operation(): on_drain must be a coderef" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $on_drain and ref $on_drain ne "CODE"
MC/DC0%0/2
defined $on_drain and ref $on_drain ne "CODE"
defined $on_drain
ref $on_drain ne "CODE"
Truth table66%2/3
A and B
A= defined $on_drainB= ref $on_drain ne "CODE"
| |||||||||||||||||||||||||||||
| 28 | if defined($on_drain) && ref($on_drain) ne 'CODE'; | ||||||||||||||||||||||||||||
| 29 | 22 | ▶ | croak "$operation(): on_cancel must be a coderef" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $on_cancel and ref $on_cancel ne "CODE"
MC/DC0%0/2
defined $on_cancel and ref $on_cancel ne "CODE"
defined $on_cancel
ref $on_cancel ne "CODE"
Truth table66%2/3
A and B
A= defined $on_cancelB= ref $on_cancel ne "CODE"
| |||||||||||||||||||||||||||||
| 30 | if defined($on_cancel) && ref($on_cancel) ne 'CODE'; | ||||||||||||||||||||||||||||
| 31 | 22 | ▶ | croak "$operation(): unknown option: " . join(', ', sort keys %option) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | if %option; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 22 | my $self = bless { | |||||||||||||||||||||||||||
| 35 | transaction => $transaction, | ||||||||||||||||||||||||||||
| 36 | kind => $kind, | ||||||||||||||||||||||||||||
| 37 | on_drain => $on_drain, | ||||||||||||||||||||||||||||
| 38 | on_cancel => $on_cancel, | ||||||||||||||||||||||||||||
| 39 | complete => 0, | ||||||||||||||||||||||||||||
| 40 | cancelled => 0, | ||||||||||||||||||||||||||||
| 41 | flow_blocked => 0, | ||||||||||||||||||||||||||||
| 42 | }, $class; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 22 | weaken($self->{transaction}); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 22 | return $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | sub _configure { | ||||||||||||||||||||||||||
|
Subroutine
_configure: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||
| 50 | 1 | my ($self, %option) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | my $operation = $self->{kind} . '_body'; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | for my $name (qw(on_drain on_cancel)) { | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | next unless exists $option{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | my $callback = delete $option{$name}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | croak "$operation(): $name must be a coderef" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $callback and ref $callback ne "CODE"
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback
ref $callback ne "CODE"
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
| |||||||||||||||||||||||||||||
| 60 | if defined($callback) && ref($callback) ne 'CODE'; | ||||||||||||||||||||||||||||
| 61 | croak "$operation(): $name is already configured" | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if defined($self->{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | $self->{$name} = $callback; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | croak "$operation(): unknown option: " . join(', ', sort keys %option) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | if %option; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 38 | ▶ | sub is_complete { | ||||||||||||||||||||||||||
|
Subroutine
is_complete: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 38 | my ($self) = @_; | |||||||||||||||||||||||||||
| 75 | 38 | ▶ | return $self->{complete} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | sub is_cancelled { | ||||||||||||||||||||||||||
|
Subroutine
is_cancelled: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | return $self->{cancelled} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 37 | ▶ | sub _assert_writable { | ||||||||||||||||||||||||||
|
Subroutine
_assert_writable: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 84 | 37 | my ($self, $operation) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | croak "$operation(): streaming body is already complete" | ||||||||||||||||||||||||||||
| 87 | 37 | ▶ | if $self->{complete}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | croak "$operation(): streaming body was cancelled" | ||||||||||||||||||||||||||||
| 89 | 37 | ▶ | if $self->{cancelled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | my $transaction = $self->{transaction} | ||||||||||||||||||||||||||||
| 92 | 37 | ▶ | or croak "$operation(): HTTP/3 Transaction is no longer available"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 37 | return $transaction; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 19 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 19 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 19 | my $operation = $self->{kind} . '_body->write'; | |||||||||||||||||||||||||||
| 101 | 19 | my $transaction = $self->_assert_writable($operation); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 19 | $bytes = Unblock::HTTP3::_Bytes::byte_string('body', $bytes); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 19 | $self->{flow_blocked} = 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 19 | my ($can_continue, $ok, $error); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 19 | local $@; | |||||||||||||||||||||||||||
| 111 | 19 | $ok = eval { | |||||||||||||||||||||||||||
| 112 | $can_continue = $transaction->_write_body( | ||||||||||||||||||||||||||||
| 113 | $self->{kind}, | ||||||||||||||||||||||||||||
| 114 | 19 | $bytes, | |||||||||||||||||||||||||||
| 115 | 0, | ||||||||||||||||||||||||||||
| 116 | $operation, | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | 19 | 1; | |||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||
| 120 | 19 | $error = $@; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 19 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | $self->{flow_blocked} = 0; | |||||||||||||||||||||||||||
| 125 | 0 | die $error; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 19 | ▶ | $self->{flow_blocked} = 0 if $can_continue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 19 | return $can_continue; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 18 | ▶ | sub complete { | ||||||||||||||||||||||||||
|
Subroutine
complete: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 18 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 134 | 18 | ▶ | $bytes = '' unless defined $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 18 | my $operation = $self->{kind} . '_body->complete'; | |||||||||||||||||||||||||||
| 137 | 18 | my $transaction = $self->_assert_writable($operation); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 18 | $bytes = Unblock::HTTP3::_Bytes::byte_string('body', $bytes); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | $transaction->_write_body( | ||||||||||||||||||||||||||||
| 142 | $self->{kind}, | ||||||||||||||||||||||||||||
| 143 | 18 | $bytes, | |||||||||||||||||||||||||||
| 144 | 1, | ||||||||||||||||||||||||||||
| 145 | $operation, | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 18 | $self->{complete} = 1; | |||||||||||||||||||||||||||
| 149 | 18 | $self->{flow_blocked} = 0; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 18 | return $self; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 80 | ▶ | sub _drain { | ||||||||||||||||||||||||||
|
Subroutine
_drain: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 155 | 80 | my ($self) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 80 | ▶ | return if $self->{complete} || $self->{cancelled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"complete"} or $self->{"cancelled"}
MC/DC50%1/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"}
$self->{"cancelled"}
Truth table66%2/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
| |||||||||||||||||||||||||||||
| 158 | 38 | ▶ | return unless $self->{flow_blocked}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | $self->{flow_blocked} = 0; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | my $callback = $self->{on_drain} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 1 | $callback->($self); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | return; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 7 | ▶ | sub _cancel { | ||||||||||||||||||||||||||
|
Subroutine
_cancel: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 169 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 7 | ▶ | return if $self->{complete} || $self->{cancelled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"complete"} or $self->{"cancelled"}
MC/DC0%0/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"}
$self->{"cancelled"}
Truth table33%1/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 7 | $self->{cancelled} = 1; | |||||||||||||||||||||||||||
| 174 | 7 | $self->{flow_blocked} = 0; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 7 | ▶ | my $callback = $self->{on_cancel} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 1 | $callback->($self); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 6 | 1; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||