📄 response.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Http * @subpackage Response * @version $Id: Response.php 8064 2008-02-16 10:58:39Z thomas $ * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** * Zend_Http_Response represents an HTTP 1.0 / 1.1 response message. It * includes easy access to all the response's different elemts, as well as some * convenience methods for parsing and validating HTTP responses. * * @package Zend_Http * @subpackage Response * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Http_Response{ /** * List of all known HTTP response codes - used by responseCodeAsText() to * translate numeric codes to messages. * * @var array */ protected static $messages = array( // Informational 1xx 100 => 'Continue', 101 => 'Switching Protocols', // Success 2xx 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', // Redirection 3xx 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', // 1.1 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved 307 => 'Temporary Redirect', // Client Error 4xx 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 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 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', // Server Error 5xx 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' ); /** * The HTTP version (1.0, 1.1) * * @var string */ protected $version; /** * The HTTP response code * * @var int */ protected $code; /** * The HTTP response code as string * (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500) * * @var string */ protected $message; /** * The HTTP response headers array * * @var array */ protected $headers = array(); /** * The HTTP response body * * @var string */ protected $body; /** * HTTP response constructor * * In most cases, you would use Zend_Http_Response::fromString to parse an HTTP * response string and create a new Zend_Http_Response object. * * NOTE: The constructor no longer accepts nulls or empty values for the code and * headers and will throw an exception if the passed values do not form a valid HTTP * responses. * * If no message is passed, the message will be guessed according to the response code. * * @param int $code Response code (200, 404, ...) * @param array $headers Headers array * @param string $body Response body * @param string $version HTTP version * @param string $message Response code as text * @throws Zend_Http_Exception */ public function __construct($code, $headers, $body = null, $version = '1.1', $message = null) { // Make sure the response code is valid and set it if (self::responseCodeAsText($code) === null) { require_once 'Zend/Http/Exception.php'; throw new Zend_Http_Exception("{$code} is not a valid HTTP response code"); } $this->code = $code; // Make sure we got valid headers and set them if (! is_array($headers)) { require_once 'Zend/Http/Exception.php'; throw new Zend_Http_Exception('No valid headers were passed'); } foreach ($headers as $name => $value) { if (is_int($name)) list($name, $value) = explode(": ", $value, 1); $this->headers[ucwords(strtolower($name))] = $value; } // Set the body $this->body = $body; // Set the HTTP version if (! preg_match('|^\d\.\d$|', $version)) { require_once 'Zend/Http/Exception.php'; throw new Zend_Http_Exception("Invalid HTTP response version: $version"); } $this->version = $version; // If we got the response message, set it. Else, set it according to // the response code if (is_string($message)) { $this->message = $message; } else { $this->message = self::responseCodeAsText($code); } } /** * Check whether the response is an error * * @return boolean */ public function isError() { $restype = floor($this->code / 100); if ($restype == 4 || $restype == 5) { return true; } return false; } /** * Check whether the response in successful * * @return boolean */ public function isSuccessful() { $restype = floor($this->code / 100); if ($restype == 2 || $restype == 1) { // Shouldn't 3xx count as success as well ??? return true; } return false; } /** * Check whether the response is a redirection * * @return boolean */ public function isRedirect() { $restype = floor($this->code / 100); if ($restype == 3) { return true; } return false; } /** * Get the response body as string * * This method returns the body of the HTTP response (the content), as it * should be in it's readable version - that is, after decoding it (if it * was decoded), deflating it (if it was gzip compressed), etc. * * If you want to get the raw body (as transfered on wire) use * $this->getRawBody() instead. * * @return string */ public function getBody() { $body = ''; // Decode the body if it was transfer-encoded switch ($this->getHeader('transfer-encoding')) { // Handle chunked body case 'chunked': $body = self::decodeChunkedBody($this->body); break; // No transfer encoding, or unknown encoding extension: // return body as is default: $body = $this->body; break; } // Decode any content-encoding (gzip or deflate) if needed switch (strtolower($this->getHeader('content-encoding'))) { // Handle gzip encoding case 'gzip': $body = self::decodeGzip($body); break; // Handle deflate encoding case 'deflate': $body = self::decodeDeflate($body); break; default: break; } return $body; } /** * Get the raw response body (as transfered "on wire") as string * * If the body is encoded (with Transfer-Encoding, not content-encoding - * IE "chunked" body), gzip compressed, etc. it will not be decoded. * * @return string */ public function getRawBody() { return $this->body; } /** * Get the HTTP version of the response * * @return string */ public function getVersion() { return $this->version; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -