Re-added REDIRECT_URL support with tests
Andy Grundman [Mon, 5 Dec 2005 16:40:56 +0000 (16:40 +0000)]
Changes
lib/Catalyst/Engine/CGI.pm
t/conf/extra.conf.in
t/optional_apache-cgi-rewrite.pl [new file with mode: 0755]

diff --git a/Changes b/Changes
index 1d087d5..b5b4831 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
 This file documents the revision history for Perl extension Catalyst.
 
+5.62
+        - Added REDIRECT_URL support for applications running behind
+          a RewriteRule in Apache. (Carl Franks)
+
 5.61    2005-12-02 00:00:00
         - Fixed ExtUtils::AutoInstall Bootstrap Code in Makefile.PL
 
index d2d041e..9f6698d 100644 (file)
@@ -112,7 +112,14 @@ sub prepare_path {
     my $scheme = $c->request->secure ? 'https' : 'http';
     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
     my $port      = $ENV{SERVER_PORT} || 80;
-    my $base_path = $ENV{SCRIPT_NAME} || '/';
+    my $base_path;
+    if ( exists $ENV{REDIRECT_URL} ) {
+        $base_path = $ENV{REDIRECT_URL};
+        $base_path =~ s/$ENV{PATH_INFO}$//;
+    }
+    else {
+        $base_path = $ENV{SCRIPT_NAME} || '/';
+    }
 
     # If we are running as a backend proxy, get the true hostname
   PROXY_CHECK:
index 3daa2a0..fff041d 100644 (file)
@@ -1,11 +1,19 @@
 # CGI
 <IfModule @CGI_MODULE@>
     ScriptAlias /cgi/ @ServerRoot@/tmp/TestApp/script/testapp_cgi.pl/
+
+    # REDIRECT_URL test
+    <IfModule mod_rewrite.c>
+        <Location /rewrite>
+            RewriteEngine on
+            RewriteRule /rewrite/(.*) /cgi/$1
+        </Location>
+    </IfModule>
 </IfModule>
 
 # FastCGI
 <IfModule mod_fastcgi.c>
     FastCgiIpcDir @ServerRoot@/tmp/tmp
-    FastCgiServer @ServerRoot@/tmp/TestApp/script/testapp_fastcgi.pl -idle-timeout 300 -processes 5
+    FastCgiServer @ServerRoot@/tmp/TestApp/script/testapp_fastcgi.pl -idle-timeout 300 -processes 1
     ScriptAlias /fastcgi/ @ServerRoot@/tmp/TestApp/script/testapp_fastcgi.pl/
 </IfModule>
diff --git a/t/optional_apache-cgi-rewrite.pl b/t/optional_apache-cgi-rewrite.pl
new file mode 100755 (executable)
index 0000000..4007330
--- /dev/null
@@ -0,0 +1,59 @@
+#!perl
+
+# Run all tests against CGI mode under Apache
+#
+# Note, to get this to run properly, you may need to give it the path to your
+# httpd.conf:
+#
+# perl t/optional/apache-cgi.pl -httpd_conf /etc/apache/httpd.conf
+
+use strict;
+use warnings;
+
+use Apache::Test;
+use Apache::TestRun ();
+
+use File::Path;
+use File::Copy::Recursive;
+use FindBin;
+use IO::Socket;
+
+# clean up
+rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
+
+# create a TestApp and copy the test libs into it
+mkdir "$FindBin::Bin/../t/tmp";
+chdir "$FindBin::Bin/../t/tmp";
+system "$FindBin::Bin/../script/catalyst.pl TestApp";
+chdir "$FindBin::Bin/..";
+File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
+
+# remove TestApp's tests so Apache::Test doesn't try to run them
+rmtree 't/tmp/TestApp/t';
+
+$ENV{CATALYST_SERVER} = 'http://localhost:8529/rewrite';
+
+Apache::TestRun->new->run(@ARGV);
+
+# clean up if the server has shut down
+# this allows the test files to stay around if the user ran -start-httpd
+if ( !check_port( 'localhost', 8529 ) ) {
+    rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
+}
+
+sub check_port {
+    my ( $host, $port ) = @_;
+
+    my $remote = IO::Socket::INET->new(
+        Proto    => "tcp",
+        PeerAddr => $host,
+        PeerPort => $port
+    );
+    if ($remote) {
+        close $remote;
+        return 1;
+    }
+    else {
+        return 0;
+    }
+}