libwww-perl-6.04 > LWP::UserAgent
libwww-perl-6.04

名前

LWP::UserAgent - Web user agent class

LWP::UserAgent - Web ユーザエージェントクラス

概要

 require LWP::UserAgent;
 
 my $ua = LWP::UserAgent->new;
 $ua->timeout(10);
 $ua->env_proxy;
 
 my $response = $ua->get('http://search.cpan.org/');
 
 if ($response->is_success) {
     print $response->decoded_content;  # or whatever
 }
 else {
     die $response->status_line;
 }

説明

The LWP::UserAgent is a class implementing a web user agent. LWP::UserAgent objects can be used to dispatch web requests.

LWP::UserAgent は web ユーザエージェントを実装するクラスです。 LWP::UserAgent オブジェクトは web リクエストを発行(dispatch)するために 使えます。

In normal use the application creates an LWP::UserAgent object, and then configures it with values for timeouts, proxies, name, etc. It then creates an instance of HTTP::Request for the request that needs to be performed. This request is then passed to one of the request method the UserAgent, which dispatches it using the relevant protocol, and returns a HTTP::Response object. There are convenience methods for sending the most common request types: get(), head(), post(), put() and delete(). When using these methods then the creation of the request object is hidden as shown in the synopsis above.

通常の使用では、アプリケーションは LWP::UserAgent クラスを作成し、 タイムアウト、プロキシ、名前などのための値でそれを設定します。 それから実行される必要があるリクエストのために HTTP::Request の インスタンスを作成します。 そしてこのリクエストは UserAgent のリクエストメソッドの一つに渡されます; これは対応するプロトコルを使ってそれを発行し、 HTTP::Response オブジェクトを返します。 もっとも一般的なリクエスト型のための便利メソッド get(), head(), post(), put(), delete() があります。 これらのメソッドを使うときは、上述の用例の通り、 リクエストオブジェクトの作成は隠蔽されます。

The basic approach of the library is to use HTTP style communication for all protocol schemes. This means that you will construct HTTP::Request objects and receive HTTP::Response objects even for non-HTTP resources like gopher and ftp. In order to achieve even more similarity to HTTP style communications, gopher menus and file directories are converted to HTML documents.

このライブラリの基本的なアプローチは、HTTP 式通信をすべての プロトコルスキームに使うことです。 つまり gopherftp のような非 HTTP リソースでも HTTP::Request オブジェクトを構築して HTTP::Response オブジェクトを受け取るということです。 より HTTP 式通信に似せるため、gopher メニューとファイルディレクトリは HTML ドキュメントに変換されます。

コンストラクタメソッド

The following constructor methods are available:

以下のコンストラクタメソッドが利用可能です:

$ua = LWP::UserAgent->new( %options )

This method constructs a new LWP::UserAgent object and returns it. Key/value pair arguments may be provided to set up the initial state. The following options correspond to attribute methods described below:

このメソッドは新しい LWP::UserAgent オブジェクトを構成して、それを 返します。 初期状態を設定するためにキー-値のペアの引数を指定できます。 以下のオプションは後述する属性メソッドに対応します:

   KEY                     DEFAULT
   -----------             --------------------
   agent                   "libwww-perl/#.###"
   from                    undef
   conn_cache              undef
   cookie_jar              undef
   default_headers         HTTP::Headers->new
   local_address           undef
   ssl_opts                { verify_hostname => 1 }
   max_size                undef
   max_redirect            7
   parse_head              1
   protocols_allowed       undef
   protocols_forbidden     undef
   requests_redirectable   ['GET', 'HEAD']
   timeout                 180

The following additional options are also accepted: If the env_proxy option is passed in with a TRUE value, then proxy settings are read from environment variables (see env_proxy() method below). If env_proxy isn't provided the PERL_LWP_ENV_PROXY environment variable controls if env_proxy() is called during initalization. If the keep_alive option is passed in, then a LWP::ConnCache is set up (see conn_cache() method below). The keep_alive value is passed on as the total_capacity for the connection cache.

以下の追加のオプションも指定できます: env_proxy オプションが真の値とともに渡されると、プロキシセッティングは 環境変数から読み込まれます(後述する env_proxy() メソッドを参照してください)。 env_proxy が提供されない場合、PERL_LWP_ENV_PROXY 環境変数は、 初期化中に env_proxy() が呼び出されるかを制御します。 keep_alive オプションが渡されると、 LWP::ConnCache が設定されます(後述する conn_cache() メソッドを 参照してください)。 keep_alive の値は接続キャッシュのための total_capacity として 渡されます。

$ua->clone

Returns a copy of the LWP::UserAgent object.

LWP::UserAgent オブジェクトのコピーを返します。

属性

The settings of the configuration attributes modify the behaviour of the LWP::UserAgent when it dispatches requests. Most of these can also be initialized by options passed to the constructor method.

設定属性は、LWP::UserAgent がリクエストを発行するときの振る舞いを 変更します。 これらのほとんどはコンストラクタメソッドに渡されたオプションによっても 初期化できます。

The following attribute methods are provided. The attribute value is left unchanged if no argument is given. The return value from each method is the old attribute value.

以下の属性メソッドが提供されています。 引数がない場合は属性値は変更されません。 それぞれのメソッドの返り値は古い属性値です。

$ua->agent
$ua->agent( $product_id )

Get/set the product token that is used to identify the user agent on the network. The agent value is sent as the "User-Agent" header in the requests. The default is the string returned by the _agent() method (see below).

ネットワーク上でユーザエージェントを識別するために使われる 製品トークン(product token)を取得または設定します。 エージェントの値はリクエストの "User-Agent" ヘッダとして送信されます。 デフォルトは _agent() メソッド(後述)で返される文字列です。

If the $product_id ends with space then the _agent() string is appended to it.

