Gossamer Forum
Home : Products : Gossamer Links : Discussions :

[POSSIBLE BUG] can't access POSTDATA from GT::CGI object

Quote Reply
[POSSIBLE BUG] can't access POSTDATA from GT::CGI object
I'm sending post requests using GT::WWW, like so, on the client end of a plugin I'm working on:

Code:
my $xml = '<foo><bar>this</bar></foo>';

my $url = 'http://www.domain.com/cgi-bin/rpc.cgi;

my $www = GT::WWW->new();
$www->protocol('http');
$www->header('Content-Type' => 'text/xml; charset=utf-8');
$www->url($url);
$www->post_data($xml);

my $response = $www->post();

return $response;

On the server end, I'm using:

Code:
my $xml = $IN->param('POSTDATA');

to retrieve the POSTDATA. I know that the data was sent, as $ENV{CONTENT_LENGTH} is set to '26', the length of the $xml variable from the client application. I don't have any problems sending and receiving POSTDATA when NOT using GT modules.

Is this a bug, or is GT::CGI puting POSTDATA somewhere else other than where the standard CGI module does?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
For those interested, I've attached a patched version of GT::CGI that seems to correct the behavior. As it turns out, GT::CGI only has special handling for multipart/form-data, but POSTDATA (which is generally any content type that is NOT application/x-www-form-urlencoded) requires NO parsing at all; there are NO name/value pairs or special processing needed.

Fixes are in load_data() and parse_str().

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
Here's a patch of what I just committed:
Code:
diff -u -r1.151 CGI.pm
--- CGI.pm 22 Jun 2006 00:50:46 -0000 1.151
+++ CGI.pm 21 Jul 2006 01:01:38 -0000
@@ -81,8 +81,13 @@
}
else {
read(STDIN, my $data, $content_length, 0);
- $data =~ s/\r?\n/&/g;
- $self->parse_str($data);
+ if ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} !~ m|^application/x-www-form-urlencoded|) {
+ $self->{post_data} = $data;
+ }
+ else {
+ $data =~ s/\r?\n/&/g;
+ $self->parse_str($data);
+ }
}
}
}
@@ -702,6 +707,20 @@
}
END_OF_SUB

+$COMPILE{post_data} = __LINE__ . <<'END_OF_SUB';
+sub post_data {
+# -------------------------------------------------------------------
+# Returns the POSTed data if it was not of type
+# application/x-www-form-urlencoded or multipart/form-data.
+#
+ my $self = shift;
+ $self = $self->class_new unless ref $self;
+ $self->load_data() unless $self->{data_loaded};
+
+ return $self->{post_data};
+}
+END_OF_SUB
+
$COMPILE{browser_info} = __LINE__ . <<'END_OF_SUB';
sub browser_info {
# -----------------------------------------------------------------------------

Instead of saving the data in param as POSTDATA (which seems quite hackish), you can retrieve this data using the post_data() method.

Adrian
Quote Reply
Re: [brewt] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
That's basicly what I did at first, but later chose to use the param() method, as that it how CGI.pm handles it. Of course, if you read the CGI.pm souce code, you'll see that it was indeed considered a hack for XML applications.

Since GT::WWW uses post_data() for it's POSTDATA input method, using it in GT::CGI for an output method is a good idea anyway.

I appreciate your time looking into this and making an official patch.

Now that I've got this problem behind me, I can finally move on to another part of my plugin. You don't know how relieved I was to discover that the problem wasn't buried deep somewhere in the 1500 lines of my code.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [brewt] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
Hi brewt,

Is this fix going to be included in a Links admin panel update ?

Thanks,

John
Significant Media
Quote Reply
Re: [Jag] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
No, but it will be included in the next release.

Adrian
Quote Reply
Re: [brewt] [POSSIBLE BUG] can't access POSTDATA from GT::CGI object In reply to
Quote:
No, but it will be included in the next release.

That's fine, as my plugin won't be ready for the masses for a while yet. I'll just need to include it in my plugin's install tar, and give the user the option to install it by adding a checkbox input field in the pre-install message section.

Philip
------------------
Limecat is not pleased.