Serve static files with http::server::simple
This documentation refers to HTTP::Server::Simple::Static version 0.07
package MyServer;
use base qw(HTTP::Server::Simple::CGI);
use HTTP::Server::Simple::Static;
my $webroot = '/var/www';
sub handle_request {
my ( $self, $cgi ) = @_;
if ( !$self->serve_static( $cgi, $webroot ) ) {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1('Not found'),
$cgi->end_html;
}
}
package main;
my $server = MyServer->new();
$server->run();
this mixin adds a method to serve static files from your HTTP::Server::Simple subclass.
Takes a reference to the \s-1CGI\s0 object and a document root path, and tries to serve a static file. Returns 0 if the file does not exist, returns 1 on success. This method sets the \*(C`Date\*(C' and \*(C`Last-Modified\*(C' \s-1HTTP\s0 headers when sending a response for a valid file. Further to this, the method supports clients which send an \*(C`If-Modified-Since\*(C' \s-1HTTP\s0 header in the request, it will return a 304 \*(C`Not Modified\*(C' response if the file is unchanged. See \s-1RFC-2616\s0 for full details. If the client makes a \*(C`HEAD\*(C' request then no message body will be returned in the response.
Bugs or wishlist requests should be submitted via http://rt.cpan.org/
The File::LibMagic module is used to detect the MIME-type of a file. The URI::Escape module is used for \s-1URI\s0 handling. The HTTP::Date module is used to format the timestamp in the Last-Modified \s-1HTTP\s0 header.
HTTP::Server::Simple, HTTP::Server::Simple::CGI
Stephen Quinney \*(C`[email protected]\*(C'
Thanks to Marcus Ramberg \*(C`[email protected]\*(C' and Simon Cozens for initial implementation.
Copyright 2006 - 2013. Stephen Quinney \*(C`[email protected]\*(C'
You may distribute this code under the same terms as Perl itself.