Source for file Response.php
Documentation is available at Response.php
* Copyright (c) 2009, RealDolmen
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of RealDolmen nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package Microsoft_Http
* @version $Id: Response.php 14561 2009-05-07 08:05:12Z unknown $
* @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
* @license http://phpazure.codeplex.com/license
* @see Microsoft_Http_Exception
require_once 'Microsoft/Http/Exception.php';
* Microsoft_Http_Response
* This class is partially based on Zend Framework Zend_Http_Response - http://framework.zend.com
* @package Microsoft_Http
* @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
* @license http://phpazure.codeplex.com/license
* List of all known HTTP response status codes
protected static $_statusMessages = array(
101 => 'Switching Protocols',
203 => 'Non-Authoritative Information',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
402 => 'Payment Required',
405 => 'Method Not Allowed',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
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',
500 => 'Internal Server Error',
501 => 'Not Implemented',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
* The HTTP version (1.0, 1.1)
* The HTTP response code as string
* (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)
* The HTTP response headers array
* HTTP response constructor
* @param int $code Response code (200, 404, 500, ...)
* @param array $headers Headers array
* @param string $body Response body
* @param string $version HTTP version
* @throws Microsoft_Http_Exception
public function __construct($code, $headers, $body = null, $version = '1.1')
$this->_message = self::$_statusMessages[$code];
if (! preg_match('|^\d\.\d$|', $version)) {
foreach ($headers as $name => $value) {
list ($name, $value) = explode(":", $value, 1);
* Check whether the response is an error
$restype = floor($this->code / 100);
return ($restype == 4 || $restype == 5);
* Check whether the response in successful
return ($restype == 2 || $restype == 1);
* Check whether the response is a redirection
* Get the HTTP version (1.0, 1.1)
* Get the HTTP response code
* Get the HTTP response code as string
* (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)
* Get the HTTP response headers array
* Get a specific header as string, or null if it is not set
* @return string|array|null
* Extract the response code from a response string
* @param string $responseString
preg_match("|^HTTP/[\d\.x]+ (\d+)|", $responseString, $m);
* Extract the HTTP message from a response
* @param string $responseString
preg_match("|^HTTP/[\d\.x]+ \d+ ([^\r\n]+)|", $responseString, $m);
* Extract the HTTP version from a response
* @param string $responseString
preg_match("|^HTTP/([\d\.x]+) \d+|", $responseString, $m);
* Extract the headers from a response string
* @param string $responseString
// First, split body and headers
$parts = preg_split('|(?:\r?\n){2}|m', $responseString, 2);
if (! $parts[0]) return $headers;
// Split headers part to lines
foreach($lines as $line) {
$line = trim($line, "\r\n");
if (preg_match("|^([\w-]+):\s+(.+)|", $line, $m)) {
if (isset ($headers[$h_name])) {
$headers[$h_name] = array($headers[$h_name]);
$headers[$h_name][] = $h_value;
$headers[$h_name] = $h_value;
} elseif (preg_match("|^\s+(.+)$|", $line, $m) && $last_header !== null) {
end($headers[$last_header]);
$last_header_key = key($headers[$last_header]);
$headers[$last_header][$last_header_key] .= $m[1];
$headers[$last_header] .= $m[1];
* Extract the body from a response string
* @param string $response_str
$parts = preg_split('|(?:\r?\n){2}|m', $responseString, 2);
* Create a new Microsoft_Http_Response object from a string
* @param string $response_str
* @return Microsoft_Http_Response
$code = self::extractCode($response_str);
$headers = self::extractHeaders($response_str);
$body = self::extractBody($response_str);
$version = self::extractVersion($response_str);
$message = self::extractMessage($response_str);
|