Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI - Hangs Script 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
I have a script, which I changed from a self baked STDIN routine and chenged it to use the CGI.pm via my $cgi = new CGI; , however now when data is posted to the script it hangs.

What is wrong , it worked fine before I decided to make the change to CGI.

thanks

1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
#!/usr/bin/perl -w

# PayPal IPN (Instant Payment Notification) CGI

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw:)all unescape);

my $cgi = new CGI;

# These modules are required to make the secure HTTP request to PayPal.

use LWP::UserAgent;
use Crypt::SSLeay;

# Set path to user modules
use lib qw(/mypath/Modules);

######################
# Use globals module #
######################
use ssplglobal;

##################
# Use SQL module #
##################
use sql;

#################
# Get POST Data #
#################
my $query;

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= "&cmd=_notify-validate";

# Post Back to PayPal to Authenticate Request

my $paypal_url = "
my $user_agent = new LWP::UserAgent;

my $request = new HTTP::Request("POST", $paypal_url);

$request->content_type("application/x-
$request->content($query);

# Make the request
my $result = $user_agent->request($request);

# HTTPS error
if (!$result->is_success) {
&send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","paypal communication error","paypal error");
die "Error communicating with PayPal server.";
exit();
}

if($result->content eq 'INVALID'){
&send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","Invalid transaction","paypal test");
die "Invalid Transaction.";
exit();
}

if($result->content eq 'VERIFIED') {
# check the $payment_status=Completed
# check that $txn_id has not been previously processed
# check that $receiver_email is your Primary PayPal email
# check that $payment_amount/$payment_currency are correct
# process payment

my $tx = $cgi->param('txn_id');

&send_mail("sspl\@stepnstomp.co.uk","paypal\@stepnstomp.co.uk","Transaction OK","TX = $tx");
die "Transaction Valid.";
exit();
}



die "Script Error!";

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I've found if i use this code it works fine
Code:
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
    # split posted variables into pairs
    my @pairs = split(/&/, $query);

    foreach my $pair (@pairs) {
        my ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $variable{$name} = $value;
    }

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I have a script, which I changed from a self baked STDIN routine and chenged it to use the CGI.pm via my $cgi = new CGI;

It appears all you are doing with the CGI module is fetching this one form field:

my $tx = $cgi->param('txn_id');

Why do you have all those 'die' functions in the script? Is that just for debugging?
 
The script isn't finished, i'm testing, but it was just hanging - which is my point, I'm hardly doing anything with CGI at the moment and it just hangs.

Yes the die statements are for debugging

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
just by looking at the code you have posted I don't know why it's hanging.
 
me neither, I've had this problem using CGI on another script that just gets a simple form with a few hidden inputs and a couple of select lists, when submitted the script hangs - and I got a warning from my host for resource abuse.

Is there a know issue with a certain version of the CGI.pm module, or have I found one?

Cheers,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Ok I've found the conflict, can some explain why it is happening.

Code:
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= "&cmd=_notify-validate";

my $cgi = new CGI;

basicaly I've narrowed it down to if I use the PayPal code with the line to read STDIN and also try to use CGI, it hangs.

It does the same if I create the cgi object first and then also try to read STDIN.

So I am assuming that you can only read STDIN once!

once STDIN is read does it become empty? is that the problem? or does it leave it in some sort of 'open' state causing some kind of 'file in use' buffer error?

Any help understanding the mechanism of reading STDIN and why you can't read it twice is much appreciated.

Regards,
1DMF



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
The first read of STDIN consumes all the bytes of "content_length"

A subsequent read of stdin has nothing in the stream. All the bytes have been previously consumed. so it is waiting for additional input to arrive. There is none. It is used up by the first read.

STDIN is normally the keyboard. Picture reading bytes from the keyboard. Once a program has captures a keystroke, the keystroke is no longer in the stream.

Same with a cgi script. STDIN is redirected to read the posted data. STDOUT is redirected back to the browser instead of the console.
 
Thanks, that's what I love about TT, learn something new everyday!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
To change that code to use CGI all the way down:

First, alter your use CGI line to use the oldstyle URLs. I don't know if PayPal will accept the newer style, but I'm suggesting this to keep the script behaving in the same way as it has been:
Code:
use CGI qw(:all unescape -oldstyle_urls);

Then, instead of using read(), do the following:
Code:
my $cgi = new CGI;
$cgi->param( cmd => 'notify_validate' );
my $query = $cgi->query_string;
Even when using POST data, the query_string() method will return the query string you're looking for.

HTH.
 
wow - once again Ishnid, your a gent, you've answered my next question without me even asking it.

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I guessed that a natural progression from "why doesn't my code work?" would be "how can I fix it?" ;-)
 
