Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

getting sometimes empty cgi form with post

by david2008 (Scribe)
on Mar 07, 2010 at 14:46 UTC ( #827238=perlquestion: print w/replies, xml ) Need Help??
david2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have the following question. I have a cgi program where i get sometimes in the cgi an empty cgi object, even i send a non empty form from my browser.
I checked in the environment variables of the %ENV hash that the content length is greater than 0, so the browser and apache could not be the problem .
Afterwards i thought that maybe the cgi module may not parse the input well, but the cgi_error function does not show anything. So i am really stuck.
I wanted to ask 2 questions
  • What could be the problem.
  • How could i tackle the problem.
Thanks in advance, David
  • Comment on getting sometimes empty cgi form with post

Replies are listed 'Best First'.
Re: getting sometimes empty cgi form with post
by liverpole (Monsignor) on Mar 07, 2010 at 14:53 UTC
    Hi david2008,

    Have you tried the following? ...

    use CGI::Carp qw(fatalsToBrowser);

    It sends fatal errors to the web page, which is a great help when you're debugging.

    You can also look at the logs, which will be some place like /var/log/httpd if you're on a Linux system.  As user "root", do:

    # tail -100f /var/log/http/error_log
    and see if you get any errors that way.

    Finally, if you don't have:

    use strict; use warnings;

    near the beginning of the CGI script(s), I'd strongly recommend putting them in.  They will often help you catch problems you wouldn't otherwise have noticed.

    Good luck!

      Hi liverpole, Thank you for your quick reply.
      I have no error in the error log.
      Even the cgi_error function does not tell anything.
      Surely i use strict and warnings.
      Thanks, David
        Yes, surely you do! :)

        Well, I'm out of ideas.  Unless you've got some code that you can post, I'm afraid I don't have a clue what could be wrong.

        Here's a suggestion -- make a copy of your program, and work on the copy (to avoid messing up the original).  Assuming the problem persists, continue to make the program smaller and smaller until one of several things happen:

        1. You figure out what the problem was.
        2. The problem goes away.
        3. The problem is still there but the program is incredibly short, and self-contained.

        Now in the first case, you've solved the problem on your own.  Sometimes it was something amazingly dumb (eg. a typo), and you've avoided the embarrassment of anyone else knowing what you did.  But at least you solved it, and probably had some fun doing so.  Now go apply the solution to the original program.

        In the second case, you've sort of solved the problem, but you just don't know how yet.  Look carefully.  Did you make few enough changes that you can undo something and make the problem reoccur?  Perhaps you've narrowed it down to the point that you have some keyword(s) that Google will help you with.  (Update:  If you still can't figure out why the problem went away, you can always come back here for more help, armed with a lot more information than you had previously).

        In the final case, you're still not done, but you're very close.  Assuming the code is fairly small and self-contained, all of Perlmonks will be eager to help.

        By "self-contained", I mean of course that there can be no external data that you haven't provided.  If you have files that the script depends on, either show them (and make sure they're as short as you can possibly make them!), or, even better, include them as data internally to your program.  Remember, we don't necessarily want to see the original code, we'd prefer to see the shortest example you can provide that still fails.

        There's nothing more fun than a challenge, but only when the problem can be recreated by anyone.  Yours unfortunately isn't yet, as you haven't shown any code, but if you work at it until you can post some example, we can very likely help you with the rest!

Re: getting sometimes empty cgi form with post
by almut (Canon) on Mar 07, 2010 at 16:30 UTC

    Don't know what the problem is... so just two suggestions that might help to narrow down on the issue:

    • Check if you can actually read the raw query string from STDIN (before instantiating the CGI object), like this

      if ($ENV{REQUEST_METHOD} eq 'POST') { my $n = read STDIN, my $query, $ENV{CONTENT_LENGTH}; warn $! unless defined $n; # (-> check error_log) # ... check $query }

      (just for debugging purposes, of course — handling query strings yourself is generally considered a no no :)

    • Try CGI::Simple instead of  It's more or less a drop-in replacement, as long as you stick to the OO style usage.

    Those tests might help to figure out whether it's maybe an obscure bug with the CGI module (not entirely impossible), or if it's that the socket (or the pipe from apache to the CGI process) is being closed prematurely for whatever reason, or some such.

      Hi Almut, Thanks for your reply.
      It really helped me a lot.

      What is interesting that when the problem occurs i read 0 bytes and when i switched to cgi::simple it gave the following error with the cgi_error function. "500 Bad read on POST! wanted 135, got 0 at ...".

      Now i have just to find out why this happens :-).
      Thanks, David
        Now i have just to find out why this happens

        You could try strace (facetiously aka "Almut's Golden Hammer" at the place I work... — yes, I do tend to slightly overuse it, but it has definitely helped me numerous times to solve problems within minutes, that other people before had messed around with for hours or days ;)

        This might help to debug how/if the query string is handled apache-internally, and where exactly the error occurs that leads to it being dropped. At least if the problem is replicable enough that you won't have to wait thousands of requests for it to occur. To ease debugging, you might want to start up apache in single process mode (option -X). Also, don't forget to trace forked processes (the CGI script) as well (option -f).

Re: getting sometimes empty cgi form with post
by Anonymous Monk on Dec 01, 2010 at 20:38 UTC
    Be sure you are not printing out anything before you are attempting to read in from STDIN. If you have something like print header; it will clear STDIN.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827238]
Approved by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-05-25 19:33 GMT
Find Nodes?
    Voting Booth?