9

私はこの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基本認証を送信しないことがわかりますが、その理由はわかりません。

このWebサイトでいくつかの投稿を見ましたが、同じ基本コードがあり、機能しません...

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

以前に何か間違ったことをしましたか?

4

1 に答える 1

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!' );
    }

クライアントを1回実行すると、次の2つの要求が発生するはずです。

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
于 2011-11-20T20:20:19.430 に答える