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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.