From 8dae231543259281f944bf5a8997b1a64420ede5 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sun, 26 Feb 2017 01:32:59 +0000 Subject: Bug 1291006 - Under PSGI/Plack, non-existing files lead to index.cgi --- app.psgi | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/app.psgi b/app.psgi index 5ac3cf14f..50e341f6b 100644 --- a/app.psgi +++ b/app.psgi @@ -46,6 +46,8 @@ 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 ); @@ -53,41 +55,54 @@ my $app = builder { 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) { -- cgit v1.2.3-24-g4f1b