| 1 | package YATT::Lite::Response; sub MY () {__PACKAGE__} | ||||||||||||||||||||||||||||
| 2 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 45 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 45 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 45 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 45 | ▶ | use parent qw(YATT::Lite::Object); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 45 | ▶ | use YATT::Lite::MFields qw/status headers body/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 45 | require Encode; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #======================================== | ||||||||||||||||||||||||||||
| 13 | # Constructors | ||||||||||||||||||||||||||||
| 14 | #======================================== | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Wrap a PSGI response (ARRAY triple, or delayed/streaming CODE) | ||||||||||||||||||||||||||||
| 17 | # into a YATT::Lite::Response. | ||||||||||||||||||||||||||||
| 18 | 9 | ▶ | sub from_psgi { | ||||||||||||||||||||||||||
|
Subroutine
from_psgi: called CC=6 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 9 | my ($pack, $res) = @_; | |||||||||||||||||||||||||||
| 20 | 9 | ▶ | if (ref $res eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | $res = $pack->_run_streaming($res); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 9 | ▶ | unless (ref $res eq 'ARRAY' and @$res >= 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $res eq "ARRAY" and @$res >= 2
MC/DC0%0/2
ref $res eq "ARRAY" and @$res >= 2
ref $res eq "ARRAY"
@$res >= 2
Truth table33%1/3
A and B
A= ref $res eq "ARRAY"B= @$res >= 2
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | croak "Invalid PSGI response: ".(defined $res ? $res : '(undef)'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 9 | my ($status, $headers, $body) = @$res; | |||||||||||||||||||||||||||
| 27 | 9 | ▶ | $pack->new(status => $status | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | , headers => ($headers ? [@$headers] : []) | ||||||||||||||||||||||||||||
| 29 | , body => $pack->_collect_body($body)); | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub _run_streaming { | ||||||||||||||||||||||||||
|
Subroutine
_run_streaming: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my ($pack, $app) = @_; | |||||||||||||||||||||||||||
| 34 | 0 | my ($status, $headers, $body, @chunks); | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | $app->(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($tuple) = @_; | |||||||||||||||||||||||||||
| 37 | 0 | ($status, $headers) = @$tuple[0, 1]; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | if (@$tuple >= 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | $body = $tuple->[2]; | |||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | $body = \@chunks; | |||||||||||||||||||||||||||
| 43 | 0 | return MY."::Writer"->new(\@chunks); | |||||||||||||||||||||||||||
| 44 | 0 | }); | |||||||||||||||||||||||||||
| 45 | 0 | [$status, $headers, $body]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | sub _collect_body { | ||||||||||||||||||||||||||
|
Subroutine
_collect_body: called CC=3 SCAR=16.3
| |||||||||||||||||||||||||||||
| 49 | 9 | my ($pack, $body) = @_; | |||||||||||||||||||||||||||
| 50 | 9 | ▶ | if (not defined $body) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 51 | 0 | []; | |||||||||||||||||||||||||||
| 52 | } elsif (ref $body eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 53 | 9 | [@$body]; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | # IO::Handle-ish body (including real filehandles). | ||||||||||||||||||||||||||||
| 56 | 0 | require Plack::Util; | |||||||||||||||||||||||||||
| 57 | 0 | my @chunks; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | Plack::Util::foreach($body, sub {push @chunks, $_[0]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | \@chunks; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | #======================================== | ||||||||||||||||||||||||||||
| 64 | # Accessors | ||||||||||||||||||||||||||||
| 65 | #======================================== | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 7 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 69 | 7 | $self->{status}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 10 | ▶ | sub is_success { | ||||||||||||||||||||||||||
|
Subroutine
is_success: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 10 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 74 | 10 | ▶ | my $status = $self->{status} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"status"} // 0
MC/DC0%0/1
$self->{"status"} // 0
$self->{"status"}
Truth table50%1/2
A
A= $self->{"status"}
| |||||||||||||||||||||||||||||
| 75 | 10 | ▶ | $status >= 200 && $status < 300; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub headers { | ||||||||||||||||||||||||||
|
Subroutine
headers: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $list = $self->{headers} // []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"headers"} // []
MC/DC0%0/1
$self->{"headers"} // []
$self->{"headers"}
Truth table0%0/2
A
A= $self->{"headers"}
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | wantarray ? @$list : $list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | ▶ | sub header { | ||||||||||||||||||||||||||
|
Subroutine
header: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 3 | (my MY $self, my $name) = @_; | |||||||||||||||||||||||||||
| 86 | 3 | ▶ | my $list = $self->{headers} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"headers"} // []
MC/DC0%0/1
$self->{"headers"} // []
$self->{"headers"}
Truth table50%1/2
A
A= $self->{"headers"}
| |||||||||||||||||||||||||||||
| 87 | 3 | my @found; | |||||||||||||||||||||||||||
| 88 | 3 | ▶ | for (my $i = 0; $i + 1 <= $#$list; $i += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | push @found, $list->[$i+1] if lc($list->[$i]) eq lc($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 3 | ▶ | wantarray ? @found : $found[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Raw body: list of chunks in list context, joined octets in scalar context. | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | sub body { | ||||||||||||||||||||||||||
|
Subroutine
body: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 4 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 97 | 4 | ▶ | my $list = $self->{body} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"body"} // []
MC/DC0%0/1
$self->{"body"} // []
$self->{"body"}
Truth table50%1/2
A
A= $self->{"body"}
| |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | wantarray ? @$list : join('', grep {defined} @$list); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Decoded text (utf-8 unless already decoded). | ||||||||||||||||||||||||||||
| 102 | 16 | ▶ | sub content { | ||||||||||||||||||||||||||
|
Subroutine
content: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 16 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 104 | 16 | ▶ | my $raw = join '', grep {defined} @{$self->{body} // []}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"body"} // []
MC/DC0%0/1
$self->{"body"} // []
$self->{"body"}
Truth table50%1/2
A
A= $self->{"body"}
| |||||||||||||||||||||||||||||
| 105 | 16 | ▶ | utf8::is_utf8($raw) ? $raw : Encode::decode(utf8 => $raw); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | #======================================== | ||||||||||||||||||||||||||||
| 109 | # Minimal writer for streaming PSGI responses. | ||||||||||||||||||||||||||||
| 110 | #======================================== | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | package YATT::Lite::Response::Writer; | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($pack, $chunks) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | bless [$chunks], $pack; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 0 | push @{$self->[0]}, @_; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub close {} | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 45 | 1; | |||||||||||||||||||||||||||