]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1291006 - Under PSGI/Plack, non-existing files lead to index.cgi
authorDylan William Hardison <dylan@hardison.net>
Sun, 26 Feb 2017 01:32:59 +0000 (01:32 +0000)
committerDylan William Hardison <dylan@hardison.net>
Sun, 26 Mar 2017 02:06:25 +0000 (19:06 -0700)
app.psgi

index 5ac3cf14f7c3c790415d15af4de4ee423253a498..50e341f6ba9fac1a015b37bbafe27f6908ec3553 100644 (file)
--- 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) {