trailing slash for non-root deployment fix by jayk
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
index 56d988f..a86023a 100644 (file)
@@ -5,6 +5,7 @@ use base 'Catalyst::Engine';
 use NEXT;
 use URI;
 
+my $uri_proto=URI->new();
 __PACKAGE__->mk_accessors('env');
 
 =head1 NAME
@@ -43,8 +44,7 @@ sub finalize_headers {
 
     $c->response->header( Status => $c->response->status );
 
-    print $c->response->headers->as_string("\015\012");
-    print "\015\012";
+    print $c->response->headers->as_string("\015\012") . "\015\012";
 }
 
 =head2 $self->prepare_connection($c)
@@ -94,10 +94,10 @@ sub prepare_headers {
     local (*ENV) = $self->env || \%ENV;
 
     # Read headers from %ENV
-    while ( my ( $header, $value ) = each %ENV ) {
+    foreach my $header ( keys %ENV ) {
         next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i;
         ( my $field = $header ) =~ s/^HTTPS?_//;
-        $c->req->headers->header( $field => $value );
+        $c->req->headers->header( $field => $ENV{$header} );
     }
 }
 
@@ -110,9 +110,16 @@ sub prepare_path {
     local (*ENV) = $self->env || \%ENV;
 
     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{REDIRECT_URL} || $ENV{SCRIPT_NAME} || '/';
+    my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
+    my $port      = $ENV{SERVER_PORT} || 80;
+    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:
@@ -130,10 +137,14 @@ sub prepare_path {
         $port = $c->request->secure ? 443 : 80;
     }
 
-    my $path = $base_path . $ENV{PATH_INFO};
+    # set the base URI
+    # base must end in a slash
+    $base_path .= '/' unless ( $base_path =~ /\/$/ );
+
+    my $path = $base_path . ( $ENV{PATH_INFO} || '' );
     $path =~ s{^/+}{};
 
-    my $uri = URI->new;
+    my $uri = $uri_proto->clone;
     $uri->scheme($scheme);
     $uri->host($host);
     $uri->port($port);
@@ -143,10 +154,6 @@ sub prepare_path {
     # sanitize the URI
     $uri = $uri->canonical;
     $c->request->uri($uri);
-
-    # set the base URI
-    # base must end in a slash
-    $base_path .= '/' unless ( $base_path =~ /\/$/ );
     my $base = $uri->clone;
     $base->path_query($base_path);
     $c->request->base($base);