MrCromeDome has asked for the wisdom of the Perl Monks concerning the following question:

This one has me thoroughly stumped:
[Thu Jun 21 10:53:08 2001] [error] [client 192.168.0.1] Premature end +of script headers: d:/www/cgi-bin/view.cgi

I am writing a web-based search utility for one of our Win32 applications. The database it will be using is millions of rows in size, spanning 15 years of information. I took a small subset of that data and developed the search utility with it.

On the smaller dataset, the script works flawlessly. When I started using the larger database, the above mentioned error started occuring. I click a link on the results page to bring up detailed information about a document, which calls my view script with a certain set of parameters. If I want a more detailed view of part of that document, I click on a second link, which calls the same view script again, this time with slightly different parameters, only this time perl triggers an application error/doc watson/gpf type error.

Failing to believe that something suddenly cropped up with the code, I opened a command prompt and executed the same script with the same parameters as the browser. As expected, the script performed as intended.

I am using ActiveState Perl and Apache 1.3.19 on Windows 2000. My DBMS is (unfortunately) MS SQL Server 7.0, which I am accessing via the DBD::ODBC module.

Is it likely something in my code, Apache, my database, ActivePerl, or the interaction of Apache and the ODBC driver? I have not posted the code as it is quite long, but would be happy to provide it if it would be of assistance.

Any help or suggestions are greatly appreciated!

MrCromeDome

Replies are listed 'Best First'.
Re: Premature end of script headers?
by Trimbach (Curate) on Jun 21, 2001 at 21:13 UTC
    If I get this correctly you're running your CGI against Database #1 and it works fine from both the command line and the browser. Changing the CGI to run against Database #2 runs fine from the command line, but not from the browser. Is this correct? If that's the case, you should make sure that Database #2 is accessible from the user that your CGI is running as... that is, if it works from the command line then Database #2 is cool with you as the user... if it don't work from the browser then it seems clear to me that Database #2 has issues with the "web" user ("nobody" on many machines.)

    Just a thought...

    Gary Blackburn
    Trained Killer

      You are exactly correct, except for one thing: even on the new database, the script does work from the browser with some parameters, but not with others. Regardless of the parameters passed though, the script entirely works from the command line.

      If the script didn't work from the command line, this would be pretty easy. But none of my die statements are ever reached. Nor does ActivePerl allow me to use CGI::Carp (at least not that I'm aware of).

Re: Premature end of script headers?
by tachyon (Chancellor) on Jun 21, 2001 at 22:29 UTC

    You will be generating an error in your code, somehow. To see what is causing the problem add these lines:

    #!/usr/bin/perl -w ensure all fatals go to browser during debugging and setup comment these 3 lines out on production code for security $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser');

    These lines need to go at the top - no if ands buts or maybes.

    They will force the error to print in the browser window for you.

    The $|=1; forces buffer flushing. We then print a valid complete header and then add CGI::Carp. Together very few errors will not appear in the browser window with these active. Some 500s still sneak through on rare occasions but this usually does the trick.

    hope this reveals your problem

    cheers

    tachyon

      I added those lines exactly as you wrote them. The results were bizarre and unexpected, but now, for whatever reason, it works :) In what way were they bizarre and unexpected? I added those lines, changed nothing else, and for whatever reason, the code works. No warnings are shown to the browser or to the server logs.

      I also didn't know that CGI::Carp was available for ActivePerl.

      Thanks for your help!
      MrCromeDome

        In what way were they bizarre and unexpected? I added those lines, changed nothing else, and for whatever reason, the code works.

        In the fall of 1980, I took some computer courses at USL. There was a program called PPOM that acurately predicted the amount of time to retrieve a printout and the amount of bugs a program would encounter.

        If PPOM had a high score, you could pretty much give up on fixing your program. Often the very same code would run error free the next day. (The same thing occurs today when I restart windows.)


        What does PPOM stand for?
        Print Phases Of the Moon
        Go figure!

        Enjoy,
        Charles K. Clarkson

        Look in the top left hand corner of the browser window. Whant *must* have changed is that your script was emitting some noise on STDOUT prior to the "Content-Type: text/html\n\n" header. Anyway glad it worked!

        Cheers

        tachyon

Re: Premature end of script headers?
by stefan k (Curate) on Jun 21, 2001 at 20:30 UTC
    hi,
    this is usually due to missing content type header. If you
    use CGI;
    you could just
    print header;
    which by default prints a HTML content type Header:
    print "Content-type: text/html\n\n";
    Make sure this gets printed before the script might die.

    Hope this helps.

    Regards... Stefan

      Please don't take this as me being rude. I think I didn't explain myself well enough.

      If I were missing a header altogether though, wouldn't I get a malformed header error, not a premature end of header?

      I have a line in my code that prints print "Content-Type: text/html\n\n"; before anything else. What has me confused is why the script runs in the browser with some parameters but not with others when the only thing I changed is the database I was using.

      Regardless of the parameters passed, the script runs fine from the command prompt. The header info produced in that manner looked ok to me too. I believe that the problem has nothing to do with the actual header. . . I think that perl dying in that manner is producing the header problems.

      Still stumped,
      MrCromeDome

        Please don't take this as me being rude.

        I no way :-)

        I'm not really familiar with the internals and the background of all this. I'm telling you my experiences. Whenever I encountered that error it was due to missing header information.
        Well, I might be wrong with this; but it's what I have experienced.

        Sorry if this isn't what you were looking for.

        Regards... Stefan