Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add default document support

I want to automatically serve the index.html when you browse to the directory.
I didn't actually find any references on how so I had to figure it out myself.

Now you can browse to http://openbsd.somedomain.net/nagios and it will do the
right thing.
  • Loading branch information...
commit 26f8c5ac08423bd5579498a4ca5d609295a79e89 1 parent 9adb474
@afresh1 authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 OpenBSDtracker
View
28 OpenBSDtracker
@@ -5,6 +5,8 @@ use WWW::Opentracker::Stats;
use Mojo::Date;
#use Convert::Bencode qw( bdecode );
+use File::Spec::Functions 'catfile';
+
my $baseurl = 'http://localhost:6969/';
app->config(
@@ -90,6 +92,32 @@ get '/detail/:hash' => sub {
$self->render( 'detail', layout => 'default' );
};
+app->hook( before_dispatch => sub {
+ my $tx = shift;
+ my $default_file = 'index.html';
+
+ # Canonical path
+ my $stash = $tx->stash;
+ my $rel = $stash->{path} || $tx->req->url->path->clone->canonicalize;
+
+ # Append the default file (that I want) if there is a trailing /
+ $rel = catfile split( '/', $rel ), $default_file
+ if substr( $rel, -1 ) eq '/';
+
+ for my $path ( @{ app->static->paths } ) {
+ my $file = catfile $path, $rel;
+ if ( -d $file ) {
+ next unless -e catfile $file, $default_file; # no file disclsure
+ $tx->redirect_to("$rel/");
+ return;
+ }
+ elsif ( -e _ ) {
+ $stash->{path} = $rel;
+ return;
+ }
+ }
+});
+
app->hook(after_dispatch => sub {
my $tx = shift;
$tx->res->headers->header(Vary => 'Accept-Encoding');
Please sign in to comment.
Something went wrong with that request. Please try again.