/
HTTPResponse.php
298 lines (264 loc) · 7.53 KB
/
HTTPResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Represents a response returned by a controller.
*
* @package framework
* @subpackage control
*/
class SS_HTTPResponse {
/**
* @var array
*/
protected static $status_codes = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Request Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
);
/**
* @var array
*/
protected static $redirect_codes = array(
301,
302,
303,
304,
305,
307
);
/**
* @var Int
*/
protected $statusCode = 200;
/**
* @var String
*/
protected $statusDescription = "OK";
/**
* HTTP Headers like "Content-Type: text/xml"
*
* @see http://en.wikipedia.org/wiki/List_of_HTTP_headers
* @var array
*/
protected $headers = array(
"Content-Type" => "text/html; charset=utf-8",
);
/**
* @var string
*/
protected $body = null;
/**
* Create a new HTTP response
*
* @param $body The body of the response
* @param $statusCode The numeric status code - 200, 404, etc
* @param $statusDescription The text to be given alongside the status code.
* See {@link setStatusCode()} for more information.
*/
function __construct($body = null, $statusCode = null, $statusDescription = null) {
$this->setBody($body);
if($statusCode) $this->setStatusCode($statusCode, $statusDescription);
}
/**
* @param String $code
* @param String $description Optional. See {@link setStatusDescription()}.
* No newlines are allowed in the description.
* If omitted, will default to the standard HTTP description
* for the given $code value (see {@link $status_codes}).
*/
function setStatusCode($code, $description = null) {
if(isset(self::$status_codes[$code])) $this->statusCode = $code;
else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING);
if($description) $this->statusDescription = $description;
else $this->statusDescription = self::$status_codes[$code];
}
/**
* The text to be given alongside the status code ("reason phrase").
* Caution: Will be overwritten by {@link setStatusCode()}.
*
* @param String $description
*/
function setStatusDescription($description) {
$this->statusDescription = $description;
}
/**
* @return Int
*/
function getStatusCode() {
return $this->statusCode;
}
/**
* @return string Description for a HTTP status code
*/
function getStatusDescription() {
return str_replace(array("\r","\n"), '', $this->statusDescription);
}
/**
* Returns true if this HTTP response is in error
*/
function isError() {
return $this->statusCode && ($this->statusCode < 200 || $this->statusCode > 399);
}
function setBody($body) {
$this->body = $body;
// Set content-length in bytes. Use mbstring to avoid problems with mb_internal_encoding() and mbstring.func_overload
$this->headers['Content-Length'] = mb_strlen($this->body,'8bit');
}
function getBody() {
return $this->body;
}
/**
* Add a HTTP header to the response, replacing any header of the same name.
*
* @param string $header Example: "Content-Type"
* @param string $value Example: "text/xml"
*/
function addHeader($header, $value) {
$this->headers[$header] = $value;
}
/**
* Return the HTTP header of the given name.
*
* @param string $header
* @returns string
*/
function getHeader($header) {
if(isset($this->headers[$header])) {
return $this->headers[$header];
} else {
return null;
}
}
/**
* @return array
*/
function getHeaders() {
return $this->headers;
}
/**
* Remove an existing HTTP header by its name,
* e.g. "Content-Type".
*
* @param unknown_type $header
*/
function removeHeader($header) {
if(isset($this->headers[$header])) unset($this->headers[$header]);
}
function redirect($dest, $code=302) {
if(!in_array($code, self::$redirect_codes)) $code = 302;
$this->setStatusCode($code);
$this->headers['Location'] = $dest;
}
/**
* Send this HTTPReponse to the browser
*/
function output() {
// Attach appropriate X-Include-JavaScript and X-Include-CSS headers
if(Director::is_ajax()) {
Requirements::include_in_response($this);
}
if(in_array($this->statusCode, self::$redirect_codes) && headers_sent($file, $line)) {
$url = $this->headers['Location'];
echo
"<p>Redirecting to <a href=\"$url\" title=\"Please click this link if your browser does not redirect you\">$url... (output started on $file, line $line)</a></p>
<meta http-equiv=\"refresh\" content=\"1; url=$url\" />
<script type=\"text/javascript\">setTimeout('window.location.href = \"$url\"', 50);</script>";
} else {
if(!headers_sent()) {
header($_SERVER['SERVER_PROTOCOL'] . " $this->statusCode " . $this->getStatusDescription());
foreach($this->headers as $header => $value) {
header("$header: $value", true, $this->statusCode);
}
}
// Only show error pages or generic "friendly" errors if the status code signifies
// an error, and the response doesn't have any body yet that might contain
// a more specific error description.
if(Director::isLive() && $this->isError() && !$this->body) {
Debug::friendlyError($this->statusCode, $this->getStatusDescription());
} else {
echo $this->body;
}
}
}
/**
* Returns true if this response is "finished", that is, no more script execution should be done.
* Specifically, returns true if a redirect has already been requested
*/
function isFinished() {
return in_array($this->statusCode, array(301, 302, 401, 403));
}
}
/**
* A {@link SS_HTTPResponse} encapsulated in an exception, which can interrupt the processing flow and be caught by the
* {@link RequestHandler} and returned to the user.
*
* Example Usage:
* <code>
* throw new SS_HTTPResponse_Exception('This request was invalid.', 400);
* throw new SS_HTTPResponse_Exception(new SS_HTTPResponse('There was an internal server error.', 500));
* </code>
*
* @package framework
* @subpackage control
*/
class SS_HTTPResponse_Exception extends Exception {
protected $response;
/**
* @see SS_HTTPResponse::__construct();
*/
public function __construct($body = null, $statusCode = null, $statusDescription = null) {
if($body instanceof SS_HTTPResponse) {
$this->setResponse($body);
} else {
$this->setResponse(new SS_HTTPResponse($body, $statusCode, $statusDescription));
}
parent::__construct($this->getResponse()->getBody(), $this->getResponse()->getStatusCode());
}
/**
* @return SS_HTTPResponse
*/
public function getResponse() {
return $this->response;
}
/**
* @param SS_HTTPResponse $response
*/
public function setResponse(SS_HTTPResponse $response) {
$this->response = $response;
}
}