Premature end of Script Headers, Error 500, Movable Type and You

| No Comments | No TrackBacks
I already explained earlier what to make of cryptic "Can't call method ... on an undefined value" errors.  Today, we are going to take a look at a different kind of errors that are often just as mysterious and frustrating: the dreaded "Error 500" when accessing any of Movable Type's .cgi scripts.  The error logs of the webserver only tell you about "Premature end of script headers" in some script.  Real informative...
What is going on?

First, a quick refresher course about cgi scripts.  Under normal conditions, a webserver will just serve files to browsers requesting them (You want "index.html"?  Here it is!).  Invisibly, the webserver also sends along some information about the file: how big is it, what encoding does it have...  All this information is sent first in the so called "HTTP Headers".

Normally you never need to worry abou the headers: your webserver adds them, the browser interprets them, and they are never actually displayed anywhere.  Computers talking to computers, basically.

Now with CGI scripts, something else is going on.  Instead of serving the contents of a file, the webserver is now serving the output of a script to a browser making a request.  Scripts can produce different output depending on how they are called, and this allows for all kinds of fun stuff to happen.

But since the webserver now can't know what output is coming, it also can't tell the browser anything about it via the HTTP headers.  This responsability has shifted to the CGI script, which is expected to produce its own HTTP headers before coming up with the 'real' content.

What happens if there is an error?

In most cases the above scenario will work out fine: request comes in, script runs, produces HTTP headers and output, webserver passes it on to the browser, everybody is happy.

But sometimes a script will fail to run and will exit with an error message.  In this case, the webserver will notice that the script ended badly and will inform the browser via a standard "Error 500" message.  The actual error message produced by the script will be logged to the error log of the webserver in most cases.

So if you are experiencing this type of error, the first place to look is the webserver error log.  It will tell you what exactly went wrong with your cgi script, and with a bit of luck you will be able to fix it.

Premature end of script headers?

So a script can either run and produce headers and output, or end with an error.  But what if it runs without errors while not poducing proper headers?  That is where "Premature end of script headers" error messages come from: it is basically the webserver noticing that a script ran without throwing any error messages, but the output didn't contain valid headers at the start.

The cause of this error can be anything: a debug message in the script that got printed before the headers, an error message that was printed instead of being reported, a badly written script that fails to produce output (but doesn't throw an error)...

How to debug this?

If you have access to your webserver and you know the exact script call that was resulting in the error, you can try logging in on your server and going to the folder where Movable Type is installed.  Then run the script from the command line, using the same parameters.  So if a URL like this returned the error in question:

http://www.mysite.com/mt/mt.cgi?foo=bar&bla=baz

Run this command from your mt/ folder:

perl mt.cgi foo=bar bla=baz | less

(Optionally, add username=xxx and password=yyy if you were trying to access a function that requires you to be logged in.)

You will see the output the webserver is seeing, including the HTTP headers at the start.  If you see any "funny" headers, or messages before the headers, that is where you should start investigating.  Where do these headers or messages come from, and how to get rid of them?

In most cases the culprit will turn out to be some plugin you installed recently, but I have also seen errors caused by image processing libraries there, so you never know until you check.  Good hunting!

No TrackBacks

TrackBack URL: http://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/23

Leave a comment