| 1 |
package Dancer2::Handler::AutoPage; |
| 2 |
# ABSTRACT: Class for handling the AutoPage feature |
| 3 |
$Dancer2::Handler::AutoPage::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use Carp 'croak'; |
| 6 |
use Dancer2::Core::Types; |
| 7 |
use Path::Tiny (); |
| 8 |
|
| 9 |
with qw< |
| 10 |
Dancer2::Core::Role::Handler |
| 11 |
Dancer2::Core::Role::StandardResponses |
| 12 |
>; |
| 13 |
|
| 14 |
sub register { |
| 15 |
my ( $self, $app ) = @_; |
| 16 |
|
| 17 |
return unless $app->config->{auto_page}; |
| 18 |
|
| 19 |
$app->add_route( |
| 20 |
method => $_, |
| 21 |
regexp => $self->regexp, |
| 22 |
code => $self->code, |
| 23 |
) for $self->methods; |
| 24 |
} |
| 25 |
|
| 26 |
sub code { |
| 27 |
sub { |
| 28 |
my $app = shift; |
| 29 |
my $prefix = shift; |
| 30 |
|
| 31 |
my $template = $app->template_engine; |
| 32 |
if ( !defined $template ) { |
| 33 |
$app->response->has_passed(1); |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
my $page = $app->request->path; |
| 38 |
my $layout_dir = $template->layout_dir; |
| 39 |
|
| 40 |
# Cheap fast path: if the request path is spelled with the layout |
| 41 |
# directory's own case, refuse it without touching the filesystem. |
| 42 |
# This is only an optimisation - it is not the authoritative check, |
| 43 |
# because on a case-insensitive filesystem (macOS/Windows) a |
| 44 |
# differently-cased request would miss it while still resolving to |
| 45 |
# the same file. The check below decides from the resolved |
| 46 |
# file instead, so it catches that case too. |
| 47 |
if ( defined $layout_dir && $page =~ m{^/\Q$layout_dir\E/} ) { |
| 48 |
$app->response->has_passed(1); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
# remove leading '/', ensuring paths relative to the view |
| 53 |
$page =~ s{^/}{}; |
| 54 |
my $view_path = $template->view_pathname($page); |
| 55 |
|
| 56 |
if ( ! $template->pathname_exists( $view_path ) ) { |
| 57 |
$app->response->has_passed(1); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
# Authoritative check: is the page's own directory the layout |
| 62 |
# directory, or inside it? Decided from what is actually on disk |
| 63 |
# rather than from the request path's spelling. |
| 64 |
# |
| 65 |
# This does not use $view_path/view_pathname for the comparison: |
| 66 |
# that method is engine-specific, and Dancer2::Template::TemplateToolkit |
| 67 |
# overrides it to return a bare template name rather than a |
| 68 |
# filesystem path, leaving TT2's own INCLUDE_PATH search to resolve |
| 69 |
# it - so it cannot be relied on to name a location on disk here. |
| 70 |
# $template->views is guaranteed absolute by |
| 71 |
# Dancer2::Core::Role::Template, so joining it with $page and |
| 72 |
# taking the parent gives the page's real directory regardless of |
| 73 |
# which template engine is in use. Its parent directory is |
| 74 |
# guaranteed to exist at this point - pathname_exists just |
| 75 |
# confirmed the page resolves to a real file. |
| 76 |
if ( defined $layout_dir ) { |
| 77 |
my $layout_dir_path = |
| 78 |
Path::Tiny::path( $template->views, $layout_dir ); |
| 79 |
my $page_dir_path = |
| 80 |
Path::Tiny::path( $template->views, $page )->parent; |
| 81 |
|
| 82 |
if ( $layout_dir_path->is_dir |
| 83 |
&& $page_dir_path->is_dir |
| 84 |
&& _dir_is_within( |
| 85 |
$page_dir_path, |
| 86 |
$layout_dir_path, |
| 87 |
Path::Tiny::path( $template->views ), |
| 88 |
) |
| 89 |
) { |
| 90 |
$app->response->has_passed(1); |
| 91 |
return; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
my $ct = $template->process( $page ); |
| 96 |
return ( $app->request->method eq 'GET' ) ? $ct : ''; |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
# Is $dir the same directory as $ancestor, or inside it? |
| 101 |
# |
| 102 |
# Comparing resolved path strings is not enough. realpath() resolves |
| 103 |
# symlinks but does not canonicalise case, so on a case-insensitive |
| 104 |
# filesystem - macOS and Windows by default - 'views/Layouts' and |
| 105 |
# 'views/layouts' name one directory while comparing as two. That is |
| 106 |
# precisely the case this guard exists to catch, so the comparison is made |
| 107 |
# on filesystem identity (device and inode) instead, which no spelling can |
| 108 |
# disguise. It settles symlinks and hardlinks on the way past. |
| 109 |
# |
| 110 |
# The walk stops at $stop_at (the views directory) rather than climbing to |
| 111 |
# the filesystem root: nothing above views is ours to reason about, and a |
| 112 |
# page cannot be inside the layout directory without being under views too. |
| 113 |
# |
| 114 |
# Some Windows configurations report an inode of 0 for every file, which |
| 115 |
# would make every directory compare equal to every other. Where that is |
| 116 |
# so, identity is unusable and the comparison falls back to matching the |
| 117 |
# paths case-insensitively - imperfect, since it assumes the folding rules |
| 118 |
# rather than asking the filesystem, but it fails closed on the case this |
| 119 |
# guard is about rather than silently passing everything. |
| 120 |
sub _dir_is_within { |
| 121 |
my ( $dir, $ancestor, $stop_at ) = @_; |
| 122 |
|
| 123 |
my @ancestor_id = stat "$ancestor" or return 0; |
| 124 |
my $usable_inode = $ancestor_id[1]; |
| 125 |
|
| 126 |
if ( !$usable_inode ) { |
| 127 |
my $a = lc Path::Tiny::path($ancestor)->stringify; |
| 128 |
my $d = lc Path::Tiny::path($dir)->stringify; |
| 129 |
return $d eq $a || index( $d, "$a/" ) == 0; |
| 130 |
} |
| 131 |
|
| 132 |
my $stop = eval { $stop_at->realpath->stringify }; |
| 133 |
my $cursor = eval { $dir->realpath } or return 0; |
| 134 |
|
| 135 |
while (1) { |
| 136 |
my @id = stat "$cursor" or return 0; |
| 137 |
# 'eq', not '=='. Where an inode number is too large for perl to |
| 138 |
# hold as an integer, stat returns it as a decimal string; comparing |
| 139 |
# numerically converts that to a float and rounds it, so two |
| 140 |
# different inodes can compare equal. perldoc -f stat says to prefer |
| 141 |
# 'eq' for exactly this reason, and it is correct for the values |
| 142 |
# that are returned numerically too. Rounding here would be a |
| 143 |
# fail-open: a page outside the layout directory would be served. |
| 144 |
return 1 if $id[0] eq $ancestor_id[0] && $id[1] eq $ancestor_id[1]; |
| 145 |
|
| 146 |
last if defined $stop && $cursor->stringify eq $stop; |
| 147 |
|
| 148 |
my $parent = $cursor->parent; |
| 149 |
last if $parent->stringify eq $cursor->stringify; # hit the root |
| 150 |
$cursor = $parent; |
| 151 |
} |
| 152 |
|
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
sub regexp {'/**'} |
| 157 |
|
| 158 |
sub methods {qw(head get)} |
| 159 |
|
| 160 |
1; |
| 161 |
|
| 162 |
__END__ |
| 163 |
|
| 164 |
=pod |
| 165 |
|
| 166 |
=encoding UTF-8 |
| 167 |
|
| 168 |
=head1 NAME |
| 169 |
|
| 170 |
Dancer2::Handler::AutoPage - Class for handling the AutoPage feature |
| 171 |
|
| 172 |
=head1 VERSION |
| 173 |
|
| 174 |
version 2.2.2 |
| 175 |
|
| 176 |
=head1 DESCRIPTION |
| 177 |
|
| 178 |
The AutoPage feature is a Handler (turned off by default) that is |
| 179 |
responsible for serving pages that match an existing template. If a |
| 180 |
view exists with a name that matches the requested path, Dancer2 |
| 181 |
processes the request using the Autopage handler. |
| 182 |
|
| 183 |
To turn it add to your config file: |
| 184 |
|
| 185 |
auto_page: 1 |
| 186 |
|
| 187 |
This allows you to easily serve simple pages without having to write a |
| 188 |
route definition for them. |
| 189 |
|
| 190 |
If there's no view with the name request, the route passes, allowing |
| 191 |
other matching routes to be dispatched. |
| 192 |
|
| 193 |
=head1 METHODS |
| 194 |
|
| 195 |
=head2 register |
| 196 |
|
| 197 |
Creates the routes. |
| 198 |
|
| 199 |
=head2 code |
| 200 |
|
| 201 |
A code reference that processes the route request. |
| 202 |
|
| 203 |
=head2 methods |
| 204 |
|
| 205 |
The methods that should be served for autopages. |
| 206 |
|
| 207 |
Default: B<head>, B<get>. |
| 208 |
|
| 209 |
=head2 regexp |
| 210 |
|
| 211 |
The regexp (path) we want to match. |
| 212 |
|
| 213 |
Default: B</:page>. |
| 214 |
|
| 215 |
=head1 AUTHOR |
| 216 |
|
| 217 |
Dancer Core Developers |
| 218 |
|
| 219 |
=head1 COPYRIGHT AND LICENSE |
| 220 |
|
| 221 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 222 |
|
| 223 |
This is free software; you can redistribute it and/or modify it under |
| 224 |
the same terms as the Perl 5 programming language system itself. |
| 225 |
|
| 226 |
=cut |