Debian Codegraph @

lib/Dancer2/Handler/File.pm

raw
1 package Dancer2::Handler::File;
2 # ABSTRACT: class for handling file content rendering
3 $Dancer2::Handler::File::VERSION = '2.2.2';
4 use Carp 'croak';
5 use Moo;
6 use HTTP::Date;
7 use Dancer2::Core::MIME;
8 use Dancer2::Core::Types;
9 use Path::Tiny ();
10
11 with qw<
12 Dancer2::Core::Role::Handler
13 Dancer2::Core::Role::StandardResponses
14 Dancer2::Core::Role::Hookable
15 >;
16
17 sub hook_aliases {
18 {
19 before_file_render => 'handler.file.before_render',
20 after_file_render => 'handler.file.after_render',
21 }
22 }
23
24 sub supported_hooks { values %{ shift->hook_aliases } }
25
26 has mime => (
27 is => 'ro',
28 isa => InstanceOf ['Dancer2::Core::MIME'],
29 default => sub { Dancer2::Core::MIME->new },
30 );
31
32 has encoding => (
33 is => 'ro',
34 default => sub {'utf-8'},
35 );
36
37 has public_dir => (
38 is => 'ro',
39 lazy => 1,
40 builder => '_build_public_dir',
41 );
42
43 has _public_dir_path => (
44 is => 'ro',
45 lazy => 1,
46 builder => '_build_public_dir_path',
47 init_arg => undef,
48 );
49
50 has regexp => (
51 is => 'ro',
52 default => sub {'/**'},
53 );
54
55 sub _build_public_dir {
56 my $self = shift;
57 return $self->app->config->{public_dir}
58 || $ENV{DANCER_PUBLIC}
59 || Path::Tiny::path( $self->app->location, 'public' )->stringify;
60 }
61
62 sub _build_public_dir_path {
63 my $self = shift;
64 return Path::Tiny::path( $self->public_dir );
65 }
66
67 sub register {
68 my ( $self, $app ) = @_;
69
70 # don't register the handler if no valid public dir
71 return if !$self->_public_dir_path->is_dir;
72
73 $app->add_route(
74 method => $_,
75 regexp => $self->regexp,
76 code => $self->code( $app->prefix ),
77 ) for $self->methods;
78 }
79
80 sub methods { ( 'head', 'get' ) }
81
82 sub code {
83 my ( $self, $prefix ) = @_;
84
85 sub {
86 my $app = shift;
87 my $prefix = shift;
88 my $path = $app->request->path_info;
89
90 if ( $path =~ /\0/ ) {
91 return $self->standard_response( $app, 400 );
92 }
93
94 if ( $prefix && $prefix ne '/' ) {
95 $path =~ s/^\Q$prefix\E//;
96 }
97
98 my $file_path = Path::Tiny::path( $self->_public_dir_path, $path );
99 my $file_path_str = $file_path->stringify;
100
101 if ( !-f $file_path_str ) {
102 $app->response->has_passed(1);
103 return;
104 }
105
106 # Path::Tiny does not collapse '../' segments, so the joined path
107 # above may still point outside public_dir even though it is a
108 # readable file. Resolve it and refuse anything that escapes,
109 # the same containment check send_file uses at
110 # Dancer2/Core/App.pm:1180-1182. This is only safe to call now: the
111 # -f check above guarantees the file (and so every directory in its
112 # path) exists, so realpath cannot die here.
113 $file_path = $file_path->realpath;
114 $file_path_str = $file_path->stringify;
115
116 if ( !$self->_public_dir_path->realpath->subsumes($file_path) ) {
117 return $self->standard_response( $app, 403 );
118 }
119
120 if ( !-r $file_path_str ) {
121 return $self->standard_response( $app, 403 );
122 }
123
124 # Now we are sure we can render the file...
125 $self->execute_hook( 'handler.file.before_render', $file_path_str );
126
127 # Read file content as bytes
128 my $content = $file_path->slurp_raw;
129
130 # Assume m/^text/ mime types are correctly encoded
131 my $content_type = $self->mime->for_file($file_path_str) || 'text/plain';
132 if ( $content_type =~ m!^text/! ) {
133 $content_type .= "; charset=" . ( $self->encoding || "utf-8" );
134 }
135
136 my @stat = stat $file_path_str;
137
138 $app->response->header('Content-Type')
139 or $app->response->header( 'Content-Type', $content_type );
140
141 $app->response->header('Content-Length')
142 or $app->response->header( 'Content-Length', $stat[7] );
143
144 $app->response->header('Last-Modified')
145 or $app->response->header(
146 'Last-Modified',
147 HTTP::Date::time2str( $stat[9] )
148 );
149
150 $app->response->content($content);
151 $app->response->is_encoded(1); # bytes are already encoded
152 $self->execute_hook( 'handler.file.after_render', $app->response );
153 return ( $app->request->method eq 'GET' ) ? $content : '';
154 };
155 }
156
157 1;
158
159 __END__
160
161 =pod
162
163 =encoding UTF-8
164
165 =head1 NAME
166
167 Dancer2::Handler::File - class for handling file content rendering
168
169 =head1 VERSION
170
171 version 2.2.2
172
173 =head1 AUTHOR
174
175 Dancer Core Developers
176
177 =head1 COPYRIGHT AND LICENSE
178
179 This software is copyright (c) 2026 by Alexis Sukrieh.
180
181 This is free software; you can redistribute it and/or modify it under
182 the same terms as the Perl 5 programming language system itself.
183
184 =cut