Make legacy IIS env fixing work again
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index cec982f..39962a6 100644 (file)
@@ -34,6 +34,8 @@ use Catalyst::EngineLoader;
 use utf8;
 use Carp qw/croak carp shortmess/;
 use Try::Tiny;
+use Plack::Middleware::Conditional;
+use Plack::Middleware::ReverseProxy;
 
 BEGIN { require 5.008004; }
 
@@ -81,7 +83,7 @@ __PACKAGE__->stats_class('Catalyst::Stats');
 
 # Remember to update this in Catalyst::Runtime as well!
 
-our $VERSION = '5.89001';
+our $VERSION = '5.89002';
 
 sub import {
     my ( $class, @arguments ) = @_;
@@ -2642,7 +2644,7 @@ sub _setup_psgi_app {
             if -e $psgi_file;
     }
 
-    return $app->_wrapped_legacy_psgi_app;
+    return $app->_wrapped_legacy_psgi_app($app->psgi_app);
 }
 
 # Note - this is for back compatibility. Catalyst should not know or care about
@@ -2650,10 +2652,10 @@ sub _setup_psgi_app {
 #        use the ReverseProxy middleware yourself if you want it in a .psgi
 #        file.
 sub _wrapped_legacy_psgi_app {
-    my ($app) = @_;
+    my ($app, $psgi_app) = @_;
 
-    return Plack::Middleware::Conditional->wrap(
-        $app->psgi_app,
+    $psgi_app = Plack::Middleware::Conditional->wrap(
+        $psgi_app,
         builder   => sub { Plack::Middleware::ReverseProxy->wrap($_[0]) },
         condition => sub {
             my ($env) = @_;
@@ -2662,6 +2664,65 @@ sub _wrapped_legacy_psgi_app {
                 || $app->config->{using_frontend_proxy};
         },
     );
+
+    # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
+    # http://lists.scsys.co.uk/pipermail/catalyst/2006-June/008361.html
+    # Thanks to Mark Blythe for this fix
+    #
+    # Note that this has probably the same effect as
+    # Plack::Middleware::LighttpdScriptNameFix and we should switch to that if
+    # we can.
+    $psgi_app = Plack::Middleware::Conditional->wrap(
+        $psgi_app,
+        builder => sub {
+            my ($to_wrap) = @_;
+            return sub {
+                my ($env) = @_;
+                $env->{PATH_INFO} ||= delete $env->{SCRIPT_NAME};
+                return $to_wrap->($env);
+            };
+        },
+        condition => sub {
+            my ($env) = @_;
+            my $server = $env->{SERVER_SOFTWARE};
+            return unless $server;
+            return $server =~ /lighttpd/ ? 1 : 0;
+        },
+    );
+
+    $psgi_app = Plack::Middleware::Conditional->wrap(
+        $psgi_app,
+        builder => sub {
+            my ($to_wrap) = @_;
+            return sub {
+                my ($env) = @_;
+
+                my @script_name = split(m!/!, $env->{PATH_INFO});
+                my @path_translated = split(m!/|\\\\?!, $env->{PATH_TRANSLATED});
+                my @path_info;
+
+                while ($script_name[$#script_name] eq $path_translated[$#path_translated]) {
+                    pop(@path_translated);
+                    unshift(@path_info, pop(@script_name));
+                }
+
+                unshift(@path_info, '', '');
+
+                $env->{PATH_INFO} = join('/', @path_info);
+                $env->{SCRIPT_NAME} = join('/', @script_name);
+
+                return $to_wrap->($env);
+            };
+        },
+        condition => sub {
+            my ($env) = @_;
+            my $server = $env->{SERVER_SOFTWARE};
+            return unless $server;
+            return $server =~ /IIS\/[6-9]\.[0-9]/ ? 1 : 0;
+        },
+    );
+
+    return $psgi_app;
 }
 
 =head2 $c->psgi_app