Response.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * Represents the required pieces of an HTTP response provided by HTTP transport
  40. * implementations and then consumed by the Apache_Solr_Response class which provides
  41. * decoding
  42. */
  43. class Apache_Solr_HttpTransport_Response
  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. 0 => "Communication Error",
  54. // Informational 1XX
  55. 100 => "Continue",
  56. 101 => "Switching Protocols",
  57. // Successful 2XX
  58. 200 => "OK",
  59. 201 => "Created",
  60. 202 => "Accepted",
  61. 203 => "Non-Authoritative Information",
  62. 204 => "No Content",
  63. 205 => "Reset Content",
  64. 206 => "Partial Content",
  65. // Redirection 3XX
  66. 300 => "Multiple Choices",
  67. 301 => "Moved Permanently",
  68. 302 => "Found",
  69. 303 => "See Other",
  70. 304 => "Not Modified",
  71. 305 => "Use Proxy",
  72. 307 => "Temporary Redirect",
  73. // Client Error 4XX
  74. 400 => "Bad Request",
  75. 401 => "Unauthorized",
  76. 402 => "Payment Required",
  77. 403 => "Forbidden",
  78. 404 => "Not Found",
  79. 405 => "Method Not Allowed",
  80. 406 => "Not Acceptable",
  81. 407 => "Proxy Authentication Required",
  82. 408 => "Request Timeout",
  83. 409 => "Conflict",
  84. 410 => "Gone",
  85. 411 => "Length Required",
  86. 412 => "Precondition Failed",
  87. 413 => "Request Entity Too Large",
  88. 414 => "Request-URI Too Long",
  89. 415 => "Unsupported Media Type",
  90. 416 => "Request Range Not Satisfiable",
  91. 417 => "Expectation Failed",
  92. // Server Error 5XX
  93. 500 => "Internal Server Error",
  94. 501 => "Not Implemented",
  95. 502 => "Bad Gateway",
  96. 503 => "Service Unavailable",
  97. 504 => "Gateway Timeout",
  98. 505 => "HTTP Version Not Supported"
  99. );
  100. /**
  101. * Get the HTTP status message based on status code
  102. *
  103. * @return string
  104. */
  105. public static function getDefaultStatusMessage($statusCode)
  106. {
  107. $statusCode = (int) $statusCode;
  108. if (isset(self::$_defaultStatusMessages[$statusCode]))
  109. {
  110. return self::$_defaultStatusMessages[$statusCode];
  111. }
  112. return "Unknown Status";
  113. }
  114. /**
  115. * The response's HTTP status code
  116. *
  117. * @var integer
  118. */
  119. private $_statusCode;
  120. /**
  121. * The response's HTTP status message
  122. *
  123. * @var string
  124. */
  125. private $_statusMessage;
  126. /**
  127. * The response's mime type
  128. *
  129. * @var string
  130. */
  131. private $_mimeType;
  132. /**
  133. * The response's character encoding
  134. *
  135. * @var string
  136. */
  137. private $_encoding;
  138. /**
  139. * The response's data
  140. *
  141. * @var string
  142. */
  143. private $_responseBody;
  144. /**
  145. * Construct a HTTP transport response
  146. *
  147. * @param integer $statusCode The HTTP status code
  148. * @param string $contentType The VALUE of the Content-Type HTTP header
  149. * @param string $responseBody The body of the HTTP response
  150. */
  151. public function __construct($statusCode, $contentType, $responseBody)
  152. {
  153. // set the status code, make sure its an integer
  154. $this->_statusCode = (int) $statusCode;
  155. // lookup up status message based on code
  156. $this->_statusMessage = self::getDefaultStatusMessage($this->_statusCode);
  157. // set the response body, it should always be a string
  158. $this->_responseBody = (string) $responseBody;
  159. // parse the content type header value for mimetype and encoding
  160. // first set default values that will remain if we can't find
  161. // what we're looking for in the content type
  162. $this->_mimeType = "text/plain";
  163. $this->_encoding = "UTF-8";
  164. if ($contentType)
  165. {
  166. // now break apart the header to see if there's character encoding
  167. $contentTypeParts = explode(';', $contentType, 2);
  168. if (isset($contentTypeParts[0]))
  169. {
  170. $this->_mimeType = trim($contentTypeParts[0]);
  171. }
  172. if (isset($contentTypeParts[1]))
  173. {
  174. // we have a second part, split it further
  175. $contentTypeParts = explode('=', $contentTypeParts[1]);
  176. if (isset($contentTypeParts[1]))
  177. {
  178. $this->_encoding = trim($contentTypeParts[1]);
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * Get the status code of the response
  185. *
  186. * @return integer
  187. */
  188. public function getStatusCode()
  189. {
  190. return $this->_statusCode;
  191. }
  192. /**
  193. * Get the status message of the response
  194. *
  195. * @return string
  196. */
  197. public function getStatusMessage()
  198. {
  199. return $this->_statusMessage;
  200. }
  201. /**
  202. * Get the mimetype of the response body
  203. *
  204. * @return string
  205. */
  206. public function getMimeType()
  207. {
  208. return $this->_mimeType;
  209. }
  210. /**
  211. * Get the charset encoding of the response body.
  212. *
  213. * @return string
  214. */
  215. public function getEncoding()
  216. {
  217. return $this->_encoding;
  218. }
  219. /**
  220. * Get the raw response body
  221. *
  222. * @return string
  223. */
  224. public function getBody()
  225. {
  226. return $this->_responseBody;
  227. }
  228. }