$product_id が空白で終わっている場合は、その値に _agent() の文字列が 追加されます。

The user agent string should be one or more simple product identifiers with an optional version number separated by the "/" character. Examples are:

ユーザエージェント文字列は、オプションで "/" 文字で 区切られたバージョン番号がついた、一つもしくはそれ以上の単純な 製品トークンです。 例を以下に示します:

  $ua->agent('Checkbot/0.4 ' . $ua->_agent);
  $ua->agent('Checkbot/0.4 ');    # same as above
  $ua->agent('Mozilla/5.0');
  $ua->agent("");                 # don't identify
$ua->_agent

Returns the default agent identifier. This is a string of the form "libwww-perl/#.###", where "#.###" is substituted with the version number of this library.

デフォルトのエージェント識別子を返します。 これは "libwww-perl/#.###" 形式の文字列で、"#.###" はこのライブラリの バージョン番号で置き換えられます。

$ua->from
$ua->from( $email_address )

Get/set the e-mail address for the human user who controls the requesting user agent. The address should be machine-usable, as defined in RFC 822. The from value is send as the "From" header in the requests. Example:

リクエストしているユーザエージェントを制御している人間であるユーザの e-mail アドレスを取得または設定します。 アドレスは RFC で定義されているように、機械で利用できなければなりません。 from の値はリクエストでの "From" ヘッダとして送信されます。 例:

  $ua->from('gaas@cpan.org');

The default is to not send a "From" header. See the default_headers() method for the more general interface that allow any header to be defaulted.

デフォルトでは、"From" ヘッダを送信しません。 任意のヘッダをデフォルトで指定できる、より一般的なインターフェースについては default_headers() メソッドを参照してください。

$ua->cookie_jar
$ua->cookie_jar( $cookie_jar_obj )

Get/set the cookie jar object to use. The only requirement is that the cookie jar object must implement the extract_cookies($request) and add_cookie_header($response) methods. These methods will then be invoked by the user agent as requests are sent and responses are received. Normally this will be a HTTP::Cookies object or some subclass.

使用するクッキー瓶(cookie jar)オブジェクトを取得または設定します。 唯一の必要条件は、クッキー瓶オブジェクトは extract_cookies($request) メソッドと add_cookie_header($response) メソッドを 実装している必要があると言うことです。 これらのメソッドは、リクエストが送信されるときとレスポンスが受信されたときに ユーザエージェントによって起動されます。 通常これは HTTP::Cookies オブジェクトかそのサブクラスです。

The default is to have no cookie_jar, i.e. never automatically add "Cookie" headers to the requests.

デフォルトではクッキー瓶を使いません; つまり自動的には "Cookie" ヘッダをリクエストに追加しません。

Shortcut: If a reference to a plain hash is passed in as the $cookie_jar_object, then it is replaced with an instance of HTTP::Cookies that is initialized based on the hash. This form also automatically loads the HTTP::Cookies module. It means that:

短縮形: もし単純なハッシュへのリファレンスが $cookie_jar_object として渡されると、このハッシュを基にして初期化された HTTP::Cookies のインスタンスで置き換えられます。 この形式はまた、自動的に HTTP::Cookies モジュールを読み込みます。 つまり、以下のようにすると:

  $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });

is really just a shortcut for:

これは実際には以下の省略形です:

  require HTTP::Cookies;
  $ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
$ua->default_headers
$ua->default_headers( $headers_obj )

Get/set the headers object that will provide default header values for any requests sent. By default this will be an empty HTTP::Headers object.

任意のレスポンスが送信されるときのデフォルトのヘッダ値を提供する ヘッダオブジェクトを取得または設定します。 デフォルトではこれは空の HTTP::Headers オブジェクトです。

$ua->default_header( $field )
$ua->default_header( $field => $value )

This is just a short-cut for $ua->default_headers->header( $field => $value ). Example:

これは単に $ua->default_headers->header( $field => $value ) の 短縮形です。 例:

  $ua->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
  $ua->default_header('Accept-Language' => "no, en");
$ua->conn_cache
$ua->conn_cache( $cache_obj )

Get/set the LWP::ConnCache object to use. See LWP::ConnCache for details.

使用する LWP::ConnCache オブジェクトを取得または設定します。 詳しくは LWP::ConnCache を参照してください。

$ua->credentials( $netloc, $realm )
$ua->credentials( $netloc, $realm, $uname, $pass )

Get/set the user name and password to be used for a realm.

あるレルムのために使われるユーザ名、パスワードを取得/設定します。

The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:

$netloc は "<host>:<port>" の形式の文字列です。 ユーザ名とパスワードはこのサーバにのみ渡されます。 例:

  $ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");
$ua->local_address
$ua->local_address( $address )

Get/set the local interface to bind to for network connections. The interface can be specified as a hostname or an IP address. This value is passed as the LocalAddr argument to IO::Socket::INET.

ネットワーク接続のために bind するローカルインターフェースを取得/設定します。 インターフェースはホスト名または IP アドレスとして指定できます。 この値は IO::Socket::INETLocalAddr 引数として渡されます。

$ua->max_size
$ua->max_size( $bytes )

Get/set the size limit for response content. The default is undef, which means that there is no limit. If the returned response content is only partial, because the size limit was exceeded, then a "Client-Aborted" header will be added to the response. The content might end up longer than max_size as we abort once appending a chunk of data makes the length exceed the limit. The "Content-Length" header, if present, will indicate the length of the full content and will normally not be the same as length($res->content).

レスポンスの内容(content)の大きさの制限を取得または設定します。 デフォルトは undef で、これは制限なしを意味します。 長さの制限を越えているために、戻されたレスポンスの内容が 一部だけであれば、"Client-Aborted" ヘッダがレスポンスに追加されます。 内容の長さが制限を超えることになるデータの固まりに追加するのを 中断するので、内容の長さは結局 max_size を 超えることになることがあります。 もし "Content-Length" ヘッダがあれば、内容全体の長さを示していて、 これは通常は length($res->content) と同じではありません。

