开发者

Why do I get "Premature end of script headers" from my CGI proxy?

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 ($respons开发者_如何学Pythone){
            writeXML($response);
        }
        else{
            print STDERR "No response exists";
        }
    }
    else{
        print STDERR "URL must be specified";
    }
}

makeRequest();

0;

__END__


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