views:

201

answers:

3

Hello, I have CGI proxy that works on my localhost, but when I try to get it work on another server I get Premature end of script headers. I have included the source below. I also tried print header instead of the text/xml and it worked localhost but it failed on the server.

use strict;
#use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use HTTP::Request::Common qw(POST);
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::Escape; 
use Data::Dumper;

my $url = param('url');

sub writeXML($) {
    my $response = shift @_;
    if ($response->is_success) {
        print CGI->header('text/xml');
        print $response->content;
        print STDERR "content response:#" . $response->content . "#\n";
    }
    else {
        print STDERR "Status Code: " . $response->status_line . "\n"; 
        print STDERR Dumper ($response);
    }
}

sub makeRequest(){
    if ($url){
        my $ua = LWP::UserAgent->new;
        my $response = $ua->request(GET $url);
        if ($response){
            writeXML($response);
        }
        else{
            print STDERR "No response exists";
        }
    }
    else{
        print STDERR "URL must be specified";
    }
}

makeRequest();

0;

__END__
+1  A: 

The script "works" when I try it from the command line:

$ t.pl url=http://www.unur.com/

gives me the home page of my web site.

That means, the host on which you are trying this is missing some libraries. To figure out which ones, you should examine the server's error log, or try running your script from the shell as shown above.

See DEBUGGING.

PS: There is absolutely no good reason for those prototypes on makeRequest and writeXML. Plus, try warn sprintf "Status: %s\n", $response->status_line; instead of those unsightly print STDERR lines.

Sinan Ünür
Thanks! I know nothing about perl... ha, and most of this script was grabbed from an older script that I found on our servers. It did look a little funky as I was editing it.
Parris
+1  A: 

See my Troubleshooting Perl CGI scripts guide for all the steps you can go through to find the problem.

brian d foy
A: 

You only output a header if the program succeeds, all your error conditions are going to cause the premature end of script headers.

Put a 'print CGI->header();' and a suitable error message to STDOUT at all the points where you're output an error message to STDERR, and you'll be to see what's going wrong.

gorilla