$ua->max_redirect
$ua->max_redirect( $n )

This reads or sets the object's limit of how many times it will obey redirection responses in a given request cycle.

これは、1 回のリクエストで従うリダイレクトレスポンスの 回数の制限を読み込みまたは設定します。

By default, the value is 7. This means that if you call request() method and the response is a redirect elsewhere which is in turn a redirect, and so on seven times, then LWP gives up after that seventh request.

デフォルトでは、この値は 7 です。 これは、request() を呼び出して、リダイレクトされた先でさらに リダイレクトされて、というのを 7 回繰り返すと、LWP は 7 回目の リクエストの後に諦めます。

$ua->parse_head
$ua->parse_head( $boolean )

Get/set a value indicating whether we should initialize response headers from the <head> section of HTML documents. The default is TRUE. Do not turn this off, unless you know what you are doing.

HTML ドキュメントの <head> セクションからレスポンスヘッダを 初期化すべきかどうかを示す値を取得または設定します。 デフォルトは TRUE です。 何をしているのかわからなければ、これをオフにしないで下さい。

$ua->protocols_allowed
$ua->protocols_allowed( \@protocols )

This reads (or sets) this user agent's list of protocols that the request methods will exclusively allow. The protocol names are case insensitive.

これはこのユーザエージェントが排他的に許可されているプロトコルのリストを 読み込み(または設定)します。 プロトコル名は大文字小文字を無視します。

For example: $ua->protocols_allowed( [ 'http', 'https'] ); means that this user agent will allow only those protocols, and attempts to use this user agent to access URLs with any other schemes (like "ftp://...") will result in a 500 error.

例: $ua->protocols_allowed( [ 'http', 'https'] ); というのは、 このユーザエージェントはこれらのプロトコル のみ許可されている ことを 意味していて、このユーザエージェントが ("ftp://..." のような) その他の スキームの URL にアクセスしようとすると 500 エラーとなります。

To delete the list, call: $ua->protocols_allowed(undef)

リストを削除するには、以下のように呼び出します: $ua->protocols_allowed(undef)

By default, an object has neither a protocols_allowed list, nor a protocols_forbidden list.

デフォルトでは、オブジェクトは protocols_allowed リストと protocols_forbidden リストのどちらも持ちません。

Note that having a protocols_allowed list causes any protocols_forbidden list to be ignored.

protocols_allowed リストがあると protocols_forbidden リストは 無視されることに注意してください。

$ua->protocols_forbidden
$ua->protocols_forbidden( \@protocols )

This reads (or sets) this user agent's list of protocols that the request method will not allow. The protocol names are case insensitive.

これはこのユーザエージェントが 許可されていない プロトコルのリストを 読み込み(または設定)します。 プロトコル名は大文字小文字を無視します。

For example: $ua->protocols_forbidden( [ 'file', 'mailto'] ); means that this user agent will not allow those protocols, and attempts to use this user agent to access URLs with those schemes will result in a 500 error.

例: $ua->protocols_forbidden( [ 'file', 'mailto'] ); は このユーザエージェントはこれらのプロトコルを 許可せず、 このユーザエージェントを使ってこれらのスキーマで URL に アクセスしようとすると 500 エラーになります。

To delete the list, call: $ua->protocols_forbidden(undef)

リストを削除するには、このように呼び出します: $ua->protocols_forbidden(undef)

$ua->requests_redirectable
$ua->requests_redirectable( \@requests )

This reads or sets the object's list of request names that $ua->redirect_ok(...) will allow redirection for. By default, this is ['GET', 'HEAD'], as per RFC 2616. To change to include 'POST', consider:

これは $ua->redirect_ok(...) がリダイレクトを許可する リクエスト名のリストを読み込みまたは設定します。 デフォルトでは、これは RFC 2616 に従って ['GET', 'HEAD'] になっています。 'POST' を含むようにするには、以下のようにします:

   push @{ $ua->requests_redirectable }, 'POST';
$ua->show_progress
$ua->show_progress( $boolean )

Get/set a value indicating whether a progress bar should be displayed on on the terminal as requests are processed. The default is FALSE.

リクエストの処理時に端末にプログレスバーを表示するかどうかを示す 値を取得/設定します。 デフォルトは偽です。

$ua->timeout
$ua->timeout( $secs )

Get/set the timeout value in seconds. The default timeout() value is 180 seconds, i.e. 3 minutes.

秒単位のタイムアウト値を取得または設定します。 デフォルトの timeout() の値は 180 秒、つまり 3 分です。

The requests is aborted if no activity on the connection to the server is observed for timeout seconds. This means that the time it takes for the complete transaction and the request() method to actually return might be longer.

サーバへの接続において timeout 秒反応がないと、リクエストは中断します。 つまり、トランザクションが完了して request() メソッドが実際に返るまでの 時間を意味します。

$ua->ssl_opts
$ua->ssl_opts( $key )
$ua->ssl_opts( $key => $value )

Get/set the options for SSL connections. Without argument return the list of options keys currently set. With a single argument return the current value for the given option. With 2 arguments set the option value and return the old. Setting an option to the value undef removes this option.

SSL 接続のオプションを取得/設定します。 引数なしの場合、現在設定されているオプションキーの一覧を返します。 1 引数の場合、与えられたオプションの現在の値を返します。 2 引数の場合、オプションの値を設定して、古いものを返します。 オプションとして undef 値を設定すると、このオプションを削除します。

The options that LWP relates to are:

LWP に関するオプションは:

verify_hostname => $bool

