comparison Lib/IMPL/Web/Application/Response.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents d1676be8afcc
children f534a60d5b01
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
12 use IMPL::Class::Property; 12 use IMPL::Class::Property;
13 13
14 #todo: add binary method to set a binary encoding, set it automatic when type isn't a text 14 #todo: add binary method to set a binary encoding, set it automatic when type isn't a text
15 15
16 BEGIN { 16 BEGIN {
17 # автозаполнение буде происходить в порядке объявления 17 # автозаполнение буде происходить в порядке объявления
18 public property query => prop_get | owner_set; # cgi query 18 public property query => prop_get | owner_set; # cgi query
19 public property status => prop_all, { validator => \&_checkHeaderPrinted }; 19 public property status => prop_all, { validator => \&_checkHeaderPrinted };
20 public property contentType => prop_all, { validator => \&_checkHeaderPrinted }; # String 20 public property contentType => prop_all, { validator => \&_checkHeaderPrinted }; # String
21 public property charset => { get => \&_charset, set => \&_charset }, { validator => \&_checkHeaderPrinted }; 21 public property charset => { get => \&_charset, set => \&_charset }, { validator => \&_checkHeaderPrinted };
22 public property expires => prop_all, { validator => \&_checkHeaderPrinted }; 22 public property expires => prop_all, { validator => \&_checkHeaderPrinted };
23 public property cookies => prop_all, { validator => \&_checkHeaderPrinted }; # Hash 23 public property cookies => prop_all, { validator => \&_checkHeaderPrinted }; # Hash
24 24
25 public property buffered => prop_all, { validator => \&_canChangeBuffer }; # Boolean 25 public property buffered => prop_all, { validator => \&_canChangeBuffer }; # Boolean
26 public property streamOut => prop_get | owner_set; # stream 26 public property streamOut => prop_get | owner_set; # stream
27 public property streamBody => {get => \&getStreamBody }; # stream 27 public property streamBody => {get => \&getStreamBody }; # stream
28 public property isHeaderPrinted => prop_get | owner_set; # Boolean 28 public property isHeaderPrinted => prop_get | owner_set; # Boolean
29 29
30 private property _bufferBody => prop_all; 30 private property _bufferBody => prop_all;
31 private property _streamBody => prop_all; 31 private property _streamBody => prop_all;
32 } 32 }
33 33
34 __PACKAGE__->PassThroughArgs; 34 __PACKAGE__->PassThroughArgs;
35 35
36 our %CTOR = ( 36 our %CTOR = (
37 'IMPL::Object::Autofill' => sub { 37 'IMPL::Object::Autofill' => sub {
38 my %args = @_; 38 my %args = @_;
39 39
40 $args{query} = CGI->new($args{query} || {}); 40 $args{query} = CGI->new($args{query} || {});
41 41
42 %args; 42 %args;
43 } 43 }
44 ); 44 );
45 45
46 sub CTOR { 46 sub CTOR {
47 my ($this,%args) = @_; 47 my ($this,%args) = @_;
48 48
49 if (lc $this->streamOut eq 'memory') { 49 if (lc $this->streamOut eq 'memory') {
50 my $dummy = ''; 50 my $dummy = '';
51 open my $hout, '>:encoding(utf8)', \$dummy or die new IMPL::Exception("Failed to create memory stream",$!); 51 open my $hout, '>:encoding(utf8)', \$dummy or die new IMPL::Exception("Failed to create memory stream",$!);
52 $this->streamOut($hout); 52 $this->streamOut($hout);
53 } elsif (not $this->streamOut) { 53 } elsif (not $this->streamOut) {
54 $this->streamOut(*STDOUT); 54 $this->streamOut(*STDOUT);
55 } else { 55 } else {
56 die new IMPL::InvalidArgumentException("Invalid parameter value",$this->streamOut); 56 die new IMPL::InvalidArgumentException("Invalid parameter value",$this->streamOut);
57 } 57 }
58 58
59 $this->buffered(1) unless defined $this->buffered; 59 $this->buffered(1) unless defined $this->buffered;
60 binmode $this->streamOut, ":encoding(".$this->charset.")"; 60 binmode $this->streamOut, ":encoding(".$this->charset.")";
61 } 61 }
62 62
63 sub _checkHeaderPrinted { 63 sub _checkHeaderPrinted {
64 my ($this,$value) = @_; 64 my ($this,$value) = @_;
65 65
66 die new IMPL::InvalidOperationException() if $this->isHeaderPrinted; 66 die new IMPL::InvalidOperationException() if $this->isHeaderPrinted;
67 } 67 }
68 68
69 sub _canChangeBuffer { 69 sub _canChangeBuffer {
70 my ($this,$value) = @_; 70 my ($this,$value) = @_;
71 71
72 die new IMPL::InvalidOperationException() if $this->isHeaderPrinted or $this->_streamBody; 72 die new IMPL::InvalidOperationException() if $this->isHeaderPrinted or $this->_streamBody;
73 } 73 }
74 74
75 sub _charset { 75 sub _charset {
76 my $this = shift; 76 my $this = shift;
77 77
78 if (@_) { 78 if (@_) {
79 my $charset = $this->query->charset(@_); 79 my $charset = $this->query->charset(@_);
80 80
81 my $hout = $this->streamOut; 81 my $hout = $this->streamOut;
82 82
83 binmode $hout; 83 binmode $hout;
84 binmode $hout, ":encoding($charset)"; 84 binmode $hout, ":encoding($charset)";
85 85
86 return $charset; 86 return $charset;
87 } else { 87 } else {
88 return $this->query->charset; 88 return $this->query->charset;
89 } 89 }
90 } 90 }
91 91
92 sub _PrintHeader { 92 sub _PrintHeader {
93 my ($this) = @_; 93 my ($this) = @_;
94 94
95 unless ($this->isHeaderPrinted) { 95 unless ($this->isHeaderPrinted) {
96 $this->isHeaderPrinted(1); 96 $this->isHeaderPrinted(1);
97 97
98 my %opt; 98 my %opt;
99 99
100 $opt{-type} = $this->contentType if $this->contentType; 100 $opt{-type} = $this->contentType if $this->contentType;
101 $opt{-status} = $this->status if $this->status; 101 $opt{-status} = $this->status if $this->status;
102 $opt{-expires} = $this->expires if $this->expires; 102 $opt{-expires} = $this->expires if $this->expires;
103 103
104 my $refCookies = $this->cookies; 104 my $refCookies = $this->cookies;
105 $opt{-cookie} = [map _createCookie($_,$refCookies->{$_}), keys %$refCookies] if $refCookies; 105 $opt{-cookie} = [map _createCookie($_,$refCookies->{$_}), keys %$refCookies] if $refCookies;
106 106
107 my $hOut = $this->streamOut; 107 my $hOut = $this->streamOut;
108 108
109 print $hOut $this->query->header( 109 print $hOut $this->query->header(
110 %opt 110 %opt
111 ); 111 );
112 } 112 }
113 } 113 }
114 114
115 sub _createCookie { 115 sub _createCookie {
116 return UNIVERSAL::isa($_[1], 'CGI::Cookie') ? $_[1] : CGI::Cookie->new(-name => $_[0], -value => $_[1] ); 116 return UNIVERSAL::isa($_[1], 'CGI::Cookie') ? $_[1] : CGI::Cookie->new(-name => $_[0], -value => $_[1] );
117 } 117 }
118 118
119 sub setCookie { 119 sub setCookie {
120 my ($this,$name,$value) = @_; 120 my ($this,$name,$value) = @_;
121 121
122 unless ($this->cookies) { 122 unless ($this->cookies) {
123 $this->cookies({$name,$value}); 123 $this->cookies({$name,$value});
124 } else { 124 } else {
125 $this->_checkHeaderPrinted(); 125 $this->_checkHeaderPrinted();
126 $this->cookies->{$name} = $value; 126 $this->cookies->{$name} = $value;
127 } 127 }
128 return $value; 128 return $value;
129 } 129 }
130 130
131 sub getStreamBody { 131 sub getStreamBody {
132 my ($this) = @_; 132 my ($this) = @_;
133 133
134 return undef unless $this->streamOut; 134 return undef unless $this->streamOut;
135 135
136 unless ($this->_streamBody) { 136 unless ($this->_streamBody) {
137 if ($this->buffered) { 137 if ($this->buffered) {
138 my $buffer = ""; 138 my $buffer = "";
139 139
140 $this->_bufferBody(\$buffer); 140 $this->_bufferBody(\$buffer);
141 141
142 open my $hBody, ">:encoding(utf-8)", \$buffer or die new IMPL::Exception("Failed to create buffer",$!); 142 open my $hBody, ">:encoding(utf-8)", \$buffer or die new IMPL::Exception("Failed to create buffer",$!);
143 143
144 Encode::_utf8_on($buffer); 144 Encode::_utf8_on($buffer);
145 145
146 $this->_streamBody($hBody); 146 $this->_streamBody($hBody);
147 } else { 147 } else {
148 $this->_PrintHeader(); 148 $this->_PrintHeader();
149 $this->_streamBody($this->streamOut); 149 $this->_streamBody($this->streamOut);
150 } 150 }
151 } 151 }
152 152
153 return $this->_streamBody; 153 return $this->_streamBody;
154 } 154 }
155 155
156 sub Complete { 156 sub Complete {
157 my ($this) = @_; 157 my ($this) = @_;
158 158
159 return 0 unless $this->streamOut; 159 return 0 unless $this->streamOut;
160 160
161 my $hOut = $this->streamOut; 161 my $hOut = $this->streamOut;
162 162
163 $this->_PrintHeader(); 163 $this->_PrintHeader();
164 164
165 close $this->_streamBody(); 165 close $this->_streamBody();
166 166
167 if ($this->buffered) { 167 if ($this->buffered) {
168 print $hOut ${$this->_bufferBody}; 168 print $hOut ${$this->_bufferBody};
169 } 169 }
170 170
171 $this->_bufferBody(undef); 171 $this->_bufferBody(undef);
172 $this->streamOut(undef); 172 $this->streamOut(undef);
173 173
174 return 1; 174 return 1;
175 } 175 }
176 176
177 sub Discard { 177 sub Discard {
178 my ($this) = @_; 178 my ($this) = @_;
179 179
180 carp "Discarding sent response" if $this->isHeaderPrinted; 180 carp "Discarding sent response" if $this->isHeaderPrinted;
181 181
182 $this->_streamBody(undef); 182 $this->_streamBody(undef);
183 $this->_bufferBody(undef); 183 $this->_bufferBody(undef);
184 $this->streamOut(undef); 184 $this->streamOut(undef);
185 } 185 }
186 186
187 1; 187 1;
188 188
189 __END__ 189 __END__