package Apache::BrowseSee; use strict; use Apache::Constants qw(:common); use File::Spec::Functions qw(catdir canonpath curdir updir); use File::Basename 'dirname'; my $document_root = '/home/insaniac/work/code/perl/www'; sub new {bless {}, shift} sub handler ($$) { my ($self,$r)= @_; $self = $self->new unless ref $self; $self->{r} = $r; $self->{r}->document_root($document_root); $self->{dir}= $r->path_info || '/'; $self->{uri}= $r->uri || '/'; $self->{dirs} = {}; $self->{files} = {}; eval { $self->fetch }; return NOT_FOUND if $@; $self->head; print "current dir:",$self->{dir},"
"; print "path info:",$r->path_info,"
"; print "uri:",$r->uri,"
"; $self->render; $self->tail; return OK; } sub fetch { my $self = shift; my $doc_root = Apache->document_root; my $base_dir = canonpath(catdir($doc_root,$self->{dir})); my $base_entry = $self->{dir} eq '/' ? '' : $self->{dir}; my $dh = Apache::gensym(); opendir $dh, $base_dir or die "Cannot open $base_dir: $!"; for (readdir $dh) { next if $_ eq curdir(); my $full_dir = catdir $base_dir, $_; my $entry = "$base_dir/$_"; if ( -d $full_dir ) { if ( $_ eq updir() ){ $entry = dirname $self->{dir}; next if catdir($base_dir,$entry) eq $doc_root; } $self->{dirs}{$_} = $entry; } else { $self->{files}{$_} = $entry; } } closedir($dh); } sub head { my $self = shift; $self->{r}->send_http_header("text/html"); print "Dir: $self->{dir}"; print "

Browse And See

"; } sub tail { my $self = shift; print ""; } sub render { my $self = shift; my $location = $self->{r}->location; print "

Current directory: ${location}$self->{dir}

"; print "

location:$location

"; print qq{$_
} for sort keys %{$self->{dirs} || {} }; print qq{$_
} for sort keys %{ $self->{files} || {} }; } 1;