INTERNAL SERVER ERROR (WIERD???)

W

wizard

Hello Friends,
Kindly help.
I am working on an script which is working with our email program of
our company.
Recently some changes were made to the script and it started giving he
nasty " 500 INTERNAL SERVER ERROR".
The Apache error logs give:-
Premature end of script headers.

Now, the script does execute and the email is sent but the webpage
gives the error. Also, this error comes up only sometimes but not
everytime we run the script in identical conditions, say one in three
times. Sometimes when I go back to the previous page and again press
the submit button , it goes properly. All the file permissions are
proper, the /usr/bin/perl -w is right, Content type is correct, pert
-cwT , all are checked , I mean , all the usual answers to the problem
are checked and they are fine. If it were a syntax error, or some
header problem , it would never have run , but it is running well for
the past 7 years. The changes that I made to it were nominal and they
do not affect the output.

Also, I dont know how to step run the script from a web browser. Also,
I dont know where to look for the bug since it does run properly
SOMETIMES.

Please help.

Regards,
Vivek. J. Joshi.
 
M

Matt Garrish

wizard said:
Hello Friends,
Kindly help.

Why do you think cross-posting this same vague problem to
comp.lang.perl.misc will be any more fruitful. Please see the
recommendations I made to you in alt.perl and actually try them first.

Matt
 
B

Brian Wakem

wizard said:
Hello Friends,
Kindly help.
I am working on an script which is working with our email program of
our company.
Recently some changes were made to the script and it started giving he
nasty " 500 INTERNAL SERVER ERROR".
The Apache error logs give:-
Premature end of script headers.

Now, the script does execute and the email is sent but the webpage
gives the error. Also, this error comes up only sometimes but not
everytime we run the script in identical conditions, say one in three
times. Sometimes when I go back to the previous page and again press
the submit button , it goes properly. All the file permissions are
proper, the /usr/bin/perl -w is right, Content type is correct, pert
-cwT , all are checked , I mean , all the usual answers to the problem
are checked and they are fine. If it were a syntax error, or some
header problem , it would never have run , but it is running well for
the past 7 years. The changes that I made to it were nominal and they
do not affect the output.

Also, I dont know how to step run the script from a web browser. Also,
I dont know where to look for the bug since it does run properly
SOMETIMES.

Please help.

Regards,
Vivek. J. Joshi.



use CGI::Carp qw(fatalsToBrowser);
 
P

Paul Lalli

wizard said:
I am working on an script which is working with our email program of
our company.
Recently some changes were made to the script and it started giving he
nasty " 500 INTERNAL SERVER ERROR".
The Apache error logs give:-
Premature end of script headers.

Is that the *last* message in the log, or the *only* message in the
log?
Now, the script does execute and the email is sent but the webpage
gives the error. Also, this error comes up only sometimes but not
everytime we run the script in identical conditions, say one in three
times. Sometimes when I go back to the previous page and again press
the submit button , it goes properly. All the file permissions are
proper, the /usr/bin/perl -w is right, Content type is correct, pert
-cwT , all are checked , I mean , all the usual answers to the problem
are checked and they are fine. If it were a syntax error, or some
header problem , it would never have run ,

Not true. Just because the CGI script doesn't return proper HTTP
headers, doesn't mean that the other Perl code in the script can't run.
but it is running well for
the past 7 years. The changes that I made to it were nominal and they
do not affect the output.

Says you. Perhaps you should tell us *what* changes you made, rather
than just asking us to take your word for it...
Also, I dont know how to step run the script from a web browser. Also,
I dont know where to look for the bug since it does run properly
SOMETIMES.

What are the results of the script when run via the command line? What
is output? Are all the proper HTTP headers printed, in all possible
executions of the script?

Paul Lalli
 
W

wizard

When the scripts are run via command line, the output is indeterminate
because many of the cgi variables are given to the perl script at run
time, and also it gets executed only if the cookies are correct, i.e.
there are modules which check the cookies.
As suggested to add the line :-
use CGI::Carp qw(fatalsToBrowser);
its already present.
I am flushing the output using:-
select(STDOUT); $| = 1; #autoflush
if (FAXMAIL) { select(FAXMAIL); $| = 1; }
The module which I have added is for adding attachments from the
folders , which dosen't get executed because I dont attach anything.
Its inside an If-condition. The last line in the Apache logs is:-
Premature end of script headers in sm.e
Also, if the syntax was not okay, it would never have got executed but
it does get executed sometimes.
 
S

Scott Bryce

wizard said:
When the scripts are run via command line, the output is
indeterminate because many of the cgi variables are given to the perl
script at run time,

Hard code some values in and see what happens.

and also it gets executed only if the cookies are
correct, i.e. there are modules which check the cookies.

Bypass the modules. Or hard code in some cookie values. Or have the
module subroutines return meaningful values for testing purposes.

The last line in the Apache logs is:- Premature end of
script headers in sm.e

Then you need to know what the script is printing for headers.

