Response.php 18.8 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\web\HttpException;
Qiang Xue committed
12
use yii\base\InvalidParamException;
Qiang Xue committed
13
use yii\helpers\FileHelper;
Qiang Xue committed
14
use yii\helpers\Html;
Qiang Xue committed
15
use yii\helpers\Json;
Qiang Xue committed
16
use yii\helpers\SecurityHelper;
17
use yii\helpers\StringHelper;
Qiang Xue committed
18 19 20

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
21
 * @author Carsten Brandt <mail@cebe.cc>
Qiang Xue committed
22 23 24 25
 * @since 2.0
 */
class Response extends \yii\base\Response
{
Qiang Xue committed
26 27 28 29 30 31 32
	/**
	 * @var integer the HTTP status code that should be used when redirecting in AJAX mode.
	 * This is used by [[redirect()]]. A 2xx code should normally be used for this purpose
	 * so that the AJAX handler will treat the response as a success.
	 * @see redirect
	 */
	public $ajaxRedirectCode = 278;
Qiang Xue committed
33 34 35 36
	/**
	 * @var string
	 */
	public $content;
Qiang Xue committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * @var string
	 */
	public $statusText;
	/**
	 * @var string the charset to use. If not set, [[\yii\base\Application::charset]] will be used.
	 */
	public $charset;
	/**
	 * @var string the version of the HTTP protocol to use
	 */
	public $version = '1.0';
	/**
	 * @var array list of HTTP status codes and the corresponding texts
	 */
Qiang Xue committed
52
	public static $httpStatuses = array(
Qiang Xue committed
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
		100 => 'Continue',
		101 => 'Switching Protocols',
		102 => 'Processing',
		118 => 'Connection timed out',
		200 => 'OK',
		201 => 'Created',
		202 => 'Accepted',
		203 => 'Non-Authoritative',
		204 => 'No Content',
		205 => 'Reset Content',
		206 => 'Partial Content',
		207 => 'Multi-Status',
		208 => 'Already Reported',
		210 => 'Content Different',
		226 => 'IM Used',
		300 => 'Multiple Choices',
		301 => 'Moved Permanently',
		302 => 'Found',
		303 => 'See Other',
		304 => 'Not Modified',
		305 => 'Use Proxy',
		306 => 'Reserved',
		307 => 'Temporary Redirect',
		308 => 'Permanent Redirect',
		310 => 'Too many Redirect',
		400 => 'Bad Request',
		401 => 'Unauthorized',
		402 => 'Payment Required',
		403 => 'Forbidden',
		404 => 'Not Found',
		405 => 'Method Not Allowed',
		406 => 'Not Acceptable',
		407 => 'Proxy Authentication Required',
		408 => 'Request Time-out',
		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 => 'Requested range unsatisfiable',
		417 => 'Expectation failed',
96
		418 => 'I\'m a teapot',
Qiang Xue committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		422 => 'Unprocessable entity',
		423 => 'Locked',
		424 => 'Method failure',
		425 => 'Unordered Collection',
		426 => 'Upgrade Required',
		428 => 'Precondition Required',
		429 => 'Too Many Requests',
		431 => 'Request Header Fields Too Large',
		449 => 'Retry With',
		450 => 'Blocked by Windows Parental Controls',
		500 => 'Internal Server Error',
		501 => 'Not Implemented',
		502 => 'Bad Gateway ou Proxy Error',
		503 => 'Service Unavailable',
		504 => 'Gateway Time-out',
		505 => 'HTTP Version not supported',
		507 => 'Insufficient storage',
		508 => 'Loop Detected',
		509 => 'Bandwidth Limit Exceeded',
		510 => 'Not Extended',
		511 => 'Network Authentication Required',
	);

120 121 122
	/**
	 * @var integer the HTTP status code to send with the response.
	 */
Qiang Xue committed
123
	private $_statusCode;
Qiang Xue committed
124 125 126
	/**
	 * @var HeaderCollection
	 */
Qiang Xue committed
127 128
	private $_headers;

Qiang Xue committed
129 130 131 132 133 134 135 136

