File Coverage

lib/Egg/Response/TieCookie.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 16 0.0
condition 0 15 0.0
subroutine 4 8 50.0
pod n/a
total 16 72 22.2


line stmt bran cond sub pod time code
1             package Egg::Response::TieCookie;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: TieCookie.pm 338 2008-05-19 11:22:55Z lushe $
6             #
7 1     1   393 use strict;
  1         2  
  1         35  
8 1     1   845 use Tie::Hash;
  1         791  
  1         11  
9              
10             our $VERSION = '3.01';
11              
12             our @ISA = 'Tie::ExtraHash';
13              
14             my $COOKIE = 0;
15             my $SECURE = 1;
16             my $DEFAULT = 2;
17              
18             sub TIEHASH {
19 0     0     my($class, $e)= @_;
20 0   0       bless [{}, $e->request->secure,
21             ($e->config->{cookie_default} || {}) ], $class;
22             }
23             sub STORE {
24 0     0     my $self= shift;
25 0   0       my $key = shift || return 0;
26 0 0         my $hash= $_[0] ? do {
27 0 0         ref($_[0]) ? do {
28 0 0         ref($_[0]) eq 'HASH' ? $_[0]: return do {
29 0           my $add= { obj=> $_[0] };
30 0 0         if (my $tmp= $self->[$COOKIE]{$key}) {
31 0           ref($tmp) eq 'ARRAY' ? do { push @$tmp, $add }
32 0 0         : do { $self->[$COOKIE]{$key}= [$tmp, $add] };
  0            
33             } else {
34 0           $self->[$COOKIE]{$key}= $add;
35             }
36             };
37             }: { value=> $_[0] };
38             }: { value => 0 };
39              
40 0 0         $hash->{value}= "" unless exists($hash->{value});
41 0   0       $hash->{name} ||= $key;
42              
43             $hash->{$_} ||= $self->[$DEFAULT]{$_} || undef
44 0   0       for qw/ domain expires path /;
      0        
45              
46 0 0 0       if (! defined($hash->{secure}) and $self->[$SECURE]) {
47 0 0         $hash->{secure}= defined($self->[$DEFAULT]{secure})
48             ? $self->[$DEFAULT]{secure}: 1;
49             }
50 0           $self->[$COOKIE]{$key}= Egg::Response::FetchCookie->new($hash);
51             }
52 0     0     sub _clear { $_[0]->[$COOKIE]= {} }
53              
54             package Egg::Response::FetchCookie;
55 1     1   354 use strict;
  1         2  
  1         32  
56 1     1   5 use base qw/ Class::Accessor::Fast /;
  1         2  
  1         119  
57              
58             __PACKAGE__->mk_accessors
59             (qw/ name value path domain expires secure max_age httponly /);
60              
61 0     0     sub new { bless $_[1], $_[0] }
62              
63             1;
64              
65             __END__
66              
67             =head1 NAME
68              
69             Egg::Response::TieCookie? - A class that preserves set Cookie.
70              
71             =head1 SYNOPSIS
72              
73             $e->cookies->{hoge}= 'boo';
74            
75             $e->cookies->{hoge}= {
76             value => 'boo',
77             path => '/home',
78             domain => 'mydomain',
79             expires => '+1d',
80             secure => 1,
81             };
82              
83             =head1 DESCRIPTION
84              
85             It is a class returned by the cookies method of L<Egg::Response>.
86              
87             Information to generate the Set-Cookie header is preserved.
88              
89             The set value is L<Egg::Response::FetchCookie> of the HASH reference base.
90             It is an object.
91              
92             The key shown in name, value, and the configuration is used to refer to the set
93             value.
94              
95             my $cookies= $e->response->cookies;
96            
97             $cookies->name or $cookies->{name} # cookie ̾¤Î»²¾È
98             $cookies->value or $cookies->{value} # ÀßÄêÃͤλ²¾È
99             $cookies->path or $cookies->{path}
100             $cookies->domain or $cookies->{domain}
101             $cookies->expires or $cookies->{expires}
102             $cookies->secure or $cookies->{secure}
103              
104             =head1 CONFIGURATION
105              
106             Cookie_default of the configuration of the project is assumed to be a default value.
107              
108             cookie_default=> {
109             path => '/',
110             domain => 'mydomain',
111             expires => '+1M',
112             secure => 1,
113             },
114              
115             =head2 path
116              
117             It is passing that enables the reference to Cookie.
118              
119             =head2 domain
120              
121             It is a domain that enables the reference to Cookie.
122              
123             =head2 expires
124              
125             It is validity term of Cookie. It specifies it by the form that expires of
126             L<CGI::Util> accepts.
127              
128             expires => '+1m' # 1 minute
129             expires => '+1h' # 1 hour
130             expires => '+1d' # 1 day
131             expires => '+1M' # 1 month
132             expires => '+1y' # 1 year
133              
134             Please note the desire that there is a thing not accepted either when lengthening
135             it too much by the specification of Cookie.
136             Cookie comes always to be annulled because past time will be given when giving it
137             by the minus.
138              
139             =head2 secure
140              
141             It makes it to Cookie with the secure flag.
142              
143             However, if it is a usual access in SSL without, this setting is disregarded.
144             When Cookie is issued only when it is accessed with SSL, it is necessary to process
145             it on own code side.
146              
147             =head1 SEE ALSO
148              
149             L<Egg::Release>,
150             L<Egg::Response>,
151             L<Tie::Hash>,
152              
153             =head1 AUTHOR
154              
155             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
160              
161             This library is free software; you can redistribute it and/or modify
162             it under the same terms as Perl itself, either Perl version 5.8.6 or,
163             at your option, any later version of Perl 5 you may have available.
164              
165             =cut
166