PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/easyrdf/easyrdf/lib/EasyRdf/Http/Response.php

https://gitlab.com/reasonat/test8
PHP | 360 lines | 156 code | 35 blank | 169 comment | 20 complexity | 117be882faf220fa73b452413d29b68a MD5 | raw file
  1. <?php
  2. /**
  3. * EasyRdf
  4. *
  5. * LICENSE
  6. *
  7. * Copyright (c) 2009-2013 Nicholas J Humfrey.
  8. * Copyright (c) 2005-2009 Zend Technologies USA Inc.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
  19. * promote products derived from this software without specific prior
  20. * written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * @package EasyRdf
  35. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc.
  37. * @license http://www.opensource.org/licenses/bsd-license.php
  38. */
  39. /**
  40. * Class that represents an HTTP 1.0 / 1.1 response message.
  41. *
  42. * @package EasyRdf
  43. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  44. * Copyright (c) 2005-2009 Zend Technologies USA Inc.
  45. * @license http://www.opensource.org/licenses/bsd-license.php
  46. */
  47. class EasyRdf_Http_Response
  48. {
  49. /**
  50. * The HTTP response status code
  51. *
  52. * @var int
  53. */
  54. private $status;
  55. /**
  56. * The HTTP response code as string
  57. * (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)
  58. *
  59. * @var string
  60. */
  61. private $message;
  62. /**
  63. * The HTTP response headers array
  64. *
  65. * @var array
  66. */
  67. private $headers = array();
  68. /**
  69. * The HTTP response body
  70. *
  71. * @var string
  72. */
  73. private $body;
  74. /**
  75. * Constructor.
  76. *
  77. * @param int $status HTTP Status code
  78. * @param array $headers The HTTP response headers
  79. * @param string $body The content of the response
  80. * @param string $version The HTTP Version (1.0 or 1.1)
  81. * @param string $message The HTTP response Message
  82. * @return object EasyRdf_Http_Response
  83. */
  84. public function __construct(
  85. $status,
  86. $headers,
  87. $body = null,
  88. $version = '1.1',
  89. $message = null
  90. ) {
  91. $this->status = intval($status);
  92. $this->body = $body;
  93. $this->version = $version;
  94. $this->message = $message;
  95. foreach ($headers as $k => $v) {
  96. $k = ucwords(strtolower($k));
  97. $this->headers[$k] = $v;
  98. }
  99. }
  100. /**
  101. * Check whether the response in successful
  102. *
  103. * @return boolean
  104. */
  105. public function isSuccessful()
  106. {
  107. return ($this->status >= 200 && $this->status < 300);
  108. }
  109. /**
  110. * Check whether the response is an error
  111. *
  112. * @return boolean
  113. */
  114. public function isError()
  115. {
  116. return ($this->status >= 400 && $this->status < 600);
  117. }
  118. /**
  119. * Check whether the response is a redirection
  120. *
  121. * @return boolean
  122. */
  123. public function isRedirect()
  124. {
  125. return ($this->status >= 300 && $this->status < 400);
  126. }
  127. /**
  128. * Get the HTTP response status code
  129. *
  130. * @return int
  131. */
  132. public function getStatus()
  133. {
  134. return $this->status;
  135. }
  136. /**
  137. * Return a message describing the HTTP response code
  138. * (Eg. "OK", "Not Found", "Moved Permanently")
  139. *
  140. * @return string
  141. */
  142. public function getMessage()
  143. {
  144. return $this->message;
  145. }
  146. /**
  147. * Get the response body as string
  148. *
  149. * @return string
  150. */
  151. public function getBody()
  152. {
  153. // Decode the body if it was transfer-encoded
  154. switch (strtolower($this->getHeader('transfer-encoding'))) {
  155. // Handle chunked body
  156. case 'chunked':
  157. return self::decodeChunkedBody($this->body);
  158. break;
  159. // No transfer encoding, or unknown encoding extension:
  160. // return body as is
  161. default:
  162. return $this->body;
  163. break;
  164. }
  165. }
  166. /**
  167. * Get the raw response body (as transfered "on wire") as string
  168. *
  169. * If the body is encoded (with Transfer-Encoding, not content-encoding -
  170. * IE "chunked" body), gzip compressed, etc. it will not be decoded.
  171. *
  172. * @return string
  173. */
  174. public function getRawBody()
  175. {
  176. return $this->body;
  177. }
  178. /**
  179. * Get the HTTP version of the response
  180. *
  181. * @return string
  182. */
  183. public function getVersion()
  184. {
  185. return $this->version;
  186. }
  187. /**
  188. * Get the response headers
  189. *
  190. * @return array
  191. */
  192. public function getHeaders()
  193. {
  194. return $this->headers;
  195. }
  196. /**
  197. * Get a specific header as string, or null if it is not set
  198. *
  199. * @param string$header
  200. * @return string|array|null
  201. */
  202. public function getHeader($header)
  203. {
  204. $header = ucwords(strtolower($header));
  205. if (array_key_exists($header, $this->headers)) {
  206. return $this->headers[$header];
  207. } else {
  208. return null;
  209. }
  210. }
  211. /**
  212. * Get all headers as string
  213. *
  214. * @param boolean $statusLine Whether to return the first status line (ie "HTTP 200 OK")
  215. * @param string $br Line breaks (eg. "\n", "\r\n", "<br />")
  216. * @return string
  217. */
  218. public function getHeadersAsString($statusLine = true, $br = "\n")
  219. {
  220. $str = '';
  221. if ($statusLine) {
  222. $str = "HTTP/{$this->version} {$this->status} {$this->message}{$br}";
  223. }
  224. // Iterate over the headers and stringify them
  225. foreach ($this->headers as $name => $value) {
  226. if (is_string($value)) {
  227. $str .= "{$name}: {$value}{$br}";
  228. } elseif (is_array($value)) {
  229. foreach ($value as $subval) {
  230. $str .= "{$name}: {$subval}{$br}";
  231. }
  232. }
  233. }
  234. return $str;
  235. }
  236. /**
  237. * Create an EasyRdf_Http_Response object from a HTTP response string
  238. *
  239. * @param string $responseStr
  240. * @return EasyRdf_Http_Response
  241. */
  242. public static function fromString($responseStr)
  243. {
  244. // First, split body and headers
  245. $matches = preg_split('|(?:\r?\n){2}|m', $responseStr, 2);
  246. if ($matches and sizeof($matches) == 2) {
  247. list ($headerLines, $body) = $matches;
  248. } else {
  249. throw new EasyRdf_Exception(
  250. "Failed to parse HTTP response."
  251. );
  252. }
  253. // Split headers part to lines
  254. $headerLines = preg_split('|[\r\n]+|m', $headerLines);
  255. $status = array_shift($headerLines);
  256. if (preg_match("|^HTTP/([\d\.x]+) (\d+) ([^\r\n]+)|", $status, $m)) {
  257. $version = $m[1];
  258. $status = $m[2];
  259. $message = $m[3];
  260. } else {
  261. throw new EasyRdf_Exception(
  262. "Failed to parse HTTP response status line."
  263. );
  264. }
  265. // Process the rest of the header lines
  266. $headers = array();
  267. foreach ($headerLines as $line) {
  268. if (preg_match("|^([\w-]+):\s+(.+)$|", $line, $m)) {
  269. $hName = ucwords(strtolower($m[1]));
  270. $hValue = $m[2];
  271. if (isset($headers[$hName])) {
  272. if (! is_array($headers[$hName])) {
  273. $headers[$hName] = array($headers[$hName]);
  274. }
  275. $headers[$hName][] = $hValue;
  276. } else {
  277. $headers[$hName] = $hValue;
  278. }
  279. }
  280. }
  281. return new EasyRdf_Http_Response($status, $headers, $body, $version, $message);
  282. }
  283. /**
  284. * Decode a "chunked" transfer-encoded body and return the decoded text
  285. *
  286. * @param string $body
  287. * @return string
  288. */
  289. public static function decodeChunkedBody($body)
  290. {
  291. $decBody = '';
  292. while (trim($body)) {
  293. if (preg_match('/^([\da-fA-F]+)[^\r\n]*\r\n/sm', $body, $m)) {
  294. $length = hexdec(trim($m[1]));
  295. $cut = strlen($m[0]);
  296. $decBody .= substr($body, $cut, $length);
  297. $body = substr($body, $cut + $length + 2);
  298. } else {
  299. throw new EasyRdf_Exception(
  300. "Failed to decode chunked body in HTTP response."
  301. );
  302. }
  303. }
  304. return $decBody;
  305. }
  306. /**
  307. * Get the entire response as string
  308. *
  309. * @param string $br Line breaks (eg. "\n", "\r\n", "<br />")
  310. * @return string
  311. */
  312. public function asString($br = "\n")
  313. {
  314. return $this->getHeadersAsString(true, $br) . $br . $this->getRawBody();
  315. }
  316. /**
  317. * Implements magic __toString()
  318. *
  319. * @return string
  320. */
  321. public function __toString()
  322. {
  323. return $this->asString();
  324. }
  325. }