/usr/share/perl5/Dancer/Renderer.pm is in libdancer-perl 1.3091+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | package Dancer::Renderer;
use strict;
use warnings;
use Carp;
use HTTP::Headers;
use Dancer::Route;
use Dancer::HTTP;
use Dancer::Cookie;
use Dancer::Factory::Hook;
use Dancer::Cookies;
use Dancer::Request;
use Dancer::Response;
use Dancer::Serializer;
use Dancer::Config 'setting';
use Dancer::FileUtils qw(path path_or_empty dirname read_file_content open_file);
use Dancer::SharedData;
use Dancer::Logger;
use Dancer::MIME;
use Dancer::Exception qw(:all);
Dancer::Factory::Hook->instance->install_hooks(
qw/before after before_serializer after_serializer before_file_render after_file_render/
);
sub render_file { get_file_response() }
sub render_action {
my $class = shift;
my $resp = $class->get_action_response();
return (defined $resp)
? response_with_headers()
: undef;
}
sub render_error {
my ($class, $error_code) = @_;
my $app = Dancer::App->current;
my $static_file = path($app->setting('public'), "$error_code.html");
my $response = Dancer::Renderer->get_file_response_for_path(
$static_file => $error_code);
return $response if $response;
return Dancer::Response->new(
status => $error_code,
headers => ['Content-Type' => 'text/html'],
content => Dancer::Renderer->html_page(
"Error $error_code" => "<h2>Unable to process your query</h2>"
. "The page you requested is not available"
)
);
}
# Takes a response object and add default headers
sub response_with_headers {
my $response = Dancer::SharedData->response();
if (Dancer::Config::setting('server_tokens')) {
$response->{headers} ||= HTTP::Headers->new;
my $powered_by = "Perl Dancer " . $Dancer::VERSION;
$response->header('X-Powered-By' => $powered_by);
$response->header('Server' => $powered_by);
}
return $response;
}
sub html_page {
my ($class, $title, $content, $style) = @_;
$style ||= 'style';
my $template = $class->templates->{'default'};
my $ts = Dancer::Template::Simple->new;
return $ts->render(
\$template,
{ title => $title,
style => $style,
version => $Dancer::VERSION,
content => $content
}
);
}
sub get_action_response {
my $class = shift;
my $depth = shift || 1;
# save the request before the filters are ran
my $request = Dancer::SharedData->request;
my ($method, $path) = ($request->method, $request->path_info);
# look for a matching route handler, for the given request
my $handler =
Dancer::App->find_route_through_apps(Dancer::SharedData->request);
my $app = ($handler && $handler->app) ? $handler->app : Dancer::App->current();
# run the before filters, before "running" the route handler
Dancer::Factory::Hook->instance->execute_hooks('before');
# recurse if something has changed
my $MAX_RECURSIVE_LOOP = 10;
if ( ($path ne Dancer::SharedData->request->path_info)
|| ($method ne Dancer::SharedData->request->method))
{
if ($depth > $MAX_RECURSIVE_LOOP) {
raise core_renderer => "infinite loop detected, "
. "check your route/filters for "
. $method . ' '
. $path;
}
return $class->get_action_response($depth + 1);
}
# redirect immediately - skip route execution
my $response = Dancer::SharedData->response();
if (defined $response && (my $status = $response->status)) {
if ($status == 302 || $status == 301) {
$class->serialize_response_if_needed();
return $response;
}
}
# execute the action
if ($handler) {
# a response may exist, produced by a before filter
return $class->serialize_response_if_needed() if defined $response && $response->exists;
# else, get the route handler's response
Dancer::App->current($handler->{app});
try {
$handler->run($request);
$class->serialize_response_if_needed();
} continuation {
my ($continuation) = @_;
# If we have a Route continuation, run the after hook, then
# propagate the continuation
my $resp = Dancer::SharedData->response();
Dancer::Factory::Hook->instance->execute_hooks('after', $resp);
$continuation->rethrow();
};
my $resp = Dancer::SharedData->response();
Dancer::Factory::Hook->instance->execute_hooks('after', $resp);
return $resp;
}
else {
return undef; # 404
}
}
sub serialize_response_if_needed {
my $response = Dancer::SharedData->response();
if (Dancer::App->current->setting('serializer') && $response->content()){
Dancer::Factory::Hook->execute_hooks('before_serializer', $response);
try {
Dancer::Serializer->process_response($response);
} continuation {
my ($continuation) = @_;
# If we have a Route continuation, run the after hook, then
# propagate the continuation
Dancer::Factory::Hook->execute_hooks('after_serializer', $response);
$continuation->rethrow();
};
Dancer::Factory::Hook->execute_hooks('after_serializer', $response);
}
return $response;
}
sub get_file_response {
my $request = Dancer::SharedData->request;
my $path_info = $request->path_info;
# requests that have \0 in path are forbidden
if ( $path_info =~ /\0/ ) {
_bad_request();
return 1;
}
my $app = Dancer::App->current;
# TODO: this should be later removed with a check whether the file exists
# and then returning a 404, path_or_empty should be removed
my $static_file = path_or_empty( $app->setting('public'), $path_info );
return if ( !$static_file
|| index( $static_file, path( $app->setting('public') ) ) != 0 );
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 $fh = open_file( '<', $static_file );
binmode $fh;
my $response = Dancer::SharedData->response() || Dancer::Response->new();
$response->status($status) if ($status);
$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;
|