Mercurial > pub > Impl
annotate Lib/IMPL/Web/HttpResponse.pm @ 234:2530d1bb9638
sync
author | sergey |
---|---|
date | Thu, 11 Oct 2012 20:11:45 +0400 |
parents | 6d8092d8ce1b |
children | 23daf2fae33a |
rev | line source |
---|---|
213 | 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 | 3 |
4 use CGI(); | |
5 use IMPL::lang qw(:declare); | |
6 use IMPL::declare { | |
7 require => { | |
8 Exception => 'IMPL::Exception', | |
9 ArgumentException => '-IMPL::InvalidArgumentException' | |
10 }, | |
11 base => [ | |
12 'IMPL::Object' => undef, | |
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 | 22 ] |
23 }; | |
24 | |
25 sub CTOR { | |
26 my ($this) = @_; | |
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 | 30 } |
31 | |
32 sub PrintResponse { | |
33 my ($this,$out) = @_; | |
34 | |
35 my $q = CGI->new({}); | |
36 | |
37 my %headers = %{$this->headers}; | |
38 | |
39 if(my $cookies = $this->cookies) { | |
40 $headers{-cookie} = [map _createCookie($_,$cookies->{$_}), keys %$cookies] if $cookies; | |
41 } | |
42 | |
43 $headers{'-status'} = $this->status || '200 OK'; | |
44 $headers{'-type'} = $this->type || 'text/html'; | |
45 | |
46 if(my $charset = $this->charset) { | |
47 $q->charset($charset); | |
48 binmode $out, ":encoding($charset)"; | |
49 } | |
50 | |
230 | 51 print $out $q->header(\%headers); |
213 | 52 |
53 if(my $body = $this->body) { | |
54 if(ref $body eq 'CODE') { | |
55 $body->($out); | |
56 } else { | |
57 print $out $body; | |
58 } | |
59 } | |
60 } | |
61 | |
62 #used to map a pair name valie to a valid cookie object | |
63 sub _createCookie { | |
64 return UNIVERSAL::isa($_[1], 'CGI::Cookie') ? $_[1] : CGI::Cookie->new(-name => $_[0], -value => $_[1] ); | |
65 } | |
66 | |
230 | 67 sub InternalError { |
68 my ($self,%args) = @_; | |
69 | |
70 $args{status} ||= '500 Internal Server Error'; | |
71 | |
72 return $self->new(%args); | |
73 } | |
74 | |
75 sub Redirect { | |
76 my ($self,%args) = @_; | |
77 | |
78 return $self->new( | |
79 status => $args{status} || '303 See other', | |
80 headers => { | |
81 location => $args{location} | |
82 } | |
83 ); | |
84 } | |
85 | |
213 | 86 1; |
87 | |
88 __END__ | |
89 | |
90 =pod | |
91 | |
92 =head1 NAME | |
93 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
94 C<IMPL::Web::HttpResponse> - Результат обработки C<HTTP> запроса. |
213 | 95 |
96 =head1 SYNOPSIS | |
97 | |
98 =head1 DESCRIPTION | |
99 | |
100 Базовый класс для ответов приложения на C<HTTP> запрос. Каждый вид ответа, | |
101 например | |
102 | |
103 Данный объект используется для формирования и передачи данных C<HTTP> ответа | |
104 напрямую. Основными полями являются C<body> и C<status>. | |
105 | |
106 Кроме свойств относящихся непосредственно к самому C<HTTP> ответу, данный объект | |
107 может содержать свойства относящиеся к процессу обработки запроса, например | |
108 механизму формирования представления. | |
109 | |
110 =head1 MEMBERS | |
111 | |
112 =head2 C<[get,set]status> | |
113 | |
114 Статус который будет отправлен сервером клиенту, например, C<200 OK> или | |
115 C<204 No response>. Если не указан, то будет C<200 OK>. | |
116 | |
117 =head2 C<[get,set]type> | |
118 | |
119 Тип содержимого, которое будет передано клиенту, если не указано, будет | |
120 C<text/html>. | |
121 | |
122 =head2 C<[get,set]charset> | |
123 | |
124 Кодировка в которой будут переданны данные. Следует задавать если и только, если | |
125 передается текстовая информация. Если указана кодировка, то она будет | |
126 автоматически применена к потоку, который будет передан методу C<PrintResponse>. | |
127 | |
128 =head2 C<[get,set]cookies> | |
129 | |
130 Опционально. Ссылка на хеш с печеньками. | |
131 | |
132 =head2 C<[get,set]headers> | |
133 | |
134 Опционально. Ссылка на хеш с дополнительными полями заголовка ответа. Формат | |
135 имен полей как у модуля C<CGI>. | |
136 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
137 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
138 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
139 $response->header->{custom_header} = "my value"; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
140 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
141 #will produce the following header |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
142 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
143 Custom-header: my value |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
144 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
145 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
146 |
213 | 147 =head2 C<[get,set]body> |
148 | |
149 Тело ответа. Может быть как простой скаляр, который будет приведен к строке и | |
150 выдан в поток вывода метода C<PrintResponse>. Также может быть ссылкой на | |
151 процедуру, в таком случае будет вызвана эта процедура и ей будет передан | |
152 первым параметром поток для вывода тела ответа. | |
153 | |
154 =head2 C<PrintResponse($outStream)> | |
155 | |
156 Формирует заголовок и выводит ответ сервера в указанный параметром поток. | |
157 | |
158 =cut |