Commit 587e4edcfc5927490b5b8562be0f3bdb6a6215c1

Remove handler when we don't have a username/password for the realm.

Also prepare for subclassing.
  
33
44require MIME::Base64;
55
6sub auth_header {
7 my($class, $user, $pass) = @_;
8 return "Basic " . MIME::Base64::encode("$user:$pass", "");
9}
10
611sub authenticate
712{
813 my($class, $ua, $proxy, $auth_param, $response,
1919 my $host_port = $url->host_port;
2020 my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization";
2121
22 my @m = (m_host_port => $host_port, realm => $realm);
23 if ($proxy) {
24 @m = (m_proxy => $url);
25 }
22 my @m = $proxy ? (m_proxy => $url) : (m_host_port => $host_port);
23 push(@m, realm => $realm);
2624
2725 my $h = $ua->get_my_handler("request_prepare", @m, sub {
2826 $_[0]{callback} = sub {
29 my($req, $ua) = @_;
30 my($user, $pass) = $ua->credentials($host_port, $realm);
31 my $auth_value = "Basic " . MIME::Base64::encode("$user:$pass", "");
32 $req->header($auth_header => $auth_value);
33 }
27 my($req, $ua, $h) = @_;
28 my($user, $pass) = $ua->credentials($host_port, $h->{realm});
29 if (defined $user) {
30 my $auth_value = $class->auth_header($user, $pass, $h);
31 $req->header($auth_header => $auth_value);
32 }
33 };
3434 });
3535
36 if (!$request->header($auth_header)) {
37 if ($ua->credentials($host_port, $realm)) {
38 add_path($h, $url->path) unless $proxy;
39 return $ua->request($request->clone, $arg, $size, $response);
40 }
36 if (!$proxy && !$request->header($auth_header) && $ua->credentials($host_port, $realm)) {
37 # we can make sure this handler applies and retry
38 add_path($h, $url->path);
39 return $ua->request($request->clone, $arg, $size, $response);
4140 }
4241
4342 my($user, $pass) = $ua->get_basic_credentials($realm, $url, $proxy);
44 return $response unless defined $user and defined $pass;
43 unless (defined $user and defined $pass) {
44 $ua->set_my_handler("request_prepare", undef, @m); # delete handler
45 return $response;
46 }
4547
4648 # XXXX check for repeated fail
4749