Dancer
view release on metacpan or search on metacpan
lib/Dancer/Renderer.pm view on Meta::CPAN
return Dancer::Renderer->get_file_response_for_path( $static_file, undef,
$request->content_type );
}
sub get_file_response_for_path {
my ($class, $static_file, $status, $mime) = @_;
if ( -f $static_file ) {
Dancer::Factory::Hook->execute_hooks( 'before_file_render',
$static_file );
my $response = Dancer::SharedData->response() || Dancer::Response->new();
# handle If-Modified-Since
my $last_modified = (stat $static_file)[9];
my $since = str2time(Dancer::SharedData->request->env->{HTTP_IF_MODIFIED_SINCE});
if( defined $since && $since >= $last_modified ) {
$response->status( 304 );
$response->content( '' );
return $response;
}
my $fh = open_file( '<', $static_file );
binmode $fh;
$response->status($status) if ($status);
$response->header( 'Last-Modified' => time2str( $last_modified ) );
$response->header('Content-Type' => (($mime && _get_full_mime_type($mime)) ||
Dancer::SharedData->request->content_type ||
_get_mime_type($static_file)));
$response->content($fh);
Dancer::Factory::Hook->execute_hooks( 'after_file_render', $response );
return $response;
}
return;
}
# private
sub _get_full_mime_type {
my $mime = Dancer::MIME->instance();
return $mime->name_or_type(shift @_);
}
sub _get_mime_type {
my $file = shift;
my $mime = Dancer::MIME->instance();
return $mime->for_file($file);
}
sub _bad_request{
my $response = Dancer::SharedData->response() || Dancer::Response->new();
$response->status(400);
$response->content('Bad Request');
}
# set of builtin templates needed by Dancer when rendering HTML pages
sub templates {
my $charset = setting('charset') || 'UTF-8';
{ default =>
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><% title %></title>
<link rel="stylesheet" href="/css/<% style %>.css" />
<meta http-equiv="Content-type" content="text/html; charset=' . $charset
. '" />
</head>
<body>
<h1><% title %></h1>
<div id="content">
<% content %>
</div>
<div id="footer">
Powered by <a href="http://perldancer.org/">Dancer</a> <% version %>
</div>
</body>
</html>',
};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer::Renderer - Rendering class for Dancer
=head1 VERSION
version 1.3522
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 1.659 second using v1.01-cache-2.11-cpan-7fcb06a456a )