reworked base and path handling
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
index 1efa954..b48cf2f 100644 (file)
@@ -106,6 +106,16 @@ sub finalize_output {
     print $c->response->output;
 }
 
+=item $c->prepare_connection
+
+=cut
+
+sub prepare_connection {
+    my $c = shift;
+    $c->req->hostname( $c->cgi->remote_host );
+    $c->req->address( $c->cgi->remote_addr );
+}
+
 =item $c->prepare_cookies
 
 Sets up cookies.
@@ -125,6 +135,8 @@ sub prepare_headers {
         ( my $field = $header ) =~ s/^HTTPS?_//;
         $c->req->headers->header( $field => $c->cgi->http($header) );
     }
+    $c->req->headers->header( 'Content-Type'   => $c->cgi->content_type );
+    $c->req->headers->header( 'Content-Length' => $c->cgi->content_length );
 }
 
 =item $c->prepare_parameters
@@ -147,19 +159,28 @@ sub prepare_parameters {
 
 sub prepare_path {
     my $c = shift;
-    $c->req->path( $c->cgi->url( -absolute => 1, -path_info => 1 ) );
-    my $loc = $c->cgi->url( -absolute => 1 );
-    no warnings 'uninitialized';
-    $c->req->{path} =~ s/^($loc)?\///;
-    $c->req->{path} .= '/' if $c->req->path eq $loc;
-    my $base = $c->cgi->url;
-    if ( $ENV{CATALYST_TEST} ) {
-        my $script = $c->cgi->script_name;
-        $base =~ s/$script$//i;
+
+    my $base;
+    {
+        my $scheme = $ENV{HTTPS} ? 'https' : 'http';
+        my $host   = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
+        my $port   = $ENV{SERVER_PORT} || 80;
+        my $path   = $ENV{SCRIPT_NAME} || '/';
+
+        $base = URI->new;
+        $base->scheme($scheme);
+        $base->host($host);
+        $base->port($port);
+        $base->path($path);
+
+        $base = $base->canonical->as_string;
     }
-    $base = URI->new($base);
-    $base->path('/') if ( $ENV{CATALYST_TEST} || !$base->path );
-    $c->req->base( $base->as_string );
+
+    my $path = $ENV{PATH_INFO} || '/';
+    $path =~  s/^\///;
+
+    $c->req->base($base);
+    $c->req->path($path);
 }
 
 =item $c->prepare_request
@@ -175,6 +196,7 @@ sub prepare_request { shift->cgi( CGI::Simple->new ) }
 sub prepare_uploads {
     my $c = shift;
     for my $name ( $c->cgi->upload ) {
+        next unless defined $name;
         $c->req->uploads->{$name} = {
             fh   => $c->cgi->upload($name),
             size => $c->cgi->upload_info( $name, 'size' ),