Fix Apache, added C::E::Apache::MP1 and C::E::Apache::MP2, added $c->finialize_cookies
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / CGI.pm
index 1588717..29b8421 100644 (file)
@@ -5,7 +5,6 @@ use base 'Catalyst::Engine';
 use URI;
 
 require CGI::Simple;
-require CGI::Cookie;
 
 $CGI::Simple::POST_MAX        = 1048576;
 $CGI::Simple::DISABLE_UPLOADS = 0;
@@ -41,11 +40,6 @@ application module:
 
     use Catalyst qw(-Engine=CGI);
 
-Catalyst::Engine::CGI generates a full set of HTTP headers, which means that
-applications using the engine must be be configured as "Non-parsed Headers"
-scripts (at least when running under Apache).  To configure this under Apache
-name the starting with C<nph->.
-
 The performance of this way of using Catalyst is not expected to be
 useful in production applications, but it may be helpful for development.
 
@@ -53,10 +47,6 @@ useful in production applications, but it may be helpful for development.
 
 =over 4
 
-=item $c->run
-
-To be called from a CGI script to start the Catalyst application.
-
 =item $c->cgi
 
 This config parameter contains the C<CGI::Simple> object.
@@ -65,7 +55,7 @@ This config parameter contains the C<CGI::Simple> object.
 
 =head1 OVERLOADED METHODS
 
-This class overloads some methods from C<Catalyst>.
+This class overloads some methods from C<Catalyst::Engine>.
 
 =over 4
 
@@ -75,23 +65,14 @@ This class overloads some methods from C<Catalyst>.
 
 sub finalize_headers {
     my $c = shift;
-    my %headers = ( -nph => 1 );
+    my %headers;
+
     $headers{-status} = $c->response->status if $c->response->status;
+
     for my $name ( $c->response->headers->header_field_names ) {
-        $headers{"-$name"} = $c->response->headers->header($name);
+        $headers{"-$name"} = $c->response->header($name);
     }
-    my @cookies;
-    while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
-        push @cookies, $c->cgi->cookie(
-            -name    => $name,
-            -value   => $cookie->{value},
-            -expires => $cookie->{expires},
-            -domain  => $cookie->{domain},
-            -path    => $cookie->{path},
-            -secure  => $cookie->{secure} || 0
-        );
-    }
-    $headers{-cookie} = \@cookies if @cookies;
+
     print $c->cgi->header(%headers);
 }
 
@@ -116,14 +97,6 @@ sub prepare_connection {
     $c->req->address( $c->cgi->remote_addr );
 }
 
-=item $c->prepare_cookies
-
-Sets up cookies.
-
-=cut
-
-sub prepare_cookies { shift->req->cookies( { CGI::Cookie->fetch } ) }
-
 =item $c->prepare_headers
 
 =cut
@@ -145,6 +118,9 @@ sub prepare_headers {
 
 sub prepare_parameters {
     my $c    = shift;
+
+    $c->cgi->parse_query_string;
     my %vars = $c->cgi->Vars;
     while ( my ( $key, $value ) = each %vars ) {
         my @values = split "\0", $value;
@@ -159,19 +135,29 @@ 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/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
+    $path =~  s/^\///;
+
+    $c->req->base($base);
+    $c->req->path($path);
 }
 
 =item $c->prepare_request
@@ -196,6 +182,10 @@ sub prepare_uploads {
     }
 }
 
+=item $c->run
+
+=cut
+
 sub run { shift->handler }
 
 =back