	public function init()
	{
		if ($this->charset === null) {
			$this->charset = Yii::$app->charset;
		}
	}

Qiang Xue committed
137 138 139
	public function begin()
	{
		parent::begin();
Qiang Xue committed
140
		$this->beginBuffer();
Qiang Xue committed
141 142 143 144
	}

	public function end()
	{
Qiang Xue committed
145
		$this->content .= $this->endBuffer();
Qiang Xue committed
146 147 148 149
		$this->send();
		parent::end();
	}

150 151 152
	/**
	 * @return integer the HTTP status code to send with the response.
	 */
Qiang Xue committed
153 154 155 156 157
	public function getStatusCode()
	{
		return $this->_statusCode;
	}

Qiang Xue committed
158
	public function setStatusCode($value, $text = null)
Qiang Xue committed
159 160
	{
		$this->_statusCode = (int)$value;
161
		if ($this->getIsInvalid()) {
Qiang Xue committed
162 163
			throw new InvalidParamException("The HTTP status code is invalid: $value");
		}
Qiang Xue committed
164
		if ($text === null) {
Qiang Xue committed
165
			$this->statusText = isset(self::$httpStatuses[$this->_statusCode]) ? self::$httpStatuses[$this->_statusCode] : '';
Qiang Xue committed
166 167 168
		} else {
			$this->statusText = $text;
		}
Qiang Xue committed
169 170
	}

Qiang Xue committed
171 172 173 174 175 176 177 178 179 180 181 182 183
	/**
	 * Returns the header collection.
	 * The header collection contains the currently registered HTTP headers.
	 * @return HeaderCollection the header collection
	 */
	public function getHeaders()
	{
		if ($this->_headers === null) {
			$this->_headers = new HeaderCollection;
		}
		return $this->_headers;
	}

Qiang Xue committed
184 185
	public function renderJson($data)
	{
Qiang Xue committed
186
		$this->getHeaders()->set('Content-Type', 'application/json');
Qiang Xue committed
187
		$this->content = Json::encode($data);
Qiang Xue committed
188
		$this->send();
Qiang Xue committed
189 190
	}

Qiang Xue committed
191
	public function renderJsonp($data, $callbackName)
Qiang Xue committed
192
	{
Qiang Xue committed
193
		$this->getHeaders()->set('Content-Type', 'text/javascript');
Qiang Xue committed
194
		$data = Json::encode($data);
Qiang Xue committed
195
		$this->content = "$callbackName($data);";
Qiang Xue committed
196
		$this->send();
Qiang Xue committed
197 198 199 200 201 202 203 204 205 206
	}

	/**
	 * Sends the response to the client.
	 * @return boolean true if the response was sent
	 */
	public function send()
	{
		$this->sendHeaders();
		$this->sendContent();
Qiang Xue committed
207 208 209 210 211 212 213 214 215 216 217

		if (function_exists('fastcgi_finish_request')) {
			fastcgi_finish_request();
		} else {
			for ($level = ob_get_level(); $level > 0; --$level) {
				if (!@ob_end_flush()) {
					ob_clean();
				}
			}
			flush();
		}
Qiang Xue committed
218 219
	}

Qiang Xue committed
220 221 222 223 224 225 226 227
	public function reset()
	{
		$this->_headers = null;
		$this->_statusCode = null;
		$this->statusText = null;
		$this->content = null;
	}

Qiang Xue committed
228 229 230 231 232
	/**
	 * Sends the response headers to the client
	 */
	protected function sendHeaders()
	{
Qiang Xue committed
233 234 235
		if (headers_sent()) {
			return;
		}
Qiang Xue committed
236 237 238 239
		$statusCode = $this->getStatusCode();
		if ($statusCode !== null) {
			header("HTTP/{$this->version} $statusCode {$this->statusText}");
		}
Qiang Xue committed
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
		if ($this->_headers) {
			$headers = $this->getHeaders();
			foreach ($headers as $name => $values) {
				foreach ($values as $value) {
					header("$name: $value", false);
				}
			}
			$headers->removeAll();
		}
		$this->sendCookies();
	}