I don't believe you have posted actual code. Take a look at the posting
guidelines, them reduce your script to the shortest script that
demonstrates the problem. Post that here. We can't help you with code we
can't see.
 
B

Brian Wakem

wizard said:
When the scripts are run via command line, the output is indeterminate
because many of the cgi variables are given to the perl script at run
time, and also it gets executed only if the cookies are correct, i.e.
there are modules which check the cookies.
As suggested to add the line :-
use CGI::Carp qw(fatalsToBrowser);
its already present.
I am flushing the output using:-
select(STDOUT); $| = 1; #autoflush
if (FAXMAIL) { select(FAXMAIL); $| = 1; }
The module which I have added is for adding attachments from the
folders , which dosen't get executed because I dont attach anything.
Its inside an If-condition. The last line in the Apache logs is:-
Premature end of script headers in sm.e
Also, if the syntax was not okay, it would never have got executed but
it does get executed sometimes.


It sounds like the script may not be producing any output at all, therefore
it runs but Apache returns an error as no header is output. Is the line
that prints the header inside a conditional loop? or a subroutine that
isn't guaranteed to run?

You need to narrow down the error and post some code as we can only guess
what the code consists of.
 
J

Juha Laiho

wizard said:
I am working on an script which is working with our email program of
our company.
Recently some changes were made to the script and it started giving he
nasty " 500 INTERNAL SERVER ERROR".
The Apache error logs give:-
Premature end of script headers.

Bad guesses on badly stated problem;

Could it be that some external program run by your script is in
some cases producing some output before the script gets to send
the HTTP headers?
 
W

wizard

Hello friends,
Thanks for your suggestions. I am posting some part of the code here. (
The entire code is of more than 1500 lines ). I will mention the parts
which are added. The old code still runs fine.

The classes below are from both perl and C programs and are interfaced
by SWIG.

The modules used by the code:-

use CGI::Carp qw(fatalsToBrowser);
require 5.001;
use POSIX qw(strftime);
use perl_verify;
use getinfo;
use personal;
use preference;
use address;
use signature;
use misc;
use bcase; #### added ####
require "cgi-lib.pl";

The added part:-
#get_briefcase_home_dir(BRIEFCASE_ROOT_DIR, domain_name,
which_dot(domain_name));
$dot = &bcase::which_dot($mydomain);
$bcase_root_dir="";
$bcase_home_dir=&bcase::get_briefcase_home_dir($bcase_root_dir,$mydomain,$dot);


The files are opened and conditions are checked here:- (present even
earlier, but posting for o/p flushing):-

