Skip to content

Commit

Permalink
Reworked Directory to not depend on Path::Class. One less dependencies!
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Feb 3, 2010
1 parent 1a25791 commit c15515d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Makefile.PL
Expand Up @@ -24,8 +24,6 @@ requires 'Filesys::Notify::Simple'; # plackup -r
requires 'Hash::MultiValue', 0.05; # Plack::Request
requires 'HTTP::Body'; # Plack::Request

requires 'Path::Class'; # App::File

build_requires 'Test::More', 0.88;
build_requires 'Test::TCP', 0.11;
test_requires 'Test::Requires';
Expand Down
31 changes: 17 additions & 14 deletions lib/Plack/App/Directory.pm
Expand Up @@ -3,9 +3,9 @@ use parent qw(Plack::App::File);
use strict;
use warnings;
use Plack::Util;
use Path::Class;
use HTTP::Date;
use Plack::MIME;
use DirHandle;

# Stolen from rack/directory.rb
my $dir_file = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>";
Expand Down Expand Up @@ -42,31 +42,34 @@ sub should_handle {
}

sub serve_path {
my($self, $env, $file, $fullpath) = @_;
my($self, $env, $dir, $fullpath) = @_;

if (-f $file) {
return $self->SUPER::serve_path($env, $file, $fullpath);
if (-f $dir) {
return $self->SUPER::serve_path($env, $dir, $fullpath);
}

my $dir = dir($file);

my @files = ([ "../", "Parent Directory", '', '', '' ]);

my @children = map { [ ($_->is_dir ? ($_->dir_list)[-1] : $_->basename), $_ ] } $dir->children;
my $dh = DirHandle->new($dir);
my @children;
while (defined(my $ent = $dh->read)) {
push @children, $ent;
}

for my $child (sort { $a->[0] cmp $b->[0] } @children) {
my($basename, $file) = @$child;
for my $basename (sort { $a cmp $b } @children) {
my $file = "$dir/$basename";
my $url = $env->{SCRIPT_NAME} . $env->{PATH_INFO} . $basename;

if ($file->is_dir) {
my $is_dir = -d $file;
my @stat = stat _;

if ($is_dir) {
$basename .= "/";
$url .= "/";
}

my $mime_type = $file->is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
my $stat = $file->stat;

push @files, [ $url, $basename, $stat->size, $mime_type, HTTP::Date::time2str($stat->mtime) ];
my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
}

my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}");
Expand Down
8 changes: 6 additions & 2 deletions lib/Plack/App/File.pm
Expand Up @@ -37,14 +37,18 @@ sub call {
sub locate_file {
my($self, $env) = @_;

my $path = $env->{PATH_INFO};
my $path = $env->{PATH_INFO} || '';
if ($path =~ m!\.\.[/\\]!) {
return $self->return_403;
}

my $docroot = $self->root || ".";
my @path = split '/', $path;
shift @path if $path[0] eq '';
if (@path) {
shift @path if $path[0] eq '';
} else {
@path = ('.');
}

my($file, @path_info);
while (@path) {
Expand Down

0 comments on commit c15515d

Please sign in to comment.