| Tripletail documentation | Contained in the Tripletail distribution. |
Tripletail::RawCookie - 汎用的なクッキー管理を行う
my $rawcookie = $TL->getRawCookie;
my $val = $rawcookie->get('Cookie1');
$rawcookie->set('Cookie2' => 'val2');
生の文字列の状態でクッキーを取り出し、また格納する。 改行などのコントロールコードが含まれないように注意する必要性がある。
クッキー有効期限、ドメイン、パス等は、 ini ファイルで指定する。
$TL->getRawCookie $TL->getRawCookie($inigroup)
$TL->getRawCookie('Cookie')
Tripletail::RawCookie オブジェクトを取得。 引数には ini で設定したグループ名を渡す。 引数省略時は 'Cookie' グループが使用される。
get$str = $cookie->get($cookiename)
指定された名前のクッキーの内容を返す。
set$cookie->set($cookiename => $str)
文字列を、指定された名前のクッキーとしてセットする。
delete$cookie->delete($cookiename)
指定された名前のクッキーを削除する。
clear$cookie->clear
全てのクッキーを削除する。
path = /cgi-bin
クッキーのパス。省略可能。 デフォルトは省略した場合と同様。
domain = example.org
クッキーのドメイン。省略可能。 デフォルトは省略した場合と同様。
expires = 30 days
クッキー有効期限。 度量衡 参照。省略可能。 省略時はブラウザが閉じられるまでとなる。
secure = 1
secureフラグの有無。省略可能。 1の場合、secureフラグを付ける。 0の場合、secureフラグを付けない。 デフォルトは0。
httponly = 1
httponlyフラグの有無。省略可能。 1の場合、httponlyフラグを付ける。 0の場合、httponlyフラグを付けない。 デフォルトは0。 現状ではIEでしか意味が無い。
生の文字列でなく Tripletail::Form を扱うクッキークラス。
Copyright 2006 YMIRLINK Inc.
This framework is free software; you can redistribute it and/or modify it under the same terms as Perl itself
このフレームワークはフリーソフトウェアです。あなたは Perl と同じライセンスの 元で再配布及び変更を行うことが出来ます。
Address bug reports and comments to: tl@tripletail.jp
HP : http://tripletail.jp/
| Tripletail documentation | Contained in the Tripletail distribution. |
# ----------------------------------------------------------------------------- # Tripletail::RawCookie - æ±ç¨çãªã¯ããã¼ç®¡çãè¡ã # ----------------------------------------------------------------------------- package Tripletail::RawCookie; use strict; use warnings; use Tripletail; sub _POST_REQUEST_HOOK_PRIORITY() { -4_000_000 } # é åºã¯åããªã our $_INSTANCES = {}; # group => Tripletail::RawCookie 1; sub _getInstance { my $class = shift; my $group = shift; if(!defined($group)) { $group = 'Cookie'; } my $obj = $_INSTANCES->{$group}; if($obj) { return $obj; } $obj = $_INSTANCES->{$group} = $class->__new($group); # postRequestããã¯ã«ãä¿åããã¦ããã¤ã³ã¹ã¿ã³ã¹ãåé¤ãã颿°ã # ã¤ã³ã¹ãã¼ã«ãããããããªããã°FCGIã¢ã¼ãã§éå»ã®ãªã¯ã¨ã¹ãã®ã¯ããã¼ã # ãã¤ã¾ã§ãæ®ãã $TL->setHook( 'postRequest', _POST_REQUEST_HOOK_PRIORITY, sub { if(%$_INSTANCES) { %$_INSTANCES = (); #$TL->log('Tripletail::RawCookie' => 'Deleted a cookie object created in this request.'); } }, ); $obj; } sub get { my $this = shift; my $name = shift; if(!defined($name)) { die __PACKAGE__."#get: arg[1] is not defined. (第1弿°ãæå®ããã¦ãã¾ãã)\n"; } elsif(ref($name)) { die __PACKAGE__."#get: arg[1] is a reference. (第1弿°ããªãã¡ã¬ã³ã¹ã§ã)\n"; } if(my $data = $this->{set_cookies}{$name}) { # setã¾ãã¯deleteããã¦ããã return $data; } $this->__readEnvIfNeeded; $this->{got_cookies}{$name}; } sub set { my $this = shift; my $name = shift; my $value = shift; if(!defined($name)) { die __PACKAGE__."#set: arg[1] is not defined. (第1弿°ãæå®ããã¦ãã¾ãã)\n"; } elsif(ref($name)) { die __PACKAGE__."#set: arg[1] is a reference. (第1弿°ããªãã¡ã¬ã³ã¹ã§ã)\n"; } if(ref($value)) { die __PACKAGE__."#set: arg[2] is a reference. (第2弿°ããªãã¡ã¬ã³ã¹ã§ã)\n"; } $this->{set_cookies}{$name} = $value; $this; } sub delete { my $this = shift; my $name = shift; if(!defined($name)) { die __PACKAGE__."#delete: arg[1] is not defined. (第1弿°ãæå®ããã¦ãã¾ãã)\n"; } elsif(ref($name)) { die __PACKAGE__."#delete: arg[1] is a reference. (第1弿°ããªãã¡ã¬ã³ã¹ã§ã)\n"; } $this->{set_cookies}{$name} = undef; $this; } sub clear { my $this = shift; $this->__readEnvIfNeeded; foreach my $key (keys %{$this->{got_cookies}},keys %{$this->{set_cookies}}) { $this->{set_cookies}{$key} = undef; } $this; } sub _makeSetCookies { # Set-Cookie:ã®å¤ã¨ãã¦ä½¿ããããã«ã¯ããã¼ãæåååããã¯ã©ã¹ã¡ã½ããã # çµæã¯é åã§è¿ãããã my $class = shift; my @result; foreach my $this (values %$_INSTANCES) { push @result, $this->__makeSetCookie; } @result; } sub _isSecure { my $this = shift; $TL->INI->get($this->{group} => 'secure'); } sub __new { my $class = shift; my $group = shift; my $this = bless {} => $class; $this->{group} = $group; $this->{read} = undef; # ç°å¢å¤æ°ãããã¼ãããå¾ã¯çã $this->{got_cookies} = {}; # ãã¼ => å¤ (飽ãã¾ã§ãã£ãã·ã¥ã{set_cookies}ãåªå ãããã) $this->{set_cookies} = {}; # ãã¼ => å¤ (undefã®å¤ã¯ã¯ããã¼ã®åé¤) $this; } sub __readEnvIfNeeded { # $ENV{HTTP_COOKIE}ãèªãã my $this = shift; if($this->{read}) { return $this; } if(my $cookie = $ENV{HTTP_COOKIE}) { $cookie =~ tr/\x0a\x0d//d; my $str; foreach my $pair (split /;/, $cookie) { $pair =~ s/ //g; my ($key, $value) = split /=/, $pair; $this->{got_cookies}{$key} = $value; } } $this->{read} = 1; $this; } sub __cookieTime { my $this = shift; my $epoch = shift; local $[ = 0; my @DoW = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime $epoch; $year += 1900; sprintf '%s, %02d-%s-%04d %02d:%02d:%02d GMT', $DoW[$wday], $mday, $MoY[$mon], $year, $hour, $min, $sec; } sub __makeSetCookie { my $this = shift; my @result; while(my ($key, $value) = each %{$this->{set_cookies}}) { my @parts; push @parts, sprintf('%s=%s', $key, defined $value ? $value : ''); if(defined($value)) { if(defined(my $expires = $TL->INI->get($this->{group} => 'expires'))) { push @parts, "expires=".$this->__cookieTime( time + $TL->parsePeriod($expires)); } } else { push @parts, "expires=".$this->__cookieTime(0); } if(defined(my $path = $TL->INI->get($this->{group} => 'path'))) { push @parts, "path=$path"; } if(defined(my $domain = $TL->INI->get($this->{group} => 'domain'))) { push @parts, "domain=$domain"; } if($TL->INI->get($this->{group} => 'secure')) { push @parts, 'secure'; } if($TL->INI->get($this->{group} => 'httponly')) { push @parts, 'httponly'; } my $line = join '; ', @parts; if(length($line) > 1024 * 4) { die __PACKAGE__."#_makeSetCookies: the cookie became too large. Decrease its content. [$line] (ã¯ããã¼ã大ãããªãããã¾ãããä¿åãããã¼ã¿ãæ¸ããã¦ãã ãã)"; } push @result, $line; } @result; } __END__