Prancer/Response.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 76.6% 23 / 30 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 18 / 18 Podpod 85.7% 6 / 7 totaltotal 87.3% 145 / 166 CC 24 SCAR 15.7
CC
24
Cov
88%
cookie
10.1
finalize
7.0
_bake_cookie
6.0
CRAP 4.8
« Prancer/Core.pm ↑ Summary Prancer/Plugin.pm »
1 package Prancer::Response;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our $VERSION = '1.05';
8  
9 3 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # even though this *should* work automatically, it was not
16 3 our @CARP_NOT = qw(Prancer Try::Tiny);
17  
18 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 6 my $class = shift;
20 6 return bless({
21 '_response' => Plack::Response->new(),
22 '_cookies' => Hash::MultiValue->new(),
23 '_headers' => Hash::MultiValue->new(),
24 }, $class);
25 }
26  
27 # set a single header
28 # or get all the keys
29 3 ▶sub header {
Subroutine header: called CC=4 SCAR=14.0
Pod documented
30 3 my $self = shift;
31  
32 # if we are given multiple args assume they are headers in key/value pairs
33 3 ▶ croak "odd number of headers" unless (@_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 3 3
34 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
35 4 my ($key, $value) = (shift(@_), shift(@_));
36 4 ▶ $self->headers->add($key => [@{$self->headers->get_all($key) || []}, $value]);
Condition50%1/2
$self->headers->get_all($key) || []
l!l
04
MC/DC0%0/1
$self->headers->get_all($key) || []
$self->headers->get_all($key)
Truth table50%1/2
A
A= $self->headers->get_all($key)
Aresult
11
00
37 }
38  
39 3 return;
40 }
41  
42 # get all the headers that have been set
43 18 ▶sub headers {
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
44 18 my $self = shift;
45 18 return $self->{'_headers'};
46 }
47  
48 # set a single cookie
49 # or get all the keys
50 3 ▶sub cookie {
Subroutine cookie: called CC=7 SCAR=23.2
Pod documented
51 3 my $self = shift;
52  
53 # return the keys if nothing is asked for
54 0 ▶ return keys(%{$self->cookies()}) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
55  
56 # if given just a key then return that
57 3 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 3 3
58 0 my $key = shift;
59 0 ▶ return $self->cookies->{$key} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
60 0 return $self->cookies->get_all($key);
61 }
62  
63 # if we are given multiple args assume they are cookies in key/value pairs
64 3 ▶ croak "odd number of cookies" unless (@_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 3 3
65 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
66 3 my ($key, $value) = (shift(@_), shift(@_));
67  
68 # take a moment to validate the cookie
69 # TODO
70  
71 3 ▶ $self->cookies->add($key => [@{$self->cookies->get_all($key) || []}, $value]);
Condition50%1/2
$self->cookies->get_all($key) || []
l!l
03
MC/DC0%0/1
$self->cookies->get_all($key) || []
$self->cookies->get_all($key)
Truth table50%1/2
A
A= $self->cookies->get_all($key)
Aresult
11
00
72 }
73  
74 3 return;
75 }
76  
77 15 ▶sub cookies {
Subroutine cookies: called CC=1 SCAR=0.0
Pod documented
78 15 my $self = shift;
79 15 return $self->{'_cookies'};
80 }
81  
82 2 ▶sub body {
Subroutine body: called CC=3 SCAR=11.0
Pod documented
83 2 my $self = shift;
84  
85 # make the response be a callback
86 2 ▶ if (ref($_[0]) && ref($_[0]) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "CODE") 1 1 2
Condition66%2/3
ref $_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $_[0] and ref $_[0] eq "CODE"
ref $_[0] ref $_[0] eq "CODE"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
87 1 $self->{'_callback'} = shift;
88 1 return;
89 }
90  
91 # just add this to the body, whatever it is
92 1 return $self->{'_response'}->body(@_);
93 }
94  
95 6 ▶sub finalize {
Subroutine finalize: called CC=7 SCAR=19.5
Pod documented
96 6 my ($self, $status) = @_;
97 6 $self->{'_response'}->status($status);
98  
99 # build the headers using something normal and then add them to the
100 # response later. for whatever reason plack is being weird about this when
101 # the same header name is being used more than once. though, i might be
102 # doing it wrong.
103 6 my $headers = HTTP::Headers::Fast->new();
104  
105 # add normal headers
106 6 for my $key (keys %{$self->headers()}) {
107 3 for my $value (@{$self->headers->get_all($key)}) {
108 3 $headers->push_header($key => $value);
109 }
110 }
111  
112 # add cookies
113 6 for my $key (keys %{$self->cookies()}) {
114 3 for my $value (@{$self->cookies->get_all($key)}) {
115 3 $headers->push_header("Set-Cookie" => $self->_bake_cookie($key, $value));
116 }
117 }
118  
119 # now add the headers we've compiled
120 6 $self->{'_response'}->headers($headers);
121  
122 6 ▶ if (ref($self->{'_callback'}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"_callback"} and ref $self->{"_callback"} eq "CODE") 1 5 6
Condition66%2/3
ref $self->{"_callback"} and ref $self->{"_callback"} eq "CODE"
!ll&&!rl&&r
501
MC/DC50%1/2
ref $self->{"_callback"} and ref $self->{"_callback"} eq "CODE"
ref $self->{"_callback"} ref $self->{"_callback"} eq "CODE"
Truth table66%2/3
A and B
A= ref $self->{"_callback"}B= ref $self->{"_callback"} eq "CODE"
ABresult
0X0
100
111
123 ref($self->{'_callback'}) eq "CODE") {
124  
125 # the extra array ref brackets around the sub are because Web::Simple,
126 # which we use as the router, will not do a callback without them. by
127 # returning an array ref we are telling Web::Simple that we are giving
128 # it a PSGI response. from the Web::Simple docs:
129 #
130 # Well, a sub is a valid PSGI response too (for ultimate streaming
131 # and async cleverness). If you want to return a PSGI sub you have
132 # to wrap it into an array ref.
133 #
134 1 ▶ return [ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 my $responder = shift;
136  
137 # this idiom here borrows heavily from the documentation on this
138 # blog post, by tatsuhiko miyagawa:
139 #
140 # http://bulknews.typepad.com/blog/2009/10/psgiplack-streaming-is-now-complete.html
141 #
142 # this effectively allows the user of this api to stream data to
143 # the client.
144  
145 # finalize will always return a three element array. the third
146 # element is supposed to be the body. because we don't have a body
147 # yet (it's in the callback), this uses splice to exclude the third
148 # element (aka the body) and just return the status code and the
149 # list of headers.
150 1 my $writer = $responder->([splice(@{$self->{'_response'}->finalize()}, 0, 2)]);
151 1 return $self->{'_callback'}->($writer);
152 1 } ];
153 }
154  
155 # just return a normal response
156 5 return $self->{'_response'}->finalize();
157 }
158  
159 3 ▶sub _bake_cookie {
Subroutine _bake_cookie: called CC=6 SCAR=17.9
160 3 my ($self, $key, $value) = @_;
161  
162 3 my @cookie = (URI::Escape::uri_escape($key) . "=" . URI::Escape::uri_escape($value->{'value'}));
163 3 ▶ push(@cookie, "domain=" . $value->{'domain'}) if $value->{'domain'};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"domain"} 2 1 3
164 3 ▶ push(@cookie, "path=" . $value->{'path'}) if $value->{'path'};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"path"} 2 1 3
165 3 ▶ push(@cookie, "expires=" . $self->_cookie_date($value->{'expires'})) if $value->{'expires'};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"expires"} 2 1 3
166 3 ▶ push(@cookie, "secure") if $value->{'secure'};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"secure"} 1 2 3
167 3 ▶ push(@cookie, "HttpOnly") if $value->{'httponly'};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"httponly"} 1 2 3
168 3 return join("; ", @cookie);
169  
170 }
171  
172 3 my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
173 3 my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
174  
175 2 ▶sub _cookie_date {
Subroutine _cookie_date: called CC=2 SCAR=7.2
176 2 my ($self, $expires) = @_;
177  
178 2 ▶ if ($expires =~ /^\-?\d+$/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires =~ /^\-?\d+$/x) 2 0 2
179 # all numbers -> epoch date
180 # (cookies use '-' as date separator, HTTP uses ' ')
181 2 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires);
182 2 $year += 1900;
183  
184 2 return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
185 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
186 }
187  
188 0 return $expires;
189 }
190  
191 3 1;
192  
193 =head1 NAME
194  
195 Prancer::Response
196  
197 =head1 SYNOPSIS
198  
199 sub handler {
200 my ($self, $env, $request, $response, $session) = @_;
201  
202 ...
203  
204 sub (GET) {
205 $response->header("Content-Type" => "text/plain");
206 $response->body("hello, goodbye");
207 return $response->finalize(200);
208 }
209 }
210  
211 # or using a callback
212 sub handler {
213  
214 ...
215  
216 sub (GET) {
217 $response->header("Content-Type" => "text/plain");
218 $response->body(sub {
219 my $writer = shift;
220 $writer->write("What is up?");
221 $writer->close();
222 });
223 return $response->finalize(200);
224 }
225 }
226  
227 =head1 METHODS
228  
229 =over
230  
231 =item header
232  
233 This method expects a list of headers to add to the response. For example:
234  
235 $response->header("Content-Type" => "text/plain");
236 $response->header("Content-Length" => 1234, "X-Foo" => "bar");
237  
238 If the header has already been set this will add another value to it and the
239 response will include the same header multiple times. To replace a header that
240 has already been set, remove the existing value first:
241  
242 $response->headers->remove("X-Foo");
243  
244 =item headers
245  
246 Returns a L<Hash::MultiValue> of all headers that have been set to be sent with
247 the response.
248  
249 =item cookie
250  
251 If called with no arguments this will return the names of all cookies that have
252 been set to be sent with the response. Otherwise, this method expects a list of
253 cookies to add to the response. For example:
254  
255 $response->cookie("foo" => {
256 'value' => "test",
257 'path' => "/",
258 'domain' => ".example.com",
259 'expires' => time + 24 * 60 * 60,
260 });
261  
262 The hashref may contain the keys C<value>, C<domain>, C<expires>, C<path>,
263 C<httponly>, and C<secure>. C<expires> can take a string or an integer (as an
264 epoch time) and B<does not> convert string formats like C<+3M>.
265  
266 =item cookies
267  
268 Returns a L<Hash::MultiValue> of all cookies that have been set to be sent with
269 the response.
270  
271 =item body
272  
273 Send buffered output to the client. Anything sent to the client with this
274 method will be buffered until C<finalize> is called. For example:
275  
276 $response->body("hello");
277 $response->body("goodbye", "world");
278  
279 If a buffered response is not desired then the body may be a callback to send a
280 streaming response to the client. Any headers or response codes set in the
281 callback will be ignored as they must all be set beforehand. Any body set
282 before a callback is set will also be ignored. For example:
283  
284 $response->body(sub {
285 my $writer = shift;
286 $writer->write("Hello, world!");
287 $writer->close();
288 return;
289 });
290  
291 =item finalize
292  
293 This requires one argument: the HTTP status code of the response. It will then
294 send a PSGI compatible response to the client. For example:
295  
296 # or hard code it
297 $response->finalize(200);
298  
299 =back
300  
301 =cut
« Prancer/Core.pm ↑ Summary Prancer/Plugin.pm »