我正在尝试将HTTP :: Proxy用于某些特定域的服务器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] }
,因为文档似乎提示,但它没有用。
非常感谢。
答案 0 :(得分: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,即收到整个响应时,过滤器不会做任何事情。
答案 1 :(得分: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
}),
);