| 1 |
# ABSTRACT: Response object for Dancer2 |
| 2 |
|
| 3 |
package Dancer2::Core::Response; |
| 4 |
$Dancer2::Core::Response::VERSION = '2.2.2'; |
| 5 |
use Moo; |
| 6 |
|
| 7 |
use Carp; |
| 8 |
use Encode; |
| 9 |
use Dancer2::Core::Types; |
| 10 |
use Dancer2::Core::MIME; |
| 11 |
|
| 12 |
use Dancer2 (); |
| 13 |
use Dancer2::Core::HTTP; |
| 14 |
|
| 15 |
use HTTP::Headers::Fast; |
| 16 |
use Scalar::Util qw(blessed); |
| 17 |
use Plack::Util; |
| 18 |
use Safe::Isa; |
| 19 |
use Sub::Quote (); |
| 20 |
|
| 21 |
use overload |
| 22 |
'@{}' => sub { $_[0]->to_psgi }, |
| 23 |
'""' => sub { $_[0] }; |
| 24 |
|
| 25 |
my $WARNED_NO_CHARSET = 0; |
| 26 |
|
| 27 |
has mime_type => ( |
| 28 |
'is' => 'ro', |
| 29 |
'isa' => InstanceOf['Dancer2::Core::MIME'], |
| 30 |
'default' => sub { Dancer2::Core::MIME->new() }, |
| 31 |
); |
| 32 |
|
| 33 |
has headers => ( |
| 34 |
is => 'ro', |
| 35 |
isa => InstanceOf['HTTP::Headers'], |
| 36 |
lazy => 1, |
| 37 |
coerce => sub { |
| 38 |
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 |
: HTTP::Headers::Fast->new(@{$value}); |
| 44 |
}, |
| 45 |
default => sub { |
| 46 |
HTTP::Headers::Fast->new(); |
| 47 |
}, |
| 48 |
handles => [qw<header push_header>], |
| 49 |
); |
| 50 |
|
| 51 |
has strict_utf8 => ( |
| 52 |
is => 'ro', |
| 53 |
isa => Bool, |
| 54 |
default => sub {0}, |
| 55 |
); |
| 56 |
|
| 57 |
has log_cb => ( |
| 58 |
is => 'ro', |
| 59 |
isa => CodeRef, |
| 60 |
predicate => 'has_log_cb', |
| 61 |
); |
| 62 |
|
| 63 |
sub headers_to_array { |
| 64 |
my $self = shift; |
| 65 |
my $headers = shift || $self->headers; |
| 66 |
|
| 67 |
my @hdrs; |
| 68 |
$headers->scan( sub { |
| 69 |
my ( $k, $v ) = @_; |
| 70 |
$k =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP |
| 71 |
$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 |
$v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP |
| 75 |
$v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here |
| 76 |
push @hdrs, $k => $v; |
| 77 |
}); |
| 78 |
|
| 79 |
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 |
default => sub {0}, |
| 87 |
); |
| 88 |
|
| 89 |
sub pass { shift->has_passed(1) } |
| 90 |
|
| 91 |
has serializer => ( |
| 92 |
is => 'rw', |
| 93 |
isa => ConsumerOf ['Dancer2::Core::Role::Serializer'], |
| 94 |
); |
| 95 |
|
| 96 |
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 |
default => sub {0}, |
| 106 |
); |
| 107 |
|
| 108 |
has is_halted => ( |
| 109 |
is => 'rw', |
| 110 |
isa => Bool, |
| 111 |
default => sub {0}, |
| 112 |
); |
| 113 |
|
| 114 |
sub halt { |
| 115 |
my ( $self, $content ) = @_; |
| 116 |
$self->content( $content ) if @_ > 1; |
| 117 |
$self->is_halted(1); |
| 118 |
} |
| 119 |
|
| 120 |
has status => ( |
| 121 |
is => 'rw', |
| 122 |
isa => Num, |
| 123 |
default => sub {200}, |
| 124 |
lazy => 1, |
| 125 |
coerce => sub { Dancer2::Core::HTTP->status(shift) }, |
| 126 |
); |
| 127 |
|
| 128 |
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 |
default => sub {1}, |
| 139 |
); |
| 140 |
|
| 141 |
around content => sub { |
| 142 |
my ( $orig, $self ) = ( shift, shift ); |
| 143 |
|
| 144 |
# called as getter? |
| 145 |
@_ or return $self->$orig; |
| 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 |
$self->is_encoded(0); |
| 162 |
|
| 163 |
# No serializer defined; encode content |
| 164 |
$self->serializer |
| 165 |
or return $self->$orig( $self->encode_content(@_) ); |
| 166 |
|
| 167 |
# serialize content |
| 168 |
my $serialized = $self->serialize(@_); |
| 169 |
$self->is_encoded(1); # All serializers return byte strings |
| 170 |
return $self->$orig( defined $serialized ? $serialized : '' ); |
| 171 |
}; |
| 172 |
|
| 173 |
has default_content_type => ( |
| 174 |
is => 'rw', |
| 175 |
isa => Str, |
| 176 |
default => sub {'text/html'}, |
| 177 |
); |
| 178 |
|
| 179 |
sub encode_content { |
| 180 |
my ( $self, $content ) = @_; |
| 181 |
|
| 182 |
return $content if $self->is_encoded; |
| 183 |
|
| 184 |
# Apply default content type if none set. |
| 185 |
my $ct = $self->content_type || |
| 186 |
$self->content_type( $self->default_content_type ); |
| 187 |
|
| 188 |
return $content if $ct !~ /^text/; |
| 189 |
my $charset = $self->headers->content_type_charset; |
| 190 |
$charset = $self->charset |
| 191 |
if !defined $charset && $self->has_charset; |
| 192 |
if ( !defined $charset || $charset eq '' ) { |
| 193 |
return $content if !utf8::is_utf8($content); |
| 194 |
my $msg = 'Response contains characters but no charset is configured; assuming UTF-8'; |
| 195 |
$self->strict_utf8 |
| 196 |
and Carp::croak($msg); |
| 197 |
if ( !$WARNED_NO_CHARSET ) { |
| 198 |
$WARNED_NO_CHARSET = 1; |
| 199 |
if ( $self->has_log_cb ) { |
| 200 |
$self->log_cb->( warning => $msg ); |
| 201 |
} |
| 202 |
else { |
| 203 |
Carp::carp($msg); |
| 204 |
} |
| 205 |
} |
| 206 |
$charset = 'UTF-8'; |
| 207 |
} |
| 208 |
|
| 209 |
# we don't want to encode an empty string, it will break the output |
| 210 |
$content or return $content; |
| 211 |
|
| 212 |
$self->content_type("$ct; charset=$charset") |
| 213 |
if $ct !~ /charset/; |
| 214 |
|
| 215 |
$self->is_encoded(1); |
| 216 |
return Encode::encode( $charset, $content ); |
| 217 |
} |
| 218 |
|
| 219 |
sub new_from_plack { |
| 220 |
my ($class, $psgi_res) = @_; |
| 221 |
|
| 222 |
return Dancer2::Core::Response->new( |
| 223 |
status => $psgi_res->status, |
| 224 |
headers => $psgi_res->headers, |
| 225 |
content => $psgi_res->body, |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
sub new_from_array { |
| 230 |
my ($class, $arrayref) = @_; |
| 231 |
|
| 232 |
return Dancer2::Core::Response->new( |
| 233 |
status => $arrayref->[0], |
| 234 |
headers => $arrayref->[1], |
| 235 |
content => $arrayref->[2][0], |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
sub to_psgi { |
| 240 |
my ($self) = @_; |
| 241 |
|
| 242 |
my $headers = $self->headers; |
| 243 |
my $status = $self->status; |
| 244 |
|
| 245 |
Plack::Util::status_with_no_entity_body($status) |
| 246 |
and return [ $status, $self->headers_to_array($headers), [] ]; |
| 247 |
|
| 248 |
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 |
$content = $self->content('') if ! defined $content; |
| 254 |
|
| 255 |
if ( !$headers->header('Content-Length') && |
| 256 |
!$headers->header('Transfer-Encoding') && |
| 257 |
defined( my $content_length = length $content ) ) { |
| 258 |
$headers->push_header( 'Content-Length' => $content_length ); |
| 259 |
} |
| 260 |
|
| 261 |
# More defaults |
| 262 |
$self->content_type or $self->content_type($self->default_content_type); |
| 263 |
return [ $status, $self->headers_to_array($headers), [ $content ], ]; |
| 264 |
} |
| 265 |
|
| 266 |
# sugar for accessing the content_type header, with mimetype care |
| 267 |
sub content_type { |
| 268 |
my $self = shift; |
| 269 |
|
| 270 |
if ( scalar @_ > 0 ) { |
| 271 |
my $mimetype = $self->mime_type->name_or_type(shift); |
| 272 |
$self->header( 'Content-Type' => $mimetype ); |
| 273 |
return $mimetype; |
| 274 |
} |
| 275 |
else { |
| 276 |
return $self->header('Content-Type'); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
has _forward => ( |
| 281 |
is => 'rw', |
| 282 |
isa => HashRef, |
| 283 |
); |
| 284 |
|
| 285 |
sub forward { |
| 286 |
my ( $self, $uri, $params, $opts ) = @_; |
| 287 |
$self->_forward( { to_url => $uri, params => $params, options => $opts } ); |
| 288 |
} |
| 289 |
|
| 290 |
sub is_forwarded { |
| 291 |
my $self = shift; |
| 292 |
$self->_forward; |
| 293 |
} |
| 294 |
|
| 295 |
sub redirect { |
| 296 |
my ( $self, $destination, $status ) = @_; |
| 297 |
$self->status( $status || 302 ); |
| 298 |
|
| 299 |
# we want to stringify the $destination object (URI object) |
| 300 |
$self->header( 'Location' => "$destination" ); |
| 301 |
} |
| 302 |
|
| 303 |
sub error { |
| 304 |
my $self = shift; |
| 305 |
|
| 306 |
my $error = Dancer2::Core::Error->new( |
| 307 |
response => $self, |
| 308 |
@_, |
| 309 |
); |
| 310 |
|
| 311 |
$error->throw; |
| 312 |
return $error; |
| 313 |
} |
| 314 |
|
| 315 |
sub serialize { |
| 316 |
my ($self, $content) = @_; |
| 317 |
|
| 318 |
my $serializer = $self->serializer |
| 319 |
or return; |
| 320 |
|
| 321 |
$content = $serializer->serialize($content) |
| 322 |
or return; |
| 323 |
|
| 324 |
$self->content_type( $serializer->content_type ); |
| 325 |
return $content; |
| 326 |
} |
| 327 |
|
| 328 |
1; |
| 329 |
|
| 330 |
__END__ |
| 331 |
|
| 332 |
=pod |
| 333 |
|
| 334 |
=encoding UTF-8 |
| 335 |
|
| 336 |
=head1 NAME |
| 337 |
|
| 338 |
Dancer2::Core::Response - Response object for Dancer2 |
| 339 |
|
| 340 |
=head1 VERSION |
| 341 |
|
| 342 |
version 2.2.2 |
| 343 |
|
| 344 |
=head1 ATTRIBUTES |
| 345 |
|
| 346 |
=head2 is_encoded |
| 347 |
|
| 348 |
Flag to tell if the content has already been encoded. |
| 349 |
|
| 350 |
=head2 is_halted |
| 351 |
|
| 352 |
Flag to tell whether or not the response should continue to be processed. |
| 353 |
|
| 354 |
=head2 status |
| 355 |
|
| 356 |
The HTTP status for the response. |
| 357 |
|
| 358 |
=head2 content |
| 359 |
|
| 360 |
The content for the response, stored as a string. If a reference is passed, the |
| 361 |
response will try coerce it to a string via double quote interpolation. |
| 362 |
|
| 363 |
=head2 default_content_type |
| 364 |
|
| 365 |
Default mime type to use for the response Content-Type header |
| 366 |
if nothing was specified |
| 367 |
|
| 368 |
=head2 charset |
| 369 |
|
| 370 |
Charset to use when encoding C<text/*> responses. An empty value disables |
| 371 |
automatic encoding. |
| 372 |
|
| 373 |
=head2 headers |
| 374 |
|
| 375 |
The attribute that store the headers in a L<HTTP::Headers::Fast> object. |
| 376 |
|
| 377 |
That attribute coerces from ArrayRef and defaults to an empty L<HTTP::Headers::Fast> |
| 378 |
instance. |
| 379 |
|
| 380 |
=head1 METHODS |
| 381 |
|
| 382 |
=head2 pass |
| 383 |
|
| 384 |
Set has_passed to true. |
| 385 |
|
| 386 |
=head2 serializer() |
| 387 |
|
| 388 |
Returns the optional serializer object used to deserialize request parameters |
| 389 |
|
| 390 |
=head2 halt |
| 391 |
|
| 392 |
Shortcut to halt the current response by setting the is_halted flag. |
| 393 |
|
| 394 |
=head2 encode_content |
| 395 |
|
| 396 |
Encodes the stored content according to the stored L<content_type>. If the content_type |
| 397 |
is not a text format C<^text>, then no encoding will take place. |
| 398 |
|
| 399 |
Internally, it uses the L<is_encoded> flag to make sure that content is not encoded twice. |
| 400 |
|
| 401 |
If it encodes the content, then it will return the encoded content. In all other |
| 402 |
cases it returns C<false>. |
| 403 |
|
| 404 |
=head2 new_from_plack |
| 405 |
|
| 406 |
Creates a new response object from a L<Plack::Response> object. |
| 407 |
|
| 408 |
=head2 new_from_array |
| 409 |
|
| 410 |
Creates a new response object from a PSGI arrayref. |
| 411 |
|
| 412 |
=head2 to_psgi |
| 413 |
|
| 414 |
Converts the response object to a PSGI array. |
| 415 |
|
| 416 |
=head2 content_type($type) |
| 417 |
|
| 418 |
A little sugar for setting or accessing the content_type of the response, via the headers. |
| 419 |
|
| 420 |
=head2 redirect ($destination, $status) |
| 421 |
|
| 422 |
Sets a header in this response to give a redirect to $destination, and sets the |
| 423 |
status to $status. If $status is omitted, or false, then it defaults to a status of |
| 424 |
302. |
| 425 |
|
| 426 |
=head2 error( @args ) |
| 427 |
|
| 428 |
$response->error( message => "oops" ); |
| 429 |
|
| 430 |
Creates a L<Dancer2::Core::Error> object with the given I<@args> and I<throw()> |
| 431 |
it against the response object. Returns the error object. |
| 432 |
|
| 433 |
=head2 serialize( $content ) |
| 434 |
|
| 435 |
$response->serialize( $content ); |
| 436 |
|
| 437 |
Serialize and return $content with the response's serializer. |
| 438 |
set content-type accordingly. |
| 439 |
|
| 440 |
=head2 header($name) |
| 441 |
|
| 442 |
Return the value of the given header, if present. If the header has multiple |
| 443 |
values, returns the list of values if called in list context, the first one |
| 444 |
if in scalar context. |
| 445 |
|
| 446 |
=head2 push_header |
| 447 |
|
| 448 |
Add the header no matter if it already exists or not. |
| 449 |
|
| 450 |
$self->push_header( 'X-Wing' => '1' ); |
| 451 |
|
| 452 |
It can also be called with multiple values to add many times the same header |
| 453 |
with different values: |
| 454 |
|
| 455 |
$self->push_header( 'X-Wing' => 1, 2, 3 ); |
| 456 |
|
| 457 |
=head2 headers_to_array($headers) |
| 458 |
|
| 459 |
Convert the C<$headers> to a PSGI ArrayRef. |
| 460 |
|
| 461 |
If no C<$headers> are provided, it will use the current response headers. |
| 462 |
|
| 463 |
=head1 AUTHOR |
| 464 |
|
| 465 |
Dancer Core Developers |
| 466 |
|
| 467 |
=head1 COPYRIGHT AND LICENSE |
| 468 |
|
| 469 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 470 |
|
| 471 |
This is free software; you can redistribute it and/or modify it under |
| 472 |
the same terms as the Perl 5 programming language system itself. |
| 473 |
|
| 474 |
=cut |