lib/Dancer2/Core/Response.pm

filter: Statementstmt 98.6% 150 / 152 Branchbran 93.1% 41 / 44 Conditioncond 75.0% 15 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 41 / 41 Podpod 84.6% 11 / 13 totaltotal 93.2% 263 / 282 CC 30 SCAR 15.4
CC
30
Cov
95%
encode_content
14.2
to_psgi
7.0
__ANON__
4.0
CRAP 4.6
« lib/Dancer2/Core/Request.pm ↑ Summary lib/Dancer2/Core/DSL.pm »
1 # ABSTRACT: Response object for Dancer2
2  
3 package Dancer2::Core::Response;
4 178 $Dancer2::Core::Response::VERSION = '2.3.0';
5 178 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 178 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 178 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 178 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 178 ▶use Dancer2::Core::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 178 ▶use Dancer2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 178 ▶use Dancer2::Core::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 178 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 178 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 178 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 178 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 178 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 use overload
22 24 ▶ '@{}' => sub { $_[0]->to_psgi },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
23 178 ▶ '""' => sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 178 my $WARNED_NO_CHARSET = 0;
26  
27 has mime_type => (
28 'is' => 'ro',
29 'isa' => InstanceOf['Dancer2::Core::MIME'],
30 60 ▶ 'default' => sub { Dancer2::Core::MIME->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 178 );
32  
33 has headers => (
34 is => 'ro',
35 isa => InstanceOf['HTTP::Headers'],
36 lazy => 1,
37 900 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
38 900 my ($value) = @_;
39 # HTTP::Headers::Fast reports that it isa 'HTTP::Headers',
40 # but there is no actual inheritance.
41 $value->$_isa('HTTP::Headers')
42 ? $value
43 900 ▶ : HTTP::Headers::Fast->new(@{$value});
Branch: 1 branch
BranchTrueFalseTotal
$value->$Dancer2::Core::Response::_isa('HTTP::Headers') ? : 868 32 900
44 },
45 865 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 865 HTTP::Headers::Fast->new();
47 },
48 178 handles => [qw<header push_header>],
49 );
50  
51 has strict_utf8 => (
52 is => 'ro',
53 isa => Bool,
54 264 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
55 178 );
56  
57 178 has log_cb => (
58 is => 'ro',
59 isa => CodeRef,
60 predicate => 'has_log_cb',
61 );
62  
63 859 ▶sub headers_to_array {
Subroutine headers_to_array: called CC=2 SCAR=7.0
Pod documented
64 859 my $self = shift;
65 859 ▶ my $headers = shift || $self->headers;
Condition66%2/3
shift() || $self->headers
l!l&&r!l&&!r
830290
MC/DC0%0/2
shift() || $self->headers
shift() $self->headers
Truth table66%2/3
A || B
A= shift()B= $self->headers
ABresult
1X1
011
000
66  
67 859 my @hdrs;
68 1848 ▶ $headers->scan( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 1848 my ( $k, $v ) = @_;
70 0 $k =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
71 1848 $k =~ s/\015|\012//g; # remove CR and LF from the name for the same
72 # reason as the value: an unsanitised name is
73 # just as usable for response splitting
74 1848 $v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
75 1848 $v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
76 1848 push @hdrs, $k => $v;
77 859 });
78  
79 859 return \@hdrs;
80 }
81  
82 # boolean to tell if the route passes or not
83 has has_passed => (
84 is => 'rw',
85 isa => Bool,
86 1140 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
87 178 );
88  
89 3 ▶sub pass { shift->has_passed(1) }
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
90  
91 178 has serializer => (
92 is => 'rw',
93 isa => ConsumerOf ['Dancer2::Core::Role::Serializer'],
94 );
95  
96 178 has charset => (
97 is => 'rw',
98 isa => Str,
99 predicate => 'has_charset',
100 );
101  
102 has is_encoded => (
103 is => 'rw',
104 isa => Bool,
105 1139 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
106 178 );
107  
108 has is_halted => (
109 is => 'rw',
110 isa => Bool,
111 1139 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
112 178 );
113  
114 12 ▶sub halt {
Subroutine halt: called CC=2 SCAR=6.9
Pod documented
115 12 my ( $self, $content ) = @_;
116 12 ▶ $self->content( $content ) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 4 8 12
117 12 $self->is_halted(1);
118 }
119  
120 has status => (
121 is => 'rw',
122 isa => Num,
123 598 ▶ default => sub {200},
Subroutine __ANON__: called CC=1 SCAR=0.0
124 lazy => 1,
125 875 ▶ coerce => sub { Dancer2::Core::HTTP->status(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 178 );
127  
128 178 has content => (
129 is => 'rw',
130 isa => Str,
131 predicate => 'has_content',
132 clearer => 'clear_content',
133 );
134  
135 has server_tokens => (
136 is => 'ro',
137 isa => Bool,
138 87 ▶ default => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
139 178 );
140  
141 1735 ▶around content => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
142 1735 my ( $orig, $self ) = ( shift, shift );
143  
144 # called as getter?
145 1735 ▶ @_ or return $self->$orig;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self->$orig 846 889 1735
146  
147 # is_encoded describes the content currently stored, not the response
148 # forever: a fresh assignment must be judged (and encoded) on its own
149 # merits rather than inheriting the previous content's encoded status.
150 #
151 # This is safe for the callers that mark already-encoded bytes with
152 # is_encoded(1) "by hand": Handler/File.pm sets is_encoded(1) *after*
153 # calling content($bytes), so by the time is_encoded is latched, this
154 # modifier (and the encode_content call below) has already run and
155 # finished with this assignment. The send_file path in Core/App.pm
156 # assigns content via the raw hash element ($response->{content} = ...),
157 # bypassing this modifier entirely, and only then sets is_encoded(1) -
158 # so it never reaches this reset at all. Neither caller sets
159 # is_encoded(1) *before* assigning content, which is the only order this
160 # reset would break.
161 846 $self->is_encoded(0);
162  
163 # No serializer defined; encode content
164 846 ▶ $self->serializer
Branch: 1 branch
BranchTrueFalseTotal
$self->serializer or return $self->$orig($self->encode_content(@_)) 112 734 846
165 or return $self->$orig( $self->encode_content(@_) );
166  
167 # serialize content
168 112 my $serialized = $self->serialize(@_);
169 112 $self->is_encoded(1); # All serializers return byte strings
170 112 ▶ return $self->$orig( defined $serialized ? $serialized : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $serialized ? : 110 2 112
171 178 };
172  
173 has default_content_type => (
174 is => 'rw',
175 isa => Str,
176 1139 ▶ default => sub {'text/html'},
Subroutine __ANON__: called CC=1 SCAR=0.0
177 178 );
178  
179 734 ▶sub encode_content {
Subroutine encode_content: called CC=14 SCAR=26.5
Pod documented
180 734 my ( $self, $content ) = @_;
181  
182 734 ▶ return $content if $self->is_encoded;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_encoded 0 734 734
183  
184 # Apply default content type if none set.
185 734 ▶ my $ct = $self->content_type ||
Condition66%2/3
$self->content_type || $self->content_type($self->default_content_type)
l!l&&r!l&&!r
1975370
MC/DC0%0/2
$self->content_type || $self->content_type($self->default_content_type)
$self->content_type $self->content_type($self->default_content_type)
Truth table66%2/3
A || B
A= $self->content_typeB= $self->content_type($self->default_content_type)
ABresult
1X1
011
000
186 $self->content_type( $self->default_content_type );
187  
188 734 ▶ return $content if $ct !~ /^text/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct =~ /^text/ 1 733 734
189 733 my $charset = $self->headers->content_type_charset;
190 733 ▶ $charset = $self->charset
Branch: 1 branch
BranchTrueFalseTotal
if not defined $charset and $self->has_charset 712 21 733
Condition100%3/3
not defined $charset and $self->has_charset
!ll&&!rl&&r
147712
MC/DC100%2/2
not defined $charset and $self->has_charset
not defined $charset $self->has_charset
Truth table100%3/3
not(A) and B
A= not defined $charsetB= $self->has_charset
ABresult
0X0
100
111
191 if !defined $charset && $self->has_charset;
192 733 ▶ if ( !defined $charset || $charset eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $charset or $charset eq "") 7 726 733
Condition66%2/3
not defined $charset or $charset eq ""
l!l&&r!l&&!r
70726
MC/DC50%1/2
not defined $charset or $charset eq ""
not defined $charset $charset eq ""
Truth table66%2/3
not(A) or B
A= not defined $charsetB= $charset eq ""
ABresult
1X1
011
000
193 7 ▶ return $content if !utf8::is_utf8($content);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($content) 3 4 7
194 4 my $msg = 'Response contains characters but no charset is configured; assuming UTF-8';
195 4 ▶ $self->strict_utf8
Branch: 1 branch
BranchTrueFalseTotal
$self->strict_utf8 and Carp::croak($msg) 2 2 4
196 and Carp::croak($msg);
197 2 ▶ if ( !$WARNED_NO_CHARSET ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WARNED_NO_CHARSET) 1 1 2
198 1 $WARNED_NO_CHARSET = 1;
199 1 ▶ if ( $self->has_log_cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_log_cb) { } 1 0 1
200 1 $self->log_cb->( warning => $msg );
201 }
202 else {
203 0 Carp::carp($msg);
204 }
205 }
206 2 $charset = 'UTF-8';
207 }
208  
209 # we don't want to encode an empty string, it will break the output
210 728 ▶ $content or return $content;
Branch: 1 branch
BranchTrueFalseTotal
$content or return $content 689 39 728
211  
212 689 ▶ $self->content_type("$ct; charset=$charset")
Branch: 1 branch
BranchTrueFalseTotal
unless $ct =~ /charset/ 675 14 689
213 if $ct !~ /charset/;
214  
215 689 $self->is_encoded(1);
216 689 return Encode::encode( $charset, $content );
217 }
218  
219 1 ▶sub new_from_plack {
Subroutine new_from_plack: called CC=1 SCAR=0.0
Pod documented
220 1 my ($class, $psgi_res) = @_;
221  
222 1 return Dancer2::Core::Response->new(
223 status => $psgi_res->status,
224 headers => $psgi_res->headers,
225 content => $psgi_res->body,
226 );
227 }
228  
229 1 ▶sub new_from_array {
Subroutine new_from_array: called CC=1 SCAR=0.0
Pod documented
230 1 my ($class, $arrayref) = @_;
231  
232 1 return Dancer2::Core::Response->new(
233 status => $arrayref->[0],
234 headers => $arrayref->[1],
235 content => $arrayref->[2][0],
236 );
237 }
238  
239 830 ▶sub to_psgi {
Subroutine to_psgi: called CC=7 SCAR=19.5
Pod documented
240 830 my ($self) = @_;
241  
242 830 my $headers = $self->headers;
243 830 my $status = $self->status;
244  
245 830 ▶ Plack::Util::status_with_no_entity_body($status)
Branch: 1 branch
BranchTrueFalseTotal
Plack::Util::status_with_no_entity_body($status) and return [$status, $self->headers_to_array($headers), []] 5 825 830
246 and return [ $status, $self->headers_to_array($headers), [] ];
247  
248 825 my $content = $self->content;
249 # It is possible to have no content and/or no content type set
250 # e.g. if all routes 'pass'. Set the default value for the content
251 # (an empty string), allowing serializer hooks to be triggered
252 # as they may change the content..
253 825 ▶ $content = $self->content('') if ! defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 38 787 825
254  
255 825 ▶ if ( !$headers->header('Content-Length') &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $headers->header("Content-Length") || $headers->header("Transfer-Encoding") and defined(my $content_length = length $content)) 798 27 825
Condition66%4/6
not $headers->header("Content-Length") || $headers->header("Transfer-Encoding") and defined(my $content_length = length $content)
!ll&&!rl&&r
270798
$headers->header('Content-Length') || $headers->header('Transfer-Encoding')
l!l&&r!l&&!r
270798
MC/DC33%1/3
not $headers->header("Content-Length") || $headers->header("Transfer-Encoding") and defined(my $content_length = length $content)
$headers->header('Content-Length') $headers->header('Transfer-Encoding') defined(my $content_length = length $content)
Truth table50%2/4
not(A || B) and C
A= $headers->header('Content-Length')B= $headers->header('Transfer-Encoding')C= defined(my $content_length = length $content)
ABCresult
1XX0
01X0
0000
0011
256 !$headers->header('Transfer-Encoding') &&
257 defined( my $content_length = length $content ) ) {
258 798 $headers->push_header( 'Content-Length' => $content_length );
259 }
260  
261 # More defaults
262 825 ▶ $self->content_type or $self->content_type($self->default_content_type);
Branch: 1 branch
BranchTrueFalseTotal
$self->content_type or $self->content_type($self->default_content_type) 821 4 825
263 825 return [ $status, $self->headers_to_array($headers), [ $content ], ];
264 }
265  
266 # sugar for accessing the content_type header, with mimetype care
267 3072 ▶sub content_type {
Subroutine content_type: called CC=2 SCAR=6.9
Pod documented
268 3072 my $self = shift;
269  
270 3072 ▶ if ( scalar @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) { } 1511 1561 3072
271 1511 my $mimetype = $self->mime_type->name_or_type(shift);
272 1511 $self->header( 'Content-Type' => $mimetype );
273 1511 return $mimetype;
274 }
275 else {
276 1561 return $self->header('Content-Type');
277 }
278 }
279  
280 178 has _forward => (
281 is => 'rw',
282 isa => HashRef,
283 );
284  
285 1 ▶sub forward {
Subroutine forward: called CC=1 SCAR=0.0
Pod undocumented
286 1 my ( $self, $uri, $params, $opts ) = @_;
287 1 $self->_forward( { to_url => $uri, params => $params, options => $opts } );
288 }
289  
290 2 ▶sub is_forwarded {
Subroutine is_forwarded: called CC=1 SCAR=0.0
Pod undocumented
291 2 my $self = shift;
292 2 $self->_forward;
293 }
294  
295 30 ▶sub redirect {
Subroutine redirect: called CC=2 SCAR=6.9
Pod documented
296 30 my ( $self, $destination, $status ) = @_;
297 30 ▶ $self->status( $status || 302 );
Condition100%2/2
$status || 302
l!l
129
MC/DC100%1/1
$status || 302
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
298  
299 # we want to stringify the $destination object (URI object)
300 30 $self->header( 'Location' => "$destination" );
301 }
302  
303 1 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
304 1 my $self = shift;
305  
306 1 my $error = Dancer2::Core::Error->new(
307 response => $self,
308 @_,
309 );
310  
311 1 $error->throw;
312 1 return $error;
313 }
314  
315 112 ▶sub serialize {
Subroutine serialize: called CC=3 SCAR=11.0
Pod documented
316 112 my ($self, $content) = @_;
317  
318 112 ▶ my $serializer = $self->serializer
Branch: 1 branch
BranchTrueFalseTotal
my $serializer = $self->serializer or return 112 0 112
319 or return;
320  
321 112 ▶ $content = $serializer->serialize($content)
Branch: 1 branch
BranchTrueFalseTotal
$content = $serializer->serialize($content) or return 110 2 112
322 or return;
323  
324 110 $self->content_type( $serializer->content_type );
325 110 return $content;
326 }
327  
328 178 1;
329  
330 __END__
« lib/Dancer2/Core/Request.pm ↑ Summary lib/Dancer2/Core/DSL.pm »