When TRUE LWP will for secure protocol schemes ensure it connects to servers that have a valid certificate matching the expected hostname. If FALSE no checks are made and you can't be sure that you communicate with the expected peer. The no checks behaviour was the default for libwww-perl-5.837 and earlier releases.

真の場合、LWP は、セキュアなプロトコルスキームで、 妥当な証明書が想定されるホスト名に一致しているサーバに 接続していることを確認します。 偽の場合、チェックは行われず、想定している接続先と通信しているかを 確認できません。 チェックしない振る舞いは、libwww-perl-5.837 以前のリリースのデフォルトでした。

This option is initialized from the PERL_LWP_SSL_VERIFY_HOSTNAME environment variable. If this environment variable isn't set; then verify_hostname defaults to 1.

このオプションは PERL_LWP_SSL_VERIFY_HOSTNAME 環境変数で初期化されます。 環境変数が設定されていない場合、verify_hostname のデフォルトは 1 です。

SSL_ca_file => $path

The path to a file containing Certificate Authority certificates. A default setting for this option is provided by checking the environment variables PERL_LWP_SSL_CA_FILE and HTTPS_CA_FILE in order.

Certificate Authority 証明書があるファイルのパス。 このオプションのデフォルト設定は、環境変数 PERL_LWP_SSL_CA_FILEHTTPS_CA_FILE をこの順序でチェックすることで提供されます。

SSL_ca_path => $path

The path to a directory containing files containing Certificate Authority certificates. A default setting for this option is provided by checking the environment variables PERL_LWP_SSL_CA_PATH and HTTPS_CA_DIR in order.

Certificate Authority 証明書があるファイルのあるディレクトリへのパス。 このオプションのデフォルト設定は、環境変数 PERL_LWP_SSL_CA_PATHHTTPS_CA_DIR をこの順序でチェックすることで提供されます。

Other options can be set and are processed directly by the SSL Socket implementation in use. See IO::Socket::SSL or Net::SSL for details.

その他のオプションは、使っている SSL Socket 実装によって直接設定および 処理されます。 詳しくは IO::Socket::SSLNet::SSL を参照してください。

The libwww-perl core no longer bundles protocol plugins for SSL. You will need to install LWP::Protocol::https separately to enable support for processing https-URLs.

libwww-perl コアはもはや SSL のためのプロトコルプラグインを同梱しません。 https-URL の処理の対応を有効にするために、個別に LWP::Protocol::https をインストールする必要があります。

プロキシ属性

The following methods set up when requests should be passed via a proxy server.

以下のメソッドはプロキシサーバー経由で渡されるべきリクエストを設定します。

$ua->proxy(\@schemes, $proxy_url)
$ua->proxy($scheme, $proxy_url)

Set/retrieve proxy URL for a scheme:

あるスキームのためのプロキシ URL を設定または取得します:

 $ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
 $ua->proxy('gopher', 'http://proxy.sn.no:8001/');

The first form specifies that the URL is to be used for proxying of access methods listed in the list in the first method argument, i.e. 'http' and 'ftp'.

最初の形式はその URL がメソッドの最初に引数のリストに入っている アクセスメソッド、つまり 'http' と 'ftp' のプロキシのために 使われることを指定します。

The second form shows a shorthand form for specifying proxy URL for a single access scheme.

2 番目の形式は一つのアクセス機能のためのプロキシ URL を 指定するための短縮した形式を示しています。

$ua->no_proxy( $domain, ... )

Do not proxy requests to the given domains. Calling no_proxy without any domains clears the list of domains. Eg:

与えられたドメインへのリクエストをプロキシしません。 何もドメインを指定しないで no_proxy を呼ぶと、ドメインのリストを クリアします。 例:

 $ua->no_proxy('localhost', 'example.com');
$ua->env_proxy

Load proxy settings from *_proxy environment variables. You might specify proxies like this (sh-syntax):

*_proxy 環境変数からプロキシ設定をロードします。 以下のように指定できるでしょう(sh での書き方):

  gopher_proxy=http://proxy.my.place/
  wais_proxy=http://proxy.my.place/
  no_proxy="localhost,example.com"
  export gopher_proxy wais_proxy no_proxy

csh or tcsh users should use the setenv command to define these environment variables.

csh または tcsh のユーザは、これらの環境変数を定義するために setenv コマンドを使わなければなりません。

On systems with case insensitive environment variables there exists a name clash between the CGI environment variables and the HTTP_PROXY environment variable normally picked up by env_proxy(). Because of this HTTP_PROXY is not honored for CGI scripts. The CGI_HTTP_PROXY environment variable can be used instead.

環境変数名に大文字小文字の区別がないシステムでは、 CGI 環境変数と、通常 env_proxy() によって読み込まれる HTTP_PROXY 環境変数の間で名前の衝突があります。 この理由により、HTTP_PROXY は CGI スクリプトのためのものではありません。 代わりに CGI_HTTP_PROXY 環境変数を使えます。

ハンドラ

Handlers are code that injected at various phases during the processing of requests. The following methods are provided to manage the active handlers:

ハンドラは、リクエストの処理中の様々なフェーズに注入されるコードです。 アクティブなハンドラを管理するために次のメソッドが提供されています:

$ua->add_handler( $phase => \&cb, %matchspec )

Add handler to be invoked in the given processing phase. For how to specify %matchspec see "Matching" in HTTP::Config.

指定された処理フェーズで起動されるハンドラを追加します。 %matchspec の指定方法については "Matching" in HTTP::Config を 参照してください。

The possible values $phase and the corresponding callback signatures are:

取り得る $phase の値と、対応するコールバックのシグネチャは:

request_preprepare => sub { my($request, $ua, $h) = @_; ... }

The handler is called before the request_prepare and other standard initialization of of the request. This can be used to set up headers and attributes that the request_prepare handler depends on. Proxy initialization should take place here; but in general don't register handlers for this phase.

