HTTP::Proxyの使い方
This module implements a HTTP proxy, using a HTTP::Daemon to accept client connections, and a LWP::UserAgent to ask for the requested pages.
The most interesting feature of this proxy object is its hability to filter the HTTP requests and responses through user-defined filters.
なPerlモジュールです。
んで、このfilterなのですが、リモートからの返答が「Transfer-Encoding:chunked」で帰ってきた場合(基本はこちらみたい)、フィルターにもコンテンツがバラバラで渡されてしまい、困る場合があります。
そのときは、HTTP::Proxy::BodyFilter::linesを使います。
use HTTP::Proxy;
use HTTP::Proxy::BodyFilter::lines;
$proxy->push_filter(
response =>HTTP::Proxy::BodyFilter::lines->new("EOF"),
response=>MyFilter->new,
mime=>"text/html"
);
$proxy->start;
package MyFilter;
use base qw(HTTP::Proxy::BodyFilter);
sub filter{
my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
unless($buffer){
my $html=$$dataref;#これでHTMLをget
$html =~ s/perl/PERL/gi;
$$dataref=$html;
}
}
1;
一応きちんと動いた。
これで、きちんとHTMLを読み込んでからごにょごにょと処理できます。