3
* Flight: An extensible micro-framework.
5
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
6
* @license MIT, http://flightphp.com/license
12
* The Response class represents an HTTP response. The object
13
* contains the response headers, HTTP status code, and response
18
* @var int HTTP status
20
protected $status = 200;
23
* @var array HTTP headers
25
protected $headers = array();
28
* @var string HTTP response body
33
* @var array HTTP status codes
35
public static $codes = array(
37
101 => 'Switching Protocols',
43
203 => 'Non-Authoritative Information',
45
205 => 'Reset Content',
46
206 => 'Partial Content',
47
207 => 'Multi-Status',
48
208 => 'Already Reported',
52
300 => 'Multiple Choices',
53
301 => 'Moved Permanently',
56
304 => 'Not Modified',
59
307 => 'Temporary Redirect',
60
308 => 'Permanent Redirect',
63
401 => 'Unauthorized',
64
402 => 'Payment Required',
67
405 => 'Method Not Allowed',
68
406 => 'Not Acceptable',
69
407 => 'Proxy Authentication Required',
70
408 => 'Request Timeout',
73
411 => 'Length Required',
74
412 => 'Precondition Failed',
75
413 => 'Payload Too Large',
76
414 => 'URI Too Long',
77
415 => 'Unsupported Media Type',
78
416 => 'Range Not Satisfiable',
79
417 => 'Expectation Failed',
81
422 => 'Unprocessable Entity',
83
424 => 'Failed Dependency',
85
426 => 'Upgrade Required',
87
428 => 'Precondition Required',
88
429 => 'Too Many Requests',
90
431 => 'Request Header Fields Too Large',
92
500 => 'Internal Server Error',
93
501 => 'Not Implemented',
95
503 => 'Service Unavailable',
96
504 => 'Gateway Timeout',
97
505 => 'HTTP Version Not Supported',
98
506 => 'Variant Also Negotiates',
99
507 => 'Insufficient Storage',
100
508 => 'Loop Detected',
102
510 => 'Not Extended',
103
511 => 'Network Authentication Required'
107
* Sets the HTTP status of the response.
109
* @param int $code HTTP status code.
110
* @return object Self reference
111
* @throws \Exception If invalid status code
113
public function status($code = null) {
114
if ($code === null) {
115
return $this->status;
118
if (array_key_exists($code, self::$codes)) {
119
$this->status = $code;
122
throw new \Exception('Invalid status code.');
129
* Adds a header to the response.
131
* @param string|array $name Header name or array of names and values
132
* @param string $value Header value
133
* @return object Self reference
135
public function header($name, $value = null) {
136
if (is_array($name)) {
137
foreach ($name as $k => $v) {
138
$this->headers[$k] = $v;
142
$this->headers[$name] = $value;
149
* Returns the headers from the response
152
public function headers() {
153
return $this->headers;
157
* Writes content to the response body.
159
* @param string $str Response content
160
* @return object Self reference
162
public function write($str) {
169
* Clears the response.
171
* @return object Self reference
173
public function clear() {
175
$this->headers = array();
182
* Sets caching headers for the response.
184
* @param int|string $expires Expiration time
185
* @return object Self reference
187
public function cache($expires) {
188
if ($expires === false) {
189
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
190
$this->headers['Cache-Control'] = array(
191
'no-store, no-cache, must-revalidate',
192
'post-check=0, pre-check=0',
195
$this->headers['Pragma'] = 'no-cache';
198
$expires = is_int($expires) ? $expires : strtotime($expires);
199
$this->headers['Expires'] = gmdate('D, d M Y H:i:s', $expires) . ' GMT';
200
$this->headers['Cache-Control'] = 'max-age='.($expires - time());
201
if (isset($this->headers['Pragma']) && $this->headers['Pragma'] == 'no-cache'){
202
unset($this->headers['Pragma']);
209
* Sends HTTP headers.
211
* @return object Self reference
213
public function sendHeaders() {
214
// Send status code header
215
if (strpos(php_sapi_name(), 'cgi') !== false) {
220
self::$codes[$this->status]
229
(isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'),
231
self::$codes[$this->status]),
237
// Send other headers
238
foreach ($this->headers as $field => $value) {
239
if (is_array($value)) {
240
foreach ($value as $v) {
241
header($field.': '.$v, false);
245
header($field.': '.$value);
249
// Send content length
250
if (($length = strlen($this->body)) > 0) {
251
header('Content-Length: '.$length);
258
* Sends a HTTP response.
260
public function send() {
261
if (ob_get_length() > 0) {
265
if (!headers_sent()) {
266
$this->sendHeaders();