CGI & HTTP 500

I'am currently writing a small app in C that should display a timetable, on a Solaris 10/Apache Sun server.
The app should open a HTML skeleton, syntaxicaly replace a couple of placeholders and display the content.
If the skeleton could not be found, an HTML error message is sent back to the user.
I compile, and test it: runs #1.
I move my executable in the cgi-bin folder and invoke it with my browser and .. bang!: HTTP 500.
The error_log shows:

malformed header from script. Bad header=<HTML>: /apache/web/cgi-bin/timesheet.cgi


The HTTP-500 arises mostly when Content-type:... is missing, or if there's not empty line before the text.
However, in my case, when I invoked it manually:

/apache/web/cgi-bin/timesheet.cgi

.. it worked perfectly, displaying it could find its skeleton:

Content-type: text/html

<html>
[... Error message ...]
</html>

So what was going on?
I replaced the binary by a small script displaying the error message, hit it with my brower ... and it worked!
So, I was facing a situation when my executable crashed when a smallscript displaying exactly the same message worked.
Okay, I give you the trick (it took me truss to figure out what was going on under the hood): remember my executable should display the error message if the skeleton was missing. But the skeleton was actually in place. Then, when I invoke the executable from a place elsewhere than the cgi-bin folder, it couldn't find the skeleton and display the error message. But invoked by Apache, in was in the right context, found its skeleton, did its placeholder-replacements and display the content ... without Content-type:... because it was missing in the skeleton!
Grrr! It took me 2 days to figure out this bug.

Comments

Popular Posts