#if ($#mailtothese>=0 || $#mailccthese>=0 || $#mailbccthese>=0 ||
$toallusers) { #send an email, call 'sendmail'
if ($#mailtothese>=0 || $#mailccthese>=0 || $#mailbccthese>=0) { #send
an email, call 'sendmail'
$receipt = "";
if (defined($cgi_data{'rr'})) {
$receipt = "-N failure,delay,success";
}
if (!open(MAIL,"|$mailprog -oem -i -R hdrs -f$from -t $receipt >
/dev/null 2> /dev/null")) {
$sendmailerr = 1;
}
}

$read_receipt = "";
if (defined($cgi_data{'mdn'})) {
$read_receipt = "Disposition-Notification-To: $from\n";
}

if (defined($cgi_data{'sv'}) and $cgi_data{'sv'} eq 'yes') { #save a
copy? must send a different mail
open(MAILME,"|$mailprog -oem -i -R hdrs -f$from $from > /dev/null
2> /dev/null");
}

( other file descriptors are also initialized and operated upon in the
same war ).

select(STDOUT); $| = 1; #autoflush
if (FAXMAIL) { select(FAXMAIL); $| = 1; }
if (MAILALL) { select(MAILALL); $| = 1; }
if (MAIL) { select(MAIL); $| = 1; }
if (MAILME) { select(MAILME); $| = 1; }

Here's how the o/p is sent through sendmail:-

if (MAIL) {
print MAIL <<_TO_END_;
X-Originating-IP: $ENV{'REMOTE_ADDR'}
Reply-To: $fullname <$replytoemail>
From: $fullname <$from>
_TO_END_
}

Some added lines:-

#attach the number of files from Web Folders here
#@afarray;
#$nooffiles;
$nooffiles=0;
$wflist=$cgi_data{'filelist'};
if ($wflist ne ""){
@afarray=split(/,/,$wflist);
$nooffiles=scalar(@afarray);

$len = $ENV{'CONTENT_LENGTH'};
my $attach_file_size=0;
#&CgiError($cgi_lib'maxdata);
#my $fname=$bcase_home_dir . "/" . $f1;
#check for the file size here
for ($ii=0; $ii<$nooffiles; $ii++) {

# $tt=index(@afarray[$ii],"tmp:/");
$tt=index($afarray[$ii],"tmp:/");
if ($tt eq -1){
# $tempstr=$bcase_home_dir . "/" . @afarray[$ii];

$tempstr=$bcase_home_dir . "/" . $afarray[$ii];
}
else{
# $tempstr=@afarray[$ii];

$tempstr=$afarray[$ii];
$tempstr=~ s/://g;
if ($tempstr ne ""){
$tempstr="/". $tempstr;
}
}
$tempstr=~ s/ /\\ /g;
$scratch=0;
$scratch=&bcase::get_file_size($tempstr);
$attach_file_size= $attach_file_size+ $scratch;
}
$attach_file_size=$attach_file_size+$len;
#&CgiError("FIle size so far " . $attach_file_size);
if ($attach_file_size>$cgi_lib'maxdata) {
&CgiDie("Request to receive too much data(more than
$cgi_lib'maxdata bytes): $cgi_lib'maxdata-$attach_file_size bytes\n");
}

for ($ii=0; $ii<$nooffiles; $ii++) {
# $tempstr= @afarray[$ii];

$tempstr= $afarray[$ii];

attach_file_wf($tempstr);
#$tempstr=$tempstr . @afarray[$ii] . ",";
}
}

The o/p to html page begins from here. There is no change to it:-

&misc::functions_before($envlp, $cgi_data{'f'}, $cgi_data{'m'},
$cgi_data{'s'}, "Message Sent");



The function recently added:-
sub attach_file_wf {
my $f1 = $_[0];
my $ctype;
my $fname=$bcase_home_dir . "/" . $f1;
my $tt;
my @mypath = split(/\/|\\/, $f1);
my $upfile1name = pop(@mypath);
my $deletefile="";
#check for the zip file
$tt=index($f1,"tmp:/");
if ($tt ne -1){ $deletefile="delete"; $fname="/tmp/" .
$upfile1name}


#my $upfile1name =$f1;
if ($upfile1name) { }
else { $upfile1name = 'anonymous'; }
#if (open (TMPFILE, "< $cgi_sfn{$f1}")) {}
#$string =~ s/regex/replacement/g;
my $ff=$fname;
$ff=~ s/ /\\ /g;
#$upfile1name=~ s/ /\\ /g;
#&CgiError("fname " . $ff);

if (open (TMPFILE, "< $fname")) {
#if (!defined($cgi_ct{$f1}) or $cgi_ct{$f1} eq "") {
#$cgi_ct{$f1} = "application\/octet-stream";
$ctype = &bcase::findmimetype($ff);
#&CgiError(" ctype " . $ctype);
if ($ctype eq ""){
$ctype = "application\/octet-stream";
}
#}
$cte = "BASE64";
$decodeflag = "-b";
if ($ctype =~ /^text\//i) {
$cte = "quoted-printable"; $decodeflag = "-q";
}
seek(TMPFILE, 0 ,2);
if (tell(TMPFILE) > 0) {
close(TMPFILE);

if (FAXMAIL) {
$upfaxfile1name = $upfile1name;
if (($upfile1name =~ /^iefaxing.sig$/i) or ($upfile1name =~
/\.grp$/i)) {
$upfaxfile1name .= ".ren";
}
print FAXMAIL <<_TO_END_;
--$mkmailid
Content-Type: $ctype; name=$upfaxfile1name
Content-Transfer-Encoding: $cte

_TO_END_
}

if (MAILALL) {
print MAILALL <<_TO_END_;
--$mkmailid
Content-Type: $ctype; name=$upfile1name
Content-Transfer-Encoding: $cte

_TO_END_
}

if (MAIL) {
print MAIL <<_TO_END_;
--$mkmailid
Content-Type: $ctype; name=$upfile1name
Content-Transfer-Encoding: $cte

_TO_END_
}

if (MAILME) {
print MAILME <<_TO_END_;
--$mkmailid
Content-Type: $ctype; name=$upfile1name
Content-Transfer-Encoding: $cte

_TO_END_
}


if (FAXMAIL) { print FAXMAIL `$mimencoder $decodeflag $ff`
or print FAXMAIL "ERROR:
Can't open $mimencoder to encode the attachment : $!\n"; }
if (MAILALL) { print MAILALL `$mimencoder $decodeflag $ff`
or print MAILALL "ERROR:
Can't open $mimencoder to encode the attachment : $!\n"; }
if (MAIL) { print MAIL `$mimencoder $decodeflag $ff` or
print MAIL "ERROR: Can't open $mimencoder to encode the attachment :
$!\n"; }
if (MAILME) { print MAILME `$mimencoder $decodeflag $ff` or
print MAILME "ERROR: Can't open $mimencoder to encode the attachment :
$!\n"; }
#unlink ($cgi_sfn{$f1}) if (-e $cgi_sfn{$f1});
if ($deletefile eq "delete"){
unlink ($fname) if (-e $fname);
}
}
else {
close(TMPFILE);
#unlink ($cgi_sfn{$f1}) if (-e $cgi_sfn{$f1});
if ($deletefile eq "delete"){
unlink ($fname) if (-e $fname);
}
}
}
}


PLEASE HELPPPPPP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top