Response.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 82.5% 33 / 40 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 95.8% 23 / 24 Podpod 100% 4 / 4 totaltotal 93.1% 178 / 191 CC 26 SCAR 13.7
CC
26
Cov
94%
_bake_cookie
9.2
BUILDARGS
7.0
_finalize_streaming
3.0
CRAP 3.9
↑ Summary Request.pm »
1 package Web::Response;
2 21 our $AUTHORITY = 'cpan:PTC';
3 21 $Web::Response::VERSION = '0.12';
4 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: common response class for web frameworks
6  
7 21 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 ▶use Web::Request::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 has status => (
15 is => 'rw',
16 isa => 'Web::Request::Types::HTTPStatus',
17 lazy => 1,
18 0 ▶ default => sub { confess "Status was not supplied" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 21 );
20  
21 has headers => (
22 is => 'rw',
23 isa => 'Web::Request::Types::HTTP::Headers',
24 lazy => 1,
25 coerce => 1,
26 41 ▶ default => sub { HTTP::Headers->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 21 handles => {
28 header => 'header',
29 content_length => 'content_length',
30 content_type => 'content_type',
31 content_encoding => 'content_encoding',
32 location => [ header => 'Location' ],
33 },
34 );
35  
36 has content => (
37 is => 'rw',
38 isa => 'Web::Request::Types::PSGIBody',
39 lazy => 1,
40 coerce => 1,
41 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 21 );
43  
44 21 has streaming_response => (
45 is => 'rw',
46 isa => 'CodeRef',
47 predicate => 'has_streaming_response',
48 );
49  
50 has cookies => (
51 traits => ['Hash'],
52 is => 'rw',
53 isa => 'HashRef[Str|HashRef[Str]]',
54 lazy => 1,
55 50 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 21 handles => {
57 has_cookies => 'count',
58 },
59 );
60  
61 21 has _encoding_obj => (
62 is => 'rw',
63 isa => 'Object',
64 predicate => 'has_encoding',
65 handles => {
66 encoding => 'name',
67 },
68 );
69  
70 56 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=7 SCAR=19.5
Pod documented
71 56 my $class = shift;
72  
73 56 ▶ if (@_ == 1 && ref($_[0]) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'ARRAY') { } 12 44 56
elsif (@_ == 1 and ref $_[0] eq 'CODE') { } 12 32 44
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
321212
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
74 return {
75 status => $_[0][0],
76 12 ▶ (@{ $_[0] } > 1
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} > 2 ? : 0 12 12
77 ? (headers => $_[0][1])
78 : ()),
79 12 ▶ (@{ $_[0] } > 2
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} > 1 ? : 2 10 12
80 ? (content => $_[0][2])
81 : ()),
82 };
83 }
84 ▶ elsif (@_ == 1 && ref($_[0]) eq 'CODE') {
Condition66%2/3
@_ == 1 and ref $_[0] eq "CODE"
!ll&&!rl&&r
32012
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "CODE"
@_ == 1 ref $_[0] eq "CODE"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
85 return {
86 12 streaming_response => $_[0],
87 };
88 }
89 else {
90 32 return $class->SUPER::BUILDARGS(@_);
91 }
92 }
93  
94 3 ▶sub redirect {
Subroutine redirect: called CC=2 SCAR=6.9
Pod documented
95 3 my $self = shift;
96 3 my ($url, $status) = @_;
97  
98 3 ▶ $self->status($status || 302);
Condition100%2/2
$status || 302
l!l
21
MC/DC100%1/1
$status || 302
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
99 3 $self->location($url);
100 }
101  
102 51 ▶sub finalize {
Subroutine finalize: called CC=3 SCAR=11.0
Pod documented
103 51 my $self = shift;
104  
105 51 ▶ return $self->_finalize_streaming
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_streaming_response 12 39 51
106 if $self->has_streaming_response;
107  
108 my $res = [
109 $self->status,
110 [
111 map {
112 39 my $k = $_;
113 map {
114 15 my $v = $_;
115 # replace LWS with a single SP
116 15 $v =~ s/\015\012[\040|\011]+/chr(32)/ge;
117 # remove CR and LF since the char is invalid here
118 15 $v =~ s/\015|\012//g;
119 15 ( $k => $v )
120 } $self->header($k);
121 } $self->headers->header_field_names
122 ],
123 $self->content
124 ];
125  
126 39 $self->_finalize_cookies($res);
127  
128 39 ▶ return $res unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 18 21 39
129  
130 21 ▶ return Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 29 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
132 29 my $chunk = shift;
133 29 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 21 8 29
134 8 return $self->_encode($chunk);
135 21 };
136 21 });
137 }
138  
139 1 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
140 1 my $self = shift;
141 1 ▶ return sub { $self->finalize };
Subroutine __ANON__: called CC=1 SCAR=0.0
142 }
143  
144 12 ▶sub _finalize_streaming {
Subroutine _finalize_streaming: called CC=3 SCAR=11.0
145 12 my $self = shift;
146  
147 12 my $streaming = $self->streaming_response;
148  
149 12 ▶ return $streaming
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding or $self->has_cookies 2 10 12
Condition66%2/3
$self->has_encoding or $self->has_cookies
l!l&&r!l&&!r
1002
MC/DC50%1/2
$self->has_encoding or $self->has_cookies
$self->has_encoding $self->has_cookies
Truth table66%2/3
A or B
A= $self->has_encodingB= $self->has_cookies
ABresult
1X1
011
000
150 unless $self->has_encoding || $self->has_cookies;
151  
152 10 ▶ return Plack::Util::response_cb($streaming, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
153 10 my $res = shift;
154 10 $self->_finalize_cookies($res);
155 10 ▶ return unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 10 10
156 25 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
157 25 my $chunk = shift;
158 25 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 10 15 25
159 15 return $self->_encode($chunk);
160 10 };
161 10 });
162 }
163  
164 23 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=7.0
165 23 my $self = shift;
166 23 my ($content) = @_;
167 23 ▶ return $content unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 23 23
168 23 return $self->_encoding_obj->encode($content);
169 }
170  
171 49 ▶sub _finalize_cookies {
Subroutine _finalize_cookies: called CC=2 SCAR=6.9
172 49 my $self = shift;
173 49 my ($res) = @_;
174  
175 49 my $cookies = $self->cookies;
176 49 for my $name (keys %$cookies) {
177 23 push @{ $res->[1] }, (
178 23 'Set-Cookie' => $self->_bake_cookie($name, $cookies->{$name}),
179 );
180 }
181  
182 49 $self->cookies({});
183 }
184  
185 23 ▶sub _bake_cookie {
Subroutine _bake_cookie: called CC=9 SCAR=22.2
186 23 my $self = shift;
187 23 my ($name, $val) = @_;
188  
189 23 ▶ return '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 23 23
190 23 ▶ $val = { value => $val }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 0 23 23
191 unless ref($val) eq 'HASH';
192  
193 my @cookie = (
194 URI::Escape::uri_escape($name)
195 . '='
196 . URI::Escape::uri_escape($val->{value})
197 23 );
198  
199 push @cookie, 'domain=' . $val->{domain}
200 23 ▶ if defined($val->{domain});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"domain"} 9 14 23
201 push @cookie, 'path=' . $val->{path}
202 23 ▶ if defined($val->{path});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"path"} 9 14 23
203 push @cookie, 'expires=' . $self->_date($val->{expires})
204 23 ▶ if defined($val->{expires});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"expires"} 9 14 23
205 push @cookie, 'max-age=' . $val->{'max-age'}
206 23 ▶ if defined($val->{'max-age'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"max-age"} 7 16 23
207 push @cookie, 'secure'
208 23 ▶ if $val->{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"secure"} 0 23 23
209 push @cookie, 'HttpOnly'
210 23 ▶ if $val->{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"httponly"} 0 23 23
211  
212 23 return join '; ', @cookie;
213 }
214  
215 # XXX DateTime?
216 21 my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
217 21 my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
218  
219 9 ▶sub _date {
Subroutine _date: called CC=2 SCAR=6.9
220 9 my $self = shift;
221 9 my ($expires) = @_;
222  
223 9 ▶ return $expires unless $expires =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $expires =~ /^\d+$/ 2 7 9
224  
225 7 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires);
226 7 $year += 1900;
227  
228 7 return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
229 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
230 }
231  
232 21 __PACKAGE__->meta->make_immutable;
233 21 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
234  
235  
236  
237 21 1;
238  
239 __END__
↑ Summary Request.pm »