From d78ac827f0dd6bc61ef8c2b8d7f096f6bfb01e0f Mon Sep 17 00:00:00 2001 From: Chad Wallace Date: Thu, 26 May 2022 09:44:53 -0700 Subject: [PATCH] Render directory contents using methods Instead of $dir_file and $dir_page lexicals in Plack::App::Directory, I made them methods, and also added render_file and render_dir methods in case a subclasser wants to do more than just sprintfs. --- lib/Plack/App/Directory.pm | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 48443ca09..4d3960f06 100644 --- a/lib/Plack/App/Directory.pm +++ b/lib/Plack/App/Directory.pm @@ -10,8 +10,11 @@ use URI::Escape; use Plack::Request; # Stolen from rack/directory.rb -my $dir_file = "%s%s%s%s"; -my $dir_page = <%s%s%s%s"; +} +sub dir_page { + return <<'PAGE'; %s @@ -37,6 +40,16 @@ table { width:100%%; }
PAGE +} + +sub render_file { + my ($self, @f) = @_; + return sprintf $self->dir_file, map { Plack::Util::encode_html($_) } @f; +} +sub render_dir { + my ($self, $path, $files) = @_; + return sprintf $self->dir_page, $path, $path, $files; +} sub should_handle { my($self, $file) = @_; @@ -99,9 +112,9 @@ sub serve_path { my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}"); my $files = join "\n", map { my $f = $_; - sprintf $dir_file, map Plack::Util::encode_html($_), @$f; + $self->render_file( @$f ); } @files; - my $page = sprintf $dir_page, $path, $path, $files; + my $page = $self->render_dir( $path, $files ); return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; }