Nile/HTTP/Response.pm

filter: Statementstmt 14.7% 25 / 169 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 11.1% 5 / 45 Podpod 5.5% 2 / 36 totaltotal 8.8% 32 / 360 CC 46 SCAR 31.7
CC
46
Cov
9%
build_cookie
90.0
build_body
56.0
headers
42.0
CRAP 23.9
« Nile/Handler/PSGI.pm ↑ Summary Nile/XML.pm »
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::HTTP::Response;
9  
10 1 our $VERSION = '0.55';
11 1 our $AUTHORITY = 'cpan:MEWSOFT';
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Nile::HTTP::Response - The HTTP response manager.
20  
21 =head1 SYNOPSIS
22  
23 # get response instance
24 $res = $app->response;
25  
26 $res->code(200);
27 #$res->status(200);
28
29 $res->header('Content-Type' => 'text/plain');
30 #$res->content_type('text/html');
31 $res->header(Content_Base => 'http://www.mewsoft.com/');
32 $res->header(Accept => "text/html, text/plain, image/*");
33 $res->header(MIME_Version => '1.0', User_Agent => 'Nile Web Client/0.27');
34 $res->cookies->{username} = {
35 value => 'mewsoft',
36 path => "/",
37 domain => '.mewsoft.com',
38 expires => time + 24 * 60 * 60,
39 };
40 #$res->body("Hello world content");
41 $res->content("Hello world content");
42  
43 # PSGI response
44 $response = $res->finalize;
45 # [$code, $headers, $body]
46 ($code, $headers, $body) = @$response;
47
48 # headers as string
49 $headers_str = $res->headers_as_string($eol)
50
51 # message as string
52 print $res->as_string($eol);
53  
54 # HTTP/1.1 200 OK
55 # Accept: text/html, text/plain, image/*
56 # User-Agent: Nile Web Client/0.27
57 # Content-Type: text/plain
58 # Content-Base: http://www.mewsoft.com/
59 # MIME-Version: 1.0
60 # Set-Cookie: username=mewsoft; domain=.mewsoft.com; path=/; expires=Fri, 25-Jul-2014 19:10:45 GMT
61 #
62 # Hello world content
63  
64 =head1 DESCRIPTION
65  
66 Nile::HTTP::Response - The HTTP response manager allows you to create PSGI response array ref.
67  
68 =cut
69  
70 1 ▶use Nile::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75 0 ▶sub main { # sub new{}
Subroutine main: not called CC=4 SCAR=30.0
Pod undocumented
76 0 my ($self, $code, $headers, $content) = @_;
77 0 ▶ $self->status($code) if defined $code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 0 0 0
78 0 ▶ $self->headers($headers) if defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $headers 0 0 0
79 0 ▶ $self->body($content) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
80 }
81 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82 =head2 headers
83  
84 $headers = $res->headers;
85 $res->headers([ 'Content-Type' => 'text/html' ]);
86 $res->headers({ 'Content-Type' => 'text/html' });
87 $res->headers( HTTP::Headers->new );
88  
89 Sets and gets HTTP headers of the response. Setter can take either an
90 array ref, a hash ref or L<HTTP::Headers> object containing a list of
91 headers.
92  
93 This is L<HTTP::Headers> object and all its methods available:
94
95 say $res->headers->header_field_names();
96 say $res->headers->remove_content_headers();
97 $res->headers->clear();
98  
99 =cut
100  
101 0 ▶sub headers {
Subroutine headers: not called CC=6 SCAR=37.4
Pod undocumented
102  
103 0 my $self = shift;
104  
105 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
106 0 my $headers = shift;
107 0 ▶ if (ref $headers eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
elsif (ref $headers eq 'HASH') { } 0 0 0
108 0 ▶ Carp::carp("Odd number of headers") if @$headers % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$headers % 2 != 0 0 0 0
109 0 $headers = HTTP::Headers->new(@$headers);
110 }
111 elsif (ref $headers eq 'HASH') {
112 0 $headers = HTTP::Headers->new(%$headers);
113 }
114 0 return $self->{headers} = $headers;
115 }
116 else {
117 0 ▶ return $self->{headers} ||= HTTP::Headers->new();
Condition0%0/3
$self->{"headers"} ||= "HTTP::Headers"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"headers"} ||= "HTTP::Headers"->new
$self->{"headers"} "HTTP::Headers"->new
Truth table0%0/3
A ||= B
A= $self->{"headers"}B= "HTTP::Headers"->new
ABresult
1X1
011
000
118 }
119 }
120 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121 =head2 header
122  
123 $res->header('X-Foo' => 'bar');
124 my $val = $res->header('X-Foo');
125  
126 Sets and gets HTTP header of the response.
127  
128 =cut
129  
130 0 ▶sub header { shift->headers->header(@_) }
Subroutine header: not called CC=1 SCAR=6.9
Pod undocumented
131 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132  
133 =head2 remove_header
134
135 # delete
136 $res->remove_header('Content-Type');
137  
138 Removes the header fields with the specified names.
139  
140 =cut
141  
142 0 ▶sub remove_header { shift->headers->remove_header(@_) }
Subroutine remove_header: not called CC=1 SCAR=6.9
Pod documented
143 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144 =head2 status
145  
146 $res->status(200);
147 $status = $res->status;
148  
149 Sets and gets HTTP status code. C<code> is an alias.
150  
151 =cut
152  
153 1 has status => (is => 'rw');
154 0 ▶sub code { shift->status(@_) }
Subroutine code: not called CC=1 SCAR=6.9
Pod undocumented
155 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156 =head2 body
157  
158 $res->body($body_str);
159 $res->body([ "Hello", "World" ]);
160 $res->body($io);
161  
162 Gets and sets HTTP response body. Setter can take either a string, an
163 array ref, or an IO::Handle-like object. C<content> is an alias.
164  
165 Note that this method doesn't automatically set I<Content-Length> for
166 the response. You have to set it manually if you want, with the
167 C<content_length> method.
168  
169 =cut
170  
171 1 has body => (is => 'rw');
172 0 ▶sub content { shift->body(@_) }
Subroutine content: not called CC=1 SCAR=6.9
Pod undocumented
173 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174 =head2 cookies
175  
176 $res->cookies->{name} = 123;
177 $res->cookies->{name} = {value => '123'};
178  
179 Returns a hash reference containing cookies to be set in the
180 response. The keys of the hash are the cookies' names, and their
181 corresponding values are a plain string (for C<value> with everything
182 else defaults) or a hash reference that can contain keys such as
183 C<value>, C<domain>, C<expires>, C<path>, C<httponly>, C<secure>,
184 C<max-age>.
185  
186 C<expires> can take a string or an integer (as an epoch time) and
187 B<does not> convert string formats such as C<+3M>.
188  
189 $res->cookies->{name} = {
190 value => 'test',
191 path => "/",
192 domain => '.example.com',
193 expires => time + 24 * 60 * 60,
194 };
195  
196 =cut
197  
198 0 ▶has cookies => (is => 'rw', isa => 'HashRef', default => sub {+{}});
Subroutine __ANON__: not called CC=1 SCAR=1.2
199 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200 =head2 content_length
201  
202 $res->content_length(123);
203  
204 A decimal number indicating the size in bytes of the message content.
205 Shortcut for the equivalent get/set method in C<< $res->headers >>.
206  
207 =cut
208  
209 0 ▶sub content_length {shift->headers->content_length(@_)}
Subroutine content_length: not called CC=1 SCAR=6.9
Pod undocumented
210 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211 =head2 content_type
212  
213 $res->content_type('text/plain');
214  
215 The Content-Type header field indicates the media type of the message content.
216 Shortcut for the equivalent get/set method in C<< $res->headers >>.
217  
218 =cut
219  
220 0 ▶sub content_type {shift->headers->content_type(@_)}
Subroutine content_type: not called CC=1 SCAR=6.9
Pod undocumented
221 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222 =head2 content_encoding
223  
224 $res->content_encoding('gzip');
225  
226 Shortcut for the equivalent get/set method in C<< $res->headers >>.
227  
228 =cut
229  
230 0 ▶sub content_encoding {shift->headers->content_encoding(@_)}
Subroutine content_encoding: not called CC=1 SCAR=6.9
Pod undocumented
231 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232 =head2 location
233  
234 Gets and sets C<Location> header.
235  
236 Note that this method doesn't normalize the given URI string in the
237 setter.
238  
239 =cut
240  
241 0 ▶sub location {shift->headers->header('Location' => @_)}
Subroutine location: not called CC=1 SCAR=6.9
Pod undocumented
242 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 =head2 redirect
244  
245 $res->redirect($url);
246 $res->redirect($url, 301);
247  
248 Sets redirect URL with an optional status code, which defaults to 302.
249  
250 Note that this method doesn't normalize the given URI string. Users of
251 this module have to be responsible about properly encoding URI paths
252 and parameters.
253  
254 =cut
255  
256 0 ▶sub redirect {
Subroutine redirect: not called CC=3 SCAR=24.8
Pod undocumented
257  
258 0 my $self = shift;
259  
260 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
261 0 my $url = shift;
262 0 ▶ my $status = shift || 302;
Condition0%0/2
shift() || 302
l!l
00
MC/DC0%0/1
shift() || 302
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
263 0 $self->location($url);
264 0 $self->status($status);
265 }
266  
267 0 return $self->location;
268 }
269 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270 =head2 finalize
271  
272 $res = $res->finalize;
273 # [$code, \@headers, $body]
274 ($code, $headers, $body) = @$res;
275  
276 Returns the status code, headers, and body of this response as a PSGI response array reference.
277  
278 =cut
279  
280 0 ▶sub finalize {
Subroutine finalize: not called CC=2 SCAR=17.9
Pod undocumented
281  
282 0 my $self = shift;
283
284 0 ▶ $self->status || $self->status(200);
Branch: 1 branch
BranchTrueFalseTotal
$self->status or $self->status(200) 0 0 0
285  
286 0 my $headers = $self->headers;
287  
288 0 my @headers;
289  
290 0 ▶ $headers->scan(sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
291 0 my ($k, $v) = @_;
292 0 $v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
293 0 $v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
294 0 push @headers, $k, $v;
295 0 });
296  
297 0 $self->build_cookies(\@headers);
298  
299 0 return [$self->status, \@headers, $self->build_body];
300 }
301 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
302 =head2 to_app
303  
304 $res_app = $res->to_app;
305  
306 A helper shortcut for C<< sub { $res->finalize } >>.
307  
308 =cut
309  
310 0 ▶sub to_app {sub {shift->finalize}}
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine to_app: not called CC=1 SCAR=6.9
Pod undocumented
311 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312 =head2 headers_as_string
313  
314 $headers = $res->headers_as_string($eol)
315  
316 Return the header fields as a formatted MIME header.
317  
318 The optional $eol parameter specifies the line ending sequence to
319 use. The default is "\n". Embedded "\n" characters in header field
320 values will be substituted with this line ending sequence.
321  
322 =cut
323  
324 0 ▶sub headers_as_string {
Subroutine headers_as_string: not called CC=4 SCAR=30.0
Pod undocumented
325
326 0 my ($self, $eol) = @_;
327  
328 0 ▶ $eol = "\n" unless defined $eol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eol 0 0 0
329
330 0 my $res = $self->finalize;
331 0 my ($code, $headers, $body) = @$res;
332  
333 #$self->headers->as_string;
334  
335 0 my @result = ();
336
337 0 ▶ for (my $i = 0; $i < @$headers; $i = $i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 0 0 0
338 0 my $k = $headers->[$i];
339 0 my $v = $headers->[$i+1];
340 0 ▶ if (index($v, "\n") >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($v, "\n") != -1) 0 0 0
341 0 $v = $self->process_newline($v, $eol);
342 }
343 0 push @result, $k . ': ' . $v;
344 }
345  
346 0 return join($eol, @result, '');
347 }
348 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349 0 ▶sub process_newline {
Subroutine process_newline: not called CC=1 SCAR=6.9
Pod undocumented
350 0 local $_ = shift;
351 0 my $eol = shift;
352 # must handle header values with embedded newlines with care
353 0 s/\s+$//; # trailing newlines and space must go
354 0 s/\n(\x0d?\n)+/\n/g; # no empty lines
355 0 s/\n([^\040\t])/\n $1/g; # intial space for continuation
356 0 s/\n/$eol/g; # substitute with requested line ending
357 0 $_;
358 }
359 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360 =head2 as_string
361  
362 $message = $res->as_string($eol);
363  
364 Returns the message formatted as a single string.
365  
366 The optional $eol parameter specifies the line ending sequence to use.
367 The default is "\n". If no $eol is given then as_string will ensure
368 that the returned string is newline terminated (even when the message
369 content is not). No extra newline is appended if an explicit $eol is
370 passed.
371  
372 =cut
373  
374  
375 0 ▶sub as_string {
Subroutine as_string: not called CC=6 SCAR=37.4
Pod undocumented
376  
377 0 my($self, $eol) = @_;
378  
379 0 ▶ $eol = "\n" unless defined $eol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eol 0 0 0
380  
381 # The calculation of content might update the headers
382 # so we need to do that first.
383 0 my $content = $self->content;
384
385 #push @header, "Server: " . server_software() if $nph;
386 #push @header, "Status: $status" if $status;
387 #push @header, "Window-Target: $target" if $target;
388 #sub server_software { $ENV{'SERVER_SOFTWARE'} || 'cmdline' }
389
390 0 ▶ my $protocol = ($ENV{SERVER_PROTOCOL} || 'HTTP/1.1') . " " .$self->code . " " . $self->http_codes->{$self->code} . $eol;
Condition0%0/2
$ENV{'SERVER_PROTOCOL'} || 'HTTP/1.1'
l!l
00
MC/DC0%0/1
$ENV{'SERVER_PROTOCOL'} || 'HTTP/1.1'
$ENV{'SERVER_PROTOCOL'}
Truth table0%0/2
A
A= $ENV{'SERVER_PROTOCOL'}
Aresult
11
00
391  
392 0 ▶ return join("",
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && length $content && !($content =~ /\n\z/) ? : 0 0 0
Condition0%0/6
@_ == 1 and length $content and not $content =~ /\n\z/
!ll&&!rl&&r
000
@_ == 1 and length $content
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and length $content and not $content =~ /\n\z/
@_ == 1 length $content not $content =~ /\n\z/
Truth table0%0/4
A and B and not(C)
A= @_ == 1B= length $contentC= not $content =~ /\n\z/
ABCresult
0XX0
10X0
1100
1111
393 #$protocol,
394 $self->headers_as_string($eol),
395 $eol,
396 $content,
397 (@_ == 1 && length($content) && $content !~ /\n\z/) ? "\n" : "",
398 );
399 }
400 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
401 =head2 render
402  
403 $res->render;
404  
405 Prints the message formatted as a single string to the standard output.
406  
407 =cut
408  
409 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
Pod undocumented
410 0 my ($self) = @_;
411 0 print $self->as_string();
412 }
413 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
414 0 ▶sub send_file {
Subroutine send_file: not called CC=1 SCAR=6.9
Pod undocumented
415
416 0 my ($self, $file, $options) = @_;
417
418 0 load Nile::HTTP::SendFile;
419
420 0 my $sender = Nile::HTTP::SendFile->new;
421  
422 0 $sender->send_file($self, $file, $options);
423  
424  
425 }
426 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
427 1 has encoded => (is => 'rw', isa => 'Bool', default => 0);
428 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
429 0 ▶sub build_body {
Subroutine build_body: not called CC=7 SCAR=40.3
Pod undocumented
430  
431 0 my $self = shift;
432  
433 0 my $body = $self->body;
434  
435 0 ▶ $body = [] unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
436
437 0 ▶ if (!$self->encoded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->encoded) 0 0 0
438 0 $self->encoded(1);
439 0 $body = Encode::encode($self->app->charset, $body);
440 }
441  
442 0 ▶ if (!ref $body or Scalar::Util::blessed($body) && overload::Method($body, q("")) && !$body->can('getline')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $body or builtin::blessed($body) and overload::Method($body, '""') and not $body->can('getline')) { } 0 0 0
Condition0%0/9
not ref $body or builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
l!l&&r!l&&!r
000
builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
!ll&&!rl&&r
000
builtin::blessed($body) and overload::Method($body, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/4
not ref $body or builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
not ref $body builtin::blessed($body) overload::Method($body, "\"\"") not $body->can("getline")
Truth table0%0/5
not(A) or B and C and not(D)
A= not ref $bodyB= builtin::blessed($body)C= overload::Method($body, "\"\"")D= not $body->can("getline")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
443 0 return [$body];
444 } else {
445 0 return $body;
446 }
447 }
448 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
449 0 ▶sub build_cookies {
Subroutine build_cookies: not called CC=2 SCAR=17.9
Pod undocumented
450 0 my($self, $headers) = @_;
451 0 ▶ while (my($name, $val) = each %{$self->cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->cookies;}) 0 0 0
452 0 my $cookie = $self->build_cookie($name, $val);
453 0 push @$headers, 'Set-Cookie' => $cookie;
454 }
455 }
456 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
457 0 ▶sub build_cookie {
Subroutine build_cookie: not called CC=9 SCAR=45.0
Pod undocumented
458  
459 0 my($self, $name, $val) = @_;
460  
461 0 ▶ return '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
462  
463 0 ▶ $val = {value => $val} unless ref $val eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 0 0 0
464  
465 0 my @cookie = (URI::Escape::uri_escape($name) . "=" . URI::Escape::uri_escape($val->{value}));
466  
467 0 ▶ push @cookie, "domain=" . $val->{domain} if $val->{domain};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"domain"} 0 0 0
468 0 ▶ push @cookie, "path=" . $val->{path} if $val->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"path"} 0 0 0
469 0 ▶ push @cookie, "expires=" . $self->cookie_date($val->{expires}) if $val->{expires};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"expires"} 0 0 0
470 0 ▶ push @cookie, "max-age=" . $val->{"max-age"} if $val->{"max-age"};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"max-age"} 0 0 0
471 0 ▶ push @cookie, "secure" if $val->{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"secure"} 0 0 0
472 0 ▶ push @cookie, "HttpOnly" if $val->{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"httponly"} 0 0 0
473  
474 0 return join "; ", @cookie;
475 }
476 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477 0 ▶sub file_response {
Subroutine file_response: not called CC=4 SCAR=30.0
Pod undocumented
478  
479 0 my ($self, $file, $mime, $status) = @_;
480
481 0 ▶ $mime ||= $self->app->mime->for_file($file) || "application/x-download",
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 0 0
Condition0%0/5
$mime ||= $self->app->mime->for_file($file) || "application/x-download"
l!l&&r!l&&!r
000
$self->app->mime->for_file($file) || 'application/x-download'
l!l
00
MC/DC0%0/2
$mime ||= $self->app->mime->for_file($file) || "application/x-download"
$mime $self->app->mime->for_file($file)
Truth table0%0/3
A ||= B
A= $mimeB= $self->app->mime->for_file($file)
ABresult
1X1
011
000
482  
483 $self->status($status) if ($status);
484
485 0 my ($size, $last_modified) = (stat $file)[7,9];
486  
487 0 $last_modified = $self->http_date($last_modified);
488  
489 #my $ifmod = $ENV{HTTP_IF_MODIFIED_SINCE};
490  
491 0 open (my $fh, '<', $file);
492 0 binmode $fh;
493  
494 0 $self->content($fh);
495  
496 0 $self->header('Last-Modified' => $last_modified);
497 0 $self->header('Content-Type' => $mime);
498 0 $self->header('Content-Length' => $size);
499  
500 }
501 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
502 1 my @MON = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
503 1 my @WDAY = qw(Sun Mon Tue Wed Thu Fri Sat);
504  
505 =head2 cookie_date
506  
507 say $res->cookie_date( time + 24 * 60 * 60);
508 #Fri, 25-Jul-2014 20:46:53 GMT
509  
510 Returns cookie formated date.
511  
512 =cut
513  
514 0 ▶sub cookie_date {
Subroutine cookie_date: not called CC=2 SCAR=17.9
Pod documented
515 0 my ($self, $expires) = @_;
516 0 ▶ if ($expires =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires =~ /^\d+$/) 0 0 0
517 0 return $self->make_date($expires, "cookie");
518 }
519 0 return $expires;
520 }
521 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
522 =head2 http_date
523  
524 say $res->http_date(time);
525 #Thu, 24 Jul 2014 20:46:53 GMT
526  
527 Returns http formated date.
528  
529 =cut
530  
531 0 ▶sub http_date {
Subroutine http_date: not called CC=1 SCAR=6.9
Pod undocumented
532 0 my ($self, $time) = @_;
533 0 return $self->make_date($time, "http");
534 }
535 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
536 0 ▶sub make_date {
Subroutine make_date: not called CC=2 SCAR=17.9
Pod undocumented
537  
538 0 my ($self, $time, $format) = @_;
539
540 # format: cookie = "-", http = " "
541 0 ▶ my $sp = $format eq "cookie" ? "-" : " ";
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'cookie' ? : 0 0 0
542  
543 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
544 0 $year += 1900;
545  
546 0 return sprintf("%s, %02d$sp%s$sp%s %02d:%02d:%02d GMT",
547 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
548 }
549 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
550 has 'http_codes' => (
551 is => 'rw',
552 isa => 'HashRef',
553 0 ▶ default => sub { +{
Subroutine __ANON__: not called CC=1 SCAR=6.9
554 # informational
555 # 100 => 'Continue', # only on HTTP 1.1
556 # 101 => 'Switching Protocols', # only on HTTP 1.1
557  
558 # processed codes
559 0 200 => 'OK',
560 201 => 'Created',
561 202 => 'Accepted',
562  
563 # 203 => 'Non-Authoritative Information', # only on HTTP 1.1
564 204 => 'No Content',
565 205 => 'Reset Content',
566 206 => 'Partial Content',
567  
568 # redirections
569 301 => 'Moved Permanently',
570 302 => 'Found',
571  
572 # 303 => '303 See Other', # only on HTTP 1.1
573 304 => 'Not Modified',
574  
575 # 305 => '305 Use Proxy', # only on HTTP 1.1
576 306 => 'Switch Proxy',
577  
578 # 307 => '307 Temporary Redirect', # on HTTP 1.1
579  
580 # problems with request
581 400 => 'Bad Request',
582 401 => 'Unauthorized',
583 402 => 'Payment Required',
584 403 => 'Forbidden',
585 404 => 'Not Found',
586 405 => 'Method Not Allowed',
587 406 => 'Not Acceptable',
588 407 => 'Proxy Authentication Required',
589 408 => 'Request Timeout',
590 409 => 'Conflict',
591 410 => 'Gone',
592 411 => 'Length Required',
593 412 => 'Precondition Failed',
594 413 => 'Request Entity Too Large',
595 414 => 'Request-URI Too Long',
596 415 => 'Unsupported Media Type',
597 416 => 'Requested Range Not Satisfiable',
598 417 => 'Expectation Failed',
599  
600 # problems with server
601 500 => 'Internal Server Error',
602 501 => 'Not Implemented',
603 502 => 'Bad Gateway',
604 503 => 'Service Unavailable',
605 504 => 'Gateway Timeout',
606 505 => 'HTTP Version Not Supported',
607 1 }});
608 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
609 =head2 status_message($code)
610  
611 The status_message() function will translate status codes to human
612 readable strings. If the $code is unknown, then C<undef> is returned.
613  
614 =cut
615  
616 0 ▶sub status_message {my $self = shift; $self->http_codes->{$_[0]};}
Subroutine status_message: not called CC=1 SCAR=6.9
Pod undocumented
617 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
618 =head2 is_info( $code )
619  
620 Return TRUE if C<$code> is an I<Informational> status code (1xx). This
621 class of status code indicates a provisional response which can't have
622 any content.
623  
624 =cut
625  
626 0 ▶sub is_info {shift; $_[0] >= 100 && $_[0] < 200; }
Subroutine is_info: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 100 and $_[0] < 200 0 0 0
627 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
628 =item is_success( $code )
629  
630 Return TRUE if C<$code> is a I<Successful> status code (2xx).
631  
632 =cut
633  
634 0 ▶sub is_success {shift; $_[0] >= 200 && $_[0] < 300; }
Subroutine is_success: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 200 and $_[0] < 300 0 0 0
635 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
636 =item is_redirect( $code )
637  
638 Return TRUE if C<$code> is a I<Redirection> status code (3xx). This class of
639 status code indicates that further action needs to be taken by the
640 user agent in order to fulfill the request.
641  
642 =cut
643  
644 0 ▶sub is_redirect {shift; $_[0] >= 300 && $_[0] < 400; }
Subroutine is_redirect: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 300 and $_[0] < 400 0 0 0
645 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
646 =item is_error( $code )
647  
648 Return TRUE if C<$code> is an I<Error> status code (4xx or 5xx). The function
649 returns TRUE for both client and server error status codes.
650  
651 =cut
652  
653 0 ▶sub is_error {shift; $_[0] >= 400 && $_[0] < 600; }
Subroutine is_error: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 400 and $_[0] < 600 0 0 0
654 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
655 =item is_client_error( $code )
656  
657 Return TRUE if C<$code> is a I<Client Error> status code (4xx). This class
658 of status code is intended for cases in which the client seems to have
659 erred.
660  
661 =cut
662  
663 0 ▶sub is_client_error {shift; $_[0] >= 400 && $_[0] < 500; }
Subroutine is_client_error: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 400 and $_[0] < 500 0 0 0
664 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665 =item is_server_error( $code )
666  
667 Return TRUE if C<$code> is a I<Server Error> status code (5xx). This class
668 of status codes is intended for cases in which the server is aware
669 that it has erred or is incapable of performing the request.
670  
671 =cut
672  
673 0 ▶sub is_server_error {shift; $_[0] >= 500 && $_[0] < 600; }
Subroutine is_server_error: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 500 and $_[0] < 600 0 0 0
674 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
675 0 ▶sub http_code_response {
Subroutine http_code_response: not called CC=1 SCAR=2.9
Pod undocumented
676
677 0 my ($self, $code) = @_;
678  
679 0 $self->code($code);
680
681 0 $self->header('Content-Type' => 'text/plain');
682
683 0 my $body = $self->status_message($code);
684  
685 0 $self->content($body);
686
687 1 ▶ use bytes; # turn off character semantics
Subroutine BEGIN: called CC=1 SCAR=0.0
688 0 $self->header('Content-Length' => length($body));
689  
690 0 return $self->finalize;
691 }
692 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
693 0 ▶sub response_403 {
Subroutine response_403: not called CC=1 SCAR=6.9
Pod undocumented
694 # 403 => 'Forbidden',
695 0 return shift->http_code_response(403);
696 }
697 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
698 0 ▶sub response_400 {
Subroutine response_400: not called CC=1 SCAR=6.9
Pod undocumented
699 # 400 => 'Bad Request',
700 0 return shift->http_code_response(400);
701 }
702 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
703 0 ▶sub response_404 {
Subroutine response_404: not called CC=1 SCAR=6.9
Pod undocumented
704 # 404 => 'Not Found',
705 0 return shift->http_code_response(404);
706 }
707 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
708  
709 =pod
710  
711 =head1 Bugs
712  
713 This project is available on github at L<https://github.com/mewsoft/Nile>.
714  
715 =head1 HOMEPAGE
716  
717 Please visit the project's homepage at L<https://metacpan.org/release/Nile>.
718  
719 =head1 SOURCE
720  
721 Source repository is at L<https://github.com/mewsoft/Nile>.
722  
723 =head1 ACKNOWLEDGMENT
724  
725 This module is based on L<Plack::Response> L<HTTP::Message>
726  
727 =head1 SEE ALSO
728  
729 See L<Nile> for details about the complete framework.
730  
731 =head1 AUTHOR
732  
733 Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org>
734 Website: http://www.mewsoft.com
735  
736 =head1 COPYRIGHT AND LICENSE
737  
738 Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com,
739 L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com>
740  
741 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
742  
743 =cut
744  
745 1 1;
« Nile/Handler/PSGI.pm ↑ Summary Nile/XML.pm »