CGI-Timeout causing "internal server error"????

C

Carl Lafferty

I have posted before and received a LOT of help on a project for my
public library. Right now I am having an issue using that information.

I was able to get my net::telnet application to work. it returns the
information that I want and I am in the process of converting it to a
usable script but I have run into a problem.

When the code executes I have the CGI display a form asking for a title
to search for and give them a submit button. say you enter "Tom Swift".
my script is then called again with that parameter, spends a few seconds
talking to the telnet server I have to talk to and then returns my data.
This works PERFECTLY (well some warnings but I will work those out
later) from the commandline. (I simulate the 'get something from the
script' code to run from cmd line) When I run it from the browser, it
simply gives me "internal server error" and my
/var/log/apache2/error.log says Premature end of script headers.

The code generated from the command line looks like this:
_________________
Content-type: text/html

<html><body bgcolor=blue text=white>Results:<br> <br>
Tom Swift and his aquatom Appleton, Victor J APPLETON
|ocm03632157|1 <br>
Tom Swift and his atomic Appleton, Victor J APPLETON
|ocm02915922|1 <br>
Tom Swift and his deep-se Appleton, Victor J APPLETON |
58014649|1 <br>
Tom Swift and his electro Appleton, Victor J APPLETON
|ocm01088658|1 <br>
Tom Swift and his flying Appleton, Victor J APPLETON |
54000408|1 <br>
Tom Swift and his repelat Appleton, Victor J APPLETON
|CNV02382170|1 <br>
Tom Swift and his space s Appleton, Victor J APPLETON
|ocm01088653|1 <br>
Tom Swift and his ultraso Appleton, Victor J APPLETON |
57004843|1 <br>
Tom Swift in the caves of Appleton, Victor J APPLETON
|ocm02635857|1 <br>
Tom Swift on the phantom Appleton, Victor J APPLETON
|ocm04678341|1 <br>
</body></html>

_____________________________
My question is that since it takes a 2-4 seconds to retrieve the data
(there is a delay as the telnet server accpets the login that is beyond
my control at all), could this be causing the browser to timeout????

Below is my code for my first real try into this as a cgi script.

Any help is appreciated. (



______________________________
#!/usr/bin/perl

use Net::Telnet;
#use warnings;
use CGI;


sub connect
{
#
# setup the connection to the server.
#
$galaxy = new Net::Telnet (Host => "10.10.20.220",
Port => 2001,
Timeout => 10,
Dump_Log => 'dump.log',
output_record_separator => "\x0d",
Input_log => 'input.log',
Option_log => 'option.log',
Output_log => 'output.log',
Prompt => '//',
Telnetmode => '0');


eval {
$ok = $galaxy->waitfor('/option:/');
$ok = $galaxy->print(">>>G");
$ok = $galaxy->waitfor('/option:/');
$ok = $galaxy->print("\x1c\xc0\xc1\xc0\xa7\xa8");
$ok = $galaxy->waitfor('/\x1e\x59/');
$ok = $galaxy->print("0001 AUTO||O|1.11 (800)");
$ok = $galaxy->waitfor('/YN|3/');
$ok = $galaxy->print("5000 5166 30");
$ok = $galaxy->waitfor('/0001 00000 /');
$ok = $galaxy->print("0020 ");
$ok = $galaxy->waitfor('/ile/');
$ok = $galaxy->print("0021 ");
$ok = $galaxy->waitfor('/NNNNNNNNNNN/');
$ok = $galaxy->print("0022 ");
$ok = $galaxy->waitfor('/NNNNNNNNNN/');
$ok = $galaxy->print("5000 5170 00 00 L ");
$ok = $galaxy->waitfor('/0004 000\x8f/');
$ok = $galaxy->print("5000 5163 00 00 01 ");
$ok = $galaxy->waitfor('/0001 00000/');
};
if ( $@ ) {print qq! <h1 align=center>Temporary Error</h1> <h3
align=center>A timeout with the server has occured, this page will be
refreshed in a few seconds</h3> !;}

}

sub logout
{
$galaxy->print("999");
$galaxy->print("0005 GALAXY||20");
$galaxy->print("0010");
$galaxy->close;
# print "</html>";

} #logout








sub displaymenu
{
print qq!
<form action="http://splinter.fclib.org/cgi-bin/newbooks.cgi" method="GET">
Enter title you wish to search for <input type="text" name="lookfor"
width="30"><br>
<input type="submit" value="Click to search">
</form>

!
}


sub displayheader
{

print qq!
Content-type: text/html


<html>
<head><title>Booklookup First Trial</title></head>
<body bgcolor="blue" text="white">
<h1 align="center">Book lookup trial</h1>
!
}



sub getsearchresults
{
&connect();
#put checks here to take into account different search types later.

$galaxy->print("5000 5002 30 0 $lookfor");
#get the first thing sent back which will look like " xxx xxxx xxx"
hence the
#funny looking split below to take care of the leading space.. all I am
#interested in is the $howmany variable
($temp) = $galaxy->waitfor("/\x8f/");
($junk, $howmany, $junk, $junk) = split (/ /,$temp);

#Initailly I am only going to worry about the first 10 hits since
#most of the hits you look for will be in the fist 10 but I am going to
include the
#loop that I need to do more than that incase I get ... loopy.
#gonna use parallel arrays initally here.. may use hashes later on
#
$main = 1;
@titles = ();
@cards = ();
@available = ();
while ($main < 2) {
$count = 1;
while ($count <= $howmany-1) {
($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");
$titles[$count] = $info . "|" . $controlnum . "|" . $hilite;
$count = $count + 1;
} #while $count
#clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
$main = $main + 1;
}



} #getsearchresults



sub displaysearchresults
{
print "Content-type: text/html\n\n";
print qq!<html><body bgcolor=blue text=white>!;

print qq!Results:<br>!;

$line = "";
foreach $line (@titles) { print "$line <br>\n"; }


} #displaysearchresults


##################main


$formdata = new CGI();
#print "Content-type: text/html\n\n";

#since I am only doing TITLE right now I am not going to
#worry about the search type for NOW
#
$lookfor = $formdata->param("lookfor");

$lookfor = "Tom Swift";
#$lookfor = <STDIN>;

if ($lookfor eq "") {
print "Content-type: text/html\n\n";
&displaymenu();
} else {
&getsearchresults();
&displaysearchresults();
print qq!</body></html>!;
&logout;
};
 
U

usenet

Carl said:
script' code to run from cmd line) When I run it from the browser, it
simply gives me "internal server error" and my

Put this at the top of your program:
CGI::Carp qw {carpout fatalsToBrowser warningsToBrowser };

If your script throws warnings in a shell then it can confuse your
webserver (because the warnings aren't in the form of a properly
formatted HTML page).

FWIW, any time a program works from a shell but not from a browser, the
first thing I look at is whether the userid which the webserver runs
under has access to the files, etc. A quick check is to 'su' to your
webserver userid (if you are able) and run the script from a shell
under that userid.
 
B

Ben Morrow

Quoth Carl Lafferty said:
I have posted before and received a LOT of help on a project for my
public library. Right now I am having an issue using that information.

I was able to get my net::telnet application to work. it returns the
information that I want and I am in the process of converting it to a
usable script but I have run into a problem.

When the code executes I have the CGI display a form asking for a title
to search for and give them a submit button. say you enter "Tom Swift".
my script is then called again with that parameter, spends a few seconds
talking to the telnet server I have to talk to and then returns my data.
This works PERFECTLY (well some warnings but I will work those out
later) from the commandline. (I simulate the 'get something from the
script' code to run from cmd line)

If you use CGI.pm (which you do) you can run the script from the
command-line and CGI.pm will deal with faking the CGI environment for
you. Then you can be *sure* you haven't messed anything else up while
converting. See CGI's docs.

Ben
 
C

Carl Lafferty

Put this at the top of your program:
CGI::Carp qw {carpout fatalsToBrowser warningsToBrowser };
I tried this and I get some weird messages back from the web server.
this one in particular
newbooks.cgi: Can't locate object method "new" via package "CGI" at
../newbooks.cgi line 153.

the line it refers to is

$formdata = new CGI;
FWIW, any time a program works from a shell but not from a browser, the
first thing I look at is whether the userid which the webserver runs
under has access to the files, etc. A quick check is to 'su' to your
webserver userid (if you are able) and run the script from a shell
under that userid.
Now that is a good question. while I am not accessing any files I AM
accessing a telnet server.

I'll attempt it as a web user from the cmd line.
 
P

Paul Lalli

Carl said:
I tried this and I get some weird messages back from the web server.
this one in particular
newbooks.cgi: Can't locate object method "new" via package "CGI" at
./newbooks.cgi line 153.

David goofed. It should have been:
use CGI::Carp qw/carpout fatalsToBrowswer warningsToBrowser/;
^^^^

Note the "use".

I don't know whether or not that's the *only* problem in your script,
but it's certainly one of them.
the line it refers to is

$formdata = new CGI;

Note also that this line is not a replacement for the
use CGI;
line. It is an addition.

Paul Lalli
 
U

usenet

Carl said:
newbooks.cgi: Can't locate object method "new" via package "CGI" at
./newbooks.cgi line 153.

Great - something you can Google!

Are you sure you have something like this in your code:

use CGI;
 
D

Dave Weaver

Not explicity related to your problem, but some things you should
consider...

#!/usr/bin/perl

use Net::Telnet;
#use warnings;

Don't comment this line out - with it perl will give you warnings when
things are wrong. If you don't like the warnings, fix the relevant
line of code, don't disable the warnings. If your smoke alarm started
beeping would you look for the smoke, or unplug the alarm?

Also, add "use strict;" and then declare all your variables using "my"
at the appropriate point. This will prevent you making spelling errors
in variable names, and force you to think about the scope of your
variables, increasing the robustness of your code.

See "Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html

....
sub getsearchresults
{
&connect();

The "&" on the subroutine call has a special meaning - if you do not
know the meaning or require its affects, you should leave off the "&".

connect();

....
$main = 1;
@titles = ();
@cards = ();
@available = ();
while ($main < 2) {

If you only want to loop X amount of times, better to use a for()
looop. For example, to loop six times:
for my $main ( 1 .. 6 ) {

And if you're not even using $main for anything (other than the loop
count) you could use:
for ( 1 .. 6 ) {

$count = 1;
while ($count <= $howmany-1) {

Again, you're simply looping for a set amount of iterations:

for my $count ( 1 .. $howmany-1 ) {
($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");
$titles[$count] = $info . "|" . $controlnum . "|" . $hilite;

To add an item to an array, just use 'push' - then there's no need to
keep a track of the end index;

push @titles, $info . "|" . $controlnum . "|" . $hilite;
$count = $count + 1;

This is no longer needed if you use the for() loop.
} #while $count
#clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
$main = $main + 1;

No longer needed if you use the for() loop.

So your code becomes:

my @titles;
for ( 1 .. 2 ) { # this is one more iteration than you had
# included here for illustrative purposes

for my $count ( 1 .. $howmany-1 ) {

# Notice that aligning the "=" on the lines below
# makes the code more readable.

($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");

push @titles, $info . "|" . $controlnum . "|" . $hilite;
}
# Clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
}

....
sub displaysearchresults
{ ....
$line = "";
foreach $line (@titles) { print "$line <br>\n"; }

Better written as:
foreach my $line (@titles) {
print "$line <br>\n";
}

Or even better:
print "$_ <br>\n" for @titles;
 
C

Charles DeRykus

Paul said:
David goofed. It should have been:
use CGI::Carp qw/carpout fatalsToBrowswer warningsToBrowser/;
^^^^

Often overlooked, the warningsToBrowser call must be invoked too or
the buffer won't be flushed, eg,

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print header();
warningsToBrowser(1);
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top