fix content-length for CGI scripts that don't print any headers
[catagits/HTTP-Request-AsCGI.git] / lib / HTTP / Request / AsCGI.pm
index f2ddbff..decddd6 100644 (file)
@@ -5,8 +5,6 @@ use warnings;
 use bytes;
 use base 'Class::Accessor::Fast';
 
-our $VERSION = '0.9';
-
 use Carp;
 use HTTP::Response;
 use IO::Handle;
@@ -17,6 +15,7 @@ use URI::Escape ();
 __PACKAGE__->mk_accessors(qw[ environment request stdin stdout stderr ]);
 
 # old typo
+
 =begin Pod::Coverage
 
   enviroment
@@ -54,6 +53,9 @@ sub new {
     $uri->port(80)          unless $uri->port;
     $uri->host_port($host)  unless !$host || ( $host eq $uri->host_port );
 
+    # Get it before canonicalized so REQUEST_URI can be as raw as possible
+    my $request_uri = $uri->path_query;
+
     $uri = $uri->canonical;
 
     my $environment = {
@@ -66,11 +68,11 @@ sub new {
         SERVER_NAME       => $uri->host,
         SERVER_PORT       => $uri->port,
         SERVER_PROTOCOL   => $request->protocol || 'HTTP/1.1',
-        SERVER_SOFTWARE   => "HTTP-Request-AsCGI/$VERSION",
+        SERVER_SOFTWARE   => 'HTTP-Request-AsCGI/' . our $VERSION,
         REMOTE_ADDR       => '127.0.0.1',
         REMOTE_HOST       => 'localhost',
         REMOTE_PORT       => int( rand(64000) + 1000 ),                   # not in RFC 3875
-        REQUEST_URI       => $uri->path_query,                            # not in RFC 3875
+        REQUEST_URI       => $request_uri,                                # not in RFC 3875
         REQUEST_METHOD    => $request->method,
         @_
     };
@@ -156,7 +158,7 @@ sub setup {
 
     {
         no warnings 'uninitialized';
-        %ENV = %{ $self->environment };
+        %ENV = (%ENV, %{ $self->environment });
     }
 
     if ( $INC{'CGI.pm'} ) {
@@ -181,7 +183,7 @@ sub response {
         $headers .= $line;
         last if $headers =~ /\x0d?\x0a\x0d?\x0a$/;
     }
-    
+
     unless ( defined $headers ) {
         $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
     }
@@ -208,7 +210,7 @@ sub response {
         $response->code($code);
         $response->message($message);
     }
-    
+
     my $length = ( stat( $self->stdout ) )[7] - tell( $self->stdout );
 
     if ( $response->code == 500 && !$length ) {
@@ -234,7 +236,7 @@ sub response {
     }
     else {
 
-        my $length = 0;
+        my $length = defined $response->content ? length( $response->content ) : 0;
 
         while ( $self->stdout->read( my $buffer, 4096 ) ) {
             $length += length($buffer);
@@ -306,36 +308,36 @@ __END__
     use CGI;
     use HTTP::Request;
     use HTTP::Request::AsCGI;
-    
+
     my $request = HTTP::Request->new( GET => 'http://www.host.com/' );
     my $stdout;
-    
+
     {
         my $c = HTTP::Request::AsCGI->new($request)->setup;
         my $q = CGI->new;
-        
+
         print $q->header,
               $q->start_html('Hello World'),
               $q->h1('Hello World'),
               $q->end_html;
-        
+
         $stdout = $c->stdout;
-        
+
         # environment and descriptors will automatically be restored
         # when $c is destructed.
     }
-    
+
     while ( my $line = $stdout->getline ) {
         print $line;
     }
-    
+
 =head1 DESCRIPTION
 
 Provides a convenient way of setting up an CGI environment from an HTTP::Request.
 
 =head1 METHODS
 
-=over 4 
+=over 4
 
 =item new ( $request [, key => value ] )
 
@@ -344,7 +346,7 @@ by optional pairs of environment key and value.
 
 =item environment
 
-Returns a hashref containing the environment that will be used in setup. 
+Returns a hashref containing the environment that will be used in setup.
 Changing the hashref after setup has been called will have no effect.
 
 =item setup