| 1 | package Net::Blossom::Server::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Net::Blossom::_ConstructorArgs (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Class::Tiny qw(status _headers), { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | body => '', | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use JSON (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | my $TOKEN = qr/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | my $JSON = JSON->new->utf8->canonical; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 24 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 24 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 24 | my %args = Net::Blossom::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 24 | my %known = map { $_ => 1 } qw(status headers body); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 24 | my @unknown = grep { !exists $known{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 24 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 23 | ▶ | $args{headers} = {} unless defined $args{headers}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | $args{_headers} = _normalize_headers($args{headers}); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 17 | delete $args{headers}; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 17 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 17 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 17 | ▶ | croak "status is required" unless defined $self->status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 16 | _validate_status($self->status); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | ▶ | $self->body('') unless defined $self->body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 11 | _validate_body($self->body); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | sub json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | my ($data, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | _validate_options(\%opts, qw(status headers)); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | my $body = $JSON->encode($data); | |||||||||||||||||||||||||||||||||||||||||
| 45 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 46 | status => _status_option(\%opts, 200), | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | headers => _headers_with_defaults(delete($opts{headers}), { | |||||||||||||||||||||||||||||||||||||||||
| 48 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 'Content-Length' => length($body), | ||||||||||||||||||||||||||||||||||||||||||
| 50 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 51 | body => $body, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | my ($body, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | _validate_options(\%opts, qw(status headers)); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $body = '' unless defined $body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | croak "text body must be a scalar" if ref($body); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 62 | status => _status_option(\%opts, 200), | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | headers => _headers_with_defaults(delete($opts{headers}), { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 'Content-Type' => 'text/plain; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 'Content-Length' => length($body), | ||||||||||||||||||||||||||||||||||||||||||
| 66 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 67 | body => $body, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub empty { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
empty: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my ($status, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | _validate_options(\%opts, qw(headers)); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | $status = 204 unless defined $status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 77 | status => $status, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | headers => _headers_with_defaults(delete($opts{headers}), { | |||||||||||||||||||||||||||||||||||||||||
| 79 | 'Content-Length' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 81 | body => '', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | sub redirect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redirect: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my ($location, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | _validate_options(\%opts, qw(status headers)); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | croak "location is required" unless defined $location && !ref($location) && length $location; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $location and not ref $location and length $location
defined $location and not ref $location
MC/DC33%1/3
defined $location and not ref $location and length $location
defined $location
not ref $location
length $location
Truth table50%2/4
A and not(B) and C
A= defined $locationB= not ref $locationC= length $location
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 91 | status => _status_option(\%opts, 307), | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | headers => _headers_with_defaults(delete($opts{headers}), { | |||||||||||||||||||||||||||||||||||||||||
| 93 | Location => $location, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 'Content-Length' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | }), | ||||||||||||||||||||||||||||||||||||||||||
| 96 | body => '', | ||||||||||||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | my ($status, $reason, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | _validate_options(\%opts, qw(headers)); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | croak "reason must be a scalar" if defined $reason && ref($reason); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $reason and ref $reason
MC/DC0%0/2
defined $reason and ref $reason
defined $reason
ref $reason
Truth table33%1/3
A and B
A= defined $reasonB= ref $reason
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my %defaults = ('Content-Length' => 0); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | $defaults{'X-Reason'} = $reason if defined $reason && length $reason; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $reason and length $reason
MC/DC0%0/2
defined $reason and length $reason
defined $reason
length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 108 | status => $status, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | headers => _headers_with_defaults(delete($opts{headers}), \%defaults), | |||||||||||||||||||||||||||||||||||||||||
| 110 | body => '', | ||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub headers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
headers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | return { map { $self->_headers->{$_}{name} => $self->_headers->{$_}{value} } keys %{$self->_headers} }; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | ▶ | sub header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
header: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 10 | ▶ | return undef unless defined $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | my $header = $self->_headers->{lc $name}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 10 | ▶ | return defined $header ? $header->{value} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | sub header_pairs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
header_pairs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | for my $key (sort { $self->_headers->{$a}{name} cmp $self->_headers->{$b}{name} } keys %{$self->_headers}) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | push @pairs, $self->_headers->{$key}{name}, $self->_headers->{$key}{value}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | return \@pairs; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | sub body_chunks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
body_chunks: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | ▶ | return [@{$self->body}] if ref($self->body) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | croak "stream body cannot be returned as chunks" if ref($self->body); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | return [$self->body]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 16 | ▶ | sub _validate_status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_status: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 16 | my ($status) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 16 | ▶ | croak "status must be a scalar" if ref($status); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 16 | ▶ | croak "status must be an HTTP status code" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | unless $status =~ /\A[1-5][0-9][0-9]\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 23 | ▶ | sub _normalize_headers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_headers: called CC=10 SCAR=23.8
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 23 | my ($headers) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 23 | ▶ | croak "headers must be a hash reference" unless ref($headers) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 22 | my %normalized; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 22 | for my $name (keys %$headers) { | |||||||||||||||||||||||||||||||||||||||||
| 155 | 26 | ▶ | croak "header names must be HTTP tokens" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
defined $name and not ref $name
MC/DC0%0/3
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
defined $name
not ref $name
$name =~ /\A$TOKEN\z/
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\A$TOKEN\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | unless defined $name && !ref($name) && $name =~ /\A$TOKEN\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 26 | ▶ | croak "header values must be defined" unless defined $headers->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 26 | ▶ | croak "header values must be scalars" if ref($headers->{$name}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | croak "header values must not contain CR or LF" | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 25 | ▶ | if $headers->{$name} =~ /[\r\n]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 22 | my $lower = lc $name; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 22 | ▶ | croak "duplicate header: $name" if exists $normalized{$lower}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | $normalized{$lower} = { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 21 | value => $headers->{$name}, | |||||||||||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 17 | return \%normalized; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 11 | ▶ | sub _validate_body { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_body: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 11 | my ($body) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 11 | ▶ | return unless ref($body); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | ▶ | if (ref($body) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | for my $chunk (@$body) { | |||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | ▶ | croak "body array values must be defined" unless defined $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 3 | ▶ | croak "body array values must be scalars" if ref($chunk); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | return if blessed($body) && ($body->can('read') || $body->can('getline')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
builtin::blessed($body) and $body->can("read") || $body->can("getline")
$body->can("read") or $body->can("getline")
MC/DC33%1/3
builtin::blessed($body) and $body->can("read") || $body->can("getline")
builtin::blessed($body)
$body->can("read")
$body->can("getline")
Truth table50%2/4
A and B or C
A= builtin::blessed($body)B= $body->can("read")C= $body->can("getline")
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | croak "body must be a scalar, array reference, or stream object"; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 15 | ▶ | sub _validate_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_options: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 15 | my ($opts, @known) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 15 | my %known = map { $_ => 1 } @known; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 15 | my @unknown = grep { !exists $known{$_} } keys %$opts; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 15 | ▶ | croak "unknown option(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 9 | ▶ | sub _headers_with_defaults { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_headers_with_defaults: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 9 | my ($headers, $defaults) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 9 | ▶ | $headers = {} unless defined $headers; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 9 | ▶ | croak "headers must be a hash reference" unless ref($headers) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 9 | my %merged = %$headers; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my %seen = map { lc($_) => 1 } keys %merged; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 9 | for my $name (keys %$defaults) { | |||||||||||||||||||||||||||||||||||||||||
| 204 | 17 | ▶ | next if $seen{lc $name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 17 | $merged{$name} = $defaults->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 9 | return \%merged; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 6 | ▶ | sub _status_option { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_status_option: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | my ($opts, $default) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 6 | my $status = delete $opts->{status}; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | ▶ | return defined $status ? $status : $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | Net::Blossom::Server::Response - Framework-neutral Blossom server response | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | use Net::Blossom::Server::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | my $response = Net::Blossom::Server::Response->json( | ||||||||||||||||||||||||||||||||||||||||||
| 230 | { ok => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 231 | status => 201, | ||||||||||||||||||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | my $status = $response->status; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | C<Net::Blossom::Server::Response> represents a server response before any PSGI, | ||||||||||||||||||||||||||||||||||||||||||
| 239 | PAGI, or other gateway adapter turns it into gateway-native output. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | Responses may carry scalar bodies, array reference bodies, or stream objects. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | Gateway adapters are responsible for mapping those body forms to their native | ||||||||||||||||||||||||||||||||||||||||||
| 243 | output protocol. | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | my $response = Net::Blossom::Server::Response->new(%args); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | Required C<status> must be an HTTP status code from C<100> through C<599>. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | Optional C<headers> defaults to an empty hash reference. Header names are | ||||||||||||||||||||||||||||||||||||||||||
| 254 | case-insensitive and duplicate case-insensitive names croak. Header values must | ||||||||||||||||||||||||||||||||||||||||||
| 255 | be defined scalars and must not contain CR or LF. | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | Optional C<body> defaults to the empty string. It may be a scalar byte string, an | ||||||||||||||||||||||||||||||||||||||||||
| 258 | array reference of scalar byte strings, or a stream object that provides C<read> | ||||||||||||||||||||||||||||||||||||||||||
| 259 | or C<getline>. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =head2 json | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | my $response = Net::Blossom::Server::Response->json($data, %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | Encodes C<$data> as canonical UTF-8 JSON. C<status> defaults to C<200>. The | ||||||||||||||||||||||||||||||||||||||||||
| 266 | response includes C<Content-Type: application/json> and C<Content-Length> unless | ||||||||||||||||||||||||||||||||||||||||||
| 267 | those headers are supplied in C<headers>. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | =head2 text | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | my $response = Net::Blossom::Server::Response->text($body, %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | Builds a text response. C<status> defaults to C<200>. The response includes | ||||||||||||||||||||||||||||||||||||||||||
| 274 | C<Content-Type: text/plain; charset=utf-8> and C<Content-Length> unless those | ||||||||||||||||||||||||||||||||||||||||||
| 275 | headers are supplied in C<headers>. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | =head2 empty | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | my $response = Net::Blossom::Server::Response->empty($status, %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | Builds an empty response. C<status> defaults to C<204>. The response includes | ||||||||||||||||||||||||||||||||||||||||||
| 282 | C<Content-Length: 0> unless supplied in C<headers>. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =head2 redirect | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | my $response = Net::Blossom::Server::Response->redirect($location, %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | Builds a redirect response. C<status> defaults to C<307>. C<$location> is | ||||||||||||||||||||||||||||||||||||||||||
| 289 | required. The response includes C<Location> and C<Content-Length: 0>. | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | =head2 error | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | my $response = Net::Blossom::Server::Response->error($status, $reason, %opts); | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | Builds an empty error response. C<$reason>, when defined and non-empty, is sent | ||||||||||||||||||||||||||||||||||||||||||
| 296 | as C<X-Reason> and therefore must not contain CR or LF. | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | =head1 ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | =head2 status | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Returns the HTTP status code. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =head2 body | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Returns the scalar, array reference, or stream body. | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =head2 headers | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | my $headers = $response->headers; | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | Returns a copy hash reference of response headers. | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | =head2 header | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | my $value = $response->header($name); | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | Returns a header value using case-insensitive lookup. Returns C<undef> when the | ||||||||||||||||||||||||||||||||||||||||||
| 321 | header is absent. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head2 header_pairs | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | my $pairs = $response->header_pairs; | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | Returns an array reference suitable for PSGI-style C<< [name => value, ...] >> | ||||||||||||||||||||||||||||||||||||||||||
| 328 | headers. | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =head2 body_chunks | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | my $chunks = $response->body_chunks; | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | Returns scalar and array reference bodies as an array reference of chunks. Stream | ||||||||||||||||||||||||||||||||||||||||||
| 335 | bodies croak because they must be consumed by the gateway adapter. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =head2 BUILDARGS | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | Normalizes constructor arguments for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =head2 BUILD | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | Validates the constructed object for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||||||||||||||||