File indexing completed on 2024-06-23 05:55:43

0001 <?php
0002 
0003 /**
0004  * Copyright (c) 2007-2011, Servigistics, Inc.
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions are met:
0009  *
0010  *  - Redistributions of source code must retain the above copyright notice,
0011  *    this list of conditions and the following disclaimer.
0012  *  - Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *  - Neither the name of Servigistics, Inc. nor the names of
0016  *    its contributors may be used to endorse or promote products derived from
0017  *    this software without specific prior written permission.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  *
0031  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
0032  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
0033  * @version $Id: $
0034  *
0035  * @package Apache
0036  * @subpackage Solr
0037  * @author Donovan Jimenez <djimenez@conduit-it.com>
0038  */
0039 
0040 /**
0041  * Represents the required pieces of an HTTP response provided by HTTP transport
0042  * implementations and then consumed by the Zend_Service_Solr_Response class which provides
0043  * decoding
0044  */
0045 class Zend_Service_Solr_HttpTransport_Response {
0046 
0047     /**
0048      * Status Messages indexed by Status Code
0049      * Obtained from: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
0050      *
0051      * @var array
0052      */
0053     static private $_defaultStatusMessages = array(
0054         // Specific to PHP Solr Client
0055         0 => "Communication Error",
0056         // Informational 1XX
0057         100 => "Continue",
0058         101 => "Switching Protocols",
0059         // Successful 2XX
0060         200 => "OK",
0061         201 => "Created",
0062         202 => "Accepted",
0063         203 => "Non-Authoritative Information",
0064         204 => "No Content",
0065         205 => "Reset Content",
0066         206 => "Partial Content",
0067         // Redirection 3XX
0068         300 => "Multiple Choices",
0069         301 => "Moved Permanently",
0070         302 => "Found",
0071         303 => "See Other",
0072         304 => "Not Modified",
0073         305 => "Use Proxy",
0074         307 => "Temporary Redirect",
0075         // Client Error 4XX
0076         400 => "Bad Request",
0077         401 => "Unauthorized",
0078         402 => "Payment Required",
0079         403 => "Forbidden",
0080         404 => "Not Found",
0081         405 => "Method Not Allowed",
0082         406 => "Not Acceptable",
0083         407 => "Proxy Authentication Required",
0084         408 => "Request Timeout",
0085         409 => "Conflict",
0086         410 => "Gone",
0087         411 => "Length Required",
0088         412 => "Precondition Failed",
0089         413 => "Request Entity Too Large",
0090         414 => "Request-URI Too Long",
0091         415 => "Unsupported Media Type",
0092         416 => "Request Range Not Satisfiable",
0093         417 => "Expectation Failed",
0094         // Server Error 5XX
0095         500 => "Internal Server Error",
0096         501 => "Not Implemented",
0097         502 => "Bad Gateway",
0098         503 => "Service Unavailable",
0099         504 => "Gateway Timeout",
0100         505 => "HTTP Version Not Supported"
0101     );
0102 
0103     /**
0104      * Get the HTTP status message based on status code
0105      *
0106      * @return string
0107      */
0108     public static function getDefaultStatusMessage($statusCode) {
0109         $statusCode = (int) $statusCode;
0110 
0111         if (isset(self::$_defaultStatusMessages[$statusCode])) {
0112             return self::$_defaultStatusMessages[$statusCode];
0113         }
0114 
0115         return "Unknown Status";
0116     }
0117 
0118     /**
0119      * The response's HTTP status code
0120      *
0121      * @var integer
0122      */
0123     private $_statusCode;
0124 
0125     /**
0126      * The response's HTTP status message
0127      *
0128      * @var string
0129      */
0130     private $_statusMessage;
0131 
0132     /**
0133      * The response's mime type
0134      *
0135      * @var string
0136      */
0137     private $_mimeType;
0138 
0139     /**
0140      * The response's character encoding
0141      *
0142      * @var string
0143      */
0144     private $_encoding;
0145 
0146     /**
0147      * The response's data
0148      *
0149      * @var string
0150      */
0151     private $_responseBody;
0152 
0153     /**
0154      * Construct a HTTP transport response
0155      * 
0156      * @param integer $statusCode The HTTP status code
0157      * @param string $contentType The VALUE of the Content-Type HTTP header
0158      * @param string $responseBody The body of the HTTP response
0159      */
0160     public function __construct($statusCode, $contentType, $responseBody) {
0161         // set the status code, make sure its an integer
0162         $this->_statusCode = (int) $statusCode;
0163 
0164         // lookup up status message based on code
0165         $this->_statusMessage = self::getDefaultStatusMessage($this->_statusCode);
0166 
0167         // set the response body, it should always be a string
0168         $this->_responseBody = (string) $responseBody;
0169 
0170         // parse the content type header value for mimetype and encoding
0171         // first set default values that will remain if we can't find
0172         // what we're looking for in the content type
0173         $this->_mimeType = "text/plain";
0174         $this->_encoding = "UTF-8";
0175 
0176         if ($contentType) {
0177             // now break apart the header to see if there's character encoding
0178             $contentTypeParts = explode(';', $contentType, 2);
0179 
0180             if (isset($contentTypeParts[0])) {
0181                 $this->_mimeType = trim($contentTypeParts[0]);
0182             }
0183 
0184             if (isset($contentTypeParts[1])) {
0185                 // we have a second part, split it further
0186                 $contentTypeParts = explode('=', $contentTypeParts[1]);
0187 
0188                 if (isset($contentTypeParts[1])) {
0189                     $this->_encoding = trim($contentTypeParts[1]);
0190                 }
0191             }
0192         }
0193     }
0194 
0195     /**
0196      * Get the status code of the response
0197      *
0198      * @return integer
0199      */
0200     public function getStatusCode() {
0201         return $this->_statusCode;
0202     }
0203 
0204     /**
0205      * Get the status message of the response
0206      *
0207      * @return string
0208      */
0209     public function getStatusMessage() {
0210         return $this->_statusMessage;
0211     }
0212 
0213     /**
0214      * Get the mimetype of the response body
0215      *
0216      * @return string
0217      */
0218     public function getMimeType() {
0219         return $this->_mimeType;
0220     }
0221 
0222     /**
0223      * Get the charset encoding of the response body.
0224      *
0225      * @return string
0226      */
0227     public function getEncoding() {
0228         return $this->_encoding;
0229     }
0230 
0231     /**
0232      * Get the raw response body
0233      *
0234      * @return string
0235      */
0236     public function getBody() {
0237         return $this->_responseBody;
0238     }
0239 
0240 }