premature end of script headers...

When I try to run the PERL script below, I get an error "error 500 internal server error: premature end of script headers"

I have another script which works perfectly using the same first line, so I know that the path to the perl interpreter is correct.

The sysop of my server has suggested that this problem can occur if STDOUT is not flushed on output - the solution being to flush stdout explicitly at the end of the program.  But I don't know how to do that.  How do you flush stdout in PERL?  I am already using "close(STDOUT);" whether that flushes STDOUT or not I don't know, but it isn't solving the problem.

What else causes this problem and what other things can I try to solve it?

Here is my script.  I got the image display code from someone else's script, so don't ask me about it because I don't know.  It outputs a 1x1 pixel transparent gif image correctly in another test script that I have that works.

#!/usr/bin/perl
$logfile = ">>counter.log";
open (LOGFILE, $logfile) or die;

# append environment info to log file
print LOGFILE @ENV;

# close log file
close(LOGFILE);

# Output a blank image.
#
#############################################################################
# first initialize the 1 X 1 transparent gif to be sent back to the user
# this is the hex code for the binary image data
$g_sHeader0  =   "\x47\x49\x46\x38\x37\x61\x01\x00\x01\x00\xe7\x00\x00";
$g_sColorMap =   "\x5d\x5d\x5d\x65\x65\x65\x68\x68\x68\x73\x73\x73\x58\x58"
                ."\x58\x4b\x4b\x4b\x3a\x3a\x3a\x26\x26\x26\x1b\x1b\x1b\x1a"
                ."\x1a\x1a\x18\x18\x18\x1c\x1c\x1c\x29\x29\x29\x2e\x2e\x2e"
                ."\x66\x66\x66\x19\x19\x19\x1e\x1e\x1e\x20\x20\x20\x21\x21"
                ."\x21\x22\x22\x22\x0f\x0f\x0f\x37\x37\x37\x62\x62\x62\x23"
                ."\x23\x23\x14\x14\x14\x50\x50\x50\x24\x24\x24\x3f\x3f\x3f"
                ."\x84\x84\x84\x92\x92\x92\x77\x77\x77\x33\x33\x33\x25\x25"
                ."\x25\x78\x78\x78\xff\xff\xff\xf2\xf2\xf2\x38\x38\x38\x46"
                ."\x46\x46\x27\x27\x27\x2a\x2a\x2a\xca\xca\xca\x94\x94\x94"
                ."\xe4\xe4\xe4\xe3\xe3\xe3\x16\x16\x16\x11\x11\x11\x4a\x4a"
                ."\x4a\x2c\x2c\x2c\xa2\xa2\xa2\x5e\x5e\x5e\x12\x12\x12\x39"
                ."\x39\x39\x40\x40\x40\xc8\xc8\xc8\xa9\xa9\xa9\x15\x15\x15"
                ."\xe2\xe2\xe2\x8a\x8a\x8a\xf1\xf1\xf1\xc4\xc4\xc4\xb5\xb5"
                ."\xb5\x10\x10\x10\x0d\x0d\x0d\xc3\xc3\xc3\x0a\x0a\x0a\x0b"
                ."\x0b\x0b\xf0\xf0\xf0\x89\x89\x89\xb1\xb1\xb1\x06\x06\x06"
                ."\x04\x04\x04\x03\x03\x03\xc1\xc1\xc1\x07\x07\x07\x05\x05"
                ."\x05\xdf\xdf\xdf\x80\x80\x80\x01\x01\x01\x00\x00\x00\x8f"
                ."\x8f\x8f\xbf\xbf\xbf\x36\x36\x36\x60\x60\x60\xef\xef\xef"
                ."\x30\x30\x30\x35\x35\x35\x70\x70\x70\x34\x34\x34\x32\x32"
                ."\x32\x42\x42\x42\x41\x41\x41\x3e\x3e\x3e\x3d\x3d\x3d\x3c"
                ."\x3c\x3c\x28\x28\x28\xbc\xbc\xbc\x2b\x2b\x2b\x7b\x7b\x7b"
                ."\x1f\x1f\x1f\x95\x95\x95\xfe\xfe\xfe\x86\x86\x86\xf5\xf5"
                ."\xf5\xf7\xf7\xf7\x13\x13\x13\x7c\x7c\x7c\xe6\xe6\xe6\xe7"
                ."\xe7\xe7\x08\x08\x08\x72\x72\x72\xdb\xdb\xdb\x02\x02\x02"
                ."\x6e\x6e\x6e\xda\xda\xda\x6d\x6d\x6d\x52\x52\x52\xa3\xa3"
                ."\xa3\x9f\x9f\x9f\x4f\x4f\x4f\xd7\xd7\xd7\xe5\xe5\xe5\x6c"
                ."\x6c\x6c\xd6\xd6\xd6\xc7\xc7\xc7\x45\x45\x45\xa7\xa7\xa7"
                ."\x7a\x7a\x7a\xfd\xfd\xfd\xa0\xa0\xa0\xce\xce\xce\x2d\x2d"
                ."\x2d\xc2\xc2\xc2\xea\xea\xea\xeb\xeb\xeb\xa6\xa6\xa6\xd5"
                ."\xd5\xd5\x59\x59\x59\xdc\xdc\xdc\xbd\xbd\xbd\xbb\xbb\xbb"
                ."\xd3\xd3\xd3\x53\x53\x53\xfc\xfc\xfc\xfa\xfa\xfa\x4d\x4d"
                ."\x4d\x67\x67\x67\xf3\xf3\xf3\x44\x44\x44\xf4\xf4\xf4\x3b"
                ."\x3b\x3b\xe8\xe8\xe8\xd8\xd8\xd8\x0e\x0e\x0e\xb7\xb7\xb7"
                ."\xe1\xe1\xe1\x61\x61\x61\x54\x54\x54\x79\x79\x79\xc9\xc9"
                ."\xc9\xab\xab\xab\x8b\x8b\x8b\x31\x31\x31\x6b\x6b\x6b\x87"
                ."\x87\x87\x83\x83\x83\x7f\x7f\x7f\x64\x64\x64\xed\xed\xed"
                ."\xb0\xb0\xb0\xae\xae\xae\xad\xad\xad\xe0\xe0\xe0\xaa\xaa"
                ."\xaa\x93\x93\x93\x90\x90\x90\x8e\x8e\x8e\x8d\x8d\x8d\x69"
                ."\x69\x69\xa8\xa8\xa8\xf8\xf8\xf8\x91\x91\x91\xba\xba\xba"
                ."\x9e\x9e\x9e\x56\x56\x56\xa5\xa5\xa5\xe9\xe9\xe9\xde\xde"
                ."\xde\x1d\x1d\x1d\x6f\x6f\x6f\xd9\xd9\xd9\xd2\xd2\xd2\xd1"
                ."\xd1\xd1\xb6\xb6\xb6\xd0\xd0\xd0\x5c\x5c\x5c\x74\x74\x74"
                ."\xcf\xcf\xcf\x9d\x9d\x9d\xc6\xc6\xc6\xc5\xc5\xc5\xfb\xfb"
                ."\xfb\xb8\xb8\xb8\x48\x48\x48\x5f\x5f\x5f\x76\x76\x76\x9c"
                ."\x9c\x9c\xdd\xdd\xdd\x88\x88\x88\x96\x96\x96\x9a\x9a\x9a"
                ."\xee\xee\xee\x7e\x7e\x7e\xcd\xcd\xcd\x4c\x4c\x4c\xb4\xb4"
                ."\xb4\xaf\xaf\xaf\xb3\xb3\xb3\xbe\xbe\xbe\x75\x75\x75\x49"
                ."\x49\x49\xcc\xcc\xcc\xa1\xa1\xa1\xf6\xf6\xf6\x63\x63\x63"
                ."\x6a\x6a\x6a\x51\x51\x51\xf9\xf9\xf9\x4e\x4e\x4e\x57\x57"
                ."\x57\xc0\xc0\xc0\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
