in reply to Does CGI::Application expose a HTTP::Request object?

I've reworked the code again, now making it a bit more clever on the mapping %ENV to HTTP::Headers: (comments welcome)
# build a HTTP::Request Object from what CGI.pm provides us with # see http://www.perlmonks.org/?node_id=854073 sub request_from_cgi { my $self = shift; my $q = $self->query; my $uri = $q->url; my $method = uc $q->request_method; my %env_to_httpheader = ( # hash of ENV keys which do not follow the HTTP_* nami +ng convention 'HTTP_TE' => 'Accept-Encoding', 'CONTENT_LENGTH' => 'Content-Length', # from here on blurry from memory: (and probably more +to include as well..), now commented out, see why below: # 'CONTENT_ENCODING' => 'Content-Encoding', + # I think there is only Accept-Encoding on requests # 'CONTENT_TYPE' => 'Content-Type', # I th +ink the content-type equivalent on the req side is Accept ); my $headers; foreach my $key (keys %ENV) { if($key =~ /^HTTP_(.*)$/ && !exists($env_to_httpheader +{$key})){ push(@{ $headers }, lc($1)); push(@{ $headers }, $ENV{$key}); }elsif(exists($env_to_httpheader{$key})){ push(@{ $headers }, $env_to_httpheader{$key}); push(@{ $headers }, $ENV{$key}); } } my $content = undef; if($method eq 'POST'){ $content = $q->param('POSTDATA'); }elsif($method eq 'PUT'){ $content = $q->param('PUTDATA'); } if(!defined $content){ my $len = $ENV{CONTENT_LENGTH} || 0; read STDIN, $content, $len; } return HTTP::Request->new( $method,$uri,$headers,$content); }
Update: The PUTDATA param being available might depend on your version of CGI.pm, or use a recent CGI::Simple, which worked for me.

Replies are listed 'Best First'.
Re^2: Does CGI::Application expose a HTTP::Request object?
by isync (Hermit) on Aug 11, 2010 at 21:26 UTC
    After some testing I've found out that basically this works except for reading the $content in on PUT. As it seems there's nothing on STDIN, although the content-length header is correct. And reading the PUTDATA param fails, nothing there.

      After more testing and reading this node the problem seems to be CGI.pm.
      Since finding out, I upgraded my CGI.pm from 3.39 to 3.49, but either CGI.pm's PUTDATA param has gone missing again or soemthing is wrong on my side. Still, binary files end up as garbled mess in the .parmaters and param parts of __QUERY_OBJ -- Can anyone explain this??

      I've given up since, and replaced CGI.pm with CGI::Simple according to the above mentioned node, and - guess what - CGI::Simple properly parses out the $q->param('PUTDATA') parameter from the request and PUT finally works for me!