我正在尝试创建一个透明的HTTP 代理。它的目的是停留在浏览器和 Web 服务器之间并且不可见。这是我正在使用的代码。不幸的是,它运行得不是很好。$base_uri
当我打开网页(由 我正在一个网站上尝试它,它返回包括“ transfer-encoding: chunked
”在内的各种响应(所以我想问题可能就在那里?!)。我认为cookies也可能存在问题,但我不知道如何解决它们(如果有的话......)。
#!/usr/bin/perl
use strict;
use HTTP::Daemon;
use LWP::UserAgent;
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new();
my $ua = LWP::UserAgent->new( max_redirect => 0, env_proxy => 0,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $d = HTTP::Daemon->new(
LocalHost => "localhost", # remove this to listen from other machines
# (i.e. open-relay... be careful of spammers!)
LocalPort => 33331
) || die;
print "[Proxy URL:", $d->url, "]\n";
fork(); fork(); fork(); # 2^3 = 8 processes
$ua->cookie_jar($cookie_jar);
my $base_uri = 'http://example.com/';
while (my $c = $d->accept) {
while (my $request = $c->get_request) {
my $uri = $base_uri . $request->uri->as_string();
my $method = $request->method;
my $req = HTTP::Request->new($method, $uri);
$request->uri($uri);
print "[[ $method >> $uri ]]\n";
my $response = $ua->simple_request($request);
$c->send_response( $response );
}
$c->close;
undef($c);
}
先感谢您!