$g_sHeader1  =   "\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x08";


#output the graphic
print "Content-Type: image/gif\n\n";
        binmode( *STDOUT );
        print STDOUT $g_sHeader0 . $g_sColorMap . $g_sHeader1 . "\x00;";
        close( STDOUT );
LVL 1
risAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adam923Commented:
binmode *STDOUT should be binmode STDOUT
if this is a unix system, which it looks to be (#!/usr/bin/perl) you should get rid of binmode all together because "binmode has no effect under UNIX" --camel p147
0
ozoCommented:
Is the environment info getting appended to counter.log?
0
maabuCommented:
Maybe you should have:
  open (LOGFILE, $logfile) || die;
instead of
  open (LOGFILE, $logfile) or die;

I do not believe "or" is a proper operator in this context.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ozoCommented:
it is unless you're using an obsolete version of perl.
0
maabuCommented:
Hmmm - I didn't know that or was useable in that context.

Here are some other things to consider on the issue:

If you're getting "premature end of script headers" then it's obvious the program is terminating prior to the output of the MIME type designation - presumably with some sort of error.  I would check to make sure you have appropriate access rights with the directory/file you're trying to write to, which appears to be the same location as your script, which may or may not be a good idea.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adam923Commented:
maabu- open or die is what's used in the "bible" of perl, the camel book
ris- what happens when you just run the script from the command line? does it compile?  what does it output?
0
bigbedCommented:
I tried perl -c on it under Unix - syntax is OK.
I ran it from the command line - OK.
I ran it from browser - failed due to permission for creating/appending to the file counter.log.
Could this be the problem?
0
ozoCommented:
It could indeed be the problem.  Can you print the headers before opening the file?
Does the file exist?
If so, what are its permissions and who is its owner?
If not, what are its directory's permisisons and whi is its owner?
What user is the CGI script running under, and what directory is it connected to?
0
risAuthor Commented:
OK, I tweaked my code a little bit.  The new script is copied below.

It turns out that the root of the problem was with the counter.log file that I was trying to write to - I didn't have permission when I ran the script from my web browser.  So it runs now and displays a 1x1 transparent GIF.  Thanks for pointing out the file permissions!

Now I have a new problem: the environment info is not being written to the file.  In my updated script, I write some normal text to the file to make sure that I can write to the file properly and that the info I write to it is actually being saved.  However, all I get is:

Environment:


<EOF>

which means that either the environment is empty (don't see how that is possible) or I'm doing something wrong.  Can somebody help me out with this?

Here's my script:

#!/usr/bin/perl
$logfile = ">>counter.log";
open (LOGFILE, $logfile) or die;

# append environment info to log file
print LOGFILE "Environment:\n";
print LOGFILE @ENV;
print LOGFILE "\n\n";

# close log file
close(LOGFILE);

# Output a blank image.
#
#############################################################################
# first initialize the 1 X 1 transparent gif to be sent back to the user
# this is the hex code for the binary image data
$g_sHeader0  =   "\x47\x49\x46\x38\x37\x61\x01\x00\x01\x00\xe7\x00\x00";
$g_sColorMap =   "\x5d\x5d\x5d\x65\x65\x65\x68\x68\x68\x73\x73\x73\x58\x58"
                ."\x58\x4b\x4b\x4b\x3a\x3a\x3a\x26\x26\x26\x1b\x1b\x1b\x1a"
                ."\x1a\x1a\x18\x18\x18\x1c\x1c\x1c\x29\x29\x29\x2e\x2e\x2e"
                ."\x66\x66\x66\x19\x19\x19\x1e\x1e\x1e\x20\x20\x20\x21\x21"
                ."\x21\x22\x22\x22\x0f\x0f\x0f\x37\x37\x37\x62\x62\x62\x23"
                ."\x23\x23\x14\x14\x14\x50\x50\x50\x24\x24\x24\x3f\x3f\x3f"
                ."\x84\x84\x84\x92\x92\x92\x77\x77\x77\x33\x33\x33\x25\x25"
                ."\x25\x78\x78\x78\xff\xff\xff\xf2\xf2\xf2\x38\x38\x38\x46"
                ."\x46\x46\x27\x27\x27\x2a\x2a\x2a\xca\xca\xca\x94\x94\x94"
                ."\xe4\xe4\xe4\xe3\xe3\xe3\x16\x16\x16\x11\x11\x11\x4a\x4a"
                ."\x4a\x2c\x2c\x2c\xa2\xa2\xa2\x5e\x5e\x5e\x12\x12\x12\x39"
                ."\x39\x39\x40\x40\x40\xc8\xc8\xc8\xa9\xa9\xa9\x15\x15\x15"
                ."\xe2\xe2\xe2\x8a\x8a\x8a\xf1\xf1\xf1\xc4\xc4\xc4\xb5\xb5"
                ."\xb5\x10\x10\x10\x0d\x0d\x0d\xc3\xc3\xc3\x0a\x0a\x0a\x0b"
                ."\x0b\x0b\xf0\xf0\xf0\x89\x89\x89\xb1\xb1\xb1\x06\x06\x06"
                ."\x04\x04\x04\x03\x03\x03\xc1\xc1\xc1\x07\x07\x07\x05\x05"
                ."\x05\xdf\xdf\xdf\x80\x80\x80\x01\x01\x01\x00\x00\x00\x8f"
                ."\x8f\x8f\xbf\xbf\xbf\x36\x36\x36\x60\x60\x60\xef\xef\xef"
                ."\x30\x30\x30\x35\x35\x35\x70\x70\x70\x34\x34\x34\x32\x32"
                ."\x32\x42\x42\x42\x41\x41\x41\x3e\x3e\x3e\x3d\x3d\x3d\x3c"
                ."\x3c\x3c\x28\x28\x28\xbc\xbc\xbc\x2b\x2b\x2b\x7b\x7b\x7b"
                ."\x1f\x1f\x1f\x95\x95\x95\xfe\xfe\xfe\x86\x86\x86\xf5\xf5"
                ."\xf5\xf7\xf7\xf7\x13\x13\x13\x7c\x7c\x7c\xe6\xe6\xe6\xe7"
                ."\xe7\xe7\x08\x08\x08\x72\x72\x72\xdb\xdb\xdb\x02\x02\x02"
                ."\x6e\x6e\x6e\xda\xda\xda\x6d\x6d\x6d\x52\x52\x52\xa3\xa3"
                ."\xa3\x9f\x9f\x9f\x4f\x4f\x4f\xd7\xd7\xd7\xe5\xe5\xe5\x6c"
                ."\x6c\x6c\xd6\xd6\xd6\xc7\xc7\xc7\x45\x45\x45\xa7\xa7\xa7"
                ."\x7a\x7a\x7a\xfd\xfd\xfd\xa0\xa0\xa0\xce\xce\xce\x2d\x2d"
                ."\x2d\xc2\xc2\xc2\xea\xea\xea\xeb\xeb\xeb\xa6\xa6\xa6\xd5"
                ."\xd5\xd5\x59\x59\x59\xdc\xdc\xdc\xbd\xbd\xbd\xbb\xbb\xbb"
                ."\xd3\xd3\xd3\x53\x53\x53\xfc\xfc\xfc\xfa\xfa\xfa\x4d\x4d"
                ."\x4d\x67\x67\x67\xf3\xf3\xf3\x44\x44\x44\xf4\xf4\xf4\x3b"
                ."\x3b\x3b\xe8\xe8\xe8\xd8\xd8\xd8\x0e\x0e\x0e\xb7\xb7\xb7"
                ."\xe1\xe1\xe1\x61\x61\x61\x54\x54\x54\x79\x79\x79\xc9\xc9"
                ."\xc9\xab\xab\xab\x8b\x8b\x8b\x31\x31\x31\x6b\x6b\x6b\x87"
                ."\x87\x87\x83\x83\x83\x7f\x7f\x7f\x64\x64\x64\xed\xed\xed"
                ."\xb0\xb0\xb0\xae\xae\xae\xad\xad\xad\xe0\xe0\xe0\xaa\xaa"
                ."\xaa\x93\x93\x93\x90\x90\x90\x8e\x8e\x8e\x8d\x8d\x8d\x69"
                ."\x69\x69\xa8\xa8\xa8\xf8\xf8\xf8\x91\x91\x91\xba\xba\xba"
                ."\x9e\x9e\x9e\x56\x56\x56\xa5\xa5\xa5\xe9\xe9\xe9\xde\xde"
                ."\xde\x1d\x1d\x1d\x6f\x6f\x6f\xd9\xd9\xd9\xd2\xd2\xd2\xd1"
                ."\xd1\xd1\xb6\xb6\xb6\xd0\xd0\xd0\x5c\x5c\x5c\x74\x74\x74"
                ."\xcf\xcf\xcf\x9d\x9d\x9d\xc6\xc6\xc6\xc5\xc5\xc5\xfb\xfb"
                ."\xfb\xb8\xb8\xb8\x48\x48\x48\x5f\x5f\x5f\x76\x76\x76\x9c"
                ."\x9c\x9c\xdd\xdd\xdd\x88\x88\x88\x96\x96\x96\x9a\x9a\x9a"
                ."\xee\xee\xee\x7e\x7e\x7e\xcd\xcd\xcd\x4c\x4c\x4c\xb4\xb4"
                ."\xb4\xaf\xaf\xaf\xb3\xb3\xb3\xbe\xbe\xbe\x75\x75\x75\x49"
                ."\x49\x49\xcc\xcc\xcc\xa1\xa1\xa1\xf6\xf6\xf6\x63\x63\x63"
                ."\x6a\x6a\x6a\x51\x51\x51\xf9\xf9\xf9\x4e\x4e\x4e\x57\x57"
                ."\x57\xc0\xc0\xc0\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
                ."\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
$g_sHeader1  =   "\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x08";


#output the graphic
print "Content-Type: image/gif\n\n";
#binmode( *STDOUT );
print $g_sHeader0 . $g_sColorMap . $g_sHeader1 . "\x00;";
close( STDOUT ); #flush STDOUT before exiting

1; #return TRUE
0
risAuthor Commented:
I believe you were the first to point out the possibility of my log file having invalid permissions which would in turn cause the program to exit before printing the content-type headers.
0
risAuthor Commented:
for some reason I thought my last comment would end up next to the accepted answer, but it didn't.  So, to clarify, by "you" in my previous comment, I meant "maabu"

Also, I changed @ENV to %ENV in my script and the environment is recorded properly now.

Thanks everyone for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.