Permalink
Browse files

Merge pull request #361 from pstadt/middleware-static-contenttype

Use alternatives instead of Plack::MIME
  • Loading branch information...
2 parents 14d6137 + c0ddf94 commit 7b1ee71bb6b93f7df95cb259b02dad05769e7969 @miyagawa miyagawa committed Jan 31, 2013
Showing with 29 additions and 3 deletions.
  1. +6 −0 lib/Plack/App/File.pm
  2. +12 −3 lib/Plack/Middleware/Static.pm
  3. +1 −0 t/Plack-Middleware/static.foo
  4. +10 −0 t/Plack-Middleware/static.t
View
@@ -86,6 +86,10 @@ sub serve_path {
my $content_type = $self->content_type || Plack::MIME->mime_type($file)
|| 'text/plain';
+ if ("CODE" eq ref $content_type) {
+ $content_type = $content_type->($file);
+ }
+
if ($content_type =~ m!^text/!) {
$content_type .= "; charset=" . ($self->encoding || "utf-8");
}
@@ -174,6 +178,8 @@ Set the file encoding for text files. Defaults to C<utf-8>.
Set the file content type. If not set L<Plack::MIME> will try to detect it
based on the file extension or fall back to C<text/plain>.
+Can be set to a callback which should work on $_[0] to check full path file
+name.
=back
@@ -4,7 +4,7 @@ use warnings;
use parent qw/Plack::Middleware/;
use Plack::App::File;
-use Plack::Util::Accessor qw( path root encoding pass_through );
+use Plack::Util::Accessor qw( path root encoding pass_through content_type );
sub call {
my $self = shift;
@@ -29,7 +29,7 @@ sub _handle_static {
return unless $matched;
}
- $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding });
+ $self->{file} ||= Plack::App::File->new({ root => $self->root || '.', encoding => $self->encoding, content_type => $self->content_type });
local $env->{PATH_INFO} = $path; # rewrite PATH
return $self->{file}->call($env);
}
@@ -67,7 +67,7 @@ If the requested document is not within the C<root> or the file is there but
not readable, this middleware will return a 403 Forbidden response.
The content type returned will be determined from the file extension by using
-L<Plack::MIME>.
+L<Plack::MIME> or using C<content_type>.
=head1 CONFIGURATIONS
@@ -115,6 +115,15 @@ When this option is set to a true value, then this middleware will never
return a 404 if it cannot find a matching file. Instead, it will simply pass
the request on to the application it is wrapping.
+=item content_type
+
+The C<content_type> option can be used to provide access to a different MIME
+database than L<Plack::MIME>.
+L<Plack::MIME> works fast and good for a list of well known file endings,
+but if you need a more accurate content based checking you can use modules
+like L<File::MimeInfo> or L<File::MMagic> for example.
+The callback should work on $_[0] which is the filename of the file.
+
=back
=head1 AUTHOR
@@ -0,0 +1 @@
+bar
@@ -11,6 +11,8 @@ use Plack::Test;
my $base = cwd;
+Plack::MIME->add_type(".foo" => "text/x-fooo");
+
my $handler = builder {
enable "Plack::Middleware::Static",
path => sub { s!^/share/!!}, root => "share";
@@ -21,6 +23,9 @@ my $handler = builder {
path => sub { s!^/share-pass/!!}, root => "share", pass_through => 1;
enable "Plack::Middleware::Static",
path => qr{\.(t|PL|txt)$}i, root => '.';
+ enable "Plack::Middleware::Static",
+ path => qr{\.foo$}i, root => '.',
+ content_type => sub { substr Plack::MIME->mime_type($_[0]),0,-1 } ;
sub {
[200, ['Content-Type' => 'text/plain', 'Content-Length' => 2], ['ok']]
};
@@ -78,6 +83,11 @@ my %test = (
my($ct, $charset) = $res->content_type;
is $charset, 'charset=utf-8';
}
+
+ {
+ my $res = $cb->(GET "http://localhost/t/Plack-Middleware/static.foo");
+ is $res->content_type, 'text/x-foo';
+ }
},
app => $handler,
);

0 comments on commit 7b1ee71

Please sign in to comment.