File Coverage

blib/lib/Plack/Middleware/Status.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Plack-Middleware-Status
3             #
4             # This software is copyright (c) 2010 by Patrick Donelan.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Plack::Middleware::Status;
10             BEGIN {
11 1     1   862 $Plack::Middleware::Status::VERSION = '1.101150';
12             }
13              
14             # ABSTRACT: Plack Middleware for mapping urls to status code-driven responses
15 1     1   8 use strict;
  1         2  
  1         39  
16 1     1   6 use parent qw/Plack::Middleware/;
  1         2  
  1         7  
17 1     1   1002 use HTTP::Status;
  1         3803  
  1         344  
18 1     1   10 use Plack::Util::Accessor qw( path status );
  1         3  
  1         12  
19 1     1   57 use Carp;
  1         2  
  1         368  
20              
21              
22             sub call {
23 9     9 1 28635 my $self = shift;
24 9         12 my $env = shift;
25              
26 9         18 my $res = $self->_handle($env);
27 9 100       33 return $res if $res;
28              
29 7         24 return $self->app->($env);
30             }
31              
32             sub _handle {
33 9     9   16 my ( $self, $env ) = @_;
34              
35 9         27 my $path_match = $self->path;
36 9         148 my $status = $self->status;
37 9         47 my $path = $env->{PATH_INFO};
38 9         17 for ($path) {
39 9 100       52 my $matched = 'CODE' eq ref $path_match ? $path_match->($_) : $_ =~ $path_match;
40 9 100       39 return unless $matched;
41             }
42              
43 3 100       12 my $message = HTTP::Status::status_message($status) or do {
44 1         361 carp "Invalid HTTP status: $status";
45 1         72 return;
46             };
47              
48 2         19 return [ $status, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($message) ], [$message] ];
49             }
50              
51             1;
52              
53             __END__