Subject: | DWIMmy LWP::UserAgent::credentials() |
Browsing the code, we have the credentials() method that uses the given
$netloc directly when populating the hash of basic_authentication:
sub credentials
{
my($self, $netloc, $realm, $uid, $pass) = @_;
@{ $self->{'basic_authentication'}{lc($netloc)}{$realm} } =
($uid, $pass);
}
while get_basic_credentials(), that receives a URI, uses host_port() on
the $uri parameter before accessing the very same hash:
sub get_basic_credentials
{
my($self, $realm, $uri, $proxy) = @_;
return if $proxy;
my $host_port = lc($uri->host_port);
if (exists $self->{'basic_authentication'}{$host_port}{$realm}) {
return @{ $self->{'basic_authentication'}{$host_port}{$realm} };
}
return (undef, undef);
}
This makes using credentials() quite difficult, because one has to know
that the $netloc should contain the port as well (which I didn't find in
the docs). I'd like to share a little patch that would make things a
little more DWIM:
--- /opt/perl/lib/site_perl/5.8.8/LWP/UserAgent.pm 2004-11-12
12:12:04.000000000 +0100
+++ UserAgent.pm 2006-03-03 18:13:24.214743280 +0100
@@ -552,7 +552,8 @@
sub credentials
{
my($self, $netloc, $realm, $uid, $pass) = @_;
- @{ $self->{'basic_authentication'}{lc($netloc)}{$realm} } =
+ $netloc = URI->new($netloc) unless ref $netloc;
+ @{ $self->{'basic_authentication'}{lc($netloc->host_port)}{$realm} } =
($uid, $pass);
}
As you can see, it basically tries to call the host_port() method on the
$netloc parameter, or on its URI incarnation in case $netloc isn't a
reference.
Just my 2c, and my very compliments for this beautiful piece of code,
Flavio.