reimplement PATH_INFO unescaping
[catagits/HTTP-Request-AsCGI.git] / lib / HTTP / Request / AsCGI.pm
index 4a81289..841f033 100644 (file)
 package HTTP::Request::AsCGI;
-
+# ABSTRACT: Set up a CGI environment from an HTTP::Request
 use strict;
 use warnings;
 use bytes;
 use base 'Class::Accessor::Fast';
 
-use Carp            qw[croak];
-use HTTP::Response  qw[];
-use IO::File        qw[SEEK_SET];
-use Symbol          qw[];
-use URI::Escape     qw[];
+use Carp;
+use HTTP::Response;
+use IO::Handle;
+use IO::File;
+use URI ();
+use URI::Escape ();
 
-__PACKAGE__->mk_accessors( qw[ is_setup
-                               is_prepared
-                               is_restored
+__PACKAGE__->mk_accessors(qw[ environment request stdin stdout stderr ]);
 
-                               should_dup
-                               should_restore
-                               should_rewind
-                               should_setup_content
+# old typo
+=begin Pod::Coverage
 
-                               environment
-                               request
-                               stdin
-                               stdout
-                               stderr ] );
+  enviroment
 
-our $VERSION = 0.5_01;
+=end Pod::Coverage
 
-sub new {
-    my $class  = ref $_[0] ? ref shift : shift;
-    my $params = {};
+=cut
 
-    if ( @_ % 2 == 0 ) {
-        $params = { @_ };
-    }
-    else {
-        $params = { request => shift, environment => { @_ } };
-    }
+*enviroment = \&environment;
 
-    return bless( {}, $class )->initialize($params);
+my %reserved = map { sprintf('%02x', ord($_)) => 1 } split //, $URI::reserved;
+sub _uri_safe_unescape {
+    my ($s) = @_;
+    $s =~ s/%([a-fA-F0-9]{2})/$reserved{lc($1)} ? "%$1" : chr(hex($1))/ge;
+    $s;
 }
 
-sub initialize {
-    my ( $self, $params ) = @_;
-
-    if ( exists $params->{request} ) {
-        $self->request( $params->{request} );
-    }
-    else {
-        croak("Mandatory parameter 'request' is missing.");
-    }
-
-    if ( exists $params->{environment} ) {
-        $self->environment( { %{ $params->{environment} } } );
-    }
-    else {
-        $self->environment( {} );
-    }
-
-    if ( exists $params->{stdin} ) {
-        $self->stdin( $params->{stdin} );
-    }
-    else {
-        $self->stdin( IO::File->new_tmpfile );
-    }
-
-    if ( exists $params->{stdout} ) {
-        $self->stdout( $params->{stdout} );
-    }
-    else {
-        $self->stdout( IO::File->new_tmpfile );
-    }
-
-    if ( exists $params->{stderr} ) {
-        $self->stderr( $params->{stderr} );
-    }
-
-    if ( exists $params->{dup} ) {
-        $self->should_dup( $params->{dup} ? 1 : 0 );
-    }
-    else {
-        $self->should_dup(1);
-    }
-
-    if ( exists $params->{restore} ) {
-        $self->should_restore( $params->{restore} ? 1 : 0 );
-    }
-    else {
-        $self->should_restore(1);
-    }
-
-    if ( exists $params->{rewind} ) {
-        $self->should_rewind( $params->{rewind} ? 1 : 0 );
-    }
-    else {
-        $self->should_rewind(1);
-    }
+sub new {
+    my $class   = shift;
+    my $request = shift;
 
-    if ( exists $params->{content} ) {
-        $self->should_setup_content( $params->{content} ? 1 : 0 );
+    unless ( @_ % 2 == 0 && eval { $request->isa('HTTP::Request') } ) {
+        croak(qq/usage: $class->new( \$request [, key => value] )/);
     }
-    else {
-        $self->should_setup_content(1);
-    }
-
-    $self->prepare;
-
-    return $self;
-}
-
-*enviroment = \&environment;
-
-sub has_stdin  { return defined $_[0]->stdin  }
-sub has_stdout { return defined $_[0]->stdout }
-sub has_stderr { return defined $_[0]->stderr }
-
-my $HTTP_Token   = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;
-my $HTTP_Version = qr/HTTP\/[0-9]+\.[0-9]+/;
-
-sub prepare {
-    my $self = shift;
 
-    my $environment = $self->environment;
-    my $request     = $self->request;
+    my $self = $class->SUPER::new( { restored => 0, setuped => 0 } );
+    $self->request($request);
+    $self->stdin( IO::File->new_tmpfile );
+    $self->stdout( IO::File->new_tmpfile );
 
     my $host = $request->header('Host');
     my $uri  = $request->uri->clone;
-
     $uri->scheme('http')    unless $uri->scheme;
     $uri->host('localhost') unless $uri->host;
     $uri->port(80)          unless $uri->port;
@@ -135,11 +54,11 @@ sub prepare {
 
     $uri = $uri->canonical;
 
-    my %cgi = (
+    my $environment = {
         GATEWAY_INTERFACE => 'CGI/1.1',
         HTTP_HOST         => $uri->host_port,
         HTTPS             => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF',  # not in RFC 3875
-        PATH_INFO         => URI::Escape::uri_unescape($uri->path),
+        PATH_INFO         => _uri_safe_unescape($uri->path),
         QUERY_STRING      => $uri->query || '',
         SCRIPT_NAME       => '/',
         SERVER_NAME       => $uri->host,
@@ -150,35 +69,9 @@ sub prepare {
         REMOTE_HOST       => 'localhost',
         REMOTE_PORT       => int( rand(64000) + 1000 ),                   # not in RFC 3875
         REQUEST_URI       => $uri->path_query,                            # not in RFC 3875
-        REQUEST_METHOD    => $request->method
-    );
-
-    if ( my $authorization = $request->header('Authorization') ) {
-
-        ( my $scheme ) = $authorization =~ /^($HTTP_Token+)/o;
-
-        if ( $scheme =~ /^Basic/i ) {
-
-            if ( ( my $username ) = $request->headers->authorization_basic ) {
-                $cgi{AUTH_TYPE}   = 'Basic';
-                $cgi{REMOTE_USER} = $username;
-            }
-        }
-        elsif ( $scheme =~ /^Digest/i ) {
-
-            if ( ( my $username ) = $authorization =~ /username="([^"]+)"/ ) {
-                $cgi{AUTH_TYPE}   = 'Digest';
-                $cgi{REMOTE_USER} = $username;
-            }
-        }
-    }
-
-    foreach my $key ( keys %cgi ) {
-
-        unless ( exists $environment->{ $key } ) {
-            $environment->{ $key } = $cgi{ $key };
-        }
-    }
+        REQUEST_METHOD    => $request->method,
+        @_
+    };
 
     foreach my $field ( $request->headers->header_field_names ) {
 
@@ -186,122 +79,67 @@ sub prepare {
         $key =~ tr/-/_/;
         $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/;
 
-        unless ( exists $environment->{ $key } ) {
-            $environment->{ $key } = $request->headers->header($field);
+        unless ( exists $environment->{$key} ) {
+            $environment->{$key} = $request->headers->header($field);
         }
     }
 
-    if ( $environment->{SCRIPT_NAME} ne '/' && $environment->{PATH_INFO} ) {
+    unless ( $environment->{SCRIPT_NAME} eq '/' && $environment->{PATH_INFO} ) {
         $environment->{PATH_INFO} =~ s/^\Q$environment->{SCRIPT_NAME}\E/\//;
         $environment->{PATH_INFO} =~ s/^\/+/\//;
     }
 
-    $self->is_prepared(1);
+    $self->environment($environment);
+
+    return $self;
 }
 
 sub setup {
     my $self = shift;
 
-    if ( $self->is_setup ) {
-        croak(   'An attempt was made to setup environment variables and '
-               . 'standard filehandles which has already been setup.' );
-    }
-
-    if ( $self->should_setup_content && $self->has_stdin ) {
-        $self->setup_content;
-    }
-
-    if ( $self->has_stdin ) {
-
-        if ( $self->should_dup ) {
-
-            if ( $self->should_restore ) {
-
-                open( my $stdin, '<&STDIN' )
-                  or croak("Couldn't dup STDIN: '$!'");
-
-                $self->{restore}->{stdin} = $stdin;
-            }
-
-            open( STDIN, '<&' . fileno($self->stdin) )
-              or croak("Couldn't dup stdin filehandle to STDIN: '$!'");
-        }
-        else {
+    $self->{restore}->{environment} = {%ENV};
 
-            my $stdin = Symbol::qualify_to_ref('STDIN');
+    binmode( $self->stdin );
 
-            if ( $self->should_restore ) {
+    if ( $self->request->content_length ) {
 
-                $self->{restore}->{stdin}     = *$stdin;
-                $self->{restore}->{stdin_ref} = \*$stdin;
-            }
+        $self->stdin->print($self->request->content)
+          or croak("Can't write request content to stdin handle: $!");
 
-            *$stdin = $self->stdin;
-        }
+        $self->stdin->seek(0, SEEK_SET)
+          or croak("Can't seek stdin handle: $!");
 
-        binmode( $self->stdin );
-        binmode( STDIN );
+        $self->stdin->flush
+          or croak("Can't flush stdin handle: $!");
     }
 
-    if ( $self->has_stdout ) {
-
-        if ( $self->should_dup ) {
+    open( $self->{restore}->{stdin}, '<&'. STDIN->fileno )
+      or croak("Can't dup stdin: $!");
 
-            if ( $self->should_restore ) {
+    open( STDIN, '<&='. $self->stdin->fileno )
+      or croak("Can't open stdin: $!");
 
-                open( my $stdout, '>&STDOUT' )
-                  or croak("Couldn't dup STDOUT: '$!'");
+    binmode( STDIN );
 
-                $self->{restore}->{stdout} = $stdout;
-            }
+    if ( $self->stdout ) {
 
-            open( STDOUT, '>&' . fileno($self->stdout) )
-              or croak("Couldn't dup stdout filehandle to STDOUT: '$!'");
-        }
-        else {
+        open( $self->{restore}->{stdout}, '>&'. STDOUT->fileno )
+          or croak("Can't dup stdout: $!");
 
-            my $stdout = Symbol::qualify_to_ref('STDOUT');
-
-            if ( $self->should_restore ) {
-
-                $self->{restore}->{stdout}     = *$stdout;
-                $self->{restore}->{stdout_ref} = \*$stdout;
-            }
-
-            *$stdout = $self->stdout;
-        }
+        open( STDOUT, '>&='. $self->stdout->fileno )
+          or croak("Can't open stdout: $!");
 
         binmode( $self->stdout );
         binmode( STDOUT);
     }
 
-    if ( $self->has_stderr ) {
-
-        if ( $self->should_dup ) {
-
-            if ( $self->should_restore ) {
+    if ( $self->stderr ) {
 
-                open( my $stderr, '>&STDERR' )
-                  or croak("Couldn't dup STDERR: '$!'");
+        open( $self->{restore}->{stderr}, '>&'. STDERR->fileno )
+          or croak("Can't dup stderr: $!");
 
-                $self->{restore}->{stderr} = $stderr;
-            }
-
-            open( STDERR, '>&' . fileno($self->stderr) )
-              or croak("Couldn't dup stdout filehandle to STDOUT: '$!'");
-        }
-        else {
-
-            my $stderr = Symbol::qualify_to_ref('STDERR');
-
-            if ( $self->should_restore ) {
-
-                $self->{restore}->{stderr}     = *$stderr;
-                $self->{restore}->{stderr_ref} = \*$stderr;
-            }
-
-            *$stderr = $self->stderr;
-        }
+        open( STDERR, '>&='. $self->stderr->fileno )
+          or croak("Can't open stderr: $!");
 
         binmode( $self->stderr );
         binmode( STDERR );
@@ -309,11 +147,6 @@ sub setup {
 
     {
         no warnings 'uninitialized';
-
-        if ( $self->should_restore ) {
-            $self->{restore}->{environment} = { %ENV };
-        }
-
         %ENV = %{ $self->environment };
     }
 
@@ -321,169 +154,86 @@ sub setup {
         CGI::initialize_globals();
     }
 
-    $self->is_setup(1);
+    $self->{setuped}++;
 
     return $self;
 }
 
-sub setup_content {
-    my $self  = shift;
-    my $stdin = shift || $self->stdin;
-
-    my $content = $self->request->content_ref;
-
-    if ( ref($content) eq 'SCALAR' ) {
-
-        if ( defined($$content) && length($$content) ) {
-
-            print( { $stdin } $$content )
-              or croak("Couldn't write request content SCALAR to stdin filehandle: '$!'");
-
-            if ( $self->should_rewind ) {
-
-                seek( $stdin, 0, SEEK_SET )
-                  or croak("Couldn't rewind stdin filehandle: '$!'");
-            }
-        }
-    }
-    elsif ( ref($content) eq 'CODE' ) {
-
-        while () {
-
-            my $chunk = &$content();
-
-            if ( defined($chunk) && length($chunk) ) {
-
-                print( { $stdin } $chunk )
-                  or croak("Couldn't write request content callback to stdin filehandle: '$!'");
-            }
-            else {
-                last;
-            }
-        }
-
-        if ( $self->should_rewind ) {
-
-            seek( $stdin, 0, SEEK_SET )
-              or croak("Couldn't rewind stdin filehandle: '$!'");
-        }
-    }
-    else {
-        croak("Couldn't write request content to stdin filehandle: 'Unknown request content $content'");
-    }
-}
-
 sub response {
-    my $self   = shift;
-    my %params = ( headers_only => 0, sync => 0, @_ );
+    my ( $self, $callback ) = @_;
 
-    return undef unless $self->has_stdout;
+    return undef unless $self->stdout;
 
-    if ( $self->should_rewind ) {
+    seek( $self->stdout, 0, SEEK_SET )
+      or croak("Can't seek stdout handle: $!");
 
-        seek( $self->stdout, 0, SEEK_SET )
-          or croak("Couldn't rewind stdout filehandle: '$!'");
+    my $headers;
+    while ( my $line = $self->stdout->getline ) {
+        $headers .= $line;
+        last if $headers =~ /\x0d?\x0a\x0d?\x0a$/;
     }
-
-    my $message  = undef;
-    my $response = HTTP::Response->new( 200, 'OK' );
-       $response->protocol('HTTP/1.1');
-
-    while ( my $line = readline($self->stdout) ) {
-
-        if ( !$message && $line =~ /^\x0d?\x0a$/ ) {
-            next;
-        }
-        else {
-            $message .= $line;
-        }
-
-        last if $message =~ /\x0d?\x0a\x0d?\x0a$/;
-    }
-
-    if ( !$message ) {
-        $response->code(500);
-        $response->message('Internal Server Error');
-        $response->date( time() );
-        $response->content( $response->error_as_HTML );
-        $response->content_type('text/html');
-        $response->content_length( length $response->content );
-
-        return $response;
+    
+    unless ( defined $headers ) {
+        $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
     }
 
-    if ( $message =~ s/^($HTTP_Version)[\x09\x20]+(\d\d\d)[\x09\x20]+([\x20-\xFF]*)\x0D?\x0A//o ) {
-        $response->protocol($1);
-        $response->code($2);
-        $response->message($3);
+    unless ( $headers =~ /^HTTP/ ) {
+        $headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers;
     }
 
-    $message =~ s/\x0D?\x0A[\x09\x20]+/\x20/gs;
-
-    foreach ( split /\x0D?\x0A/, $message ) {
+    my $response = HTTP::Response->parse($headers);
+    $response->date( time() ) unless $response->date;
 
-        s/[\x09\x20]*$//;
+    my $message = $response->message;
+    my $status  = $response->header('Status');
 
-        if ( /^($HTTP_Token+)[\x09\x20]*:[\x09\x20]*([\x20-\xFF]+)$/o ) {
-            $response->headers->push_header( $1 => $2 );
-        }
-        else {
-            # XXX what should we do on bad headers?
-        }
+    if ( $message && $message =~ /^(.+)\x0d$/ ) {
+        $response->message($1);
     }
 
-    my $status = $response->header('Status');
+    if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) {
 
-    if ( $status && $status =~ /^(\d\d\d)[\x09\x20]+([\x20-\xFF]+)$/ ) {
-        $response->code($1);
-        $response->message($2);
-    }
+        my $code    = $1;
+        my $message = $2 || HTTP::Status::status_message($code);
 
-    if ( !$response->date ) {
-        $response->date(time());
+        $response->code($code);
+        $response->message($message);
     }
+    
+    my $length = ( stat( $self->stdout ) )[7] - tell( $self->stdout );
 
-    if ( $params{headers_only} ) {
-
-        if ( $params{sync} ) {
+    if ( $response->code == 500 && !$length ) {
 
-            my $position = tell( $self->stdout )
-              or croak("Couldn't get file position from stdout filehandle: '$!'");
-
-            sysseek( $self->stdout, $position, SEEK_SET )
-              or croak("Couldn't seek stdout filehandle: '$!'");
-        }
+        $response->content( $response->error_as_HTML );
+        $response->content_type('text/html');
 
         return $response;
     }
 
-    my $content        = undef;
-    my $content_length = 0;
+    if ($callback) {
 
-    while () {
+        my $handle = $self->stdout;
 
-        my $r = read( $self->stdout, $content, 65536, $content_length );
+        $response->content( sub {
 
-        if ( defined $r ) {
-
-            if ( $r == 0 ) {
-                last;
-            }
-            else {
-                $content_length += $r;
+            if ( $handle->read( my $buffer, 4096 ) ) {
+                return $buffer;
             }
-        }
-        else {
-            croak("Couldn't read response content from stdin filehandle: '$!'");
-        }
+
+            return undef;
+        });
     }
+    else {
 
-    if ( $content_length ) {
+        my $length = 0;
 
-        $response->content_ref(\$content);
+        while ( $self->stdout->read( my $buffer, 4096 ) ) {
+            $length += length($buffer);
+            $response->add_content($buffer);
+        }
 
-        if ( !$response->content_length ) {
-            $response->content_length($content_length);
+        if ( $length && !$response->content_length ) {
+            $response->content_length($length);
         }
     }
 
@@ -493,317 +243,134 @@ sub response {
 sub restore {
     my $self = shift;
 
-    if ( !$self->should_restore ) {
-        croak(   'An attempt was made to restore environment variables and '
-               . 'standard filehandles which has not been saved.' );
-    }
-
-    if ( !$self->is_setup ) {
-        croak(   'An attempt was made to restore environment variables and '
-               . 'standard filehandles which has not been setup.' );
-    }
-
-    if ( $self->is_restored ) {
-        croak(   'An attempt was made to restore environment variables and '
-               . 'standard filehandles which has already been restored.' );
-    }
-
     {
         no warnings 'uninitialized';
         %ENV = %{ $self->{restore}->{environment} };
     }
 
-    if ( $self->has_stdin ) {
-
-        my $stdin = $self->{restore}->{stdin};
-
-        if ( $self->should_dup ) {
-
-            STDIN->fdopen( fileno($stdin), '<' )
-              or croak("Couldn't restore STDIN: '$!'");
-        }
-        else {
-
-            my $stdin_ref = $self->{restore}->{stdin_ref};
-              *$stdin_ref = $stdin;
-        }
-
-        if ( $self->should_rewind ) {
-
-            seek( $self->stdin, 0, SEEK_SET )
-              or croak("Couldn't rewind stdin filehandle: '$!'");
-        }
-    }
-
-    if ( $self->has_stdout ) {
+    open( STDIN, '<&'. fileno($self->{restore}->{stdin}) )
+      or croak("Can't restore stdin: $!");
 
-        my $stdout = $self->{restore}->{stdout};
+    sysseek( $self->stdin, 0, SEEK_SET )
+      or croak("Can't seek stdin: $!");
 
-        if ( $self->should_dup ) {
+    if ( $self->{restore}->{stdout} ) {
 
-            STDOUT->flush
-              or croak("Couldn't flush STDOUT: '$!'");
+        STDOUT->flush
+          or croak("Can't flush stdout: $!");
 
-            STDOUT->fdopen( fileno($stdout), '>' )
-              or croak("Couldn't restore STDOUT: '$!'");
-        }
-        else {
+        open( STDOUT, '>&'. fileno($self->{restore}->{stdout}) )
+          or croak("Can't restore stdout: $!");
 
-            my $stdout_ref = $self->{restore}->{stdout_ref};
-              *$stdout_ref = $stdout;
-        }
-
-        if ( $self->should_rewind ) {
-
-            seek( $self->stdout, 0, SEEK_SET )
-              or croak("Couldn't rewind stdout filehandle: '$!'");
-        }
+        sysseek( $self->stdout, 0, SEEK_SET )
+          or croak("Can't seek stdout: $!");
     }
 
-    if ( $self->has_stderr ) {
-
-        my $stderr = $self->{restore}->{stderr};
-
-        if ( $self->should_dup ) {
-
-            STDERR->flush
-              or croak("Couldn't flush STDERR: '$!'");
-
-            STDERR->fdopen( fileno($stderr), '>' )
-              or croak("Couldn't restore STDERR: '$!'");
-        }
-        else {
+    if ( $self->{restore}->{stderr} ) {
 
-            my $stderr_ref = $self->{restore}->{stderr_ref};
-              *$stderr_ref = $stderr;
-        }
+        STDERR->flush
+          or croak("Can't flush stderr: $!");
 
-        if ( $self->should_rewind ) {
+        open( STDERR, '>&'. fileno($self->{restore}->{stderr}) )
+          or croak("Can't restore stderr: $!");
 
-            seek( $self->stderr, 0, SEEK_SET )
-              or croak("Couldn't rewind stderr filehandle: '$!'");
-        }
+        sysseek( $self->stderr, 0, SEEK_SET )
+          or croak("Can't seek stderr: $!");
     }
 
-    $self->{restore} = {};
-
-    $self->is_restored(1);
+    $self->{restored}++;
 
     return $self;
 }
 
 sub DESTROY {
     my $self = shift;
-
-    if ( $self->should_restore && $self->is_setup && !$self->is_restored ) {
-        $self->restore;
-    }
+    $self->restore if $self->{setuped} && !$self->{restored};
 }
 
 1;
 
 __END__
 
-=head1 NAME
-
-HTTP::Request::AsCGI - Setup a Common Gateway Interface environment from a HTTP::Request
-
 =head1 SYNOPSIS
 
     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 is automatically restored
+        
+        # environment and descriptors will automatically be restored
         # when $c is destructed.
     }
-
+    
     while ( my $line = $stdout->getline ) {
         print $line;
     }
-
+    
 =head1 DESCRIPTION
 
-Provides a convinient way of setting up an CGI environment from a HTTP::Request.
+Provides a convenient way of setting up an CGI environment from an HTTP::Request.
 
 =head1 METHODS
 
-=over 4
-
-=item * new
-
-Constructor, this method takes a hash of parameters. The following parameters are
-valid:
-
-=over 8
-
-=item * request
-
-    request => HTTP::Request->new( GET => 'http://www.host.com/' )
-
-=item * stdin
-
-A filehandle to be used as standard input, defaults to a temporary filehandle.
-If C<stdin> is C<undef>, standard input will be left as is.
-
-    stdin => IO::File->new_tmpfile
-    stdin => IO::String->new
-    stdin => $fh
-    stdin => undef
-
-=item * stdout
-
-A filehandle to be used as standard output, defaults to a temporary filehandle.
-If C<stdout> is C<undef>, standard output will be left as is.
-
-    stdout => IO::File->new_tmpfile
-    stdout => IO::String->new
-    stdout => $fh
-    stdout => undef
-
-=item * stderr
-
-A filehandle to be used as standard error, defaults to C<undef>. If C<stderr> is
-C<undef>, standard error will be left as is.
-
-    stderr => IO::File->new_tmpfile
-    stderr => IO::String->new
-    stderr => $fh
-    stderr => undef
+=over 4 
 
-=item * environment
+=item new ( $request [, key => value ] )
 
-A C<HASH> of additional environment variables to be used in CGI.
-C<HTTP::Request::AsCGI> doesn't autmatically merge C<%ENV>, it has to be
-explicitly given if that is desired. Environment variables given in this
-C<HASH> isn't overridden by C<HTTP::Request::AsCGI>.
+Constructor.  The first argument must be a instance of HTTP::Request, followed
+by optional pairs of environment key and value.
 
-    environment => \%ENV
-    environment => { PATH => '/bin:/usr/bin', SERVER_SOFTWARE => 'Apache/1.3' }
+=item environment
 
-Following standard meta-variables (in addition to protocol-specific) is 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.
 
-    AUTH_TYPE
-    CONTENT_LENGTH
-    CONTENT_TYPE
-    GATEWAY_INTERFACE
-    PATH_INFO
-    SCRIPT_NAME
-    SERVER_NAME
-    SERVER_PORT
-    SERVER_PROTOCOL
-    SERVER_SOFTWARE
-    REMOTE_ADDR
-    REMOTE_HOST
-    REMOTE_USER
-    REQUEST_METHOD
-    QUERY_STRING
+=item setup
 
-Following non-standard but common meta-variables is setup:
+Sets up the environment and descriptors.
 
-    HTTPS
-    REMOTE_PORT
-    REQUEST_URI
+=item restore
 
-Following meta-variables is B<not> setup but B<must> be provided in CGI:
+Restores the environment and descriptors. Can only be called after setup.
 
-    PATH_TRANSLATED
+=item request
 
-Following meta-variables is B<not> setup but common in CGI:
+Returns the request given to constructor.
 
-    DOCUMENT_ROOT
-    SCRIPT_FILENAME
-    SERVER_ROOT
+=item response
 
-=item * dup
+Returns a HTTP::Response. Can only be called after restore.
 
-Boolean to indicate whether to C<dup> standard filehandle or to assign the
-typeglob representing the standard filehandle. Defaults to C<true>.
+=item stdin
 
-    dup => 0
-    dup => 1
+Accessor for handle that will be used for STDIN, must be a real seekable
+handle with an file descriptor. Defaults to a tempoary IO::File instance.
 
-=item * restore
+=item stdout
 
-Boolean to indicate whether or not to restore environment variables and standard
-filehandles. Defaults to C<true>.
+Accessor for handle that will be used for STDOUT, must be a real seekable
+handle with an file descriptor. Defaults to a tempoary IO::File instance.
 
-    restore => 0
-    restore => 1
+=item stderr
 
-If C<true> standard filehandles and environment variables will be saved duiring
-C<setup> for later use in C<restore>.
-
-=item * rewind
-
-Boolean to indicate whether or not to rewind standard filehandles. Defaults
-to C<true>.
-
-    rewind => 0
-    rewind => 1
-
-=item * content
-
-Boolean to indicate whether or not to request content should be written to
-C<stdin> filehandle when C<setup> is invoked. Defaults to C<true>.
-
-    content => 0
-    content => 1
-
-=back
-
-=item * setup
-
-Attempts to setup standard filehandles and environment variables.
-
-=item * restore
-
-Attempts to restore standard filehandles and environment variables.
-
-=item * response
-
-Attempts to parse C<stdout> filehandle into a L<HTTP::Response>.
-
-=item * request
-
-Accessor for L<HTTP::Request> that was given to constructor.
-
-=item * environment
-
-Accessor for environment variables to be used in C<setup>.
-
-=item * stdin
-
-Accessor/Mutator for standard input filehandle.
-
-=item * stdout
-
-Accessor/Mutator for standard output filehandle.
-
-=item * stderr
-
-Accessor/Mutator for standard error filehandle.
+Accessor for handle that will be used for STDERR, must be a real seekable
+handle with an file descriptor.
 
 =back
 
-=head1 DEPRECATED
-
-XXX Constructor
-
 =head1 SEE ALSO
 
 =over 4
@@ -820,13 +387,4 @@ XXX Constructor
 
 Thomas L. Shinnick for his valuable win32 testing.
 
-=head1 AUTHOR
-
-Christian Hansen, C<ch@ngmedia.com>
-
-=head1 LICENSE
-
-This library is free software. You can redistribute it and/or modify
-it under the same terms as perl itself.
-
 =cut