Microsoft_Http
[ class tree: Microsoft_Http ] [ index: Microsoft_Http ] [ all elements ]

Source for file Response.php

Documentation is available at Response.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_Http
  30.  * @version    $Id: Response.php 14561 2009-05-07 08:05:12Z unknown $
  31.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  */
  34.  
  35. /**
  36.  * @see Microsoft_Http_Exception
  37.  */
  38. require_once 'Microsoft/Http/Exception.php';
  39.  
  40. /**
  41.  * Microsoft_Http_Response
  42.  * 
  43.  * This class is partially based on Zend Framework Zend_Http_Response - http://framework.zend.com
  44.  * 
  45.  * @category   Microsoft
  46.  * @package    Microsoft_Http
  47.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  48.  * @license    http://phpazure.codeplex.com/license
  49.  */
  50. {
  51.     /**
  52.      * List of all known HTTP response status codes
  53.      *
  54.      * @var array 
  55.      */
  56.     protected static $_statusMessages array(
  57.         // Informational 1xx
  58.         100 => 'Continue',
  59.         101 => 'Switching Protocols',
  60.  
  61.         // Success 2xx
  62.         200 => 'OK',
  63.         201 => 'Created',
  64.         202 => 'Accepted',
  65.         203 => 'Non-Authoritative Information',
  66.         204 => 'No Content',
  67.         205 => 'Reset Content',
  68.         206 => 'Partial Content',
  69.  
  70.         // Redirection 3xx
  71.         300 => 'Multiple Choices',
  72.         301 => 'Moved Permanently',
  73.         302 => 'Found',  // 1.1
  74.         303 => 'See Other',
  75.         304 => 'Not Modified',
  76.         305 => 'Use Proxy',
  77.         // 306 is deprecated but reserved
  78.         307 => 'Temporary Redirect',
  79.  
  80.         // Client Error 4xx
  81.         400 => 'Bad Request',
  82.         401 => 'Unauthorized',
  83.         402 => 'Payment Required',
  84.         403 => 'Forbidden',
  85.         404 => 'Not Found',
  86.         405 => 'Method Not Allowed',
  87.         406 => 'Not Acceptable',
  88.         407 => 'Proxy Authentication Required',
  89.         408 => 'Request Timeout',
  90.         409 => 'Conflict',
  91.         410 => 'Gone',
  92.         411 => 'Length Required',
  93.         412 => 'Precondition Failed',
  94.         413 => 'Request Entity Too Large',
  95.         414 => 'Request-URI Too Long',
  96.         415 => 'Unsupported Media Type',
  97.         416 => 'Requested Range Not Satisfiable',
  98.         417 => 'Expectation Failed',
  99.  
  100.         // Server Error 5xx
  101.         500 => 'Internal Server Error',
  102.         501 => 'Not Implemented',
  103.         502 => 'Bad Gateway',
  104.         503 => 'Service Unavailable',
  105.         504 => 'Gateway Timeout',
  106.         505 => 'HTTP Version Not Supported',
  107.         509 => 'Bandwidth Limit Exceeded'
  108.     );
  109.     
  110.     /**
  111.      * The HTTP version (1.0, 1.1)
  112.      *
  113.      * @var string 
  114.      */
  115.     protected $_version;
  116.  
  117.     /**
  118.      * The HTTP response code
  119.      *
  120.      * @var int 
  121.      */
  122.     protected $_code;
  123.  
  124.     /**
  125.      * The HTTP response code as string
  126.      * (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)
  127.      *
  128.      * @var string 
  129.      */
  130.     protected $_message;
  131.  
  132.     /**
  133.      * The HTTP response headers array
  134.      *
  135.      * @var array 
  136.      */
  137.     protected $_headers = array();
  138.  
  139.     /**
  140.      * The HTTP response body
  141.      *
  142.      * @var string 
  143.      */
  144.     protected $_body;
  145.  
  146.     /**
  147.      * HTTP response constructor
  148.      *
  149.      * @param int $code Response code (200, 404, 500, ...)
  150.      * @param array $headers Headers array
  151.      * @param string $body Response body
  152.      * @param string $version HTTP version
  153.      * @throws Microsoft_Http_Exception
  154.      */
  155.     public function __construct($code$headers$body null$version '1.1')
  156.     {
  157.         // Code
  158.         $this->_code = $code;
  159.         
  160.         // Message
  161.         $this->_message = self::$_statusMessages[$code];
  162.         
  163.         // Body
  164.         $this->_body = $body;
  165.         
  166.         // Version
  167.         if (preg_match('|^\d\.\d$|'$version)) {
  168.             throw new Microsoft_Http_Exception('No valid HTTP version was passed: ' $version);
  169.         }
  170.         $this->_version = $version;
  171.         
  172.         // Headers
  173.         if (!is_array($headers))
  174.         {
  175.             throw new Microsoft_Http_Exception('No valid headers were passed');
  176.         }
  177.         else 
  178.         {
  179.             foreach ($headers as $name => $value{
  180.                 if (is_int($name))
  181.                     list($name$valueexplode(":"$value1);
  182.  
  183.                 $this->_headers[ucwords(strtolower($name))trim($value);
  184.             }
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * Check whether the response is an error
  190.      *
  191.      * @return boolean 
  192.      */
  193.     public function isError()
  194.     {
  195.         $restype floor($this->code 100);
  196.         return ($restype == || $restype == 5);
  197.     }
  198.  
  199.     /**
  200.      * Check whether the response in successful
  201.      *
  202.      * @return boolean 
  203.      */
  204.     public function isSuccessful()
  205.     {
  206.         $restype floor($this->_code / 100);
  207.         return ($restype == || $restype == 1);
  208.     }
  209.  
  210.     /**
  211.      * Check whether the response is a redirection
  212.      *
  213.      * @return boolean 
  214.      */
  215.     public function isRedirect()
  216.     {
  217.         $restype floor($this->_code / 100);
  218.         return ($restype == 3);
  219.     }
  220.  
  221.     /**
  222.      * Get the HTTP version (1.0, 1.1)
  223.      *
  224.      * @return string 
  225.      */
  226.     public function getVersion()
  227.     {
  228.         return $this->_version;
  229.     }
  230.  
  231.     /**
  232.      * Get the HTTP response code
  233.      *
  234.      * @return int 
  235.      */
  236.     public function getCode()
  237.     {
  238.         return $this->_code;   
  239.     }
  240.  
  241.     /**
  242.      * Get the HTTP response code as string
  243.      * (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)
  244.      *
  245.      * @return string 
  246.      */
  247.     public function getMessage()
  248.     {
  249.         return $this->_message;   
  250.     }
  251.  
  252.     /**
  253.      * Get the HTTP response headers array
  254.      *
  255.      * @return array 
  256.      */
  257.     public function getHeaders()
  258.     {
  259.         if (!is_array($this->_headers))
  260.         {
  261.             $this->_headers = array();
  262.         }
  263.         return $this->_headers;
  264.     }
  265.     
  266.     /**
  267.      * Get a specific header as string, or null if it is not set
  268.      *
  269.      * @param string $header 
  270.      * @return string|array|null
  271.      */
  272.     public function getHeader($header)
  273.     {
  274.         $header ucwords(strtolower($header));
  275.         if (!is_string($header|| isset($this->_headers[$header])) return null;
  276.  
  277.         return $this->_headers[$header];
  278.     }
  279.  
  280.     /**
  281.      * The HTTP response body
  282.      *
  283.      * @return string 
  284.      */
  285.     public function getBody()
  286.     {
  287.         return $this->_body;
  288.     }
  289.  
  290.     /**
  291.      * Extract the response code from a response string
  292.      *
  293.      * @param string $responseString 
  294.      * @return int 
  295.      */
  296.     public static function extractCode($responseString)
  297.     {
  298.         preg_match("|^HTTP/[\d\.x]+ (\d+)|"$responseString$m);
  299.  
  300.         if (isset($m[1])) {
  301.             return (int) $m[1];
  302.         else {
  303.             return false;
  304.         }
  305.     }
  306.  
  307.     /**
  308.      * Extract the HTTP message from a response
  309.      *
  310.      * @param string $responseString 
  311.      * @return string 
  312.      */
  313.     public static function extractMessage($responseString)
  314.     {
  315.         preg_match("|^HTTP/[\d\.x]+ \d+ ([^\r\n]+)|"$responseString$m);
  316.  
  317.         if (isset($m[1])) {
  318.             return $m[1];
  319.         else {
  320.             return false;
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * Extract the HTTP version from a response
  326.      *
  327.      * @param string $responseString 
  328.      * @return string 
  329.      */
  330.     public static function extractVersion($responseString)
  331.     {
  332.         preg_match("|^HTTP/([\d\.x]+) \d+|"$responseString$m);
  333.  
  334.         if (isset($m[1])) {
  335.             return $m[1];
  336.         else {
  337.             return false;
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Extract the headers from a response string
  343.      *
  344.      * @param string $responseString 
  345.      * @return array 
  346.      */
  347.     public static function extractHeaders($responseString)
  348.     {
  349.         $headers array();
  350.         
  351.         // First, split body and headers
  352.         $parts preg_split('|(?:\r?\n){2}|m'$responseString2);
  353.         if ($parts[0]return $headers;
  354.         
  355.         // Split headers part to lines
  356.         $lines explode("\n"$parts[0]);
  357.         unset($parts);
  358.         $last_header null;
  359.  
  360.         foreach($lines as $line{
  361.             $line trim($line"\r\n");
  362.             if ($line == ""break;
  363.  
  364.             if (preg_match("|^([\w-]+):\s+(.+)|"$line$m)) {
  365.                 unset($last_header);
  366.                 $h_name strtolower($m[1]);
  367.                 $h_value $m[2];
  368.  
  369.                 if (isset($headers[$h_name])) {
  370.                     if (is_array($headers[$h_name])) {
  371.                         $headers[$h_namearray($headers[$h_name]);
  372.                     }
  373.  
  374.                     $headers[$h_name][$h_value;
  375.                 else {
  376.                     $headers[$h_name$h_value;
  377.                 }
  378.                 $last_header $h_name;
  379.             elseif (preg_match("|^\s+(.+)$|"$line$m&& $last_header !== null{
  380.                 if (is_array($headers[$last_header])) {
  381.                     end($headers[$last_header]);
  382.                     $last_header_key key($headers[$last_header]);
  383.                     $headers[$last_header][$last_header_key.= $m[1];
  384.                 else {
  385.                     $headers[$last_header.= $m[1];
  386.                 }
  387.             }
  388.         }
  389.  
  390.         return $headers;
  391.     }
  392.  
  393.     /**
  394.      * Extract the body from a response string
  395.      *
  396.      * @param string $response_str 
  397.      * @return string 
  398.      */
  399.     public static function extractBody($responseString)
  400.     {
  401.         $parts preg_split('|(?:\r?\n){2}|m'$responseString2);
  402.         if (isset($parts[1])) 
  403.             return $parts[1];
  404.         }
  405.         return '';
  406.     }
  407.     
  408.     /**
  409.      * Create a new Microsoft_Http_Response object from a string
  410.      *
  411.      * @param string $response_str 
  412.      * @return Microsoft_Http_Response 
  413.      */
  414.     public static function fromString($response_str)
  415.     {
  416.         $code    self::extractCode($response_str);
  417.         $headers self::extractHeaders($response_str);
  418.         $body    self::extractBody($response_str);
  419.         $version self::extractVersion($response_str);
  420.         $message self::extractMessage($response_str);
  421.  
  422.         return new Microsoft_Http_Response($code$headers$body$version$message);
  423.     }
  424. }

Documentation generated on Thu, 26 Nov 2009 08:05:13 +0100 by phpDocumentor 1.4.3