annotate Lib/IMPL/Web/HttpResponse.pm @ 387:4cc6cc370fb2

DOM refactoring
author sergey
date Tue, 11 Feb 2014 01:13:47 +0400
parents 32aceba4ee6d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
213
sergey
parents:
diff changeset
1 use strict;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
2 package IMPL::Web::HttpResponse;
213
sergey
parents:
diff changeset
3
sergey
parents:
diff changeset
4 use CGI();
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
5 use IMPL::lang qw(:declare :hash);
213
sergey
parents:
diff changeset
6 use IMPL::declare {
sergey
parents:
diff changeset
7 require => {
sergey
parents:
diff changeset
8 Exception => 'IMPL::Exception',
sergey
parents:
diff changeset
9 ArgumentException => '-IMPL::InvalidArgumentException'
sergey
parents:
diff changeset
10 },
sergey
parents:
diff changeset
11 base => [
sergey
parents:
diff changeset
12 'IMPL::Object' => undef,
sergey
parents:
diff changeset
13 'IMPL::Object::Autofill' => '@_'
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
14 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
15 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
16 status => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
17 type => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
18 charset => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
19 cookies => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
20 headers => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
21 body => PROP_ALL
213
sergey
parents:
diff changeset
22 ]
sergey
parents:
diff changeset
23 };
sergey
parents:
diff changeset
24
sergey
parents:
diff changeset
25 sub CTOR {
sergey
parents:
diff changeset
26 my ($this) = @_;
sergey
parents:
diff changeset
27
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
28 $this->headers({}) unless $this->headers();
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
29 $this->cookies({}) unless $this->cookies();
213
sergey
parents:
diff changeset
30 }
sergey
parents:
diff changeset
31
sergey
parents:
diff changeset
32 sub PrintResponse {
sergey
parents:
diff changeset
33 my ($this,$out) = @_;
sergey
parents:
diff changeset
34
sergey
parents:
diff changeset
35 my $q = CGI->new({});
sergey
parents:
diff changeset
36
sergey
parents:
diff changeset
37 my %headers = %{$this->headers};
sergey
parents:
diff changeset
38
sergey
parents:
diff changeset
39 if(my $cookies = $this->cookies) {
sergey
parents:
diff changeset
40 $headers{-cookie} = [map _createCookie($_,$cookies->{$_}), keys %$cookies] if $cookies;
sergey
parents:
diff changeset
41 }
sergey
parents:
diff changeset
42
sergey
parents:
diff changeset
43 $headers{'-status'} = $this->status || '200 OK';
sergey
parents:
diff changeset
44 $headers{'-type'} = $this->type || 'text/html';
sergey
parents:
diff changeset
45
sergey
parents:
diff changeset
46 if(my $charset = $this->charset) {
sergey
parents:
diff changeset
47 $q->charset($charset);
sergey
parents:
diff changeset
48 binmode $out, ":encoding($charset)";
256
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 245
diff changeset
49 } else {
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 245
diff changeset
50 $q->charset('');
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 245
diff changeset
51 binmode $out;
213
sergey
parents:
diff changeset
52 }
sergey
parents:
diff changeset
53
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
54 print $out $q->header(\%headers);
213
sergey
parents:
diff changeset
55
sergey
parents:
diff changeset
56 if(my $body = $this->body) {
sergey
parents:
diff changeset
57 if(ref $body eq 'CODE') {
sergey
parents:
diff changeset
58 $body->($out);
sergey
parents:
diff changeset
59 } else {
sergey
parents:
diff changeset
60 print $out $body;
sergey
parents:
diff changeset
61 }
sergey
parents:
diff changeset
62 }
sergey
parents:
diff changeset
63 }
sergey
parents:
diff changeset
64
sergey
parents:
diff changeset
65 #used to map a pair name valie to a valid cookie object
sergey
parents:
diff changeset
66 sub _createCookie {
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
67 return UNIVERSAL::isa($_[1], 'CGI::Cookie')
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
68 ? $_[1]
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
69 : ( defined $_[1]
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
70 ? CGI::Cookie->new(-name => $_[0], -value => $_[1] )
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
71 : CGI::Cookie->new(-name => $_[0], -expires => '-1d', -value => '')
23daf2fae33a *security subsytem bugfixes
sergey
parents: 230
diff changeset
72 );
213
sergey
parents:
diff changeset
73 }
sergey
parents:
diff changeset
74
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
75 sub InternalError {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
76 my ($self,%args) = @_;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
77
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
78 $args{status} ||= '500 Internal Server Error';
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
79
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
80 return $self->new(%args);
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
81 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
82
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
83 sub Redirect {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
84 my ($self,%args) = @_;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
85
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
86 return $self->new(
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
87 status => $args{status} || '303 See other',
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
88 headers => {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
89 location => $args{location}
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
90 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
91 );
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
92 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
93
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
94 sub NoContent {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
95 my ($self,%args) = @_;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
96
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
97 return $self->new(
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
98 status => $args{status} || '204 No Content'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
99 );
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
100 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 239
diff changeset
101
213
sergey
parents:
diff changeset
102 1;
sergey
parents:
diff changeset
103
sergey
parents:
diff changeset
104 __END__
sergey
parents:
diff changeset
105
sergey
parents:
diff changeset
106 =pod
sergey
parents:
diff changeset
107
sergey
parents:
diff changeset
108 =head1 NAME
sergey
parents:
diff changeset
109
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
110 C<IMPL::Web::HttpResponse> - Результат обработки C<HTTP> запроса.
213
sergey
parents:
diff changeset
111
sergey
parents:
diff changeset
112 =head1 SYNOPSIS
sergey
parents:
diff changeset
113
sergey
parents:
diff changeset
114 =head1 DESCRIPTION
sergey
parents:
diff changeset
115
sergey
parents:
diff changeset
116 Базовый класс для ответов приложения на C<HTTP> запрос. Каждый вид ответа,
sergey
parents:
diff changeset
117 например
sergey
parents:
diff changeset
118
sergey
parents:
diff changeset
119 Данный объект используется для формирования и передачи данных C<HTTP> ответа
sergey
parents:
diff changeset
120 напрямую. Основными полями являются C<body> и C<status>.
sergey
parents:
diff changeset
121
sergey
parents:
diff changeset
122 Кроме свойств относящихся непосредственно к самому C<HTTP> ответу, данный объект
sergey
parents:
diff changeset
123 может содержать свойства относящиеся к процессу обработки запроса, например
sergey
parents:
diff changeset
124 механизму формирования представления.
sergey
parents:
diff changeset
125
sergey
parents:
diff changeset
126 =head1 MEMBERS
sergey
parents:
diff changeset
127
sergey
parents:
diff changeset
128 =head2 C<[get,set]status>
sergey
parents:
diff changeset
129
sergey
parents:
diff changeset
130 Статус который будет отправлен сервером клиенту, например, C<200 OK> или
sergey
parents:
diff changeset
131 C<204 No response>. Если не указан, то будет C<200 OK>.
sergey
parents:
diff changeset
132
sergey
parents:
diff changeset
133 =head2 C<[get,set]type>
sergey
parents:
diff changeset
134
sergey
parents:
diff changeset
135 Тип содержимого, которое будет передано клиенту, если не указано, будет
sergey
parents:
diff changeset
136 C<text/html>.
sergey
parents:
diff changeset
137
sergey
parents:
diff changeset
138 =head2 C<[get,set]charset>
sergey
parents:
diff changeset
139
sergey
parents:
diff changeset
140 Кодировка в которой будут переданны данные. Следует задавать если и только, если
sergey
parents:
diff changeset
141 передается текстовая информация. Если указана кодировка, то она будет
sergey
parents:
diff changeset
142 автоматически применена к потоку, который будет передан методу C<PrintResponse>.
sergey
parents:
diff changeset
143
sergey
parents:
diff changeset
144 =head2 C<[get,set]cookies>
sergey
parents:
diff changeset
145
sergey
parents:
diff changeset
146 Опционально. Ссылка на хеш с печеньками.
sergey
parents:
diff changeset
147
sergey
parents:
diff changeset
148 =head2 C<[get,set]headers>
sergey
parents:
diff changeset
149
sergey
parents:
diff changeset
150 Опционально. Ссылка на хеш с дополнительными полями заголовка ответа. Формат
sergey
parents:
diff changeset
151 имен полей как у модуля C<CGI>.
sergey
parents:
diff changeset
152
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
153 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
154
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
155 $response->header->{custom_header} = "my value";
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
156
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
157 #will produce the following header
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
158
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
159 Custom-header: my value
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
160
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
161 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
162
213
sergey
parents:
diff changeset
163 =head2 C<[get,set]body>
sergey
parents:
diff changeset
164
sergey
parents:
diff changeset
165 Тело ответа. Может быть как простой скаляр, который будет приведен к строке и
sergey
parents:
diff changeset
166 выдан в поток вывода метода C<PrintResponse>. Также может быть ссылкой на
sergey
parents:
diff changeset
167 процедуру, в таком случае будет вызвана эта процедура и ей будет передан
sergey
parents:
diff changeset
168 первым параметром поток для вывода тела ответа.
sergey
parents:
diff changeset
169
sergey
parents:
diff changeset
170 =head2 C<PrintResponse($outStream)>
sergey
parents:
diff changeset
171
sergey
parents:
diff changeset
172 Формирует заголовок и выводит ответ сервера в указанный параметром поток.
sergey
parents:
diff changeset
173
sergey
parents:
diff changeset
174 =cut