<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package LWP::Authen::Basic;

use strict;

our $VERSION = '6.39';

require MIME::Base64;

sub auth_header {
    my($class, $user, $pass) = @_;
    return "Basic " . MIME::Base64::encode("$user:$pass", "");
}

sub authenticate
{
    my($class, $ua, $proxy, $auth_param, $response,
       $request, $arg, $size) = @_;

    my $realm = $auth_param-&gt;{realm} || "";
    my $url = $proxy ? $request-&gt;{proxy} : $request-&gt;uri_canonical;
    return $response unless $url;
    my $host_port = $url-&gt;host_port;
    my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization";

    my @m = $proxy ? (m_proxy =&gt; $url) : (m_host_port =&gt; $host_port);
    push(@m, realm =&gt; $realm);

    my $h = $ua-&gt;get_my_handler("request_prepare", @m, sub {
        $_[0]{callback} = sub {
            my($req, $ua, $h) = @_;
            my($user, $pass) = $ua-&gt;credentials($host_port, $h-&gt;{realm});
	    if (defined $user) {
		my $auth_value = $class-&gt;auth_header($user, $pass, $req, $ua, $h);
		$req-&gt;header($auth_header =&gt; $auth_value);
	    }
        };
    });
    $h-&gt;{auth_param} = $auth_param;

    if (!$proxy &amp;&amp; !$request-&gt;header($auth_header) &amp;&amp; $ua-&gt;credentials($host_port, $realm)) {
	# we can make sure this handler applies and retry
        add_path($h, $url-&gt;path);
        return $ua-&gt;request($request-&gt;clone, $arg, $size, $response);
    }

    my($user, $pass) = $ua-&gt;get_basic_credentials($realm, $url, $proxy);
    unless (defined $user and defined $pass) {
	$ua-&gt;set_my_handler("request_prepare", undef, @m);  # delete handler
	return $response;
    }

    # check that the password has changed
    my ($olduser, $oldpass) = $ua-&gt;credentials($host_port, $realm);
    return $response if (defined $olduser and defined $oldpass and
                         $user eq $olduser and $pass eq $oldpass);

    $ua-&gt;credentials($host_port, $realm, $user, $pass);
    add_path($h, $url-&gt;path) unless $proxy;
    return $ua-&gt;request($request-&gt;clone, $arg, $size, $response);
}

sub add_path {
    my($h, $path) = @_;
    $path =~ s,[^/]+\z,,;
    push(@{$h-&gt;{m_path_prefix}}, $path);
}

1;
</pre></body></html>