ハンドラは、request_prepare とその他のリクエストの標準的な初期化の 前に呼び出されます。 これは request_prepare ハンドラが依存するヘッダと属性を 設定するのに使えます。 プロキシ初期化はここに置く必要があります; しかし一般的にはこのフェーズにハンドルを登録しないでください。

request_prepare => sub { my($request, $ua, $h) = @_; ... }

The handler is called before the request is sent and can modify the request any way it see fit. This can for instance be used to add certain headers to specific requests.

ハンドラは、リクエストを送信する前に呼び出され、 適合するならどのような形でもリクエストを変更できます。 これは例えば特定のリクエストにある種のヘッダを追加するのに使えます。

The method can assign a new request object to $_[0] to replace the request that is sent fully.

メソッドは、送られるリクエストを完全に置き換えるために、 $_[0] に新しいリクエストオブジェクトを代入できます。

The return value from the callback is ignored. If an exception is raised it will abort the request and make the request method return a "400 Bad request" response.

コールバックからの返り値は無視されます。 例外が発生すると、リクエストを中止して、 リクエストメソッドは "400 Bad request" レスポンスを返します。

request_send => sub { my($request, $ua, $h) = @_; ... }

This handler gets a chance of handling requests before they're sent to the protocol handlers. It should return an HTTP::Response object if it wishes to terminate the processing; otherwise it should return nothing.

このハンドラは、プロトコルハンドラに送られる前に リクエストを扱う機会を与えます。 処理を終わらせることを望むなら HTTP::Response オブジェクトを返す 必要があります; さもなければ何も返さない必要があります。

The response_header and response_data handlers will not be invoked for this response, but the response_done will be.

response_headerresponse_data のハンドラは このレスポンスでは起動されませんが、 response_done は起動されます。

response_header => sub { my($response, $ua, $h) = @_; ... }

This handler is called right after the response headers have been received, but before any content data. The handler might set up handlers for data and might croak to abort the request.

このハンドラは、レスポンスヘッダを受信した直後、本体データを 受信する前に呼び出されます。 ハンドラはデータのためにハンドラを設定したりリクエストを中止するために croak したりできます。

The handler might set the $response->{default_add_content} value to control if any received data should be added to the response object directly. This will initially be false if the $ua->request() method was called with a $content_file or $content_cb argument; otherwise true.

ハンドラは、受信データが直接レスポンスオブジェクトに追加されるかどうかを 制御するために $response->{default_add_content} の値を設定できます。 これは、 $content_file または $content_cb 引数付きで $ua->request() メソッドが呼び出されたときは偽です; さもなければ真です。

response_data => sub { my($response, $ua, $h, $data) = @_; ... }

This handler is called for each chunk of data received for the response. The handler might croak to abort the request.

このハンドラは、レスポンスとしてデータの塊を受信する毎に呼び出されます。 このハンドラはリクエストを中止するために croak できます。

This handler needs to return a TRUE value to be called again for subsequent chunks for the same request.

このハンドラは、同じリクエストの引き続く塊のために再び呼び出されるために、 真の値を返す必要があります。

response_done => sub { my($response, $ua, $h) = @_; ... }

The handler is called after the response has been fully received, but before any redirect handling is attempted. The handler can be used to extract information or modify the response.

このハンドルは、レスポンスが完全に受信された後、リダイレクト処理を 試みる前に呼び出されます。 ハンドラは、情報を取り出したりレスポンスを修正したりするのに使えます。

response_redirect => sub { my($response, $ua, $h) = @_; ... }

The handler is called in $ua->request after response_done. If the handler returns an HTTP::Request object we'll start over with processing this request instead.

このハンドルは $ua->request で、response_done の後に呼び出されます。 ハンドラが HTTP::Request オブジェクトを返すと、代わりにこのリクエストを 処理してやり直します。

$ua->remove_handler( undef, %matchspec )
$ua->remove_handler( $phase, %matchspec )

Remove handlers that match the given %matchspec. If $phase is not provided remove handlers from all phases.

%matchspec に一致するハンドラを削除します。 $phase が指定されないと、全てのフェーズからハンドラを削除します。

Be careful as calling this function with %matchspec that is not not specific enough can remove handlers not owned by you. It's probably better to use the set_my_handler() method instead.

自分が所有していないハンドラを削除しないのに十分な %matchspec で この関数を呼び出すように注意してください。 おそらく代わりに set_my_handler() メソッドを使った方が良いです。

The removed handlers are returned.

削除されたハンドラが返されます。

$ua->set_my_handler( $phase, $cb, %matchspec )

Set handlers private to the executing subroutine. Works by defaulting an owner field to the %matchspec that holds the name of the called subroutine. You might pass an explicit owner to override this.

サブルーチンを実行するのにプライベートなハンドラを設定します。 owner フィールドに、呼び出したサブルーチン名を保持した %matchspec を デフォルトで設定することで動作します。 これを上書きするために明示的な owner を渡すことができます。

If $cb is passed as undef, remove the handler.

$cb が undef として渡されると、ハンドラを削除します。

$ua->get_my_handler( $phase, %matchspec )
$ua->get_my_handler( $phase, %matchspec, $init )

Will retrieve the matching handler as hash ref.

一致するハンドラをハッシュリファレンスとして受け取ります。

If $init is passed passed as a TRUE value, create and add the handler if it's not found. If $init is a subroutine reference, then it's called with the created handler hash as argument. This sub might populate the hash with extra fields; especially the callback. If $init is a hash reference, merge the hashes.

$init に真の値が渡されると、見つからない場合に 新しくハンドラを作って追加します。 $init がサブルーチンリファレンスの場合、 作成されたハンドラハッシュを引数としてこれが呼び出されます。 このサブルーチンは、ハッシュに追加のフィールド、特にコールバックを 追加できます。 $init がハッシュリファレンスなら、ハッシュをマージします。

