| 1 | package Dancer::Response; | ||||||||||||||||||||||||||||
| 2 | 230 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: Response object for Dancer | ||||||||||||||||||||||||||||
| 4 | 230 | $Dancer::Response::VERSION = '1.3522'; | |||||||||||||||||||||||||||
| 5 | 230 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 230 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 230 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 230 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 230 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 230 | ▶ | use Dancer::HTTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 230 | ▶ | use Dancer::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 230 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 230 | ▶ | use Dancer::SharedData; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 230 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 230 | ▶ | use Dancer::Continuation::Halted; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 230 | __PACKAGE__->attributes(qw/content pass streamed/); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # constructor | ||||||||||||||||||||||||||||
| 22 | 1035 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 1035 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 24 | 1035 | $self->attributes_defaults( | |||||||||||||||||||||||||||
| 25 | status => 200, | ||||||||||||||||||||||||||||
| 26 | content => '', | ||||||||||||||||||||||||||||
| 27 | pass => 0, | ||||||||||||||||||||||||||||
| 28 | halted => 0, | ||||||||||||||||||||||||||||
| 29 | forward => '', | ||||||||||||||||||||||||||||
| 30 | encoded => 0, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | 1035 | ▶ | $self->{headers} = HTTP::Headers->new(@{ $args{headers} || [] }); | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'headers'} || []
MC/DC100%1/1
$args{'headers'} || []
$args{'headers'}
Truth table100%2/2
A
A= $args{'headers'}
| |||||||||||||||||||||||||||||
| 33 | 1035 | Dancer::SharedData->response($self); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # helpers for the route handlers | ||||||||||||||||||||||||||||
| 37 | 484 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 484 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 484 | return length($self->content); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1237 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 1237 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1237 | ▶ | if (scalar @_ > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 41 | my $status = shift; | |||||||||||||||||||||||||||
| 47 | 41 | my $numeric_status = Dancer::HTTP->status($status); | |||||||||||||||||||||||||||
| 48 | 41 | ▶ | if ($numeric_status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 41 | return $self->{status} = $numeric_status; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | carp "Unrecognised HTTP status $status"; | |||||||||||||||||||||||||||
| 52 | 0 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 1196 | return $self->{status}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 494 | ▶ | sub content_type { | ||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 494 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 494 | ▶ | if (scalar @_ > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 25 | my $mimetype = Dancer::MIME->instance(); | |||||||||||||||||||||||||||
| 64 | 25 | $self->header('Content-Type' => $mimetype->name_or_type(shift)); | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 469 | return $self->header('Content-Type'); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 512 | ▶ | sub has_passed { | ||||||||||||||||||||||||||
|
Subroutine
has_passed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 512 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 512 | return $self->pass; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 15 | ▶ | sub forward { | ||||||||||||||||||||||||||
|
Subroutine
forward: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 15 | my ($self, $uri, $params, $opts) = @_; | |||||||||||||||||||||||||||
| 77 | 15 | $self->{forward} = { to_url => $uri, | |||||||||||||||||||||||||||
| 78 | params => $params, | ||||||||||||||||||||||||||||
| 79 | options => $opts }; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 524 | ▶ | sub is_forwarded { | ||||||||||||||||||||||||||
|
Subroutine
is_forwarded: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 524 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 524 | $self->{forward}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | sub _already_encoded { | ||||||||||||||||||||||||||
|
Subroutine
_already_encoded: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 3 | $self->{encoded}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 13 | ▶ | sub halt { | ||||||||||||||||||||||||||
|
Subroutine
halt: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 13 | my ($self, $content) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 13 | ▶ | if ( blessed($content) && $content->isa('Dancer::Response') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($content) and $content->isa("Dancer::Response")
MC/DC50%1/2
builtin::blessed($content) and $content->isa("Dancer::Response")
builtin::blessed($content)
$content->isa("Dancer::Response")
Truth table66%2/3
A and B
A= builtin::blessed($content)B= $content->isa("Dancer::Response")
| |||||||||||||||||||||||||||||
| 96 | 1 | $content->{halted} = 1; | |||||||||||||||||||||||||||
| 97 | 1 | Dancer::SharedData->response($content); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 12 | ▶ | $self->content($content) if defined $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 12 | $self->{halted} = 1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 301 | ▶ | sub halted { | ||||||||||||||||||||||||||
|
Subroutine
halted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 301 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | return $self->{halted} | ||||||||||||||||||||||||||||
| 108 | 301 | } | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1570 | ▶ | sub header { | ||||||||||||||||||||||||||
|
Subroutine
header: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 1570 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 1570 | my $header = shift; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1570 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1015 | $self->{headers}->header( $header => @_ ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||
| 118 | 555 | return $self->{headers}->header($header); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 25 | ▶ | sub push_header { | ||||||||||||||||||||||||||
|
Subroutine
push_header: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 25 | my $header = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 25 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 25 | foreach my $h(@_) { | |||||||||||||||||||||||||||
| 128 | 25 | $self->{headers}->push_header( $header => $h ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 0 | return $self->{headers}->header($header); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 35 | ▶ | sub headers { | ||||||||||||||||||||||||||
|
Subroutine
headers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 35 | $self->{headers}->header(@_); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 505 | ▶ | sub headers_to_array { | ||||||||||||||||||||||||||
|
Subroutine
headers_to_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 505 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Time to finalise cookie headers, now | ||||||||||||||||||||||||||||
| 145 | 505 | $self->build_cookie_headers; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | my $headers = [ | ||||||||||||||||||||||||||||
| 148 | map { | ||||||||||||||||||||||||||||
| 149 | 352 | my $k = $_; | |||||||||||||||||||||||||||
| 150 | map { | ||||||||||||||||||||||||||||
| 151 | 369 | my $v = $_; | |||||||||||||||||||||||||||
| 152 | 369 | $v =~ s/^(.+)\r?\n(.*)$/$1\r\n $2/; | |||||||||||||||||||||||||||
| 153 | 369 | ( $k => $v ) | |||||||||||||||||||||||||||
| 154 | 352 | } $self->{headers}->header($_); | |||||||||||||||||||||||||||
| 155 | } $self->{headers}->header_field_names | ||||||||||||||||||||||||||||
| 156 | 505 | ]; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 505 | return $headers; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Given a cookie name and object, add it to the cookies we're going to send. | ||||||||||||||||||||||||||||
| 162 | # Stores them in a hashref within the response object until the response is | ||||||||||||||||||||||||||||
| 163 | # being built, so that, if the same cookie is set multiple times, only the last | ||||||||||||||||||||||||||||
| 164 | # value given to it will appear in a Set-Cookie header. | ||||||||||||||||||||||||||||
| 165 | 44 | ▶ | sub add_cookie { | ||||||||||||||||||||||||||
|
Subroutine
add_cookie: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 44 | my ($self, $name, $cookie) = @_; | |||||||||||||||||||||||||||
| 167 | 44 | ▶ | if ($self->{_built_cookies}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | die "Too late to set another cookie, headers already built"; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 44 | $self->{_cookies}{$name} = $cookie; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # When the response is about to be rendered, that's when we build up the | ||||||||||||||||||||||||||||
| 175 | # Set-Cookie headers | ||||||||||||||||||||||||||||
| 176 | 505 | ▶ | sub build_cookie_headers { | ||||||||||||||||||||||||||
|
Subroutine
build_cookie_headers: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 505 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | 505 | for my $name (keys %{ $self->{_cookies} }) { | |||||||||||||||||||||||||||
| 179 | 14 | my $header = $self->{_cookies}{$name}->to_header; | |||||||||||||||||||||||||||
| 180 | 14 | $self->push_header( | |||||||||||||||||||||||||||
| 181 | 'Set-Cookie' => $header, | ||||||||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 505 | $self->{_built_cookies}++; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 230 | 1; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||||||||||