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   1580 use parent qw/Plack::Middleware/;
  2         223  
  2         16  
4 2     2   11993 use warnings;
  2         3  
  2         36  
5 2     2   7 use strict;
  2         2  
  2         37  
6              
7 2     2   423 use Plack::App::MCCS;
  2         3  
  2         49  
8 2     2   8 use Plack::Util::Accessor qw/path root defaults types encoding/;
  2         2  
  2         8  
9              
10             our $VERSION = "0.007001";
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             =back
61              
62             Refer to L for a complete explanation of them.
63              
64             =head1 METHODS
65              
66             =head2 call( \%env )
67              
68             Attempts to handle a request by using Plack::App::MCCS.
69              
70             =cut
71              
72             sub call {
73 5     5 1 13969 my ($self, $env) = @_;
74              
75 5         9 my $res = $self->_handle_static($env);
76              
77 5 100 66     30 return $res
78             if $res && $res->[0] != 404;
79              
80 3         88 return $self->app->($env);
81             }
82            
83             sub _handle_static {
84 5     5   27 my($self, $env) = @_;
85              
86             return
87 5 50       11 unless $self->path;
88              
89 5         88 my $path = $env->{PATH_INFO};
90              
91 5         7 for ($path) {
92 5 100       8 my $matched = ref $self->path eq 'CODE' ? $self->path->($_, $env) : $_ =~ $self->path;
93 5 100       65 return unless $matched;
94             }
95              
96 2   50     7 my %opts = (root => $self->root || '.');
97 2         14 foreach (qw/defaults types encoding/) {
98 6 50       27 $opts{$_} = $self->$_
99             if defined $self->$_;
100             }
101              
102 2   33     23 $self->{mccs} ||= Plack::App::MCCS->new(%opts);
103              
104 2         6 local $env->{PATH_INFO} = $path; # rewrite PATH
105              
106 2         8 return $self->{mccs}->call($env);
107             }
108              
109             =head1 BUGS AND LIMITATIONS
110              
111             No bugs have been reported.
112              
113             Please report any bugs or feature requests to
114             C, or through the web interface at
115             L.
116              
117             =head1 SEE ALSO
118              
119             L.
120              
121             =head1 AUTHOR
122              
123             Ido Perlmuter
124              
125             =head1 ACKNOWLEDGMENTS
126              
127             This module is just an adapation of L by Tatsuhiko Miyagawa
128             to use L instead.
129              
130             =head1 LICENSE AND COPYRIGHT
131              
132             Copyright (c) 2011-2015, Ido Perlmuter C<< ido@ido50.net >>.
133              
134             This module is free software; you can redistribute it and/or
135             modify it under the same terms as Perl itself, either version
136             5.8.1 or any later version. See L
137             and L.
138              
139             The full text of the license can be found in the
140             LICENSE file included with this module.
141              
142             =head1 DISCLAIMER OF WARRANTY
143              
144             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
145             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
146             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
147             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
148             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
149             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
150             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
151             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
152             NECESSARY SERVICING, REPAIR, OR CORRECTION.
153              
154             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
155             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
156             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
157             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
158             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
159             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
160             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
161             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
162             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
163             SUCH DAMAGES.
164              
165             =cut
166              
167             1;
168             __END__