	/**
	 * Sends the cookies to the client.
	 */
	protected function sendCookies()
	{
		if ($this->_cookies === null) {
			return;
		}
		$request = Yii::$app->getRequest();
		if ($request->enableCookieValidation) {
			$validationKey = $request->getCookieValidationKey();
		}
		foreach ($this->getCookies() as $cookie) {
			$value = $cookie->value;
			if ($cookie->expire != 1  && isset($validationKey)) {
				$value = SecurityHelper::hashData(serialize($value), $validationKey);
Qiang Xue committed
268
			}
Qiang Xue committed
269
			setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly);
Qiang Xue committed
270
		}
Qiang Xue committed
271
		$this->getCookies()->removeAll();
Qiang Xue committed
272 273 274 275 276 277 278 279 280 281 282
	}

	/**
	 * Sends the response content to the client
	 */
	protected function sendContent()
	{
		echo $this->content;
		$this->content = null;
	}

Qiang Xue committed
283
	/**
284 285 286
	 * Sends a file to the browser.
	 * @param string $filePath the path of the file to be sent.
	 * @param string $attachmentName the file name shown to the user. If null, it will be determined from `$filePath`.
Qiang Xue committed
287
	 * @param string $mimeType the MIME type of the content. If null, it will be guessed based on `$filePath`
Qiang Xue committed
288
	 */
Qiang Xue committed
289
	public function sendFile($filePath, $attachmentName = null, $mimeType = null)
