Add the HTTP::Request::AsCGI part of RT#50082
[catagits/HTTP-Request-AsCGI.git] / lib / HTTP / Request / AsCGI.pm
index 77d035f..f2ddbff 100644 (file)
@@ -1,18 +1,38 @@
 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';
 
+our $VERSION = '0.9';
+
 use Carp;
 use HTTP::Response;
 use IO::Handle;
 use IO::File;
+use URI ();
+use URI::Escape ();
+
+__PACKAGE__->mk_accessors(qw[ environment request stdin stdout stderr ]);
+
+# old typo
+=begin Pod::Coverage
+
+  enviroment
+
+=end Pod::Coverage
+
+=cut
 
-__PACKAGE__->mk_accessors(qw[ enviroment request stdin stdout stderr ]);
+*enviroment = \&environment;
 
-our $VERSION = 0.5_03;
+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" : pack('C', hex($1))/ge;
+    $s
+}
 
 sub new {
     my $class   = shift;
@@ -36,7 +56,7 @@ sub new {
 
     $uri = $uri->canonical;
 
-    my $enviroment = {
+    my $environment = {
         GATEWAY_INTERFACE => 'CGI/1.1',
         HTTP_HOST         => $uri->host_port,
         HTTPS             => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF',  # not in RFC 3875
@@ -55,23 +75,30 @@ sub new {
         @_
     };
 
+    # RFC 3875 says PATH_INFO is not URI-encoded. That's really
+    # annoying for applications that you can't tell "%2F" vs "/", but
+    # doing the partial decoding then makes it impossible to tell
+    # "%252F" vs "%2F". Encoding everything is more compatible to what
+    # web servers like Apache or lighttpd do, anyways.
+    $environment->{PATH_INFO} = URI::Escape::uri_unescape($environment->{PATH_INFO});
+
     foreach my $field ( $request->headers->header_field_names ) {
 
         my $key = uc("HTTP_$field");
         $key =~ tr/-/_/;
         $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/;
 
-        unless ( exists $enviroment->{$key} ) {
-            $enviroment->{$key} = $request->headers->header($field);
+        unless ( exists $environment->{$key} ) {
+            $environment->{$key} = $request->headers->header($field);
         }
     }
 
-    unless ( $enviroment->{SCRIPT_NAME} eq '/' && $enviroment->{PATH_INFO} ) {
-        $enviroment->{PATH_INFO} =~ s/^\Q$enviroment->{SCRIPT_NAME}\E/\//;
-        $enviroment->{PATH_INFO} =~ s/^\/+/\//;
+    unless ( $environment->{SCRIPT_NAME} eq '/' && $environment->{PATH_INFO} ) {
+        $environment->{PATH_INFO} =~ s/^\Q$environment->{SCRIPT_NAME}\E/\//;
+        $environment->{PATH_INFO} =~ s/^\/+/\//;
     }
 
-    $self->enviroment($enviroment);
+    $self->environment($environment);
 
     return $self;
 }
@@ -79,7 +106,7 @@ sub new {
 sub setup {
     my $self = shift;
 
-    $self->{restore}->{enviroment} = {%ENV};
+    $self->{restore}->{environment} = {%ENV};
 
     binmode( $self->stdin );
 
@@ -129,7 +156,7 @@ sub setup {
 
     {
         no warnings 'uninitialized';
-        %ENV = %{ $self->enviroment };
+        %ENV = %{ $self->environment };
     }
 
     if ( $INC{'CGI.pm'} ) {
@@ -227,7 +254,7 @@ sub restore {
 
     {
         no warnings 'uninitialized';
-        %ENV = %{ $self->{restore}->{enviroment} };
+        %ENV = %{ $self->{restore}->{environment} };
     }
 
     open( STDIN, '<&'. fileno($self->{restore}->{stdin}) )
@@ -274,10 +301,6 @@ sub DESTROY {
 
 __END__
 
-=head1 NAME
-
-HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
-
 =head1 SYNOPSIS
 
     use CGI;
@@ -298,7 +321,7 @@ HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
         
         $stdout = $c->stdout;
         
-        # enviroment and descriptors will automatically be restored 
+        # environment and descriptors will automatically be restored
         # when $c is destructed.
     }
     
@@ -308,7 +331,7 @@ HTTP::Request::AsCGI - Setup a CGI enviroment from a HTTP::Request
     
 =head1 DESCRIPTION
 
-Provides a convinient way of setting up an CGI enviroment from a HTTP::Request.
+Provides a convenient way of setting up an CGI environment from an HTTP::Request.
 
 =head1 METHODS
 
@@ -316,21 +339,21 @@ Provides a convinient way of setting up an CGI enviroment from a HTTP::Request.
 
 =item new ( $request [, key => value ] )
 
-Contructor, first argument must be a instance of HTTP::Request
-followed by optional pairs of environment key and value.
+Constructor.  The first argument must be a instance of HTTP::Request, followed
+by optional pairs of environment key and value.
 
-=item enviroment
+=item environment
 
 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
 
-Setups the environment and descriptors.
+Sets up the environment and descriptors.
 
 =item restore
 
-Restores the enviroment and descriptors. Can only be called after setup.
+Restores the environment and descriptors. Can only be called after setup.
 
 =item request
 
@@ -373,13 +396,4 @@ handle with an file descriptor.
 
 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