Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Perl CGI complain about "Premature end of script headers"?

Tags:

cgi

perl

I'm sure someone could answer this very quickly, but I'm just new to perl...

I'm trying to modify demarc (a simple network monitoring tool) to do a system call to a simple script. The script itself does nothing, I'm just trying to do a 'proof-of-concept' because I keep getting an internal server error. Permissions to the script have been set to 777. When I comment the system() call, everything's fine. So that makes me suspect that it's the system() call where the error's happening. I've also tried exec(), but that didn't work also. The error could not be in the script itself since there's only an echo "test" in it.

Have I missed any permissions or is there some other way of making this work? Any advise would be appreciated.

sub generate_ticket {
   my @args = ("$base_path/test.pl");
   exec(@args);
}

This is called somewhere in file like this:

} elsif ($FORM{'delete_type'}=~/generate/) {
    my $message = &generate_ticket($delete_array_ref);
    #&ack_events($delete_array_ref);
    $events_deleted = (@$delete_array_ref);
    &push_message("<FONT COLOR=red><B>Result: $message.</B></FONT>");
}

test.pl:

#!/usr/bin/perl
print "Test";

Error log: [Mon Nov 30 14:58:22 2009] [error] [client 127.0.0.1] Premature end of script headers: demarc, referer: http://localhost/dm/demarc?td=show_events&limit=60&sid=35

like image 517
EDJ Avatar asked Nov 30 '09 19:11

EDJ


3 Answers

"Premature end of script headers" is not a terribly useful error message on its own. It could be caused by any of a number of things, such as:

  • not being executable (permissions problem)
  • failing compilation (syntax error, dependency problem, etc.)
  • terminating prematurely during regular execution
  • producing something other than proper HTTP headers as your script's first output

However, in this case, if we are to take your example script literally (print "TEST"), and you output this before your HTTP headers, then you are not producing HTTP headers first, so it's the last one. The web server expects headers, not "TEST."

If that's not the case, we need to see more of the context of your code to know what might have happened. Could be a permissions problem executing test.pl, for example.

like image 82
Adam Bellaire Avatar answered Oct 13 '22 22:10

Adam Bellaire


One way to find the cause of the premature-thing is to make the errors go to the browser. You just have to send the content-type header early in the application, for example like this, somewhere at the top of your code:

BEGIN {
    print "Content-type: text/plain\n\n";
}

Now you should be able to see the error in the browser.

like image 31
zoul Avatar answered Oct 13 '22 21:10

zoul


You probably want system, not exec:

The exec function executes a system command and never returns-- use system instead of exec if you want it to return.

See the documentation for exec.

like image 2
Leonardo Herrera Avatar answered Oct 13 '22 22:10

Leonardo Herrera