0

我正在尝试对某些特定域使用 HTTP::Proxy to server 403 错误。我已经设法修改了标题,但代理继续为原始页面提供服务。

这是我正在使用的代码:

package Filters::Filter403;

use strict;
use warnings;
use HTTP::Proxy::HeaderFilter::simple;
use HTTP::Proxy::BodyFilter::simple;

our $header = HTTP::Proxy::HeaderFilter::simple->new (
    sub {
        $_[2]->code( 403 );
        $_[2]->message ( 'Forbidden' );
    }
);

our $body = HTTP::Proxy::BodyFilter::simple->new (
    sub {       
        $_[1] = \<<'HTML';
<!DOCTYPE html>
<html><head><title>403 Forbidden</title><style type="text/css">
body { padding: 40pt; }
body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; }
div { width: 200px; background: #eee; padding: 2em; }
</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html>
HTML
    }
);

1;

这是我调用这两个过滤器的主脚本中的代码,因此您可以更好地查看:

use HTTP::Proxy qw(:log);
use Getopt::Long;

use FindBin qw($Bin);
use lib $Bin;

use Filters;
use HTTP::Proxy::BodyFilter::complete;

my $port = 3128;
my $fail_at;
my $outputfile = '/var/log/cvmfs-test/webproxy.output';
my $errorfile = '/var/log/cvmfs-test/webproxy.error';

my $ret = GetOptions ( "port=i" => \$port,
                       "fail=s" => \$fail_at );

my @fail_at = split(/,/, $fail_at);

# Opening file for log
open (LOG, '>>', $outputfile);

my $proxy = HTTP::Proxy->new;
$proxy->port( $port );
$proxy->logfh( *LOG );
$proxy->logmask( ALL );

if ($fail_at[0] ne 'all') {
    foreach my $url (@fail_at) {
        $proxy->push_filter(
            host => $url,
            response => HTTP::Proxy::BodyFilter::complete->new,
            response => $Filters::Filter403::header,
            response => $Filters::Filter403::body
        );
    }
}
else {
    $proxy->push_filter (
        response => HTTP::Proxy::BodyFilter::complete->new(),
        response => $Filters::Filter403::header,
        response => $Filters::Filter403::body
    );
}

my $pid = fork();

# Command for the forked process
if ( defined($pid) and $pid == 0 ) {
    open (my $errfh, '>', $errorfile);
    STDOUT->fdopen( \*$errfh, 'w' ) || die "Couldn't set STDERR to $errorfile: $!\n";
    $proxy->start;
}

# Command for the main script
unless ($pid == 0) {
    print "Proxy HTTP started on port $port with PID $pid.\n";
    print "You can read its output in $outputfile.\n";
}

exit 0;

你能帮我么?我尝试了 Daxim 解决方案,我也尝试使用$ { $_[1] }文档似乎建议,但它不起作用。

非常感谢你。

4

2 回答 2

1

我找到了解决方案。问题是之后的每个过滤器HTTP::Proxy::BodyFilter::complete->new都是用空数据执行的。我发现加载一个沉重的页面,我的代码被添加了多次。

这是工作过滤器:

our $body = HTTP::Proxy::BodyFilter::simple->new (
    sub {
        my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
        unless (defined ($buffer)){
            my $html = 
                    '<!DOCTYPE html>'.
                    '<html><head><title>403 Forbidden</title><style type="text/css">'.
                    'body { padding: 40pt; }'.
                    'body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; }'.
                    'div { width: 200px; background: #eee; padding: 2em; }'.
                    '</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html>';

            $$dataref = $html;
        }
    }
);

这样,过滤器不会做任何事情,除非 $buffer 是 undef,即当接收到整个响应时。

于 2012-06-05T09:17:53.340 回答
0

请参阅HTTP::Proxy::BodyFilter::complete

$proxy->push_filter(
    response => HTTP::Proxy::BodyFilter::complete->new,
    response => HTTP::Proxy::BodyFilter::simple->new(sub {
        $_[1] = \<<'HTML';
<!DOCTYPE html>
<html><head><title>403 Forbidden</title><style type="text/css">
body { padding: 40pt; }
body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; }
div { width: 200px; background: #eee; padding: 2em; }
</style></head><body><div><h1>403</h1><h2>Forbidden</h2></div></body></html>
HTML
    }),
);
于 2012-06-04T20:19:03.110 回答