Problems with STDIN and POST data

Discussion in 'Perl Misc' started by SergioQ, Feb 13, 2008.

  1. SergioQ

    SergioQ Guest

    Hello all,

    Am having some very serious problems reading the POST data that is
    being sent to my Perl script. The full code (minus my email address
    is below). And essentially what I am getting back via email reports
    is that that there is data being sent via POST, and tht it has a
    length of 1431. Yet all my attempts to read it are failing
    miserably. If anyone can help me out, I would soooo appreciate it.

    Thank you so much.

    Here is the FULL CODE:

    #!/usr/bin/perl

    use strict;
    use MainMod;
    use HTTP::Request;
    use LWP::UserAgent;
    use CGI qw:)standard);

    my $out_msg = "";
    my %variable;
    my $serg_bffr;

    read STDIN, $serg_bffr, $ENV{'CONTENT_LENGTH'};

    my $sendmail = "/usr/sbin/sendmail -t";
    open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";

    print SENDMAIL ---my email address goes here---
    print SENDMAIL ---my email address goes here---
    print SENDMAIL "Subject: IPN Stuff\n";
    print SENDMAIL "\n";
    print SENDMAIL "$ENV{REQUEST_METHOD}\n";
    print SENDMAIL "$ENV{'CONTENT_LENGTH'}\n";
    print SENDMAIL length($serg_bffr) . "\n";
    print SENDMAIL "$serg_bffr\n";

    close(SENDMAIL);

    PrintItOut("done");
    exit (0);
    SergioQ, Feb 13, 2008
    #1
    1. Advertising

  2. SergioQ

    Guest

    SergioQ <> wrote:
    > Hello all,
    >
    > Am having some very serious problems reading the POST data that is
    > being sent to my Perl script. The full code (minus my email address
    > is below). And essentially what I am getting back via email reports
    > is that that there is data being sent via POST, and tht it has a
    > length of 1431. Yet all my attempts to read it are failing
    > miserably.


    "Failing miserably" is about as bad as "doesn't work". What actually
    happens?

    Does your hard drive reformat itself and then your cpu set itself on fire?

    > Here is the FULL CODE:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use MainMod;
    > use HTTP::Request;
    > use LWP::UserAgent;
    > use CGI qw:)standard);


    Since you are using it, why not use CGI to read from STDIN for you?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Feb 13, 2008
    #2
    1. Advertising

  3. SergioQ

    SergioQ Guest

    On Feb 13, 2:36 pm, wrote:

    > "Failing miserably" is about as bad as "doesn't work".  What actually
    > happens?

    I see that there is PPOST data, and it has length, but what I read
    back is nothing.



    > Since you are using it, why not use CGI to read from STDIN for you?



    This is a Perl script for Paypal's IPN. Below is their sample
    script. And you'll see there's a section "# post back to PayPal
    system to validate" and since I don't know what's coming in, and in
    what format, how can I tack it on and send it back?


    THIS IS FROM PAYPAL
    #!/usr/bin/perl

    # read post from PayPal system and add 'cmd'
    read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
    $query .= '&cmd=_notify-validate';

    # post back to PayPal system to validate
    use LWP::UserAgent;
    $ua = new LWP::UserAgent;
    $req = new HTTP::Request 'POST','http://www.paypal.com/cgi-bin/
    webscr';
    # note: if you have SSL encryption Enabled, use <https://
    www.paypal.com/cgi-bin/webscr> above
    $req->content_type('application/x-www-form-urlencoded');
    $req->content($query);
    $res = $ua->request($req);

    # split posted variables into pairs
    @pairs = split(/&/, $query);
    $count = 0;
    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $variable{$name} = $value;
    $count++;
    }

    # assign posted variables to local variables
    # note: additional IPN variables also available -- see IPN
    documentation
    $item_name = $variable{'item_name'};
    $receiver_email = $variable{'receiver_email'};
    $item_number = $variable{'item_number'};
    $invoice = $variable{'invoice'};
    $a = $variable{'custom'};
    $payment_status = $variable{'payment_status'};
    $payment_gross = $variable{'payment_gross'};
    $txn_id = $variable{'txn_id'};
    $payer_email = $variable{'payer_email'};
    $amount = $variable{'amount'};
    $on0 = $variable{'option_name1'};
    $os0 = $variable{'option_selection1'};
    $status = $res->content;
    $note = $variable{'memo'};


    if ($res->is_error) {
    # HTTP error
    }
    elsif ($res->content eq 'VERIFIED') {
    # check the payment_status=Completed
    # check that txn_id has not been previously processed
    # check that receiver_email is an email address in your PayPal account
    # process payment
    # print to screen the following if the IPN POST was VERIFIED


    print "content-type: text/plain\n\nOK\n";
    print "<html><head><title>IPN Screendump</title></head>\n";
    print "<body>your email address is <b>$payer_email</b>\n";
    print "<br>you paid <b>$payment_gross</b>\n";
    print "<br>you paid for <b>$item_number</b>\n";
    print "<br>the color of your order was <b>$os0</b>\n";
    print "<br>the value of custom was <b>$a</b>\n";
    print "<br>the status was<b>$status</b>\n";
    print "<br>the note said <b>$note</b>\n";
    print "<br>the transaction id was <b>$txn_id</b>\n";
    print "<br>the payment status was<b>$payment_status</b>\n";
    print "</body></html>\n";


    }
    elsif ($res->content eq 'INVALID') {
    # log for manual investigation
    # print to screen the following if the IPN POST was INVALID

    print "content-type: text/plain\n\nOK\n";

    print "<html><head><title>IPN Screendump</title></head>\n";
    print "<br>the status was<b>$status</b>\n";
    print "</body></html>\n";


    }
    else {
    # error
    }
    SergioQ, Feb 13, 2008
    #3
  4. SergioQ

    Guest

    SergioQ <> wrote:
    > On Feb 13, 2:36=A0pm, wrote:
    >
    > > "Failing miserably" is about as bad as "doesn't work". =A0What actually
    > > happens?

    > I see that there is PPOST data, and it has length, but what I read
    > back is nothing.


    Then check the return value of the "read" function and if it is undef
    (as opposed to 0) see what is in $!.

    Are you sure that whatever is generating these POST requests, or whatever
    is proxying them to you, isn't lying about CONTENT_LENGTH?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Feb 13, 2008
    #4
  5. SergioQ

    Todd Wade Guest

    On Feb 13, 2:36 pm, wrote:
    > SergioQ <> wrote:
    >
    > > Am having some very serious problems reading the POST data that is
    > > being sent to my Perl script. The full code (minus my email address
    > > is below). And essentially what I am getting back via email reports
    > > is that that there is data being sent via POST, and tht it has a
    > > length of 1431. Yet all my attempts to read it are failing
    > > miserably.
    > >
    > > use CGI qw:)standard);
    > > ...
    > > read STDIN, $serg_bffr, $ENV{'CONTENT_LENGTH'};
    > > ...

    >
    > Since you are using it, why not use CGI to read from STDIN for you?


    IIRC It does automatically when use()d in this manner. Thats why
    $serg_bffr is empty.

    Todd W.
    Todd Wade, Feb 14, 2008
    #5
  6. Todd Wade wrote:
    > On Feb 13, 2:36 pm, wrote:
    >> SergioQ <> wrote:
    >>>
    >>> use CGI qw:)standard);
    >>> ...
    >>> read STDIN, $serg_bffr, $ENV{'CONTENT_LENGTH'};

    >>
    >> Since you are using it, why not use CGI to read from STDIN for you?

    >
    > IIRC It does automatically when use()d in this manner. Thats why
    > $serg_bffr is empty.


    I think your memory gives way. ;-) IIRC, STDIN is emtied only when you
    create a CGI object or call one of the CGI.pm functions.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 14, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,951
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    785
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,342
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    424
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    291
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page