Skip to content

Commit

Permalink
Bug 1291006 - Under PSGI/Plack, non-existing files lead to index.cgi
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanwh committed Mar 26, 2017
1 parent c636314 commit 8dae231
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions app.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,63 @@ use constant STATIC => qw(
skins
);

$ENV{BZ_PLACK} = 'Plack/' . Plack->VERSION;

my $app = builder {
my $static_paths
= join( '|', sort { length $b <=> length $a || $a cmp $b } STATIC );
enable 'Static',
path => qr{^/($static_paths)/},
root => Bugzilla::Constants::bz_locations->{cgi_path};

$ENV{BZ_PLACK} = 'Plack/' . Plack->VERSION;

my $map = Plack::App::URLMap->new;

my @cgis = glob('*.cgi');
my $shutdown_app
= Plack::App::WrapCGI->new( script => 'shutdown.cgi' )->to_app;
my @scripts = glob('*.cgi');

foreach my $cgi_script (@cgis) {
my %cgi_app;
foreach my $script (@scripts) {
my $base_name = basename($script);

next if $base_name eq 'shutdown.cgi';
my $app
= eval { Plack::App::WrapCGI->new( script => $cgi_script )->to_app };
= eval { Plack::App::WrapCGI->new( script => $script )->to_app };

# Some CGI scripts won't compile if not all optional Perl modules are
# installed. That's expected.
if ($@) {
warn "Cannot compile $cgi_script. Skipping!\n";
unless ($app) {
warn "Cannot compile $script: $@\nSkipping!\n";
next;
}

my $wrapper = sub {
my $ret = Bugzilla::init_page();
my $res
= ( $ret eq '-1' && $cgi_script ne 'editparams.cgi' )
= ( $ret eq '-1' && $script ne 'editparams.cgi' )
? $shutdown_app->(@_)
: $app->(@_);
Bugzilla::_cleanup();
return $res;
};
$cgi_app{$base_name} = $wrapper;
}

my $base_name = basename($cgi_script);
$map->map( '/' => $wrapper ) if $cgi_script eq 'index.cgi';
$map->map( '/rest' => $wrapper ) if $cgi_script eq 'rest.cgi';
$map->map( "/$base_name" => $wrapper );
foreach my $cgi_name ( keys %cgi_app ) {
mount "/$cgi_name" => $cgi_app{$cgi_name};
}
$map->to_app;

# so mount / => $app will make *all* files redirect to the index.
# instead we use an inline middleware to rewrite / to /index.cgi
enable sub {
my $app = shift;
return sub {
my $env = shift;
$env->{PATH_INFO} = '/index.cgi' if $env->{PATH_INFO} eq '/';
return $app->($env);
};
};

mount "/rest" => $cgi_app{"rest.cgi"};

};

unless (caller) {
Expand Down

0 comments on commit 8dae231

Please sign in to comment.