File Coverage

blib/lib/Plack/App/Hash.pm
Criterion Covered Total %
statement 53 53 100.0
branch 18 18 100.0
condition 12 18 66.6
subroutine 11 11 100.0
pod 2 3 66.6
total 96 103 93.2


line stmt bran cond sub pod time code
1 1     1   56469 use 5.006; use strict; use warnings;
  1     1   4  
  1     1   5  
  1         1  
  1         18  
  1         4  
  1         1  
  1         53  
2              
3             package Plack::App::Hash;
4              
5             our $VERSION = '1.001';
6              
7 1     1   395 BEGIN { require Plack::Component; our @ISA = 'Plack::Component' }
  1         11334  
8              
9 1     1   7 use Plack::Util ();
  1         2  
  1         11  
10 1     1   380 use Array::RefElem ();
  1         462  
  1         20  
11 1     1   386 use HTTP::Status ();
  1         3919  
  1         27  
12             #use Digest::SHA;
13              
14 1     1   342 use Plack::Util::Accessor qw( content headers auto_type default_type );
  1         232  
  1         7  
15              
16             sub prepare_app {
17 16     16 1 35652 my $self = shift;
18 16 100       35 require Plack::MIME if $self->auto_type;
19             }
20              
21             sub call {
22 16     16 1 1250 my $self = shift;
23 16         18 my $env = shift;
24              
25 16         25 my $path = $env->{'PATH_INFO'};
26 16         51 $path =~ s!\A/!!;
27              
28 16         34 my $content = $self->content;
29 16 100 66     97 return $self->error( 404 ) unless $content and exists $content->{ $path };
30 15 100       32 return $self->error( 500 ) if ref $content->{ $path };
31              
32 14   66     24 my $headers = ( $self->headers || $self->headers( {} ) )->{ $path };
33              
34 14 100       76 if ( not defined $headers ) {
    100          
35 8         11 $headers = [];
36             }
37             elsif ( not ref $headers ) {
38 2         9 require JSON::MaybeXS;
39 2         28 $headers = JSON::MaybeXS::decode_json $headers;
40             }
41              
42 13 100       29 return $self->error( 500 ) if 'ARRAY' ne ref $headers;
43              
44             {
45 12         14 my $auto = $self->auto_type;
  12         22  
46 12         40 my $default = $self->default_type;
47 12 100 100     68 last unless $auto or $default;
48 5 100       12 last if Plack::Util::header_exists $headers, 'Content-Type';
49 3   66     34 $auto &&= Plack::MIME->mime_type( $path );
50 3   66     33 push @$headers, 'Content-Type' => $_ for $auto || $default || ();
      33        
51             }
52              
53 12 100       40 if ( not Plack::Util::header_exists $headers, 'Content-Length' ) {
54 10         106 push @$headers, 'Content-Length' => length $content;
55             }
56              
57 12         34 my @body;
58 12         45 Array::RefElem::av_push @body, $content->{ $path };
59 12         57 return [ 200, $headers, \@body ];
60             }
61              
62             sub error {
63 3     3 0 5 my $status = pop;
64 3         5 my $pkg = __PACKAGE__;
65 3         10 my $body = [ qq(\n$pkg $status

) . HTTP::Status::status_message $status ];

66 3         25 return [ $status, [
67             'Content-Type' => 'text/html',
68             'Content-Length' => length $body->[0],
69             ], $body ];
70             }
71              
72             1;
73              
74             __END__