Skip to content
Browse files

Merge pull request #364 from avar/add-ability-to-pass-env-to-static-m…

…iddleware-v2

Plack::Middleware::Static: add ability to pass $env to callbacks
  • Loading branch information...
2 parents 5f5eae7 + 0f1a5be commit 528c07f64a5028266ce5709b14836b0ced333254 @miyagawa miyagawa committed
Showing with 17 additions and 2 deletions.
  1. +9 −2 lib/Plack/Middleware/Static.pm
  2. +8 −0 t/Plack-Middleware/static.t
View
11 lib/Plack/Middleware/Static.pm
@@ -25,7 +25,7 @@ sub _handle_static {
my $path = $env->{PATH_INFO};
for ($path) {
- my $matched = 'CODE' eq ref $path_match ? $path_match->($_) : $_ =~ $path_match;
+ my $matched = 'CODE' eq ref $path_match ? $path_match->($_, $env) : $_ =~ $path_match;
return unless $matched;
}
@@ -37,6 +37,8 @@ sub _handle_static {
1;
__END__
+=encoding utf8
+
=head1 NAME
Plack::Middleware::Static - serve static files with Plack
@@ -96,6 +98,11 @@ The callback should operate on C<$_> and return a true or false value. Any
changes it makes to C<$_> are used when looking for the static file in the
C<root>.
+In addition to C<$_> being set the callback receives two arguments,
+C<$_> and C<$env>. You can inspect C<$env> for more advanced static
+handling, e.g. giving different static assets to different user
+agents.
+
The configuration above serves C</static/foo.png> from
C<static-files/foo.png>, not C<static-files/static/foo.png>. The callback
specified in the C<path> option matches against C<$_> munges this value using
@@ -115,7 +122,7 @@ the request on to the application it is wrapping.
=head1 AUTHOR
-Tokuhiro Matsuno, Tatsuhiko Miyagawa
+Tokuhiro Matsuno, Tatsuhiko Miyagawa, Ævar Arnfjörð Bjarmason
=head1 SEE ALSO
View
8 t/Plack-Middleware/static.t
@@ -15,6 +15,9 @@ my $handler = builder {
enable "Plack::Middleware::Static",
path => sub { s!^/share/!!}, root => "share";
enable "Plack::Middleware::Static",
+ path => sub { s!^/more_share/!! if $_[1]->{PATH_INFO} =~ m!^/more_share/! },
+ root => "share";
+ enable "Plack::Middleware::Static",
path => sub { s!^/share-pass/!!}, root => "share", pass_through => 1;
enable "Plack::Middleware::Static",
path => qr{\.(t|PL|txt)$}i, root => '.';
@@ -59,6 +62,11 @@ my %test = (
}
{
+ my $res = $cb->(GET "http://localhost/more_share/face.jpg");
+ is $res->content_type, 'image/jpeg';
+ }
+
+ {
my $res = $cb->(GET "http://localhost/share-pass/faceX.jpg");
is $res->code, 200, 'pass through';
is $res->content, 'ok';

0 comments on commit 528c07f

Please sign in to comment.
Something went wrong with that request. Please try again.