File Coverage

blib/lib/Plack/App/Vhost.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition n/a
subroutine 10 10 100.0
pod 2 3 66.6
total 48 49 97.9


line stmt bran cond sub pod time code
1             package Plack::App::Vhost;
2 2     2   15717 use 5.008005;
  2         7  
  2         79  
3 2     2   12 use strict;
  2         4  
  2         72  
4 2     2   16 use warnings;
  2         3  
  2         105  
5              
6             our $VERSION = "0.01";
7              
8 2     2   1097 use parent qw/Plack::Component/;
  2         573  
  2         8  
9 2     2   20980 use Plack::Util::Accessor qw/vhosts fallback/;
  2         470  
  2         12  
10 2     2   91 use List::Util 1.28 qw/pairs/;
  2         59  
  2         550  
11              
12             sub prepare_app {
13 2     2 1 431 my $self = shift;
14 2 100       6 $self->vhosts([]) unless defined $self->vhosts;
15 2 100   1   71 $self->fallback(sub { $self->res_404 }) unless defined $self->fallback;
  1         5  
16             }
17              
18             sub call {
19 4     4 1 856 my ($self, $env) = @_;
20              
21 4         5 for my $vhost (pairs @{ $self->vhosts }) {
  4         10  
22 5         50 my ($rx, $app) = @$vhost;
23 5 100       92 return $app->($env) if $env->{HTTP_HOST} =~ $rx;
24             }
25              
26 2         10 return $self->fallback->($env);
27             }
28              
29             sub res_404 {
30 1     1 0 2 my $self = shift;
31 1         16 return [404, ['Content-Type' => 'text/plain', 'Content-Length' => 9], ['not found']];
32             }
33              
34             1;
35             __END__