• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Premature end of script headers / Internal Server Error

We have a site using Perl which began giving the Internal Server Error. It consists of two pages. The first is a login page called "login". The second is a ordering page called "order". I'm not too familiar with Perl so I'm hoping to get some info to clear up this error.

We first open the login page domain.com/cgi-bin/login and looking at the error logs it gives the error:
      Use of uninitialized value in concatenation (.) or string at login line 22.
Here is the code from that page with line numbers and comments/line spaces removed:
  1: #!/usr/bin/perl -w
  8: use CGI;
  9: $q = new CGI;
14: my $order_cgi = "http://domain.com/cgi-bin/order";
17: my $errstr = "<h2>Invalid username or password</h2>" if $q->param('ERROR');
20: print $q->header;
22: print <<"EOF"
23: <html>
.... etc, etc..

On the login page we enter a username/password, click enter and the ordering page is loaded which is at domain.com/cgi-bin/order. This page doesn't load but instead comes up with the classic Internal Server Error. looking at the error logs it shows:
[Tue May 17 12:22:41 2005] [error] [client my.ip.here] Premature end of script headers: /home/httpd/vhosts/domain.com/cgi-bin/order
Here is the first part of the code up to the end of the headers:
<%args>
$Username => ""
</%args>
<%init>
use DBI;
my $dbh = DBI->connect("DBI:CSV:f_dir=/home/rhaynes/db")
 || die "Cannot connect: " . $DBI::errstr;
 my $sth = $dbh->prepare("select * from robin where Username=?");
 my $nr=$sth->execute($Username);
 my @row = $sth->fetchrow_array();
</%init>
<html>
<head>
<script language="JavaScript">
<!--
a whole bunch of variable initialized with:
var
and some functions
//-->
</script>
<title>Home Page</title>
</head>

What would be causing these errors?
0
wookaka
Asked:
wookaka
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
StormyWatersCommented:
If I recall correctly, and mind you I'm not a CGI programmer, there aren't any quotes around "EOF" when using it the way you have on line 22. I don't know if that's the only error. It should be:

print << EOF; #semicolon here.
<html>
...etc...
EOF #not here.
0
 
wookakaAuthor Commented:
Unfortunately when removing the quotes the login page now gives Internal Server Error and doesn't load at all.
The semicolon doesn't seem to make a difference.
0
 
TintinCommented:
The easiest way to see the perl errors is by adding:

use CGI::Carp qw(fatalsToBrowser);

to your script.

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
wookakaAuthor Commented:
I tried:

use CGI::Carp qw(fatalsToBrowser);
use CGI;
$q = new CGI;

and

use CGI::Carp qw(fatalsToBrowser);
$q = new CGI;

...the first doesn't change anyting and the second gives an error saying that CGI wasn't initialized.
0
 
TintinCommented:
The 2nd one won't work as you are trying to initialise a CGI object without using the CGI module.

Try adding:

$|=1;

near the top of the script to turn off buffering.
0
 
ahoffmannCommented:
please start the script from command line and post result verbatim
0
 
FishMongerCommented:
My first assumtion is that the database connection is failing and the die statement is being executed/printed prior to the headers.

>> The semicolon doesn't seem to make a difference.
It does make a difference.  If you send warnings to the browser in addition to the fatal errors, you'll see the error that's being generated when you don't have the semicolon.


use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

warningsToBrowser(1);   # put this after printing the headers
0
 
wookakaAuthor Commented:
I'm not sure what command you would like me to use to "start the script from command line and post result verbatim"??? I used bash and got the following:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
/home/httpd/vhosts/domain.com/cgi-bin/order: use: command not found
: command not founddomain.com/cgi-bin/order:
/home/httpd/vhosts/domain.com/cgi-bin/order: line 24: syntax error near unexpected token `qw(f'
'home/httpd/vhosts/domain.com/cgi-bin/order: line 24: `use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

After looking at the database it was loading it looks like that was a problem. The customer had uploaded an old page to try to get things working but did not fix the problem. I put the correct page up so the database looks like it should be loading correct.  In the general error logs we're still getting the premature end of script headers. I've included the top portion of the corrected orders page below. Spaces and comments removed to keep things short.
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);      ### added to show errors in browser
$q = new CGI;
$db_file = "/home/httpd/vhosts/domain.com/db/robin.csv";
$login_url = "http://domain.com/cgi-bin/login";
$dberror_url = "http://domain.com/cgi-bin/dberror";
$user = $q->param('Username');
$pass = $q->param('Password');
unless ($user && $pass)
{
  print $q->redirect($login_url . "?ERROR=1");
  exit;
}
unless (open(DB, $db_file))
{
  print $q->redirect($dberror_url);
  exit;
}
<DB>; # Get rid of the header line...
while (<DB>)
{
   @row = split /,/;
   last if $row[0] eq "$user";
}
close DB;
if ($row[0] ne "$user" || $row[1] ne "$pass")
{
   print $q->redirect($login_url . "?ERROR=1");
   exit;
}
print $q->header;
print <<"EOF";       ## added the semicolon
<html>
<head>
etc, etc,

I've tried putting warningsToBrowser(1); in several locations but none actually display the errors on the browser. Where excatly should this go? after the last EOF? I apologise I'm not at all familiar with Perl so detailed instructions on exactly where code should go is much appreciated.

Thanks
0
 
FishMongerCommented:
print $q->header;
warningsToBrowser(1);
print <<"EOF";
<html>   #  remove this line because it's already printed via print $q->header;
<head>  #  remove this line also
etc, etc,

It may not make a difference but I don't think you need the exit statement after the redirect.
0
 
ahoffmannCommented:
>  I'm not sure what command you would like me to use ..
the command is the script you posted
In bash simply do:
   perl your-script-name here
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now