Source for file Response.php

Documentation is available at Response.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2007-2011, Servigistics, Inc.
  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.  *
  9.  *  - Redistributions of source code must retain the above copyright notice,
  10.  *    this list of conditions and the following disclaimer.
  11.  *  - Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  *  - Neither the name of Servigistics, Inc. nor the names of
  15.  *    its contributors may be used to endorse or promote products derived from
  16.  *    this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31.  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32.  * @version $Id: $
  33.  *
  34.  * @package Apache
  35.  * @subpackage Solr
  36.  * @author Donovan Jimenez <djimenez@conduit-it.com>
  37.  */
  38.  
  39. /**
  40.  * Represents the required pieces of an HTTP response provided by HTTP transport
  41.  * implementations and then consumed by the Apache_Solr_Response class which provides
  42.  * decoding
  43.  */
  44. {
  45.     /**
  46.      * Status Messages indexed by Status Code
  47.      * Obtained from: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  48.      *
  49.      * @var array 
  50.      */
  51.     static private $_defaultStatusMessages array(
  52.         // Specific to PHP Solr Client
  53.         => "Communication Error",
  54.         
  55.         // Informational 1XX
  56.         100 => "Continue",
  57.         101 => "Switching Protocols",
  58.         
  59.         // Successful 2XX
  60.         200 => "OK",
  61.         201 => "Created",
  62.         202 => "Accepted",
  63.         203 => "Non-Authoritative Information",
  64.         204 => "No Content",
  65.         205 => "Reset Content",
  66.         206 => "Partial Content",
  67.         
  68.         // Redirection 3XX
  69.         300 => "Multiple Choices",
  70.         301 => "Moved Permanently",
  71.         302 => "Found",
  72.         303 => "See Other",
  73.         304 => "Not Modified",
  74.         305 => "Use Proxy",
  75.         307 => "Temporary Redirect",
  76.         
  77.         // Client Error 4XX
  78.         400 => "Bad Request",
  79.         401 => "Unauthorized",
  80.         402 => "Payment Required",
  81.         403 => "Forbidden",
  82.         404 => "Not Found",
  83.         405 => "Method Not Allowed",
  84.         406 => "Not Acceptable",
  85.         407 => "Proxy Authentication Required",
  86.         408 => "Request Timeout",
  87.         409 => "Conflict",
  88.         410 => "Gone",
  89.         411 => "Length Required",
  90.         412 => "Precondition Failed",
  91.         413 => "Request Entity Too Large",
  92.         414 => "Request-URI Too Long",
  93.         415 => "Unsupported Media Type",
  94.         416 => "Request Range Not Satisfiable",
  95.         417 => "Expectation Failed",
  96.         
  97.         // Server Error 5XX
  98.         500 => "Internal Server Error",
  99.         501 => "Not Implemented",
  100.         502 => "Bad Gateway",
  101.         503 => "Service Unavailable",
  102.         504 => "Gateway Timeout",
  103.         505 => "HTTP Version Not Supported"
  104.     );
  105.     
  106.     /**
  107.      * Get the HTTP status message based on status code
  108.      *
  109.      * @return string 
  110.      */
  111.     public static function getDefaultStatusMessage($statusCode)
  112.     {
  113.         $statusCode = (int) $statusCode;
  114.         
  115.         if (isset(self::$_defaultStatusMessages[$statusCode]))
  116.         {
  117.             return self::$_defaultStatusMessages[$statusCode];
  118.         }
  119.         
  120.         return "Unknown Status";
  121.     }
  122.     
  123.     /**
  124.      * The response's HTTP status code
  125.      *
  126.      * @var integer 
  127.      */
  128.     private $_statusCode;
  129.     
  130.     /**
  131.      * The response's HTTP status message
  132.      *
  133.      * @var string 
  134.      */
  135.     private $_statusMessage;
  136.     
  137.     /**
  138.      * The response's mime type
  139.      *
  140.      * @var string 
  141.      */
  142.     private $_mimeType;
  143.     
  144.     /**
  145.      * The response's character encoding
  146.      *
  147.      * @var string 
  148.      */
  149.     private $_encoding;
  150.     
  151.     /**
  152.      * The response's data
  153.      *
  154.      * @var string 
  155.      */
  156.     private $_responseBody;
  157.     
  158.     /**
  159.      * Construct a HTTP transport response
  160.      * 
  161.      * @param integer $statusCode The HTTP status code
  162.      * @param string $contentType The VALUE of the Content-Type HTTP header
  163.      * @param string $responseBody The body of the HTTP response
  164.      */
  165.     public function __construct($statusCode$contentType$responseBody)
  166.     {
  167.         // set the status code, make sure its an integer
  168.         $this->_statusCode = (int) $statusCode;
  169.         
  170.         // lookup up status message based on code
  171.         $this->_statusMessage self::getDefaultStatusMessage($this->_statusCode);
  172.         
  173.         // set the response body, it should always be a string
  174.         $this->_responseBody = (string) $responseBody;
  175.         
  176.         // parse the content type header value for mimetype and encoding
  177.         // first set default values that will remain if we can't find
  178.         // what we're looking for in the content type
  179.         $this->_mimeType "text/plain";
  180.         $this->_encoding "UTF-8";
  181.         
  182.         if ($contentType)
  183.         {
  184.             // now break apart the header to see if there's character encoding
  185.             $contentTypeParts explode(';'$contentType2);
  186.  
  187.             if (isset($contentTypeParts[0]))
  188.             {
  189.                 $this->_mimeType trim($contentTypeParts[0]);
  190.             }
  191.  
  192.             if (isset($contentTypeParts[1]))
  193.             {
  194.                 // we have a second part, split it further
  195.                 $contentTypeParts explode('='$contentTypeParts[1]);
  196.  
  197.                 if (isset($contentTypeParts[1]))
  198.                 {
  199.                     $this->_encoding trim($contentTypeParts[1]);
  200.                 }
  201.             }
  202.         }
  203.     }
  204.     
  205.     /**
  206.      * Get the status code of the response
  207.      *
  208.      * @return integer 
  209.      */
  210.     public function getStatusCode()
  211.     {
  212.         return $this->_statusCode;
  213.     }
  214.     
  215.     /**
  216.      * Get the status message of the response
  217.      *
  218.      * @return string 
  219.      */
  220.     public function getStatusMessage()
  221.     {
  222.         return $this->_statusMessage;
  223.     }
  224.     
  225.     /**
  226.      * Get the mimetype of the response body
  227.      *
  228.      * @return string 
  229.      */
  230.     public function getMimeType()
  231.     {
  232.         return $this->_mimeType;
  233.     }
  234.     
  235.     /**
  236.      * Get the charset encoding of the response body.
  237.      *
  238.      * @return string 
  239.      */
  240.     public function getEncoding()
  241.     {
  242.         return $this->_encoding;
  243.     }
  244.     
  245.     /**
  246.      * Get the raw response body
  247.      *
  248.      * @return string 
  249.      */
  250.     public function getBody()
  251.     {
  252.         return $this->_responseBody;
  253.     }
  254. }

Documentation generated on Wed, 04 May 2011 11:01:16 -0400 by phpDocumentor 1.4.3