Qiang Xue committed
290
	{
291
		if ($mimeType === null && ($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) {
292
			$mimeType = 'application/octet-stream';
Qiang Xue committed
293
		}
294 295
		if ($attachmentName === null) {
			$attachmentName = basename($filePath);
296
		}
297
		$handle = fopen($filePath, 'rb');
Qiang Xue committed
298
		$this->sendStreamAsFile($handle, $attachmentName, $mimeType);
299
	}
300

301 302 303 304
	/**
	 * Sends the specified content as a file to the browser.
	 * @param string $content the content to be sent. The existing [[content]] will be discarded.
	 * @param string $attachmentName the file name shown to the user.
Qiang Xue committed
305
	 * @param string $mimeType the MIME type of the content.
306
	 */
307
	public function sendContentAsFile($content, $attachmentName, $mimeType = 'application/octet-stream')
308 309 310 311 312 313 314 315 316 317 318 319 320
	{
		$this->getHeaders()
			->addDefault('Pragma', 'public')
			->addDefault('Accept-Ranges', 'bytes')
			->addDefault('Expires', '0')
			->addDefault('Content-Type', $mimeType)
			->addDefault('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
			->addDefault('Content-Transfer-Encoding', 'binary')
			->addDefault('Content-Length', StringHelper::strlen($content))
			->addDefault('Content-Disposition', "attachment; filename=\"$attachmentName\"");

		$this->content = $content;
		$this->send();
Qiang Xue committed
321 322
	}

323 324 325 326
	/**
	 * Sends the specified stream as a file to the browser.
	 * @param resource $handle the handle of the stream to be sent.
	 * @param string $attachmentName the file name shown to the user.
Qiang Xue committed
327
	 * @param string $mimeType the MIME type of the stream content.
328 329
	 * @throws HttpException if the requested range cannot be satisfied.
	 */
330
	public function sendStreamAsFile($handle, $attachmentName, $mimeType = 'application/octet-stream')
Qiang Xue committed
331
	{
332
		$headers = $this->getHeaders();
Qiang Xue committed
333 334 335
		fseek($handle, 0, SEEK_END);
		$fileSize = ftell($handle);

336 337 338 339 340
		$range = $this->getHttpRange($fileSize);
		if ($range === false) {
			$headers->set('Content-Range', "bytes */$fileSize");
			throw new HttpException(416, Yii::t('yii', 'Requested range not satisfiable'));
		}
Qiang Xue committed
341

342 343
		list($begin, $end) = $range;
		if ($begin !=0 || $end != $fileSize - 1) {
Qiang Xue committed
344
			$this->setStatusCode(206);
345
			$headers->set('Content-Range', "bytes $begin-$end/$fileSize");
Qiang Xue committed
346 347 348 349 350 351 352 353
		} else {
			$this->setStatusCode(200);
		}

		if (isset($options['mimeType'])) {
			$headers->set('Content-Type', $options['mimeType']);
		}

354
		$length = $end - $begin + 1;
Qiang Xue committed
355

356 357 358 359 360 361 362 363
		$headers->addDefault('Pragma', 'public')
			->addDefault('Accept-Ranges', 'bytes')
			->addDefault('Expires', '0')
			->addDefault('Content-Type', $mimeType)
			->addDefault('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
			->addDefault('Content-Transfer-Encoding', 'binary')
			->addDefault('Content-Length', $length)
			->addDefault('Content-Disposition', "attachment; filename=\"$attachmentName\"");
Qiang Xue committed
364

365
		$this->send();
Qiang Xue committed
366

367
		fseek($handle, $begin);
Qiang Xue committed
368 369
		set_time_limit(0); // Reset time limit for big files
		$chunkSize = 8 * 1024 * 1024; // 8MB per chunk
370 371 372
		while (!feof($handle) && ($pos = ftell($handle)) <= $end) {
			if ($pos + $chunkSize > $end) {
				$chunkSize = $end - $pos + 1;
Qiang Xue committed
373 374 375 376 377 378 379
			}
			echo fread($handle, $chunkSize);
			flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
		}
		fclose($handle);
	}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	/**
	 * Determines the HTTP range given in the request.
	 * @param integer $fileSize the size of the file that will be used to validate the requested HTTP range.
	 * @return array|boolean the range (begin, end), or false if the range request is invalid.
	 */
	protected function getHttpRange($fileSize)
	{
		if (!isset($_SERVER['HTTP_RANGE']) || $_SERVER['HTTP_RANGE'] === '-') {
			return array(0, $fileSize - 1);
		}
		if (!preg_match('/^bytes=(\d*)-(\d*)$/', $_SERVER['HTTP_RANGE'], $matches)) {
			return false;
		}
		if ($matches[1] === '') {
			$start = $fileSize - $matches[2];
			$end = $fileSize - 1;
		} elseif ($matches[2] !== '') {
			$start = $matches[1];
			$end = $matches[2];
			if ($end >= $fileSize) {
				$end = $fileSize - 1;
			}
		} else {
			$start = $matches[1];
			$end = $fileSize - 1;
		}
		if ($start < 0 || $start > $end) {
			return false;
		} else {
			return array($start, $end);
		}
	}

Qiang Xue committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426
	/**
	 * Sends existing file to a browser as a download using x-sendfile.
	 *
	 * X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver
	 * that in turn processes the request, this way eliminating the need to perform tasks like reading the file
	 * and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great
	 * increase in performance as the web application is allowed to terminate earlier while the webserver is
	 * handling the request.
	 *
	 * The request is sent to the server through a special non-standard HTTP-header.
	 * When the web server encounters the presence of such header it will discard all output and send the file
	 * specified by that header using web server internals including all optimizations like caching-headers.
	 *
	 * As this header directive is non-standard different directives exists for different web servers applications:
Qiang Xue committed
427 428 429 430 431 432 433
	 * 
	 * - Apache: [X-Sendfile](http://tn123.org/mod_xsendfile) 
	 * - Lighttpd v1.4: [X-LIGHTTPD-send-file](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file)
	 * - Lighttpd v1.5: [X-Sendfile](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file)
	 * - Nginx: [X-Accel-Redirect](http://wiki.nginx.org/XSendfile)
	 * - Cherokee: [X-Sendfile and X-Accel-Redirect](http://www.cherokee-project.com/doc/other_goodies.html#x-sendfile)
	 *
Qiang Xue committed
434 435 436
	 * So for this method to work the X-SENDFILE option/module should be enabled by the web server and
	 * a proper xHeader should be sent.
	 *
Qiang Xue committed
437 438 439 440
	 * **Note**
	 * 
	 * This option allows to download files that are not under web folders, and even files that are otherwise protected 
	 * (deny from all) like `.htaccess`.
Qiang Xue committed
441
	 *
Qiang Xue committed
442 443
	 * **Side effects**
	 * 
Qiang Xue committed
444 445 446
	 * If this option is disabled by the web server, when this method is called a download configuration dialog
	 * will open but the downloaded file will have 0 bytes.
	 *
Qiang Xue committed
447 448
	 * **Known issues**
	 * 
Qiang Xue committed
449
	 * There is a Bug with Internet Explorer 6, 7 and 8 when X-SENDFILE is used over an SSL connection, it will show
Qiang Xue committed
450 451 452 453 454 455
	 * an error message like this: "Internet Explorer was not able to open this Internet site. The requested site 
	 * is either unavailable or cannot be found.". You can work around this problem by removing the `Pragma`-header.
	 *
	 * **Example**
	 * 
	 * ~~~
456 457
	 * Yii::app()->request->xSendFile('/home/user/Pictures/picture1.jpg');
	 * ~~~
Qiang Xue committed
458
	 *
Qiang Xue committed
459
	 * @param string $filePath file name with full path
460 461 462
	 * @param string $mimeType the MIME type of the file. If null, it will be determined based on `$filePath`.
	 * @param string $attachmentName file name shown to the user. If null, it will be determined from `$filePath`.
	 * @param string $xHeader the name of the x-sendfile header.
Qiang Xue committed
463
	 */
Qiang Xue committed
464
	public function xSendFile($filePath, $attachmentName = null, $mimeType = null, $xHeader = 'X-Sendfile')
Qiang Xue committed
465
	{
466 467
		if ($mimeType === null && ($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) {
			$mimeType = 'application/octet-stream';
Qiang Xue committed
468
		}
469 470
		if ($attachmentName === null) {
			$attachmentName = basename($filePath);
Qiang Xue committed
471
		}
Qiang Xue committed
472

473 474 475 476
		$this->getHeaders()
			->addDefault($xHeader, $filePath)
			->addDefault('Content-Type', $mimeType)
			->addDefault('Content-Disposition', "attachment; filename=\"$attachmentName\"");
Qiang Xue committed
477

Qiang Xue committed
478
		$this->send();
Qiang Xue committed
479
	}
Qiang Xue committed
480 481 482

	/**
	 * Redirects the browser to the specified URL.
Qiang Xue committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	 * This method will send out a "Location" header to achieve the redirection.
	 * In AJAX mode, this normally will not work as expected unless there are some
	 * client-side JavaScript code handling the redirection. To help achieve this goal,
	 * this method will use [[ajaxRedirectCode]] as the HTTP status code when performing
	 * redirection in AJAX mode. The following JavaScript code may be used on the client
	 * side to handle the redirection response:
	 *
	 * ~~~
	 * $(document).ajaxSuccess(function(event, xhr, settings) {
	 *     if (xhr.status == 278) {
	 *         window.location = xhr.getResponseHeader('Location');
	 *     }
	 * });
	 * ~~~
	 *
	 * @param array|string $url the URL to be redirected to. [[\yii\helpers\Html::url()]]
	 * will be used to normalize the URL. If the resulting URL is still a relative URL
	 * (one without host info), the current request host info will be used.
Qiang Xue committed
501
	 * @param boolean $terminate whether to terminate the current application
Qiang Xue committed
502 503
	 * @param integer $statusCode the HTTP status code. Defaults to 302.
	 * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]]
Qiang Xue committed
504
	 * for details about HTTP status code.
Qiang Xue committed
505
	 * Note that if the request is an AJAX request, [[ajaxRedirectCode]] will be used instead.
Qiang Xue committed
506
	 */
Qiang Xue committed
507
	public function redirect($url, $terminate = true, $statusCode = 302)
Qiang Xue committed
508
	{
Qiang Xue committed
509
		$url = Html::url($url);
Qiang Xue committed
510 511 512
		if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
			$url = Yii::$app->getRequest()->getHostInfo() . $url;
		}
Qiang Xue committed
513
		if (Yii::$app->getRequest()->getIsAjax()) {
Qiang Xue committed
514 515
			$statusCode = $this->ajaxRedirectCode;
		}
Qiang Xue committed
516 517
		$this->getHeaders()->set('Location', $url);
		$this->setStatusCode($statusCode);
Qiang Xue committed
518 519 520
		if ($terminate) {
			Yii::$app->end();
		}
Qiang Xue committed
521
	}
522

523 524 525 526 527 528 529 530 531 532 533 534 535
	/**
	 * Refreshes the current page.
	 * The effect of this method call is the same as the user pressing the refresh button of his browser
	 * (without re-posting data).
	 * @param boolean $terminate whether to terminate the current application after calling this method
	 * @param string $anchor the anchor that should be appended to the redirection URL.
	 * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
	 */
	public function refresh($terminate = true, $anchor = '')
	{
		$this->redirect(Yii::$app->getRequest()->getUrl() . $anchor, $terminate);
	}

Qiang Xue committed
536 537
	private $_cookies;

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	/**
	 * Returns the cookie collection.
	 * Through the returned cookie collection, you add or remove cookies as follows,
	 *
	 * ~~~
	 * // add a cookie
	 * $response->cookies->add(new Cookie(array(
	 *     'name' => $name,
	 *     'value' => $value,
	 * ));
	 *
	 * // remove a cookie
	 * $response->cookies->remove('name');
	 * // alternatively
	 * unset($response->cookies['name']);
	 * ~~~
	 *
	 * @return CookieCollection the cookie collection.
	 */
	public function getCookies()
	{
Qiang Xue committed
559 560 561 562
		if ($this->_cookies === null) {
			$this->_cookies = new CookieCollection;
		}
		return $this->_cookies;
563
	}
Qiang Xue committed
564 565 566 567

	/**
	 * @return boolean whether this response has a valid [[statusCode]].
	 */
568
	public function getIsInvalid()
Qiang Xue committed
569 570 571 572 573 574 575
	{
		return $this->getStatusCode() < 100 || $this->getStatusCode() >= 600;
	}

	/**
	 * @return boolean whether this response is informational
	 */
576
	public function getIsInformational()
Qiang Xue committed
577 578 579 580 581
	{
		return $this->getStatusCode() >= 100 && $this->getStatusCode() < 200;
	}

	/**
582
	 * @return boolean whether this response is successful
Qiang Xue committed
583
	 */
584
	public function getIsSuccessful()
Qiang Xue committed
585 586 587 588 589 590 591
	{
		return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300;
	}

	/**
	 * @return boolean whether this response is a redirection
	 */
592
	public function getIsRedirection()
Qiang Xue committed
593 594 595 596 597 598 599
	{
		return $this->getStatusCode() >= 300 && $this->getStatusCode() < 400;
	}

	/**
	 * @return boolean whether this response indicates a client error
	 */
600
	public function getIsClientError()
Qiang Xue committed
601 602 603 604 605 606 607
	{
		return $this->getStatusCode() >= 400 && $this->getStatusCode() < 500;
	}

	/**
	 * @return boolean whether this response indicates a server error
	 */
608
	public function getIsServerError()
Qiang Xue committed
609 610 611 612 613 614 615
	{
		return $this->getStatusCode() >= 500 && $this->getStatusCode() < 600;
	}

	/**
	 * @return boolean whether this response is OK
	 */
616
	public function getIsOk()
Qiang Xue committed
617 618 619 620 621 622 623
	{
		return 200 === $this->getStatusCode();
	}

	/**
	 * @return boolean whether this response indicates the current request is forbidden
	 */
624
	public function getIsForbidden()
Qiang Xue committed
625 626 627 628 629 630 631
	{
		return 403 === $this->getStatusCode();
	}

	/**
	 * @return boolean whether this response indicates the currently requested resource is not found
	 */
632
	public function getIsNotFound()
Qiang Xue committed
633 634 635 636 637 638 639
	{
		return 404 === $this->getStatusCode();
	}

	/**
	 * @return boolean whether this response is empty
	 */
640
	public function getIsEmpty()
Qiang Xue committed
641 642 643
	{
		return in_array($this->getStatusCode(), array(201, 204, 304));
	}
Qiang Xue committed
644
}