1 | <?php |
||
13 | class HTTPResponse { |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected static $status_codes = array( |
||
19 | 100 => 'Continue', |
||
20 | 101 => 'Switching Protocols', |
||
21 | 200 => 'OK', |
||
22 | 201 => 'Created', |
||
23 | 202 => 'Accepted', |
||
24 | 203 => 'Non-Authoritative Information', |
||
25 | 204 => 'No Content', |
||
26 | 205 => 'Reset Content', |
||
27 | 206 => 'Partial Content', |
||
28 | 301 => 'Moved Permanently', |
||
29 | 302 => 'Found', |
||
30 | 303 => 'See Other', |
||
31 | 304 => 'Not Modified', |
||
32 | 305 => 'Use Proxy', |
||
33 | 307 => 'Temporary Redirect', |
||
34 | 400 => 'Bad Request', |
||
35 | 401 => 'Unauthorized', |
||
36 | 403 => 'Forbidden', |
||
37 | 404 => 'Not Found', |
||
38 | 405 => 'Method Not Allowed', |
||
39 | 406 => 'Not Acceptable', |
||
40 | 407 => 'Proxy Authentication Required', |
||
41 | 408 => 'Request Timeout', |
||
42 | 409 => 'Conflict', |
||
43 | 410 => 'Gone', |
||
44 | 411 => 'Length Required', |
||
45 | 412 => 'Precondition Failed', |
||
46 | 413 => 'Request Entity Too Large', |
||
47 | 414 => 'Request-URI Too Long', |
||
48 | 415 => 'Unsupported Media Type', |
||
49 | 416 => 'Request Range Not Satisfiable', |
||
50 | 417 => 'Expectation Failed', |
||
51 | 422 => 'Unprocessable Entity', |
||
52 | 429 => 'Too Many Requests', |
||
53 | 500 => 'Internal Server Error', |
||
54 | 501 => 'Not Implemented', |
||
55 | 502 => 'Bad Gateway', |
||
56 | 503 => 'Service Unavailable', |
||
57 | 504 => 'Gateway Timeout', |
||
58 | 505 => 'HTTP Version Not Supported', |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected static $redirect_codes = array( |
||
65 | 301, |
||
66 | 302, |
||
67 | 303, |
||
68 | 304, |
||
69 | 305, |
||
70 | 307 |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * @var int |
||
75 | */ |
||
76 | protected $statusCode = 200; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $statusDescription = "OK"; |
||
82 | |||
83 | /** |
||
84 | * HTTP Headers like "Content-Type: text/xml" |
||
85 | * |
||
86 | * @see http://en.wikipedia.org/wiki/List_of_HTTP_headers |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $headers = array( |
||
90 | "Content-Type" => "text/html; charset=utf-8", |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $body = null; |
||
97 | |||
98 | /** |
||
99 | * Create a new HTTP response |
||
100 | * |
||
101 | * @param string $body The body of the response |
||
102 | * @param int $statusCode The numeric status code - 200, 404, etc |
||
103 | * @param string $statusDescription The text to be given alongside the status code. |
||
104 | * See {@link setStatusCode()} for more information. |
||
105 | */ |
||
106 | public function __construct($body = null, $statusCode = null, $statusDescription = null) { |
||
112 | |||
113 | /** |
||
114 | * @param string $code |
||
115 | * @param string $description Optional. See {@link setStatusDescription()}. |
||
116 | * No newlines are allowed in the description. |
||
117 | * If omitted, will default to the standard HTTP description |
||
118 | * for the given $code value (see {@link $status_codes}). |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setStatusCode($code, $description = null) { |
||
135 | |||
136 | /** |
||
137 | * The text to be given alongside the status code ("reason phrase"). |
||
138 | * Caution: Will be overwritten by {@link setStatusCode()}. |
||
139 | * |
||
140 | * @param string $description |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setStatusDescription($description) { |
||
147 | |||
148 | /** |
||
149 | * @return int |
||
150 | */ |
||
151 | public function getStatusCode() { |
||
154 | |||
155 | /** |
||
156 | * @return string Description for a HTTP status code |
||
157 | */ |
||
158 | public function getStatusDescription() { |
||
161 | |||
162 | /** |
||
163 | * Returns true if this HTTP response is in error |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function isError() { |
||
170 | |||
171 | /** |
||
172 | * @param string $body |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setBody($body) { |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getBody() { |
||
186 | |||
187 | /** |
||
188 | * Add a HTTP header to the response, replacing any header of the same name. |
||
189 | * |
||
190 | * @param string $header Example: "Content-Type" |
||
191 | * @param string $value Example: "text/xml" |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function addHeader($header, $value) { |
||
198 | |||
199 | /** |
||
200 | * Return the HTTP header of the given name. |
||
201 | * |
||
202 | * @param string $header |
||
203 | * @returns string |
||
204 | */ |
||
205 | public function getHeader($header) { |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getHeaders() { |
||
218 | |||
219 | /** |
||
220 | * Remove an existing HTTP header by its name, |
||
221 | * e.g. "Content-Type". |
||
222 | * |
||
223 | * @param string $header |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function removeHeader($header) { |
||
230 | |||
231 | /** |
||
232 | * @param string $dest |
||
233 | * @param int $code |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function redirect($dest, $code = 302) { |
||
245 | |||
246 | /** |
||
247 | * Send this HTTPReponse to the browser |
||
248 | */ |
||
249 | public function output() { |
||
307 | |||
308 | /** |
||
309 | * Returns true if this response is "finished", that is, no more script execution should be done. |
||
310 | * Specifically, returns true if a redirect has already been requested |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | public function isFinished() { |
||
317 | |||
318 | } |
||
319 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: