annotate lib/IMPL/Web/HttpResponse.pm @ 413:af8d359ee4cc ref20150831

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