名前

Blosxom::Header::ja - 一意に HTTP ヘッダを指定する

概要

use Blosxom::Header;

my $header = tie my %header, 'Blosxom::Header';

$header{status} = '304 Not Modified';

my $value   = $header{status}; 
my $bool    = exists $header{satus}; 
my $deleted = delete $header{status};
my @keys    = keys %header;

%header = ();

$header->set(
    Status        => '304 Not Modified',
    Last_Modified => 'Wed, 23 Sep 2009 13:36:33 GMT',
);

my $status  = $header->get( 'Status' );
my $bool    = $header->exists( 'ETag' );
my @deleted = $header->delete( qw/Content-Disposition Content-Length/ );

$header->push_cookie( @cookies );
$header->push_p3p( @p3p );

$header->clear;

仕様

ブロッサムは単一の CGI スクリプトとして動作する、 ウェブログ・アプリケーションの一つである。 このアプリケーションは、$header というハッシュのリファレンスをグローバル化する。 $header は CGI モジュールの header() 関数に渡され、HTTP ヘッダを生成する。

package blosxom;
use CGI;
our $header = { -type => 'text/html' };
# プラグインを読み込む
print CGI::header( $header );

header() は $header のキーについて、大文字と小文字を区別しない。 また、キーの先頭にハイフンを付加するかどうかも任意だ。 他方、ハッシュのキーは大文字と小文字を区別する。 したがって、複数のプラグインが共存するような状況では、一意に $header を操作することができない。

このモジュールは、$header を操作するためのインタフェースである。 具体的には、キーの大文字と小文字を区別しないハッシュとして実装した。

tie my %header, 'Blosxom::Header';

Blosxom::Header インスタンスは、メモリ空間内に1つしか作成されない。 これは、$header がグローバル変数であるということに対応している。

HTTP::Headers に似たインタフェースも用意されている。

my $h = Blosxom::Header->instance; 
# または
my $h = tied %header;

$h は tie() インタフェースと互いに矛盾しない。 HTTP::Headers との違いは、1つのフィールドに1つの値しか割り当てられない という点だ。したがって、push() メソッドがない。 ただし、Set-Cookie ヘッダだけが複数の値を持つ。

$h->push_cookie( @cookies );

これは、header() 関数の制約による。

メソッド

$header = Blosxom::Header->instance

Blosxom::Header インスタンスを返す。インスタンスが存在しなければ、 新たに作成する。

$header = Blosxom::Header->has_instance

Blosxom::Header インスタンスが存在すれば、インスタンスを返す。 存在しなければ undef を返す。

$header = Blosxom::Header->new

このメソッドは 0.04 で取り除かれます。

$header->set( $field => $value )
$header->set( $f1 => $v1, $f2 => $v2, ... )

$field で指定したフィールドに、$value を代入する。 フィールド名は大文字と小文字を区別しない。 またハイフンの代わりに、アンダーバーを使うこともできる。

$value は基本的に文字列をとる。 特に、フィールド名として 'Set-Cookie' あるいは 'P3P' を指定したとき、リストのリファレンスをとる。

$header->set( Set_Cookie => [ $cookie1, $cookie2 ] );
$header->set( P3P => [ qw/CAO DSP LAW CURa/ ] );
$value = $header->get( $field )
@values = $header->get( $field )

引数としてフィールド名をとり、文字列を返す。 フィールドが存在しなければ undef を返す。 また、リスト・コンテキストで、リストを返す。

my @cookie = $header->get( 'Set_Cookie' );
my @p3p    = $header->get( 'P3P' );
$bool = $header->exists( $field )

引数としてフィールド名をとり、フィールドが存在するとき真を返す。 存在しないとき偽を返す。

@deleted = $header->delete( @fields )

指定したフィールドをすべて削除する。 削除したフィールドの値を返す。

$header->push_cookie( @cookies )

Set-Cookie ヘッダを追加する。

$header->push_p3p( @p3p )

CGI モジュール固有のパラメータ

以下のメソッドは、引数をつけたとき set()、つけないとき get() として働く。

$header->attachment
$header->attachment( 'foo.png' );
$header->charset

文字セットを指定する。$header->type に 'charset' が含まれている場合、 この属性は無視される。

$header->charset( 'utf-8' );
$header->cookie
$header->cookie( [ 'foo', 'bar' ] );
$header->cookie( 'baz' );
$header->expires
$header->expires( '+30s' ); # 30 秒後
$header->expires( '+10m' ); # 10 分後
$header->expires( '+1h'  ); # 1 時間後
$header->expires( '-1d'  ); # 昨日
$header->expires( 'now'  ); # 現在
$header->expires( '+3M'  ); # 3ヶ月後
$header->expires( '+10y' ); # 10 年後

# at the indicated time & date
$header->expires( 'Thu, 25 Apr 1999 00:40:33 GMT' );
$header->nph
$header->nph( 1 );
$header->p3p
$header->p3p( [ qw/CAO DSP LAW CURa/ ] );
$header->p3p( 'CAO DSP LAW CURa' );
$header->status
$header->status( '304 Not Modified' );
$header->target
$header->target( 'ResultsWindow' );
$header->type

メディア・タイプを指定する。

$header->type( 'text/plain' );

この属性が undef の場合、header() 関数がデフォルトの Content-Type ヘッダを 出力してしまう。Content-Type ヘッダを出力したくない場合、空の文字列を代入 しなければならない。

$header->type( q{} );

DEPENDENCIES

Blosxom 2.0.0 or higher.

SEE ALSO

CGI, Class::Singleton, perltie

作者

穴澤亮 (anazawa@cpan.org)

著作権

Perl Artistic ライセンスに準ずる。