We I'd already fixed it, but using the self baked PayPal way, but now I can use the CGI method, which was my original intention.

What confused me was I thought STDIN was where the post/mime data was stored and could be read again and again, like a holding variable, but obviously it doesn't work like that.

It must be a store for the data, as you don't have to read STDIN at the top of a script before doing anything else, but it doesn't like an attempted read on it if it's empty, why is that?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
With "normal" filehandles (i.e. ones that relate to opened files), if you read from them, you have to use the seek() function to get back to the start before you read them again. STDIN is consumed by each read, so it can't be read twice.
 
I see - thanks for that Ishnid.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
doesn't seem to work ishnid? i've had to drop using CGI as I cannot get it to work.

using CGI
Code:
my $cgi = new CGI;

$cgi->param( cmd => 'notify_validate' );

my $query = $cgi->query_string;

my $user_agent = new LWP::UserAgent;

my $request = new HTTP::Request("POST", $paypal_url);

$request->content_type("application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]

$request->content($query);

# Make the request
my $result = $user_agent->request($request);

die "result=" . $result->content;

I get
result=<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--
Script info: script: webscr, cmd: _home, template: p/wel/sandbox-outside, date: Aug. 4, 2006 08:53:42 PDT; country: US, language: en_US, xslt server:
web version: 41.0-236808 branch: live-410_int
content version: 41.0-229712
pexml version: 41.0-236729
page XSL: /default/en_US/homepage/SandBox-outside.xsl
-->
<title>Welcome - PayPal</title>
<meta http-equiv="keywords" content="">
<meta http-equiv="description" content="">
<link rel="stylesheet" type="text/css" href="<link rel="stylesheet" type="text/css" href="<link rel="stylesheet" type="text/css" href="<link rel="stylesheet" type="text/css" href="<link rel="stylesheet" type="text/css" href="<link rel="stylesheet" type="text/css" href="<!--[if IE 6]><link rel="stylesheet" type="text/css" href="[endif]-->

<!--[if IE 7]><link rel="stylesheet" type="text/css" href="[endif]-->
<link rel="stylesheet" type="text/css" href="<style type="text/css"></style>
<link rel="shortcut icon" href="<script type="text/javascript" src="</head>
<body id="xptSandbox">
<div id="xptContentMain"><table align="center" border="0" cellpadding="0" cellspacing="0" width="760">
<tr><td></td></tr>
<tr><td valign="top"><div>
<img src=" border="0" alt=""><br><br><div class="textCenter" id="sandbox_redirect">
<hr>
<br><br><span class="emphasis">To access the PayPal Sandbox, please log in to <a href=" Developer Central</a>.</span><br><br><br><hr>
</div>
</div></td></tr>
</table></div>
<script type="text/javascript" src=" type="text/javascript">
<!--
var ppns = new PayPalNaturalSearch(' ppns.addEngines(new Array(
"A9.com",
".altavista.com",
"clusty.com",
"google.co.jp",
"google.co.kr",
"google.ru",
" "icerocket.com",
"infospace.com",
"mooter.com",
"search.msn.",
"snap.com",
"s[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: result=
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: --------------------------------------------------------------------------------[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: To access the PayPal Sandbox, please log in to PayPal Developer Central.--------------------------------------------------------------------------------[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi: [Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
[Wed Aug 16 11:44:29 2006] PayPal_IPN.cgi:
earch.yahoo.com",
"search.yahoo.co.jp"
," "aolsearch.aol.com",
"search.aol.com",
"web.ask.com",
"pictures.ask.com",
"images.google.com",
"groups.google.com",
" " "search.netscape.com",
"s.teoma.com/",
" )); // End of aEngines array.
ppns.init();
-->
</script>
</body>
</html>

For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

i've removed the oldstlye urls switch and still no joy, not sure what i'm sending or receiving using the CGI method but I cannot get it to work, oh well nice thought, but it's a lot easier just to use
Code:
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});

# split posted variables into pairs
    my @pairs = split(/&/, $query);

    foreach my $pair (@pairs) {
        my ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $data{$name} = $value;
    }

$query .= "&cmd=_notify-validate";

the die "result=" . $result->content; using this method gives me
result=VERIFIED



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
that'll learn me, copying and pasting from TT supplied code - lol

it should be
Code:
$cgi->param( cmd => '_notify-validate' );
but it still doesn't work.

OK i'm getting a response from PayPal now, but every transaction is responding as 'Invalid', including valid test ones that work with the other code.

so what ever is being posted to paypal's server is not what is received, I am guessing CGI is altering something in the query string that paypal doesn't like or can't cope with and so is denying the transaction.

any ideas what this is and how i might fix it?

note: I had to put the oldstlye_urls switch back in!

thanks 1DMF


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top