Add the HTTP::Request::AsCGI part of RT#50082
[catagits/HTTP-Request-AsCGI.git] / lib / HTTP / Request / AsCGI.pm
index 7b16729..f2ddbff 100644 (file)
@@ -5,10 +5,14 @@ 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 ]);
 
@@ -23,6 +27,13 @@ __PACKAGE__->mk_accessors(qw[ environment request stdin stdout stderr ]);
 
 *enviroment = \&environment;
 
+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;
     my $request = shift;
@@ -64,6 +75,13 @@ 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");
@@ -313,7 +331,7 @@ __END__
     
 =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
 
@@ -321,8 +339,8 @@ Provides a convinient way of setting up an CGI environment from a HTTP::Request.
 
 =item new ( $request [, key => value ] )
 
-Constructor, 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 environment
 
@@ -331,7 +349,7 @@ 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