0

I need to change the proxy server in one Perl run (process). But LWP::UserAgent is always remembering the first proxy value.

When I start my program with proxy x.x.x.2, this address is always used as proxy.

What is wrong?

The simple program Example:

getHTTP('http://my.com/', "http://x.x.x.1:3128");
getHTTP('http://my.com/', "http://x.x.x.2:3128");
getHTTP('http://my.com/', "");
getHTTP('http://my.com/', "http://x.x.x.3:3128");

sub getHTTP {
    my ($url, $proxy) = @_;
    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new;
    $ua->proxy(http => $proxy);
    my $req = HTTP::Request->new(GET => $url);
    return $ua->request($req)->as_string;
}

Output from HTTP log:

x.x.x.1 - - [09/Feb/2015:15:28:06 +0100] "GET / HTTP/1.0" 200 3467
x.x.x.1 - - [09/Feb/2015:15:29:07 +0100] "GET / HTTP/1.0" 200 3467
y.y.y.y - - [09/Feb/2015:15:29:08 +0100] "GET / HTTP/1.0" 200 3467
x.x.x.1 - - [09/Feb/2015:15:29:09 +0100] "GET / HTTP/1.0" 200 3467

(y.y.y.y is my no proxy address)

OS: Debian GNU/Linux 7

NEW findings: When I run next perl or shell scripts. The same result as above.

proxy.sh:

curl -x "http://x.x.x.1:3128" http://my.com/
curl -x "http://x.x.x.2:3128" http://my.com/
curl -x "http://x.x.x.3:3128" http://my.com/

Result: always x.x.x.1 proxy

proxy.pl:

`curl -x "http://x.x.x.4:3128" http://my.com/`;
`curl -x "http://x.x.x.5:3128" http://my.com/`;
`curl -x "http://x.x.x.6:3128" http://my.com/`;

Result: always x.x.x.4 proxy

It looks like the proxy settings cannot change in one shell process.

perLHawk
  • 90
  • 7
  • This looks fine. Is it different from the actual code that you are running? – Borodin Feb 09 '15 at 17:04
  • (My original code is much more complicated.) But when I run this simple example, the output is the same. In other words, this simple program causes described behavior. – perLHawk Feb 10 '15 at 07:31
  • Could it be due to proxy configuration? Are the proxies connected somehow to do load balancing or can they select which interface is used? – G. Cito Feb 11 '15 at 00:20
  • I don't think so. Please see on my new findings! – perLHawk Feb 11 '15 at 08:04

1 Answers1

-1

I don't know what's going on here, exactly, and in fact I think you may have found a bug in LWP (if what you're showing us is what you're actually executing). However, is there a reason why you're building the GET request from HTTP::Request instead of directly through the User Agent object? In other words, instead of doing this:

my $req = HTTP::Request->new(GET => $url);
return $ua->request($req)->as_string;

Do this:

my $res = $ua->get($url);
return $res->as_string;

In theory, the two methods should be equivalent, but I wonder if the proxy settings aren't correctly propagating to HTTP::Request when you do it this way.

mwp
  • 8,217
  • 20
  • 26