Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

perl script output gets downloaded instead of displayed in browser

Hi,

This is an old apache server 1.3.31

When running a perl test script, the output is generated, but the browser asks me to download the file with the output instead of displaying it in the browser.

This is from the configuration:

<VirtualHost 62.xx.xxx.xxx:80>
ServerName xxx
ServerAlias xxx
ServerAdmin webmaster@xxxx
DocumentRoot /www/xxx/doc
CustomLog /www/xxx/log/access_log combined
ErrorLog /www/xxx/log/error_log
ScriptAlias /cgi-local/ /www/xxx/cgi/

AddHandler cgi-script .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>

<Location /cgi-local>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>

</VirtualHost>

Also I have this in the main configuration part:

    AddHandler cgi-script .cgi
    AddHandler cgi-script .pl

What is missing?

Thanks!
0
netslt
Asked:
netslt
  • 6
  • 6
  • 4
  • +3
2 Solutions
 
sentnerCommented:
This likely means the perl script is not generating the correct mime type header line, so the browser doesn't know what to do with the result.  The first thing output by the script should be:  print "Content-type: text/html\r\n\r\n";


0
 
netsltAuthor Commented:
The Line

print "Content-type: text/text\n\n";

Was in the script, I changed it to your version but no change - still offers to download the output.

This is the full script:

#!/usr/bin/perl5.8.0

print "Content-type: text/html\r\n\r\n";

print "test\n";


So what else could it be?

Thanks again
0
 
crazedsanityCommented:
Is this the only perl script on this server exhibiting the behavior?  Or do all perl scripts behave this way?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
sentnerCommented:
Are you certain that the file that your browser offers to download is the output, and not the script itself?  
0
 
sentnerCommented:
Also, have you checked the error and access logs to see if it's reporting any problems?
0
 
crazedsanityCommented:
Good point.  If you download the source of the perl script, then the server isn't configured properly.  If it is downloading the output, then it is something in the script.  
0
 
netsltAuthor Commented:
Hm. The File downloaded contains
---
Content-type: text/html

test
---
So it seems the perl script definitely gets executed but there is a problem with the Content-type-line????

Again, the full script is
---
#!/usr/bin/perl5.8.0
print "Content-type: text/html\r\n\r\n";
print "test\n";
---

Thanks
0
 
netsltAuthor Commented:
I checked the logfiles: no error

80.218.xx.xxx - - [26/Oct/2011:23:01:20 +0200] "GET /cgi-local/test.pl HTTP/1.1" 200 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
0
 
tel2Commented:
Hi netslt,

I tried your script and it worked fine in my environment, i.e. the browser just displayed "test".
Does anything different happen if you change the "\r\n\r\n" to "\n\n"?  I usually use the latter, but the former seems to also work (in my environment - dunno about yours, which is why I'm asking (can you tell I'm grasping at straws?)).
0
 
sentnerCommented:
Have you tried with a different browser or from a different computer?  Like tel2, I'm grasping at straws here, since if your web server is properly executing the file and sending the output with the right content type, it should be being picked up by the browser as html and displayed...
0
 
crazedsanityCommented:
Printing the content-type isn't what you're looking for, as that shouldn't be printed in the final output.  Instead, create that as an HTTP header...
0
 
tel2Commented:
Hi crazedsanity (love the name!),

Are you saying that this code is wrong:
    #!/usr/bin/perl5.8.0
    print "Content-type: text/html\r\n\r\n";
    print "test\n";
?

That is the kind of code I usually use to send output ("test" in this case) to a webpage with Perl.

Or do you mean something else?
0
 
crazedsanityCommented:
I'm not certain how to do it in perl.  In PHP, it looks like this:
<?php
header("Content-type: text/html");
echo "test\n";

Open in new window


When this code was run, it would print "test" to the screen.  Just printing the words "Content-type: text/html" is no different that printing "test".
0
 
sentnerCommented:
crazedsanity,

The content-type header is what tells the browser what type of data is following.  That is a standard for CGI scripts.  The actual data below it can be of course put into actual HTML including a header and such, but the content-type comes in the response header, which is the first thing printed.

Or perhaps I'm misunderstanding what you're saying?
0
 
sentnerCommented:
Ah, that php code is equivalent to printing the "Content-type: text/html" followed by a blank line in perl.  

http://oreilly.com/openbook/cgi/ch03_02.html
0
 
FishMongerCommented:
Try this:
#!/usr/bin/perl5.8.0

use CGI qw(:standard);

print header(), start_html(), h1('Hello World'), end_html();

Open in new window

0
 
FishMongerCommented:
Let's add one more line for good measure.

#!/usr/bin/perl5.8.0

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header(), start_html(), h1('Hello World'), end_html();

Open in new window

0
 
kyanwanCommented:
I'm the kind of person who always checks if the network is plugged in ... very last of all, so - since no-one else has suggested it ....

Have you set your file's permissions to executable?

chmod 755 yourfile.pl

Believe me, it's led to embarrassment for me before ... the "oh I'd never make a simple mistake" mindset ... 4 hours of debugging - all the way down to network hacking - all because of a typo.  ( Yeah - 1 out of place character.   I've got the guts to admit it, though. )
0
 
netsltAuthor Commented:
@kyanwan

Yes, it is 755. As I already wrote: the script gets executed, but the output gets downloaded instead of displayed. i f it would not have execute rights it would not get executed & create no output.
0
 
netsltAuthor Commented:
@FishMonger: Your solution works - the output is displayed in the browser correctly.

Any idea why it does not work with the standard version of the script?

Thanks!
0
 
FishMongerCommented:
Since the Content-type header string was being displayed in the rendered page, I would conclude that something in the web server setup is causing it to also output the header.  Check the html source code of of the page and see if it starts out with this <!DOCTYPE html PUBLIC "-//W3C, which is the beginning portion of the htnl header, and then following that is your Content-type header string.

Using the CGI module as I showed will only output the header if it wasn't already outputted.  This is one of the reasons I prefer to use the module.
0
 
netsltAuthor Commented:
Thanks!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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