On Jul 13, Gisle Aas said:
>Either that or we could attach it to the request object. Attaching it
>to the request give more flexibility and it could potentially be
>defaulted from the $ua->default_header settings.
Ah. I looked more closely at the code in LWP::UserAgent when it creates
the $protocol object, and found that I can just store the information in
the user agent and have it passed onto the Protocol class. See my code
below.
>> And I haven't found a way to create my own LWP::Protocol::http subclass
>> that is used instead of the original one. That's why I had to subclass
>> LWP::UserAgent and LWP::Protocol as well.
>
>You should be able to override protocol handlers with:
>
> LWP::Protocol::implementor("http", "MyClass")
Thanks.
Here's my code, interspersed with explanations:
package HTTP::ProxyPlex;
use base 'HTTP::Proxy';
sub from {
my $self = shift;
my $old = $self->{from};
$self->{from} = shift if @_;
return $old;
}
sub _init_agent {
my $self = shift;
# I've decided not to subclass LWP::UserAgent, for the time being
# and instead just create satellite data in it -- it works for me
# the 'outgoing' field is the array reference holding addresses
# to come from when creating the outbound connection.
my $agent = LWP::UserAgent->new(
env_proxy => 1,
keep_alive => 2,
parse_head => 0,
timeout => $self->timeout,
) or die "Cannot initialize proxy agent: $!";
$agent->{outgoing} = $self->from;
$self->agent($agent);
return $agent;
}
package LWP::ProtoPlex::http;
use base 'LWP::Protocol::http';
use LWP::Protocol;
LWP::Protocol::implementor(http => 'LWP::ProtoPlex::http');
sub _extra_sock_opts {
my $self = shift;
# We get the LWP::UserAgent object from the LWP::ProtoPlex::http
# object, and if it's got the array reference in it, we pull one
# randomly from it; otherwise, return nothing.
if (my $out = $self->{ua}->{outgoing}) {
$self->{outgoing} = $out->[rand @$out];
return ( LocalAddr => $self->{outgoing} );
}
return;
}
package LWP::ProtoPlex::http::Socket;
use base 'LWP::Protocol::http::Socket';
1;
So I apologize. In the end, the LWP suite isn't too hard to flex around
this problem. The only troublesome part now will be looking at the
LWP::Protocol::MyFTP class to see what I need to do to get FTP working in
the same manner.
As for not subclassing LWP::UserAgent, I don't think I would have gained
anything from it, except perhaps allowing 'outgoing' to be set via new(),
or creating some other method. Actually... since this isn't really a
proxy issue, I might create a subclass and distribute it to CPAN. It
would probably behave exactly like I described at the beginning of this
paragraph.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart