libwww-perl-5.813 > HTTP::Headers
libwww-perl-5.813

名前

HTTP::Headers - Class encapsulating HTTP Message headers

HTTP::Headers - HTTPメッセージヘッダをカプセル化するクラス

概要

 require HTTP::Headers;
 $h = HTTP::Headers->new;

 $h->header('Content-Type' => 'text/plain');  # set
 $ct = $h->header('Content-Type');            # get
 $h->remove_header('Content-Type');           # delete

説明

The HTTP::Headers class encapsulates HTTP-style message headers. The headers consist of attribute-value pairs also called fields, which may be repeated, and which are printed in a particular order. The field names are cases insensitive.

HTTP::HeadersクラスはHTTP形式メッセージヘッダをカプセル化します。 ヘッダはフィールドとも呼ばれる属性-値の組み合わせで構成され、それは 繰り返しがあるかも知れず、特定の順序で出力されます。 フィールド名は大文字小文字を無視します。

Instances of this class are usually created as member variables of the HTTP::Request and HTTP::Response classes, internal to the library.

このクラスのインスタンスは通常、HTTP::RequestHTTP::Response クラスのメンバ変数として、ライブラリ内部用に 作成されます。

The following methods are available:

以下のメソッドが利用できます:

$h = HTTP::Headers->new

Constructs a new HTTP::Headers object. You might pass some initial attribute-value pairs as parameters to the constructor. E.g.:

新しい HTTP::Headers オブジェクトを組み立てます。 コンストラクタのパラメータを通して初期の属性-値の組を渡すことが出来ます。 例えば:

 $h = HTTP::Headers->new(
       Date         => 'Thu, 03 Feb 1994 00:00:00 GMT',
       Content_Type => 'text/html; version=3.2',
       Content_Base => 'http://www.perl.org/');

The constructor arguments are passed to the header method which is described below.

コンストラクタの引数は、後述する header メソッドに渡されます。

$h->clone

Returns a copy of this HTTP::Headers object.

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

$h->header( $field )
$h->header( $field => $value, ... )

Get or set the value of one or more header fields. The header field name ($field) is not case sensitive. To make the life easier for perl users who wants to avoid quoting before the => operator, you can use '_' as a replacement for '-' in header names.

ひとつまたは複数のヘッダフィールドの値を取得あるいは設定します。 ヘッダフィールド名($field)は大文字小文字を区別しません。 => 演算子の前にクォートしたくない Perl ユーザに簡単なように、ヘッダ名での '-' の代わりとして '_' を使うことが出来ます。

The header() method accepts multiple ($field => $value) pairs, which means that you can update several fields with a single invocation.

header()メソッドは複数の($field => $values) の組をうけとるので、 1 回の呼び出しでたくさんのフィールドを更新することが出来ます。

The $value argument may be a plain string or a reference to an array of strings for a multi-valued field. If the $value is provided as undef then the field is removed. If the $value is not given, then that header field will remain unchanged.

$value 引数は普通の文字列または複数の値を持つフィールドのための 文字列のリストへのリファレンスにすることができます。 $value 引数が undef の場合、フィールドは削除されます。 $value が指定されなければヘッダは変更されません。

The old value (or values) of the last of the header fields is returned. If no such field exists undef will be returned.

最後のヘッダフィールドの古い値 (または値) が返されます。 そのようなフィールドがない場合は、undef が返されます。

A multi-valued field will be returned as separate values in list context and will be concatenated with ", " as separator in scalar context. The HTTP spec (RFC 2616) promise that joining multiple values in this way will not change the semantic of a header field, but in practice there are cases like old-style Netscape cookies (see HTTP::Cookies) where "," is used as part of the syntax of a single field value.

複数の値を持つフィールドは、 リストコンテキストでは別々の値として返され、 スカラコンテキストでは ", " を区切り文字としてつなげられます。 HTTP 仕様 (RFC 2616) では、この方法で複数の値を結合してもヘッダ フィールドの意味は変わらないことを約束していますが、実際には 旧式の Netscape クッキー(HTTP::Cookies 参照)のように、一つの フィールドの値の文法の一部として "," を使っている場合があります。

Examples:

例:

 $header->header(MIME_Version => '1.0',
                 User_Agent   => 'My-Web-Client/0.01');
 $header->header(Accept => "text/html, text/plain, image/*");
 $header->header(Accept => [qw(text/html text/plain image/*)]);
 @accepts = $header->header('Accept');  # get multiple values
 $accepts = $header->header('Accept');  # get values as a single string
$h->push_header( $field => $value )

Add a new field value for the specified header field. Previous values for the same field are retained.

指定されたヘッダに新しいフィールドの値を追加します。 同じフィールドの以前の値は残ります。

As for the header() method, the field name ($field) is not case sensitive and '_' can be used as a replacement for '-'.

header() メソッドに関しては、フィールド名 ($field) は大文字小文字は 関係なく、'-' の代わりとして '_' が使えます。

The $value argument may be a scalar or a reference to a list of scalars.

$value 引数にはスカラやスカラのリストへのリファレンスを指定することが 出来ます。

 $header->push_header(Accept => 'image/jpeg');
 $header->push_header(Accept => [map "image/$_", qw(gif png tiff)]);
$h->init_header( $field => $value )

Set the specified header to the given value, but only if no previous value for that field is set.

指定されたヘッダにすでに値がセットされていない場合のみ、与えられた値を セットします。

The header field name ($field) is not case sensitive and '_' can be used as a replacement for '-'.

ヘッダフィールド名 ($field) は大文字小文字は関係なく、'-' の代わりとして '_' が使えます。

The $value argument may be a scalar or a reference to a list of scalars.

$value 引数は、スカラか、スカラのリストへのリファレンスが取れます。

$h->remove_header( $field, ... )

This function removes the header fields with the specified names.

この関数は指定された名前を持つヘッダフィールドを削除します。

The header field names ($field) are not case sensitive and '_' can be used as a replacement for '-'.

ヘッダフィールド名 ($field) は大文字小文字は関係なく、'-' の代わりとして '_' が使えます。

The return value is the values of the fields removed. In scalar context the number of fields removed is returned.

返り値は、削除されたフィールドの値です。 スカラ古今テキストでは、削除されたフィールドの数が返されます。

Note that if you pass in multiple field names then it is generally not possible to tell which of the returned values belonged to which field.

複数のフィールド名を渡した場合、返り値の内どれがどのフィールドの 値であったかを知ることは一般的にはできないことに注意してください。

$h->remove_content_headers

This will remove all the header fields used to describe the content of a message. All header field names prefixed with Content- falls into this category, as well as Allow, Expires and Last-Modified. RFC 2616 denote these fields as Entity Header Fields.

これは、メッセージの内容を記述するために使われている全ての ヘッダフィールドを削除します。 フィールド名の頭に Content- が付く全てのフィールドと、 Allow, Expires, Last-Modified フィールドが対象となります。 RFC 2616 ではこれらのフィールドは Entity Header Fields と 名付けられています。

The return value is a new HTTP::Headers object that contains the removed headers only.

返り値は、削除されたヘッダのみを含む新しい HTTP::Headers オブジェクトです。

$h->clear

This will remove all header fields.

これは全てのヘッダフィールドを削除します。

$h->header_field_names

Returns the list of distinct names for the fields present in the header. The field names have case as suggested by HTTP spec, and the names are returned in the recommended "Good Practice" order.

ヘッダにあるフィールド名の重複を除いたリストを返します。 フィールド名は HTTP 仕様で推奨されているように大文字小文字を保持していて、 推薦されている "Good Practice" 順で返されます。

In scalar context return the number of distinct field names.

スカラコンテキストでは重複を除いたフィールド名の数を返します。

$h->scan( \&process_header_field )

Apply a subroutine to each header field in turn. The callback routine is called with two parameters; the name of the field and a single value (a string). If a header field is multi-valued, then the routine is called once for each value. The field name passed to the callback routine has case as suggested by HTTP spec, and the headers will be visited in the recommended "Good Practice" order.

各ヘッダフィールドそれぞれにサブルーチンを適用します。 コールバックルーチンは 2 引数で呼び出されます:フィールド名と 1 つの値(文字列)です。 ヘッダフィールドが複数の値を持っていれば、ルーチンはそれぞれの値につき 1 回呼ばれます。 コールバックルーチンに渡されるフィールド名は HTTP 仕様で提案されている大文字/小文字をもち、推奨されている "Good Practice" の順でやってきます。

Any return values of the callback routine are ignored. The loop can be broken by raising an exception (die), but the caller of scan() would have to trap the exception itself.

コールバックルーチンからの返り値は全て無視されます。 ループは例外 (die) の発生によって終了する場合がありますが、 scan() の呼び出し元は例外を自分自身でトラップする必要があります。

$h->as_string
$h->as_string( $eol )

Return the header fields as a formatted MIME header. Since it internally uses the scan method to build the string, the result will use case as suggested by HTTP spec, and it will follow recommended "Good Practice" of ordering the header fields. Long header values are not folded.

フォーマットされた MIME ヘッダとしてヘッダフィールドを返します。 文字列を組み立てるために内部で scan メソッドを使っているので、結果は HTTP 仕様で提案されている大文字小文字で、ヘッダ・フィールドの順序の 推奨されている "Good Practice" に従います。 長いヘッダの値はたたまれません。

The optional $eol parameter specifies the line ending sequence to use. The default is "\n". Embedded "\n" characters in header field values will be substituted with this line ending sequence.

オプションの $eol パラメータは使用する行末シーケンスを指定します。 デフォルトは "\n" です。 ヘッダフィールドの値に埋め込まれた "\n" 文字は、この行末シーケンスで 置きかえられます。

CONVENIENCE METHODS

The most frequently used headers can also be accessed through the following convenience Methods. These methods can both be used to read and to set the value of a header. The header value is set if you pass an argument to the method. The old header value is always returned. If the given header did not exist then undef is returned.

よく利用されるヘッダは以下の便宜メソッドを通してもアクセスすることが出来ます。 これらのメソッドはヘッダの値を読むこと設定することの両方に使うことできます。 メソッドに引数を渡せばヘッダの値が設定されます。 ヘッダの古い値は常に返されます。 与えられたヘッダが存在しない場合は、undef が返されます。

Methods that deal with dates/times always convert their value to system time (seconds since Jan 1, 1970) and they also expect this kind of value when the header value is set.

日付/時刻を扱うメソッドは常にその値をシステム時刻(1970 年 1 月 1 日からの 秒数)に変換し、ヘッダの値が設定される場合には、値がそうした種類であることを 期待します。

$h->date

This header represents the date and time at which the message was originated. E.g.:

このヘッダはメッセージが発行された日付と時刻を表します。 例えば:

  $h->date(time);  # set current date
$h->expires

This header gives the date and time after which the entity should be considered stale.

このヘッダは、それ以降はそのエントリが新鮮でない(stale)であると考えられる 日付と時刻を示します。

$h->if_modified_since
$h->if_unmodified_since

These header fields are used to make a request conditional. If the requested resource has (or has not) been modified since the time specified in this field, then the server will return a 304 Not Modified response instead of the document itself.

これらヘッダフィールドはリクエストを条件付きにします。 もしリクエストされたリソースがこのフィールドで指定された時刻から変更された (あるいは変更されていない)なら、、サーバはドキュメント自身の代りに 304 Not Modified レスポンスを返します。

$h->last_modified

This header indicates the date and time at which the resource was last modified. E.g.:

このヘッダはそのリソースが最後に変更された日付と時刻を返します。 例えば:

  # check if document is more than 1 hour old
  if (my $last_mod = $h->last_modified) {
      if ($last_mod < time - 60*60) {
          ...
      }
  }
$h->content_type

The Content-Type header field indicates the media type of the message content. E.g.:

Content-Type ヘッダフィールドはメッセージ内容のメディアタイプを示します。 例えば:

  $h->content_type('text/html');

The value returned will be converted to lower case, and potential parameters will be chopped off and returned as a separate value if in an array context. If there is no such header field, then the empty string is returned. This makes it safe to do the following:

返される値は小文字に変換され、潜在的なパラメータが切り落とされ、 配列コンテキストであれば、分けられた値として返されます。 そのようなヘッダフィールドがない場合、空文字列が返されます。 これは以下のようにすることを安全にします:

  if ($h->content_type eq 'text/html') {
     # we enter this place even if the real header value happens to
     # be 'TEXT/HTML; version=3.0'
     ...
  }
$h->content_encoding

The Content-Encoding header field is used as a modifier to the media type. When present, its value indicates what additional encoding mechanism has been applied to the resource.

Content-Encoding ヘッダフィールドはメディアタイプの修飾子として 使われます。 もしあれば、その値はどんな追加のエンコーディング機能がそのリソースに 適用されたのかを示します。

$h->content_length

A decimal number indicating the size in bytes of the message content.

メッセージ内容の大きさをバイト数で示す 10 進数。

$h->content_language

The natural language(s) of the intended audience for the message content. The value is one or more language tags as defined by RFC 1766. Eg. "no" for some kind of Norwegian and "en-US" for English the way it is written in the US.

メッセージ内容が対象としている聞き手の自然言語。 この値は RFC1766 で定義されている 1 つまたは複数の言語タグです。 例えば "no" は何らかのノルウェー語、"en-US" は アメリカで使われている英語です。

$h->title

The title of the document. In libwww-perl this header will be initialized automatically from the <TITLE>...</TITLE> element of HTML documents. This header is no longer part of the HTTP standard.

ドキュメントのタイトル。 libwww-perl では、このヘッダは自動的に HTML ドキュメントの <TITLE>...</TITLE> 要素から初期化されます。 このヘッダはもはや HTTP 標準の一部ではありません。

$h->user_agent

This header field is used in request messages and contains information about the user agent originating the request. E.g.:

このヘッダはリクエストメッセージで使われ、そのリクエストを発行した ユーザエージェントについての情報が入っています。 例えば:

  $h->user_agent('Mozilla/1.2');
$h->server

The server header field contains information about the software being used by the originating server program handling the request.

server ヘッダフィールドには、そのリクエストを取り扱う サーバプログラムによって使われたソフトウェアについての情報が入っています。

$h->from

This header should contain an Internet e-mail address for the human user who controls the requesting user agent. The address should be machine-usable, as defined by RFC822. E.g.:

このヘッダにはリクエストしているユーザエージェントを制御している人のための インターネット e-mail アドレスが入っていなければなりません。 そのアドレスは、RFC822 によって定義されているように、機械で 使えなければなりません。 例えば:

  $h->from('King Kong <king@kong.com>');

This header is no longer part of the HTTP standard.

このヘッダはもはや HTTP 標準の一部ではありません。

$h->referer

Used to specify the address (URI) of the document from which the requested resource address was obtained.

そこからリクエストされたリソースアドレスが取得された、ドキュメントの アドレス (URI) を指定するために使われます。

The "Free On-line Dictionary of Computing" as this to say about the word referer:

"Free On-line Dictionary of Computing" では、referer について 以下のように書かれています:

     <World-Wide Web> A misspelling of "referrer" which
     somehow made it into the {HTTP} standard.  A given {web
     page}'s referer (sic) is the {URL} of whatever web page
     contains the link that the user followed to the current
     page.  Most browsers pass this information as part of a
     request.

     (1998-10-19)

By popular demand referrer exists as an alias for this method so you can avoid this misspelling in your programs and still send the right thing on the wire.

一般的な需要により、referrer もこのメソッドへの別名として 存在しているので、プログラム内にスペルミスをすることなく、正しく 通信させることができます。

When setting the referrer, this method removes the fragment from the given URI if it is present, as mandated by RFC2616. Note that the removal does not happen automatically if using the header(), push_header() or init_header() methods to set the referrer.

リファラを設定するときに、このメソッドは RFC2616 で指示されているように URI 中にフラグメントがあった場合はこれを除去します。 リファラを設定するときに header(), push_header(), init_header() を使うと、この除去作業は 自動的には 行われない ことに注意してください。

$h->www_authenticate

This header must be included as part of a 401 Unauthorized response. The field value consist of a challenge that indicates the authentication scheme and parameters applicable to the requested URI.

このヘッダは 401 Unauthorized レスポンスの一部として 入らなければなりません。 フィールドの値は、リクエストされた URI へ適用できる 認証スキームとパラメータを示すチャレンジにより構成されます。

$h->proxy_authenticate

This header must be included in a 407 Proxy Authentication Required response.

このヘッダは 407 Proxy Authentication Required レスポンスに 入らなければなりません。

$h->authorization
$h->proxy_authorization

A user agent that wishes to authenticate itself with a server or a proxy, may do so by including these headers.

サーバまたはプロキシでそれ自身を認証して欲しいユーザエージェントは、 これらのヘッダを入れることによりそうなるかもしれません。

$h->authorization_basic

This method is used to get or set an authorization header that use the "Basic Authentication Scheme". In array context it will return two values; the user name and the password. In scalar context it will return "uname:password" as a single string value.

このヘッダは「基本認証機能」を使う認証ヘッダを取得および設定するために 使われます。 配列コンテキストでは 2 つの値を返します; ユーザ名とパスワードです。 スカラコンテキストでは 1 つの文字列として "uname:password" を返します。

When used to set the header value, it expects two arguments. E.g.:

ヘッダの値を設定するために使われるときは、2つの引数が期待されます。 例えば:

  $h->authorization_basic($uname, $password);

The method will croak if the $uname contains a colon ':'.

もし $uname にコロン ':' が入っていれば croak します。

$h->proxy_authorization_basic

Same as authorization_basic() but will set the "Proxy-Authorization" header instead.

authorization_basic() と同様ですが、 しかし代わりに "Proxy-Authorization" ヘッダを設定します。

NON-CANONICALIZED FIELD NAMES

The header field name spelling is normally canonicalized including the '_' to '-' translation. There are some application where this is not appropriate. Prefixing field names with ':' allow you to force a specific spelling. For example if you really want a header field name to show up as foo_bar instead of "Foo-Bar", you might set it like this:

ヘッダフィールド名は、通常('_' から '-' への変換を含めて)正規化されます。 これが適切でないアプリケーションもあります。 フィールド名の先頭に ':' を付けることで、正規化しないようにすることができます。 例えば、もし "Foo-Bar" ではなく本当に foo_bar というフィールド名の ヘッダにしたい場合は、以下のようにします:

  $h->header(":foo_bar" => 1);

These field names are returned with the ':' intact for $h->header_field_names and the $h->scan callback, but the colons do not show in $h->as_string.

これらのフィールド名は、$h->header_field_names と $h->scan の コールバックではそのまま ':' 付きで返されますが、 $h->as_string ではコロンは表示されません。

コピーライト

Copyright 1995-2005 Gisle Aas.

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