## Apache Zeta Components Manual :: File Source for http.php

## Source for file http.php

Documentation is available at http.php

1. <?php
2. /**
3. * File containing the ezcMvcHttpResponseWriter class
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package MvcTools
26. */
28. /**
29. * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
30. *
31. * @package MvcTools
32. * @version //autogentag//
33. * @mainclass
34. */
35. class ezcMvcHttpResponseWriter extends ezcMvcResponseWriter
36. {
37. /**
38. * Contains the response struct
39. *
40. * @var ezcMvcResponse
41. */
42. protected $response ;
44. /**
45. * Contains an array of header name to value mappings
46. *
47. * @var array(string=>string)
48. */
49. public $headers ;
51. /**
52. * Creates a new ezcMvcHttpResponseWriter class to write $response
53. *
54. * @param ezcMvcResponse $response
55. */
56. public function __construct ( ezcMvcResponse $response )
57. {
58. $this-> response = $response;
59. $this-> headers = array();
60. }
62. /**
63. * Takes the raw protocol depending response body, and the protocol
64. * abstract response headers and forges a response to the client. Then it sends
65. * the assembled response to the client.
66. */
67. public function handleResponse ()
68. {
69. // process all headers
70. $this-> processStandardHeaders ();
71. if ( $this-> response -> cache instanceof ezcMvcResultCache )
72. {
73. $this-> processCacheHeaders ();
74. }
75. if ( $this-> response -> content instanceof ezcMvcResultContent )
76. {
77. $this-> processContentHeaders ();
78. }
80. // process the status headers through objects
81. if ( $this-> response -> status instanceof ezcMvcResultStatusObject )
82. {
83. $this-> response -> status ->process( $this );
84. }
86. // automatically add content-length header
87. $this-> headers ['Content-Length'] = strlen ( $this-> response -> body );
89. // write output
90. foreach ( $this-> headers as $header => $value )
91. {
92. header ( "$header: $value" );
93. }
94. // do cookies
95. foreach ( $this-> response -> cookies as $cookie )
96. {
97. $this->processCookie( $cookie );
98. }
99. echo $this-> response -> body ;
100. }
102. /**
103. * Takes a $cookie and uses PHP's setcookie() function to add cookies to the output stream.
104. *
105. * @param ezcMvcResultCookie $cookie
106. */
107. private function processCookie( ezcMvcResultCookie $cookie )
108. {
109. $args = array();
110. $args[] = $cookie->name;
111. $args[] = $cookie->value;
112. if ( $cookie->expire instanceof DateTime )
113. {
114. $args[] = $cookie->expire->format( 'U' );
115. }
116. else
117. {
118. $args[] = null;
119. }
120. $args[] = $cookie->domain;
121. $args[] = $cookie->path;
122. $args[] = $cookie->secure;
123. $args[] = $cookie->httpOnly;
124. call_user_func_array ( 'setcookie', $args );
125. }
127. /**
128. * Checks whether there is a DateTime object in $obj->$prop and sets a header accordingly.
129. *
130. * @param Object $obj
131. * @param string $prop
132. * @param string $headerName
133. * @param bool $default
134. */
135. private function doDate( $obj, $prop, $headerName, $default = false )
136. {
137. if ( $obj->$prop instanceof DateTime )
138. {
139. $headerDate = clone $obj->$prop;
140. $headerDate->setTimezone( new DateTimeZone( "UTC" ) );
141. $this-> headers [$headerName] = $headerDate->format( 'D, d M Y H:i:s \G\M\T' );
142. return;
143. }
145. if ( $default )
146. {
147. $headerDate = new DateTime( "UTC" );
148. $this-> headers [$headerName] = $headerDate->format( 'D, d M Y H:i:s \G\M\T' );
149. }
150. }
152. /**
153. * Processes the standard headers that are not subdivided into other structs.
154. */
155. protected function processStandardHeaders ()
156. {
157. $res = $this-> response ;
159. // generator
160. $this-> headers ['X-Powered-By'] = $res->generator !== ''
161. ? $res->generator
162. : "eZ Components MvcTools";
164. $this->doDate( $res, 'date', 'Date', true );
165. }
167. /**
168. * Processes the caching related headers.
169. */
170. protected function processCacheHeaders ()
171. {
172. $cache = $this-> response -> cache ;
174. if ( $cache->vary )
175. {
176. $this-> headers ['Vary'] = $cache->vary;
177. }
178. $this->doDate( $cache, 'expire', 'Expires' );
179. if ( count ( $cache->controls ) )
180. {
181. $this-> headers ['Cache-Control'] = join ( ', ', $cache->controls );
182. }
183. if ( $cache->pragma )
184. {
185. $this-> headers ['Pragma'] = $cache->pragma;
186. }
187. $this->doDate( $cache, 'lastModified', 'Last-Modified' );
188. }
190. /**
191. * Processes the content type related headers.
192. */
193. protected function processContentHeaders ()
194. {
195. $content = $this-> response -> content ;
196. $defaultContentType = 'text/html';
198. if ( $content->language )
199. {
200. $this-> headers ['Content-Language'] = $content->language;
201. }
202. if ( $content->type || $content->charset )
203. {
204. $contentType = $content->type ? $content->type : $defaultContentType;
205. if ( $content->charset )
206. {
207. $contentType .= '; charset=' . $content->charset;
208. }
209. $this-> headers ['Content-Type'] = $contentType;
210. }
211. if ( $content->encoding )
212. {
213. $this-> headers ['Content-Encoding'] = $content->encoding;
214. }
216. if ( $content->disposition instanceof ezcMvcResultContentDisposition )
217. {
218. $this-> processContentDispositionHeaders ( $content->disposition );
219. }
220. }
222. /**
223. * Processed the content disposition related headers.
224. *
225. * See http://tools.ietf.org/html/rfc2183#section-2, but implemented with limitations.
226. *
227. * @param ezcMvcResultContentDisposition $disp
228. */
229. protected function processContentDispositionHeaders ( ezcMvcResultContentDisposition $disp )
230. {
231. // type
232. $value = $disp->type;
234. // filename
235. if ( $disp->filename !== null )
236. {
237. $value .= "; filename";
238. if ( strpbrk ( $disp->filename,
239. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" ) === false )
240. {
241. // case 1: ASCII characters only
242. if ( strpbrk ( $disp->filename, '\(\)<>@,;:\\"/\[\]?= ' ) === false )
243. {
244. // case 1a: no tspecials
245. $value .= '=' . $disp->filename;
246. }
247. else
248. {
249. // case 1b: with tspecials
250. $value .= '="' . str_replace ( '"', '\"', $disp->filename ) . '"';
251. }
252. }
253. else
254. {
255. // case 2: non-ASCII characters (and thus UTF-8 encoded)
256. $value .= "*=utf-8''" . urlencode ( $disp->filename );
257. }
258. }
260. // dates
261. if ( $disp->creationDate !== null )
262. {
263. $value .= '; creation-date="' . $disp->creationDate->format( DateTime::RFC2822 ) . '"';
264. }
265. if ( $disp->modificationDate !== null )
266. {
267. $value .= '; modification-date="' . $disp->modificationDate->format( DateTime::RFC2822 ) . '"';
268. }
269. if ( $disp->readDate !== null )
270. {
271. $value .= '; read-date="' . $disp->readDate->format( DateTime::RFC2822 ) . '"';
272. }
274. // size
275. if ( $disp->size !== null )
276. {
277. $value .= "; size=" . (int) $disp->size;
278. }
280. $this-> headers ['Content-Disposition'] = $value;
281. }
282. }
283. ?>

Documentation generated by phpDocumentor 1.4.3