Response.pm

filter: Statementstmt 99.0% 105 / 106 Branchbran 82.5% 33 / 40 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 96.0% 24 / 25 Podpod 100% 4 / 4 totaltotal 93.2% 180 / 193 CC 26 SCAR 13.5
CC
26
Cov
94%
_bake_cookie
9.2
BUILDARGS
7.0
_finalize_streaming
3.0
CRAP 3.9
« Request.pm ↑ Summary Request/Types.pm »
1 package Web::Response;
2 BEGIN {
3 21 ▶ $Web::Response::AUTHORITY = 'cpan:DOY';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 21 $Web::Response::VERSION = '0.11';
7 }
8 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # ABSTRACT: common response class for web frameworks
10  
11 21 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 ▶use Web::Request::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has status => (
19 is => 'rw',
20 isa => 'Web::Request::Types::HTTPStatus',
21 lazy => 1,
22 0 ▶ default => sub { confess "Status was not supplied" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 21 );
24  
25 has headers => (
26 is => 'rw',
27 isa => 'Web::Request::Types::HTTP::Headers',
28 lazy => 1,
29 coerce => 1,
30 41 ▶ default => sub { HTTP::Headers->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 21 handles => {
32 header => 'header',
33 content_length => 'content_length',
34 content_type => 'content_type',
35 content_encoding => 'content_encoding',
36 location => [ header => 'Location' ],
37 },
38 );
39  
40 has content => (
41 is => 'rw',
42 isa => 'Web::Request::Types::PSGIBody',
43 lazy => 1,
44 coerce => 1,
45 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 21 );
47  
48 21 has streaming_response => (
49 is => 'rw',
50 isa => 'CodeRef',
51 predicate => 'has_streaming_response',
52 );
53  
54 has cookies => (
55 traits => ['Hash'],
56 is => 'rw',
57 isa => 'HashRef[Str|HashRef[Str]]',
58 lazy => 1,
59 50 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 21 handles => {
61 has_cookies => 'count',
62 },
63 );
64  
65 21 has _encoding_obj => (
66 is => 'rw',
67 isa => 'Object',
68 predicate => 'has_encoding',
69 handles => {
70 encoding => 'name',
71 },
72 );
73  
74 sub BUILDARGS {
75 56 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=7 SCAR=19.5
Pod documented
76  
77 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
78 return {
79 status => $_[0][0],
80 12 ▶ (@{ $_[0] } > 1
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} > 2 ? : 0 12 12
81 ? (headers => $_[0][1])
82 : ()),
83 12 ▶ (@{ $_[0] } > 2
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} > 1 ? : 2 10 12
84 ? (content => $_[0][2])
85 : ()),
86 };
87 }
88 ▶ 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
89 return {
90 12 streaming_response => $_[0],
91 };
92 }
93 else {
94 32 return $class->SUPER::BUILDARGS(@_);
95 }
96 }
97  
98 sub redirect {
99 3 ▶ my $self = shift;
Subroutine redirect: called CC=2 SCAR=6.9
Pod documented
100 3 my ($url, $status) = @_;
101  
102 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
103 3 $self->location($url);
104 }
105  
106 sub finalize {
107 51 ▶ my $self = shift;
Subroutine finalize: called CC=3 SCAR=11.0
Pod documented
108  
109 51 ▶ return $self->_finalize_streaming
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_streaming_response 12 39 51
110 if $self->has_streaming_response;
111  
112 my $res = [
113 $self->status,
114 [
115 map {
116 39 my $k = $_;
117 map {
118 15 my $v = $_;
119 # replace LWS with a single SP
120 15 $v =~ s/\015\012[\040|\011]+/chr(32)/ge;
121 # remove CR and LF since the char is invalid here
122 15 $v =~ s/\015|\012//g;
123 15 ( $k => $v )
124 } $self->header($k);
125 } $self->headers->header_field_names
126 ],
127 $self->content
128 ];
129  
130 39 $self->_finalize_cookies($res);
131  
132 39 ▶ return $res unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 18 21 39
133  
134 return Plack::Util::response_cb($res, sub {
135 return sub {
136 29 ▶ my $chunk = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
137 29 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 21 8 29
138 8 return $self->_encode($chunk);
139 21 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
140 21 });
141 }
142  
143 sub to_app {
144 1 ▶ my $self = shift;
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
145 1 ▶ return sub { $self->finalize };
Subroutine __ANON__: called CC=1 SCAR=0.0
146 }
147  
148 sub _finalize_streaming {
149 12 ▶ my $self = shift;
Subroutine _finalize_streaming: called CC=3 SCAR=11.0
150  
151 12 my $streaming = $self->streaming_response;
152  
153 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
154 unless $self->has_encoding || $self->has_cookies;
155  
156 return Plack::Util::response_cb($streaming, sub {
157 10 ▶ my $res = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
158 10 $self->_finalize_cookies($res);
159 10 ▶ return unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 10 10
160 return sub {
161 25 ▶ my $chunk = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
162 25 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 10 15 25
163 15 return $self->_encode($chunk);
164 10 };
165 10 });
166 }
167  
168 sub _encode {
169 23 ▶ my $self = shift;
Subroutine _encode: called CC=2 SCAR=7.0
170 23 my ($content) = @_;
171 23 ▶ return $content unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 23 23
172 23 return $self->_encoding_obj->encode($content);
173 }
174  
175 sub _finalize_cookies {
176 49 ▶ my $self = shift;
Subroutine _finalize_cookies: called CC=2 SCAR=6.9
177 49 my ($res) = @_;
178  
179 49 my $cookies = $self->cookies;
180 49 for my $name (keys %$cookies) {
181 23 push @{ $res->[1] }, (
182 23 'Set-Cookie' => $self->_bake_cookie($name, $cookies->{$name}),
183 );
184 }
185  
186 49 $self->cookies({});
187 }
188  
189 sub _bake_cookie {
190 23 ▶ my $self = shift;
Subroutine _bake_cookie: called CC=9 SCAR=22.2
191 23 my ($name, $val) = @_;
192  
193 23 ▶ return '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 23 23
194 23 ▶ $val = { value => $val }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 0 23 23
195 unless ref($val) eq 'HASH';
196  
197 my @cookie = (
198 URI::Escape::uri_escape($name)
199 . '='
200 . URI::Escape::uri_escape($val->{value})
201 23 );
202  
203 push @cookie, 'domain=' . $val->{domain}
204 23 ▶ if defined($val->{domain});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"domain"} 9 14 23
205 push @cookie, 'path=' . $val->{path}
206 23 ▶ if defined($val->{path});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"path"} 9 14 23
207 push @cookie, 'expires=' . $self->_date($val->{expires})
208 23 ▶ if defined($val->{expires});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"expires"} 9 14 23
209 push @cookie, 'max-age=' . $val->{'max-age'}
210 23 ▶ if defined($val->{'max-age'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $val->{"max-age"} 7 16 23
211 push @cookie, 'secure'
212 23 ▶ if $val->{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"secure"} 0 23 23
213 push @cookie, 'HttpOnly'
214 23 ▶ if $val->{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"httponly"} 0 23 23
215  
216 23 return join '; ', @cookie;
217 }
218  
219 # XXX DateTime?
220 21 my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
221 21 my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
222  
223 sub _date {
224 9 ▶ my $self = shift;
Subroutine _date: called CC=2 SCAR=6.9
225 9 my ($expires) = @_;
226  
227 9 ▶ return $expires unless $expires =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $expires =~ /^\d+$/ 2 7 9
228  
229 7 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires);
230 7 $year += 1900;
231  
232 7 return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
233 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
234 }
235  
236 21 __PACKAGE__->meta->make_immutable;
237 21 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239  
240  
241 21 1;
242  
243 __END__
244  
245 =pod
246  
247 =head1 NAME
248  
249 Web::Response - common response class for web frameworks
250  
251 =head1 VERSION
252  
253 version 0.11
254  
255 =head1 SYNOPSIS
256  
257 use Web::Request;
258  
259 my $app = sub {
260 my ($env) = @_;
261 my $req = Web::Request->new_from_env($env);
262 # ...
263 return $req->new_response(status => 404)->finalize;
264 };
265  
266 =head1 DESCRIPTION
267  
268 Web::Response is a response class for L<PSGI> applications. Generally, you will
269 want to create instances of this class via C<new_response> on the request
270 object, since that allows a framework which subclasses L<Web::Request> to also
271 return an appropriate subclass of Web::Response.
272  
273 All attributes on Web::Response objects are writable, and the final state of
274 them will be used to generate a real L<PSGI> response when C<finalize> is
275 called.
276  
277 =head1 METHODS
278  
279 =head2 status($status)
280  
281 Sets (and returns) the status attribute, as described above.
282  
283 =head2 headers($headers)
284  
285 Sets (and returns) the headers attribute, as described above.
286  
287 =head2 header($name, $val)
288  
289 Shortcut for C<< $ret->headers->header($name, $val) >>.
290  
291 =head2 content_length($length)
292  
293 Shortcut for C<< $ret->headers->content_length($length) >>.
294  
295 =head2 content_type($type)
296  
297 Shortcut for C<< $ret->headers->content_type($type) >>.
298  
299 =head2 content_encoding($encoding)
300  
301 Shortcut for C<< $ret->headers->content_encoding($encoding) >>.
302  
303 =head2 location($location)
304  
305 Shortcut for C<< $ret->headers->header('Location', $location) >>.
306  
307 =head2 content($content)
308  
309 Sets (and returns) the C<content> attribute, as described above.
310  
311 =head2 streaming_response
312  
313 Sets and returns the streaming response coderef, as described above.
314  
315 =head2 has_streaming_response
316  
317 Returns whether or not a streaming response was provided.
318  
319 =head2 cookies($cookies)
320  
321 Sets (and returns) the C<cookies> attribute, as described above.
322  
323 =head2 has_cookies
324  
325 Returns whether or not any cookies have been defined.
326  
327 =head2 redirect($location, $status)
328  
329 Sets the C<Location> header to $location, and sets the status code to $status
330 (defaulting to 302 if not given).
331  
332 =head2 finalize
333  
334 Returns a valid L<PSGI> response, based on the values given. This can be either
335 an arrayref or a coderef, depending on if an immediate or streaming response
336 was provided. If both were provided, the streaming response will be preferred.
337  
338 =head2 to_app
339  
340 Returns a PSGI application which just returns the response in this object
341 directly.
342  
343 =head1 CONSTRUCTOR
344  
345 =head2 new(%params)
346  
347 Returns a new Web::Response object. Valid parameters are:
348  
349 =over 4
350  
351 =item status
352  
353 The HTTP status code for the response.
354  
355 =item headers
356  
357 The headers to return with the response. Can be provided as an arrayref, a
358 hashref, or an L<HTTP::Headers> object. Defaults to an L<HTTP::Headers> object
359 with no contents.
360  
361 =item content
362  
363 The content of the request. Can be provided as a string, an object which
364 overloads C<"">, an arrayref containing a list of either of those, a
365 filehandle, or an object that implements the C<getline> and C<close> methods.
366 Defaults to C<[]>.
367  
368 =item streaming_response
369  
370 Instead of C<status>/C<headers>/C<content>, you can provide a coderef which
371 implements the streaming response API described in the L<PSGI> specification.
372  
373 =item cookies
374  
375 A hashref of cookies to return with the response. The values in the hashref can
376 either be the string values of the cookies, or a hashref whose keys can be any
377 of C<value>, C<domain>, C<path>, C<expires>, C<max-age>, C<secure>,
378 C<httponly>. In addition to the date format that C<expires> normally uses,
379 C<expires> can also be provided as a UNIX timestamp (an epoch time, as returned
380 from C<time>). Defaults to C<{}>.
381  
382 =back
383  
384 In addition, a single parameter which is a valid PSGI response (a three element
385 arrayref or a coderef) will also be accepted, and will populate the attributes
386 as appropriate. If an arrayref is passed, the first element will be stored as
387 the C<status> attribute, the second element if it exists will be interpreted as
388 in the PSGI specification to create an L<HTTP::Headers> object and stored in
389 the C<headers> attribute, and the third element if it exists will be stored as
390 the C<content> attribute. If a coderef is passed, it will be stored in the
391 C<streaming_response> attribute.
392  
393 =head1 AUTHOR
394  
395 Jesse Luehrs <doy@tozt.net>
396  
397 =head1 COPYRIGHT AND LICENSE
398  
399 This software is copyright (c) 2013 by Jesse Luehrs.
400  
401 This is free software; you can redistribute it and/or modify it under
402 the same terms as the Perl 5 programming language system itself.
403  
404 =cut
« Request.pm ↑ Summary Request/Types.pm »