File Coverage

blib/lib/Plack/App/Proxy/Selective.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Plack::App::Proxy::Selective;
2              
3 3     3   164872 use strict;
  3         8  
  3         181  
4 3     3   22 use warnings;
  3         8  
  3         175  
5              
6 3     3   21 use parent qw/Plack::Component/;
  3         4  
  3         30  
7              
8 3     3   36709 use Plack::Util::Accessor qw/filter base_dir/;
  3         603  
  3         30  
9 3     3   5305 use Plack::App::Proxy;
  3         287686  
  3         118  
10 3     3   5768 use Plack::App::Directory;
  3         53988  
  3         98  
11 3     3   61 use Path::Class;
  3         8  
  3         249  
12 3     3   6241 use Regexp::Common qw/URI/;
  0            
  0            
13              
14             our $VERSION = '0.10';
15              
16             my $statics;
17              
18             sub match_uri {
19             my ($env, $source) = @_;
20              
21             $env->{'REQUEST_URI'} =~ /$RE{URI}{HTTP}{-keep}/;
22             my $path = $6 or warn "$1 seems not as static file.";
23              
24             # strip slases before/after source dir
25             $source =~ s/^\///;
26             $source =~ s/\/$//;
27              
28             # make star and plus not greedy
29             $source =~ s/(\*|\+)$/$1?/;
30              
31             if ( $path =~ s/$source\/?// ) {
32             return $path;
33             }
34             else {
35             return undef;
36             }
37             }
38              
39             sub server_local {
40             my ($base_dir, $target_dir) = @_;
41             return Plack::App::Directory->new(root => $base_dir->subdir($target_dir));
42             }
43              
44             sub call {
45             my ($self, $env) = @_;
46              
47             my %filter = %{$self->filter};
48              
49             while( my ($host, $mapping) = each %filter ) {
50              
51             if ( $env->{'HTTP_HOST'} =~ /$host/ ) {
52              
53             for my $source_dir ( keys %{$mapping} ) {
54              
55             if ( my $path = match_uri($env, $source_dir) ) {
56             my $dir = server_local($self->base_dir, $mapping->{$source_dir})->to_app;
57             $env->{PATH_INFO} = $path;
58             return $dir->($env);
59             }
60              
61             }
62             }
63             }
64              
65             my $proxy = Plack::App::Proxy->new->to_app;
66             $env->{'plack.proxy.url'} = $env->{'REQUEST_URI'};
67             return $proxy->($env);
68             }
69              
70              
71             1;
72             __END__