blib/lib/Dancer/Response.pm

filter: Statementstmt 96.2% 102 / 106 Branchbran 77.7% 14 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 27 / 27 Podpod 66.6% 10 / 15 totaltotal 91.2% 156 / 171 SCAR 25.7
CC
13
Cov
93%
halt
4.1
push_header
3.1
status
3.1
CRAP 13.1
« blib/lib/Dancer/Session/Abstract.pm ↑ Summary blib/lib/Dancer/Serializer/JSON.pm »
1 package Dancer::Response;
2 221 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Response object for Dancer
4 221 $Dancer::Response::VERSION = '1.3520';
5 221 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 221 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 221 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 221 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 221 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 221 ▶use Dancer::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 221 ▶use Dancer::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 221 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 221 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 221 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 221 ▶use Dancer::Continuation::Halted;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 221 __PACKAGE__->attributes(qw/content pass streamed/);
20  
21 # constructor
22 sub init {
23 1035 ▶ my ( $self, %args ) = @_;
Subroutine init: called CC=2 SCAR=6.9
Pod documented
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} || [] });
Branch: 1 branch
BranchTrueFalseTotal
$args{"headers"} or [] 541 494 1035
33 1035 Dancer::SharedData->response($self);
34 }
35  
36 # helpers for the route handlers
37 sub exists {
38 484 ▶ my $self = shift;
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
39 484 return length($self->content);
40 }
41  
42 sub status {
43 1237 ▶ my $self = shift;
Subroutine status: called CC=3 SCAR=11.3
Pod documented
44  
45 1237 ▶ if (scalar @_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) { } 41 1196 1237
46 41 my $status = shift;
47 41 my $numeric_status = Dancer::HTTP->status($status);
48 41 ▶ if ($numeric_status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric_status) { } 41 0 41
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 sub content_type {
60 494 ▶ my $self = shift;
Subroutine content_type: called CC=2 SCAR=6.9
Pod documented
61  
62 494 ▶ if (scalar @_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) { } 25 469 494
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 sub has_passed {
71 512 ▶ my $self = shift;
Subroutine has_passed: called CC=1 SCAR=0.0
Pod documented
72 512 return $self->pass;
73 }
74  
75 sub forward {
76 15 ▶ my ($self, $uri, $params, $opts) = @_;
Subroutine forward: called CC=1 SCAR=0.0
Pod undocumented
77 15 $self->{forward} = { to_url => $uri,
78 params => $params,
79 options => $opts };
80 }
81  
82 sub is_forwarded {
83 524 ▶ my $self = shift;
Subroutine is_forwarded: called CC=1 SCAR=0.0
Pod undocumented
84 524 $self->{forward};
85 }
86  
87 sub _already_encoded {
88 3 ▶ my $self = shift;
Subroutine _already_encoded: called CC=1 SCAR=0.0
89 3 $self->{encoded};
90 }
91  
92 sub halt {
93 13 ▶ my ($self, $content) = @_;
Subroutine halt: called CC=4 SCAR=14.0
Pod documented
94  
95 13 ▶ if ( blessed($content) && $content->isa('Dancer::Response') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($content) and $content->isa('Dancer::Response')) { } 1 12 13
Condition66%2/3
builtin::blessed($content) and $content->isa("Dancer::Response")
!ll&&!rl&&r
1201
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $content 12 0 12
101 12 $self->{halted} = 1;
102 }
103 }
104  
105 sub halted {
106 301 ▶ my $self = shift;
Subroutine halted: called CC=1 SCAR=0.0
Pod documented
107 return $self->{halted}
108 301 }
109  
110 sub header {
111 1570 ▶ my $self = shift;
Subroutine header: called CC=2 SCAR=6.9
Pod documented
112 1570 my $header = shift;
113  
114 1570 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1015 555 1570
115 1015 $self->{headers}->header( $header => @_ );
116 }
117 else {
118 555 return $self->{headers}->header($header);
119 }
120 }
121  
122 sub push_header {
123 25 ▶ my $self = shift;
Subroutine push_header: called CC=3 SCAR=11.4
Pod undocumented
124 25 my $header = shift;
125  
126 25 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 25 0 25
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 sub headers {
137 35 ▶ my $self = shift;
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
138 35 $self->{headers}->header(@_);
139 }
140  
141 sub headers_to_array {
142 505 ▶ my $self = shift;
Subroutine headers_to_array: called CC=1 SCAR=0.0
Pod documented
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 sub add_cookie {
166 44 ▶ my ($self, $name, $cookie) = @_;
Subroutine add_cookie: called CC=2 SCAR=7.6
Pod undocumented
167 44 ▶ if ($self->{_built_cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_built_cookies"}) 0 44 44
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 sub build_cookie_headers {
177 505 ▶ my $self = shift;
Subroutine build_cookie_headers: called CC=2 SCAR=6.9
Pod undocumented
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 221 1;
187  
188 __END__
« blib/lib/Dancer/Session/Abstract.pm ↑ Summary blib/lib/Dancer/Serializer/JSON.pm »