| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Egg::Response::Headers; | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt> | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # $Id: Headers.pm 337 2008-05-14 12:30:09Z lushe $ | 
| 6 |  |  |  |  |  |  | # | 
| 7 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 8 | 1 |  |  | 1 |  | 6 | use Carp qw/croak/; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 273 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '3.00'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new { | 
| 13 | 0 |  |  | 0 |  |  | my $class= shift; | 
| 14 | 0 |  |  |  |  |  | tie my %headers, 'Egg::Response::Headers::TieHash', @_; | 
| 15 | 0 |  |  |  |  |  | bless \%headers, $class; | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  | sub header { | 
| 18 | 0 |  |  | 0 |  |  | my $self= shift; | 
| 19 | 0 |  | 0 |  |  |  | my $key = shift || croak q{ I want key. }; | 
| 20 | 0 | 0 |  |  |  |  | return $self->{$key} unless @_; | 
| 21 | 0 |  |  |  |  |  | $self->{$key}= shift; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | sub remove { | 
| 24 | 0 |  |  | 0 |  |  | my $self= shift; | 
| 25 | 0 |  | 0 |  |  |  | my $key = shift || croak q{ I want key. }; | 
| 26 | 0 |  |  |  |  |  | CORE::delete($self->{$key}); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | *delete= \&remove; | 
| 29 |  |  |  |  |  |  | sub clear { | 
| 30 | 0 |  |  | 0 |  |  | my $self= shift; | 
| 31 | 0 |  |  |  |  |  | %{$self}= (); | 
|  | 0 |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | 1; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | package Egg::Response::Headers::TieHash; | 
| 36 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 37 | 1 |  |  | 1 |  | 629 | use Tie::Hash::Indexed; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | use Tie::Hash; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | our @ISA = 'Tie::ExtraHash'; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | my $ForwardRegex= qr{^(?:content_type|content_language|location|status)$}; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub TIEHASH { | 
| 45 |  |  |  |  |  |  | my($class, $response)= @_; | 
| 46 |  |  |  |  |  |  | tie my %param, 'Tie::Hash::Indexed'; | 
| 47 |  |  |  |  |  |  | bless [\%param, $response], $class; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | sub FETCH { | 
| 50 |  |  |  |  |  |  | my($self, $key, $org)= &_getkey; | 
| 51 |  |  |  |  |  |  | return $self->[1]->$key if $key=~m{$ForwardRegex}; | 
| 52 |  |  |  |  |  |  | $self->[0]{$key}; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | sub STORE { | 
| 55 |  |  |  |  |  |  | my($self, $key, $org, $value)= &_getkey; | 
| 56 |  |  |  |  |  |  | return $self->[1]->$key($value) if $key=~m{$ForwardRegex}; | 
| 57 |  |  |  |  |  |  | if ($value eq "") { | 
| 58 |  |  |  |  |  |  | delete($self->[0]{$key}) if exists($self->[0]{$key}); | 
| 59 |  |  |  |  |  |  | } else { | 
| 60 |  |  |  |  |  |  | if ($self->[0]{$key}) { | 
| 61 |  |  |  |  |  |  | ref($self->[0]{$key}[0]) eq 'ARRAY' | 
| 62 |  |  |  |  |  |  | ? do { push @{$self->[0]{$key}}, [$org, $value] } | 
| 63 |  |  |  |  |  |  | : do { $self->[0]{$key}= [$self->[0]{$key}, [$org, $value]] }; | 
| 64 |  |  |  |  |  |  | } else { | 
| 65 |  |  |  |  |  |  | $self->[0]{$key}= [$org, $value]; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | sub DELETE { | 
| 70 |  |  |  |  |  |  | my($self, $key)= &_getkey; | 
| 71 |  |  |  |  |  |  | delete($self->[0]{$key}); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | sub EXISTS { | 
| 74 |  |  |  |  |  |  | my($self, $key)= &_getkey; | 
| 75 |  |  |  |  |  |  | exists($self->[0]{$key}); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | sub _getkey { | 
| 78 |  |  |  |  |  |  | my($self, $org)= splice @_, 0, 2; | 
| 79 |  |  |  |  |  |  | $org=~s{_} [-]g; | 
| 80 |  |  |  |  |  |  | my $key= lc($org); | 
| 81 |  |  |  |  |  |  | $key=~s{-} [_]g; | 
| 82 |  |  |  |  |  |  | ($self, $key, $org, @_); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | 1; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | __END__ | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 NAME | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Egg::Response::Headers - Response header class for Egg. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # The response header is set. | 
| 96 |  |  |  |  |  |  | $e->response->headers->{'X-Header'}= 'hoge'; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | # The response header is set. | 
| 99 |  |  |  |  |  |  | $e->response->headers->header( 'X-Header' => 'hoge' ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # The response header is deleted. | 
| 102 |  |  |  |  |  |  | $e->response->headers->remove('X-Header'); | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # All the response headers are clear. | 
| 105 |  |  |  |  |  |  | $e->response->headers->clear; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | It is make a response a header class only for L<Egg::Response>. | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head1 METHODS | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 new | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Constructor. | 
| 116 |  |  |  |  |  |  | L<Egg::Response::Headers::TieHash> The object is returned drinking. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | my $headers= $e->response->headers; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | The value becomes ARRAY reference of the following content. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =over 4 | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =item * Original name. Because lc is done as for the key, former name is preserved. | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =item * Value of header. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =back | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head2 header ([KEY], [VALUE]) | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | KEY is always necessary. | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | The value is set when VALUE is given, and the content corresponding to KEY is | 
| 135 |  |  |  |  |  |  | returned when omitting it. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | my $hoge= $headers->header('X-Hoge'); | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | $headers->header( 'X-Hoge' => 'foo' ); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head2 remove ([KEY]) | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | The header corresponding to KEY is deleted. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | $headers->remove('X-Hoge'); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =over 4 | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =item * Alias = delete | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =back | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head2 clear | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | All set headers are cleared. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | $headers->clear; | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | L<Egg::Release>, | 
| 162 |  |  |  |  |  |  | L<Egg::Request>, | 
| 163 |  |  |  |  |  |  | L<Tie::Hash>, | 
| 164 |  |  |  |  |  |  | L<Tie::Hash::Indexed>, | 
| 165 |  |  |  |  |  |  | L<Carp>, | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =head1 AUTHOR | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Masatoshi Mizuno, E<lt>lusheE<64>cpan.orgE<gt> | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 176 |  |  |  |  |  |  | it under the same terms as Perl itself, either Perl version 5.8.6 or, | 
| 177 |  |  |  |  |  |  | at your option, any later version of Perl 5 you may have available. | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =cut | 
| 180 |  |  |  |  |  |  |  |