File Coverage

blib/lib/Plack/Middleware/MCCS.pm
Criterion Covered Total %
statement 31 31 100.0
branch 8 10 80.0
condition 4 8 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 51 57 89.4


line stmt bran cond sub pod time code
1             package Plack::Middleware::MCCS;
2              
3 2     2   1467 use parent qw/Plack::Middleware/;
  2         211  
  2         8  
4 2     2   13882 use warnings;
  2         3  
  2         51  
5 2     2   8 use strict;
  2         3  
  2         41  
6              
7 2     2   430 use Plack::App::MCCS;
  2         2  
  2         46  
8 2     2   6 use Plack::Util::Accessor qw/path root defaults types encoding min_cache_dir/;
  2         2  
  2         6  
9              
10             our $VERSION = "1.000000";
11             $VERSION = eval $VERSION;
12              
13             =head1 NAME
14              
15             Plack::Middleware::MCCS - Middleware for serving static files with Plack::App::MCCS
16              
17             =head1 EXTENDS
18              
19             L
20              
21             =head1 SYNOPSIS
22              
23             # in your app.psgi:
24             use Plack::Builder;
25              
26             builder {
27             enable 'Plack::Middleware::MCCS',
28             path => qr{^/static/},
29             root => '/path/to/static_files';
30             $app;
31             };
32              
33             =head1 DESCRIPTION
34              
35             This package allows serving static files with L in the form of a
36             middleware. It allows for more flexibility with regards to which paths are to be
37             served by C, as the app can only be Ced onto a certain path prefix.
38             The middleware, however, can serve requests that match a certain regular expression.
39              
40             =head1 CONFIGURATIONS
41              
42             The only required configuration option is B. You should either provide a regular
43             expression, or a subroutine to match against requests. For more info about the C
44             option, look at L, it's exactly the same.
45              
46             Other configuration options are those supported by L. None are required,
47             but you will mostly provide the C option. If you do not provide it, the current
48             working directory is assumed. These are the supported options:
49              
50             =over
51              
52             =item * root
53              
54             =item * defaults
55              
56             =item * types
57              
58             =item * encoding
59              
60             =item * min_cache_dir
61              
62             =back
63              
64             Refer to L for a complete explanation of them.
65              
66             =head1 METHODS
67              
68             =head2 call( \%env )
69              
70             Attempts to handle a request by using Plack::App::MCCS.
71              
72             =cut
73              
74             sub call {
75 5     5 1 12692 my ($self, $env) = @_;
76              
77 5         9 my $res = $self->_handle_static($env);
78              
79 5 100 66     31 return $res
80             if $res && $res->[0] != 404;
81              
82 3         12 return $self->app->($env);
83             }
84              
85             sub _handle_static {
86 5     5   6 my($self, $env) = @_;
87              
88             return
89 5 50       11 unless $self->path;
90              
91 5         51 my $path = $env->{PATH_INFO};
92              
93 5         8 for ($path) {
94 5 100       8 my $matched = ref $self->path eq 'CODE' ? $self->path->($_, $env) : $_ =~ $self->path;
95 5 100       52 return unless $matched;
96             }
97              
98 2   50     5 my %opts = (root => $self->root || '.');
99 2         13 foreach (qw/defaults types encoding min_cache_dir/) {
100 8 50       34 $opts{$_} = $self->$_
101             if defined $self->$_;
102             }
103              
104 2   33     22 $self->{mccs} ||= Plack::App::MCCS->new(%opts);
105              
106 2         5 local $env->{PATH_INFO} = $path; # rewrite PATH
107              
108 2         8 return $self->{mccs}->call($env);
109             }
110              
111             =head1 BUGS AND LIMITATIONS
112              
113             No bugs have been reported.
114              
115             Please report any bugs or feature requests to
116             C, or through the web interface at
117             L.
118              
119             =head1 SEE ALSO
120              
121             L.
122              
123             =head1 AUTHOR
124              
125             Ido Perlmuter
126              
127             =head1 ACKNOWLEDGMENTS
128              
129             This module is just an adapation of L by Tatsuhiko Miyagawa
130             to use L instead.
131              
132             =head1 LICENSE AND COPYRIGHT
133              
134             Copyright (c) 2011-2016, Ido Perlmuter C<< ido@ido50.net >>.
135              
136             This module is free software; you can redistribute it and/or
137             modify it under the same terms as Perl itself, either version
138             5.8.1 or any later version. See L
139             and L.
140              
141             The full text of the license can be found in the
142             LICENSE file included with this module.
143              
144             =head1 DISCLAIMER OF WARRANTY
145              
146             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
147             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
148             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
149             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
150             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
151             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
152             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
153             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
154             NECESSARY SERVICING, REPAIR, OR CORRECTION.
155              
156             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
157             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
158             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
159             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
160             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
161             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
162             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
163             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
164             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
165             SUCH DAMAGES.
166              
167             =cut
168              
169             1;
170             __END__