perl - HTTP::Proxy: how to replace a whole html page -
i'm trying use http::proxy server 403 error specific domain. managed modify headers, proxy continue serve original page.
here code i'm using:
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; here code main script i'm calling 2 filters, can have better look:
use http::proxy qw(:log); use getopt::long; use findbin qw($bin); use lib $bin; use filters; use http::proxy::bodyfilter::complete; $port = 3128; $fail_at; $outputfile = '/var/log/cvmfs-test/webproxy.output'; $errorfile = '/var/log/cvmfs-test/webproxy.error'; $ret = getoptions ( "port=i" => \$port, "fail=s" => \$fail_at ); @fail_at = split(/,/, $fail_at); # opening file log open (log, '>>', $outputfile); $proxy = http::proxy->new; $proxy->port( $port ); $proxy->logfh( *log ); $proxy->logmask( ); if ($fail_at[0] ne 'all') { foreach $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 ); } $pid = fork(); # command forked process if ( defined($pid) , $pid == 0 ) { open (my $errfh, '>', $errorfile); stdout->fdopen( \*$errfh, 'w' ) || die "couldn't set stderr $errorfile: $!\n"; $proxy->start; } # command main script unless ($pid == 0) { print "proxy http started on port $port pid $pid.\n"; print "you can read output in $outputfile.\n"; } exit 0; can please me? tried daxim solution , tried use $ { $_[1] } documentation seems suggest, didn't works.
thank much.
i have found solution. problem every filter after http::proxy::bodyfilter::complete->new executed empty data. found loading heavy page, code added multiple times.
here working filter:
our $body = http::proxy::bodyfilter::simple->new ( sub { ( $self, $dataref, $message, $protocol, $buffer ) = @_; unless (defined ($buffer)){ $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; } } ); this way, filter doesn't unless $buffer undef, i.e. when whole response received.
Comments
Post a Comment