Fix IIS FastCGI support as-per we do with lighty + refactroing and tests.
Tomas Doran [Wed, 7 Jan 2009 18:29:58 +0000 (18:29 +0000)]
Changes
lib/Catalyst/Engine/FastCGI.pm
t/unit_core_engine_fixenv-iis6.t [new file with mode: 0644]
t/unit_core_engine_fixenv-lighttpd.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index e73ac51..406c65a 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 # This file documents the revision history for Perl extension Catalyst.
 
 5.7XXXXXX XXXX
+        - Add environment hack for FastCGI under IIS (Simon Bertrang)
+          - Test for this and preexisting Lighty hack (Simon Bertrang)
         - Change streaming test to serve itself rather than 01use.t, making test
           sync for engines easier (t0m)
         - Workaround change in LWP that broke a cookie test (RT #40037)
index 7d54beb..ec85acd 100644 (file)
@@ -134,13 +134,8 @@ sub run {
 
     while ( $request->Accept >= 0 ) {
         $proc_manager && $proc_manager->pm_pre_dispatch();
-        
-        # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
-        # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
-        # Thanks to Mark Blythe for this fix
-        if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
-            $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
-        }
+
+        $self->_fix_env( \%env );
         
         $class->handle_request( env => \%env );
         
@@ -207,6 +202,43 @@ sub daemon_detach {
     POSIX::setsid();
 }
 
+=head2 $self->_fix_env( $env )
+
+Adjusts the environment variables when necessary.
+
+=cut
+
+sub _fix_env
+{
+    my $self = shift;
+    my $env = shift;
+
+    return unless ( $env->{SERVER_SOFTWARE} );
+
+    # 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
+    if ( $env->{SERVER_SOFTWARE} =~ /lighttpd/ ) {
+        $env->{PATH_INFO} ||= delete $env->{SCRIPT_NAME};
+    }
+    # Fix the environment variables PATH_INFO and SCRIPT_NAME when running under IIS 6.0
+    elsif ( $env->{SERVER_SOFTWARE} =~ /IIS\/6.0/ ) {
+        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);
+    }
+}
+
 1;
 __END__
 
diff --git a/t/unit_core_engine_fixenv-iis6.t b/t/unit_core_engine_fixenv-iis6.t
new file mode 100644 (file)
index 0000000..3b36c3e
--- /dev/null
@@ -0,0 +1,62 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+eval "use FCGI";
+plan skip_all => 'FCGI required' if $@;
+
+plan tests => 2;
+
+require Catalyst::Engine::FastCGI;
+
+my %env = (
+    'SCRIPT_NAME' => '/koo/blurb',
+    'PATH_INFO' => '/koo/blurb',
+    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+    'REQUEST_METHOD' => 'GET',
+    'SCRIPT_FILENAME' => 'C:\\Foo\\script\\blurb',
+    'INSTANCE_META_PATH' => '/LM/W3SVC/793536',
+    'SERVER_SOFTWARE' => 'Microsoft-IIS/6.0',
+    'AUTH_PASSWORD' => '',
+    'AUTH_TYPE' => '',
+    'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)',
+    'REMOTE_PORT' => '1281',
+    'QUERY_STRING' => '',
+    'URL' => '/koo/blurb',
+    'HTTP_ACCEPT_LANGUAGE' => 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3',
+    'FCGI_ROLE' => 'RESPONDER',
+    'HTTP_KEEP_ALIVE' => '300',
+    'CONTENT_TYPE' => '',
+    'LOCAL_ADDR' => '127.0.0.1',
+    'GATEWAY_INTERFACE' => 'CGI/1.1',
+    'HTTPS' => 'off',
+    'DOCUMENT_ROOT' => 'C:\\Foo\\script',
+    'REMOTE_HOST' => '127.0.0.1',
+    'PATH_TRANSLATED' => 'C:\\Foo\\script\\blurb',
+    'APPL_PHYSICAL_PATH' => 'C:\\Foo\\script\\',
+    'SERVER_NAME' => '127.0.0.1',
+    'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
+    'HTTP_CONNECTION' => 'keep-alive',
+    'INSTANCE_ID' => '793536',
+    'CONTENT_LENGTH' => '0',
+    'AUTH_USER' => '',
+    'APPL_MD_PATH' => '/LM/W3SVC/793536/Root/koo',
+    'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
+    'REMOTE_USER' => '',
+    'SERVER_PORT_SECURE' => '0',
+    'SERVER_PORT' => 83,
+    'REMOTE_ADDR' => '127.0.0.1',
+    'SERVER_PROTOCOL' => 'HTTP/1.1',
+    'REQUEST_URI' => '/koo/blurb',
+    'APP_POOL_ID' => 'DefaultAppPool',
+    'HTTP_HOST' => '127.0.0.1:83'
+);
+
+Catalyst::Engine::FastCGI->_fix_env(\%env);
+
+is($env{PATH_INFO}, '//blurb', 'check PATH_INFO');
+is($env{SCRIPT_NAME}, '/koo', 'check SCRIPT_NAME');
+
diff --git a/t/unit_core_engine_fixenv-lighttpd.t b/t/unit_core_engine_fixenv-lighttpd.t
new file mode 100644 (file)
index 0000000..9f37e30
--- /dev/null
@@ -0,0 +1,46 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+eval "use FCGI";
+plan skip_all => 'FCGI required' if $@;
+
+plan tests => 2;
+
+require Catalyst::Engine::FastCGI;
+
+my %env = (
+    'SCRIPT_NAME'          => '/bar',
+    'SERVER_NAME'          => 'localhost:8000',
+    'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
+    'HTTP_CONNECTION'      => 'keep-alive',
+    'PATH_INFO'            => '',
+    'HTTP_ACCEPT'          => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+    'REQUEST_METHOD'       => 'GET',
+    'SCRIPT_FILENAME'      => '/tmp/Foo/root/bar',
+    'HTTP_ACCEPT_CHARSET'  => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
+    'SERVER_SOFTWARE'      => 'lighttpd/1.4.15',
+    'QUERY_STRING'         => '',
+    'REMOTE_PORT'          => '22207',
+    'SERVER_PORT'          => 8000,
+    'REDIRECT_STATUS'      => '200',
+    'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
+    'REMOTE_ADDR'          => '127.0.0.1',
+    'FCGI_ROLE'            => 'RESPONDER',
+    'HTTP_KEEP_ALIVE'      => '300',
+    'SERVER_PROTOCOL'      => 'HTTP/1.1',
+    'REQUEST_URI'          => '/bar',
+    'GATEWAY_INTERFACE'    => 'CGI/1.1',
+    'SERVER_ADDR'          => '127.0.0.1',
+    'DOCUMENT_ROOT'        => '/tmp/Foo/root',
+    'HTTP_HOST'            => 'localhost:8000',
+);
+
+Catalyst::Engine::FastCGI->_fix_env(\%env);
+
+is($env{PATH_INFO}, '/bar', 'check PATH_INFO');
+ok(!exists($env{SCRIPT_NAME}), 'check SCRIPT_NAME');
+