$ua->handlers( $phase, $request )
$ua->handlers( $phase, $response )

Returns the handlers that apply to the given request or response at the given processing phase.

指定された処理フェーズで指定されたリクエストまたはレスポンスに 適用されるハンドラを返します。

リクエストメソッド

The methods described in this section are used to dispatch requests via the user agent. The following request methods are provided:

この章で記述されているメソッドはユーザエージェント経由でリクエストを 発行するために使われます。 以下のリクエストメソッドが提供されています:

$ua->get( $url )
$ua->get( $url , $field_name => $value, ... )

This method will dispatch a GET request on the given $url. Further arguments can be given to initialize the headers of the request. These are given as separate name/value pairs. The return value is a response object. See HTTP::Response for a description of the interface it provides.

このメソッドは、与えられた $url に GET リクエストを発行します。 リクエストのヘッダを初期化するために追加の引数を与えることもできます。 これらは別々の名前/値の組で与えられます。 返り値はレスポンスオブジェクトです。 これが提供するインターフェースの説明については HTTP::Response を 参照してください。

There will still be a response object returned when LWP can't connect to the server specified in the URL or when other failures in protocol handlers occur. These internal responses use the standard HTTP status codes, so the responses can't be differentiated by testing the response status code alone. Error responses that LWP generates internally will have the "Client-Warning" header set to the value "Internal response". If you need to differentiate these internal responses from responses that a remote server actually generates, you need to test this header value.

LWP が URL で指定されたサーバに接続できない場合や、プロトコルハンドラ内で その他のエラーが発生した場合でも、レスポンスオブジェクトが返されます。 これらの内部レスポンスは標準の HTTP ステータスコードを使うので、 レスポンスステータスコードだけをテストしても、レスポンスを 区別することはできません。 LWP が内部で生成したエラーレスポンスは、"Client-Warning" ヘッダに "Internal response" という値が設定されます。 これらの内部レスポンスを実際にリモートサーバが生成したレスポンスと 区別する必要がある場合は、このヘッダの値をテストする必要があります。

Fields names that start with ":" are special. These will not initialize headers of the request but will determine how the response content is treated. The following special field names are recognized:

":" で始まるフィールド名は特殊です。 これらはリクエストのヘッダの初期化はせず、レスポンスオブジェクトが どのように扱われるかを決定します。 以下の特殊フィールド名を認識します:

    :content_file   => $filename
    :content_cb     => \&callback
    :read_size_hint => $bytes

If a $filename is provided with the :content_file option, then the response content will be saved here instead of in the response object. If a callback is provided with the :content_cb option then this function will be called for each chunk of the response content as it is received from the server. If neither of these options are given, then the response content will accumulate in the response object itself. This might not be suitable for very large response bodies. Only one of :content_file or :content_cb can be specified. The content of unsuccessful responses will always accumulate in the response object itself, regardless of the :content_file or :content_cb options passed in.

:content_file オプションで $filename が指定されると、レスポンスの内容は レスポンスオブジェクトの代わりにこのファイルに保存されます。 :content_cb オプションでコールバック関数が指定されると、 レスポンスの内容の塊ををサーバから受信する毎にこの関数が呼び出されます。 これらのオプションのどちらも指定されなかった場合、レスポンスの内容は レスポンスオブジェクト自身に蓄積されます。 これはレスポンスボディがとても大きい場合には向いていません。 :content_file:content_cb のどちらか一つのみが指定できます。 失敗したレスポンスの内容は、:content_file:content_cb の オプションが指定されているかどうかに関わらず 常にレスポンスオブジェクト自身に蓄積されます。

The :read_size_hint option is passed to the protocol module which will try to read data from the server in chunks of this size. A smaller value for the :read_size_hint will result in a higher number of callback invocations.

:read_size_hint オプションは、このサイズの塊でサーバからデータを 読み込もうとするようにプロトコルモジュールに渡されます。 :read_size_hint の値を小さくすると、コールバックの起動回数は 多くなります。

The callback function is called with 3 arguments: a chunk of data, a reference to the response object, and a reference to the protocol object. The callback can abort the request by invoking die(). The exception message will show up as the "X-Died" header field in the response returned by the get() function.

コールバック関数は 3 引数で呼び出されます: データの塊、 レスポンスオブジェクトへのリファレンス、プロトコルオブジェクトへの リファレンス、です。 コールバックは die() を起動することでリクエストを中断できます。 例外メッセージは、get() 関数で返されるレスポンスの中の "X-Died" ヘッダフィールドで得られます。

$ua->head( $url )
$ua->head( $url , $field_name => $value, ... )

This method will dispatch a HEAD request on the given $url. Otherwise it works like the get() method described above.

このメソッドは、与えられた $url に HEAD リクエストを発行します。 その他は上述の get() メソッドと同様に動作します。

$ua->post( $url, \%form )
$ua->post( $url, \@form )
$ua->post( $url, \%form, $field_name => $value, ... )
$ua->post( $url, $field_name => $value,... Content => \%form )
$ua->post( $url, $field_name => $value,... Content => \@form )
$ua->post( $url, $field_name => $value,... Content => $content )

This method will dispatch a POST request on the given $url, with %form or @form providing the key/value pairs for the fill-in form content. Additional headers and content options are the same as for the get() method.

このメソッドは、与えられた $url に、フォームの内容として %form または @form で与えられるキー/値の組を使って POST リクエストを発行します。 追加のヘッダと内容のオプションは get() メソッドと同じです。

This method will use the POST() function from HTTP::Request::Common to build the request. See HTTP::Request::Common for a details on how to pass form content and other advanced features.

このメソッドは、リクエストを構築するために HTTP::Request::Common の POST() 関数を使います。 フォームの内容の渡し方とその他の高度な機能に関する詳細については HTTP::Request::Common を参照してください。

$ua->put( $url, \%form )
$ua->put( $url, \@form )
$ua->put( $url, \%form, $field_name => $value, ... )
$ua->put( $url, $field_name => $value,... Content => \%form )
$ua->put( $url, $field_name => $value,... Content => \@form )
$ua->put( $url, $field_name => $value,... Content => $content )

This method will dispatch a PUT request on the given $url, with %form or @form providing the key/value pairs for the fill-in form content. Additional headers and content options are the same as for the get() method.

このメソッドは、与えられた $url に PUT リクエストを発行します; %form または @form でフォームの内容のためのキー/値の組を提供します。 追加のヘッダと本体オプションは get() メソッドと同様です。

This method will use the PUT() function from HTTP::Request::Common to build the request. See HTTP::Request::Common for a details on how to pass form content and other advanced features.

このメソッドは、リクエストを構築するのに HTTP::Request::Common の PUT() 関数を使います。 フォームの内容の渡し方とその他の高度な機能に関する詳細については HTTP::Request::Common を参照してください。

$ua->delete( $url )
$ua->delete( $url, $field_name => $value, ... )

This method will dispatch a DELETE request on the given $url. Additional headers and content options are the same as for the get() method.

このメソッドは、与えられた $url に DELETE リクエストを発行します。 追加のヘッダと内容オプションは get() メソッドと同様です。

This method will use the DELETE() function from HTTP::Request::Common to build the request. See HTTP::Request::Common for a details on how to pass form content and other advanced features.

このメソッドは、リクエストを構築するのに HTTP::Request::Common の DELETE() 関数を使います。 フォームの内容の渡し方とその他の高度な機能に関する詳細については HTTP::Request::Common を参照してください。

$ua->mirror( $url, $filename )

This method will get the document identified by $url and store it in file called $filename. If the file already exists, then the request will contain an "If-Modified-Since" header matching the modification time of the file. If the document on the server has not changed since this time, then nothing happens. If the document has been updated, it will be downloaded again. The modification time of the file will be forced to match that of the server.

このメソッドは $url で識別されるドキュメントを取得し、$filename で 指定されるファイルに保管します。 ファイルが既に存在する場合、リクエストにはそのファイルの修正時刻に一致する "If-Modified-Since" ヘッダが含まれます。 ドキュメントが更新されている場合は、再びダウンロードされます。 ファイルの修正時刻はサーバ上での修正時刻と同じになります。

The return value is the the response object.

返り値はレスポンスオブジェクトです。

$ua->request( $request )
$ua->request( $request, $content_file )
$ua->request( $request, $content_cb )
$ua->request( $request, $content_cb, $read_size_hint )

This method will dispatch the given $request object. Normally this will be an instance of the HTTP::Request class, but any object with a similar interface will do. The return value is a response object. See HTTP::Request and HTTP::Response for a description of the interface provided by these classes.

このメソッドは与えられた $request オブジェクトを発行します。 通常これは HTTP::Request クラスの実体ですが、似たような インターフェースを持つどのようなオブジェクトでも動作します。 返り値はレスポンスオブジェクトです。 これらのクラスによって提供されるインターフェースの記述については HTTP::RequestHTTP::Response を参照してください。

The request() method will process redirects and authentication responses transparently. This means that it may actually send several simple requests via the simple_request() method described below.

request() メソッドはリダイレクトと認証を透過的に処理します。 これは、実際には後述する simple_request() メソッドを使って単純な リクエストを複数回送信するかもしれないことを意味します。

The request methods described above; get(), head(), post() and mirror(), will all dispatch the request they build via this method. They are convenience methods that simply hides the creation of the request object for you.

上述のリクエストメソッド: get(), head(), post(), mirror() は全て 構築したリクエストをこのメソッド経由で発行します。 これらは単にリクエストメソッドの作成をあなたから隠すための 便利メソッドです。

The $content_file, $content_cb and $read_size_hint all correspond to options described with the get() method above.

$content_file, $content_cb, $read_size_hint は全て上述の get() メソッドで記述したオプションに対応します。

You are allowed to use a CODE reference as content in the request object passed in. The content function should return the content when called. The content can be returned in chunks. The content function will be invoked repeatedly until it return an empty string to signal that there is no more content.

リクエストオブジェクトに渡す content としてコードリファレンスも使えます。 content 関数は、呼び出されたときに内容を返すようにします。 内容は塊で返すこともできます。 content 関数は、もう内容がないことを示すために空文字列が返されるまで 繰り返し起動されます。

$ua->simple_request( $request )
$ua->simple_request( $request, $content_file )
$ua->simple_request( $request, $content_cb )
$ua->simple_request( $request, $content_cb, $read_size_hint )

This method dispatches a single request and returns the response received. Arguments are the same as for request() described above.

このメソッドは 1 回のリクエストを発行し、受信したレスポンスを返します。 引数は上述の request() のものと同じです。

The difference from request() is that simple_request() will not try to handle redirects or authentication responses. The request() method will in fact invoke this method for each simple request it sends.

request() との違いは、simple_request() はリダイレクトや認証を 扱おうとしないことです。 実際のところ、request() メソッドは、単純なリクエストを送る度に このメソッドを起動します。

$ua->is_online

Tries to determine if you have access to the Internet. Returns TRUE if the built-in heuristics determine that the user agent is able to access the Internet (over HTTP). See also LWP::Online.

インターネットにアクセスできるかを判断しようとします。 ユーザーエージェントがインターネットに (HTTP で) アクセスできると 組み込みのヒューリスティックが判断した場合、真を返します。 LWP::Online も参照してください。

$ua->is_protocol_supported( $scheme )

You can use this method to test whether this user agent object supports the specified scheme. (The scheme might be a string (like 'http' or 'ftp') or it might be an URI object reference.)

このユーザエージェントが指定された scheme を サポートしているかどうかを調べるために使うことができます。 (scheme には ('http' や 'ftp' のような)文字列や、 URI オブジェクトリファレンスを指定できます。)

Whether a scheme is supported, is determined by the user agent's protocols_allowed or protocols_forbidden lists (if any), and by the capabilities of LWP. I.e., this will return TRUE only if LWP supports this protocol and it's permitted for this particular object.

あるスキーマが対応しているかどうかは、(もしあれば)ユーザエージェントの protocols_allowedprotocols_forbidden のリスト、および LWP の能力によって決定されます。 つまり、このメソッドは LWP がこのプロトコルに対応していて、かつ それが このオブジェクトに対して許可されている場合にのみ TRUE を返します。

コールバックメソッド

The following methods will be invoked as requests are processed. These methods are documented here because subclasses of LWP::UserAgent might want to override their behaviour.

以下のメソッドはリクエストの処理中に起動されます。 これらのメソッドは、LWP::UserAgent のサブクラスが振る舞いを オーバーライドしたいかもしれないので、ここで記述されています。

$ua->prepare_request( $request )

This method is invoked by simple_request(). Its task is to modify the given $request object by setting up various headers based on the attributes of the user agent. The return value should normally be the $request object passed in. If a different request object is returned it will be the one actually processed.

このメソッドは simple_request() によって起動されます。 その使命は、与えられた $request オブジェクトを、ユーザエージェントの 属性によって様々なヘッダを設定することで変更することです。 返り値は通常は渡された $request オブジェクトです。 異なる request オブジェクトが返された場合、それは実際に処理されたものです。

The headers affected by the base implementation are; "User-Agent", "From", "Range" and "Cookie".

基本実装によって影響を受けるヘッダは、 "User-Agent", "From", "Range", "Cookie" です。

$ua->redirect_ok( $prospective_request, $response )

This method is called by request() before it tries to follow a redirection to the request in $response. This should return a TRUE value if this redirection is permissible. The $prospective_request will be the request to be sent if this method returns TRUE.

このメソッドは、$response のリクエストのリダイレクトに 従おうとする前に、request() によって呼ばれます。 このリダイレクトを許可する場合には真の値を返します。 $prospective_request は、このメソッドが真を返した場合に 送信されるリクエストです。

The base implementation will return FALSE unless the method is in the object's requests_redirectable list, FALSE if the proposed redirection is to a "file://..." URL, and TRUE otherwise.

基本実装では、メソッドがそのオブジェクトの requests_redirectable リストに ない場合と、予定されているリダイレクトが "file://..." URL の場合に 偽を返し、それ以外では真を返します。

$ua->get_basic_credentials( $realm, $uri, $isproxy )

This is called by request() to retrieve credentials for documents protected by Basic or Digest Authentication. The arguments passed in is the $realm provided by the server, the $uri requested and a boolean flag to indicate if this is authentication against a proxy server.

基本認証またはダイジェスト認証により保護されている文書のための 証明物 (credentials) の取得のために request() によって呼ばれます。 渡される引数は、$realm がサーバから提供されるレルム、 $uri がリクエストされる URI、$isproxy がこれがプロキシサーバーに 対する認証かどうかを示す真偽値フラグです。

The method should return a username and password. It should return an empty list to abort the authentication resolution attempt. Subclasses can override this method to prompt the user for the information. An example of this can be found in lwp-request program distributed with this library.

このメソッドはユーザ名とパスワードを返します。 認証解決の試みを中断させるためには空リストを返します。 サブクラスはユーザに情報を尋ねるためにこのモジュールを上書きできます。 この例はこのライブラリと一緒に配布される lwp-request プログラムにあります。

The base implementation simply checks a set of pre-stored member variables, set up with the credentials() method.

基本実装は単にあらかじめ保管されているメンバ変数をチェックし、 credentials() メソッドを設定します。

$ua->progress( $status, $request_or_response )

This is called frequently as the response is received regardless of how the content is processed. The method is called with $status "begin" at the start of processing the request and with $state "end" before the request method returns. In between these $status will be the fraction of the response currently received or the string "tick" if the fraction can't be calculated.

これは、内容がどのように処理されるかにかかわらず、レスポンスを 受信する度に呼び出されます。 このメソッドは、リクエストの処理開始時には $status に "begin" を設定して呼び出され、 リクエストメソッドが返る前には $status に "end" を設定して呼び出されます。 その間では、$status は現在受信したレスポンスの割合か、 割合が計算できない場合は文字列 "tick" です。

When $status is "begin" the second argument is the request object, otherwise it is the response object.

2 番目の引数は、$status が "begin" の場合はリクエストオブジェクトで、 そうでない場合はレスポンスオブジェクトです。

SEE ALSO

See LWP for a complete overview of libwww-perl5. See lwpcook and the scripts lwp-request and lwp-download for examples of usage.

libwww-perl5 の完全な概要は LWP を参照してください。 使い方の例については、lwpcook および lwp-requestlwp-mirror のスクリプトを参照してください。

See HTTP::Request and HTTP::Response for a description of the message objects dispatched and received. See HTTP::Request::Common and HTML::Form for other ways to build request objects.

発行したり受信したりするメッセージオブジェクトの記述については HTTP::RequestHTTP::Response を参照してください。 リクエストオブジェクトを構築するその他の方法については HTTP::Request::CommonHTML::Form を参照してください。

See WWW::Mechanize and WWW::Search for examples of more specialized user agents based on LWP::UserAgent.

LWP::UserAgent を基とした、より特殊化したユーザエージェントの例については WWW::MechanizeWWW::Search を参照してください。

コピーライト

Copyright 1995-2009 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.