Date: | Tue, 08 Feb 2005 09:07:57 +0100 |
From: | Max Maischein <corion [...] corion.net> |
To: | bug-HTTP-Server-Simple-Static [...] rt.cpan.org |
Subject: | serve_static() determines MIME type for the wrong thing |
In HTTP::Server::Simple v0.01, the subroutine serve_static() is a bit wrong:
sub serve_static {
my ($self,$cgi,$base) = @_;
my $path = $cgi->url( -absolute => 1, -path_info => 1 );
# Sanitize the path and try it.
$path = $base.canonpath(uri_unescape($path));
if (-e $path and open FILE, $path) {
local $/;
my $content=<FILE>;
my $mimeobj= $mime->mimeTypeOf($base);
my $mime = ($mimeobj ? $mimeobj->type :
$magic->checktype_contents($content));
print "HTTP/1.1 200 OK\n";
print "Content-type: ", $mime, "\n";
print "Content-length: ", length($content), "\n\n";
print $content;
return 1 ;
}
return 0;
}
The line
my $mimeobj= $mime->mimeTypeOf($base);
is wrong. It gets the mime type of the base directory, and not of a
served file. It should be
my $mimeobj= $mime->mimeTypeOf($path);
to actually get the mime type of the served object. Also, the server
should (optionally) warn if it doesn't find a file, for easier local
diagnosis :-)
Thanks for writing this useful extension to HTTP::Server::Simple!
Have fun,
-max