LWP :: UserAgent HTTP基本身份验证

时间:2011-11-20 19:14:47

标签: perl lwp lwp-useragent

我试图运行这个perl5程序:

 #!/usr/bin/env perl                                                             

use strict;                                                                     
use warnings;                                                                   
use LWP;                                                                        

my $ua = LWP::UserAgent->new('Mozilla');                                        
$ua->credentials("test.server.com:39272", "realm-name", 'user_name', 'some_pass');                       
my $res = $ua->get('http://test.server.com:39272/');                  

print $res->content;

另一方面,我有HTTP ::守护进程:

#!/usr/bin/env perl                                                                                       

use strict;                                                                     
use warnings;                                                                   

use HTTP::Daemon;                                                               

my $hd = HTTP::Daemon->new or die;                                              

print "Contact URL: ", $hd->url, "\n";                                          
while (my $hc = $hd->accept) {                                                  
  while (my $hr = $hc->get_request) {                                           
    if ($hr->method eq 'GET') {                                                 
      print $hr->as_string, "\n";                                               
    }                                                                           
  }                                                                             
  $hc->close;                                                                   
  undef($hc);                                                                   
}    

它只是打印:

Contact URL: http://test.server.com:39272/
GET / HTTP/1.1
Connection: TE, close
Host: test.server.com:39272
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.03

所以我看到LWP :: UserAgent不发送HTTP Basic auth,但我不知道为什么。

我在这个网站上看过一些帖子,但他们有相同的基本代码,但事实并非如此 工作...

如果我使用HTTP :: Request,那么它可以工作:

my $req = GET 'http://test.server.com:39272/';                        
$req->authorization_basic('my_id', 'my_pass');                                  
my $res = $ua->request($req);

输出:

GET / HTTP/1.1
Connection: TE, close
Authorization: Basic bXlfaWQ6bXlfcGFzcw==
Host: test.server.com:39272
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.03

我以前做错了吗?

1 个答案:

答案 0 :(得分:19)

如果服务器告诉它它正在尝试访问该领域,LWP将仅发送领域的凭据。特定用户可能只能访问特定领域或具有不同领域的不同密码。 LWP不知道哪一个在没有领域的情况下从其凭证中挑选出来。此外,LWP不会使用您在凭证中存储的数据,除非它受到质疑。你不是那样做的。

如果通过指定Authorization标头直接提供凭据,则不进行领域检查。如果你自己明确地设置它,你总是可以发送你喜欢的任何标题,所以你看到它并不奇怪。

您只需要一个更好的测试服务器:

use strict;                                                                     
use warnings;                                                                   

use HTTP::Daemon;                                                               
use HTTP::Status;

my $server = HTTP::Daemon->new or die;                                              

print "Contact URL: ", $server->url, "\n";                                          
while (my $connection = $server->accept) {                                                  
    while (my $request = $connection->get_request) {                                           
        print $request->as_string;
        unless( $request->header( 'Authorization' ) ) {                                                 
            $connection->send_response( make_challenge() )                                               
            }
        else {
            $connection->send_response( make_response() )                                               
            }   
        }                                                                             
    $connection->close;                                                                   
    }  

sub make_challenge {
    my $response = HTTP::Response->new( 
        401 => 'Authorization Required',
        [ 'WWW-Authenticate' => 'Basic realm="Buster"' ],
         );
    }

sub make_response {
    my $response = HTTP::Response->new( 
        200 => 'Huzzah!',
        [ 'Content-type' => 'text/plain' ],
         );

    $response->message( 'Huzzah!' );
    }

当您运行一次客户端时,应该有两个请求:

GET / HTTP/1.1
Connection: TE, close
Host: macpro.local:52902
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.02

GET / HTTP/1.1
Connection: TE, close
Authorization: Basic dXNlcl9uYW1lOnNvbWVfcGFzcw==
Host: macpro.local:52902
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.02
相关问题