Commit 587e4edcfc5927490b5b8562be0f3bdb6a6215c1
- Diff rendering mode:
- inline
- side by side
lib/LWP/Authen/Basic.pm
(22 / 15)
|   | |||
| 3 | 3 | ||
| 4 | 4 | require MIME::Base64; | |
| 5 | 5 | ||
| 6 | sub auth_header { | ||
| 7 | my($class, $user, $pass) = @_; | ||
| 8 | return "Basic " . MIME::Base64::encode("$user:$pass", ""); | ||
| 9 | } | ||
| 10 | |||
| 6 | 11 | sub authenticate | |
| 7 | 12 | { | |
| 8 | 13 | my($class, $ua, $proxy, $auth_param, $response, | |
| … | … | ||
| 19 | 19 | my $host_port = $url->host_port; | |
| 20 | 20 | my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization"; | |
| 21 | 21 | ||
| 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); | ||
| 26 | 24 | ||
| 27 | 25 | my $h = $ua->get_my_handler("request_prepare", @m, sub { | |
| 28 | 26 | $_[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 | }; | ||
| 34 | 34 | }); | |
| 35 | 35 | ||
| 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); | ||
| 41 | 40 | } | |
| 42 | 41 | ||
| 43 | 42 | 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 | } | ||
| 45 | 47 | ||
| 46 | 48 | # XXXX check for repeated fail | |
| 47 | 49 |

