HttpSocketLib.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. App::uses('HttpSocket', 'Network/Http');
  3. App::uses('CurlLib', 'Tools.Lib');
  4. /**
  5. * Wrapper for curl, php or file_get_contents with some fixes or improvements:
  6. * - All 2xx OK status codes will return the proper result
  7. * - Response will be properly utf8 encoded on WINDOWS, as well
  8. * - Timeout is reduced to 5 by default and can easily be adjusted
  9. * - file_get_contents wrapper is fixed for HTTP1.1 to default to "Connection: close"
  10. * to avoid leaving the connection open
  11. * - Caching possibilities included
  12. * - Auto-Fallback if curl is not available
  13. *
  14. * TODO: throw exceptions instead of error stuff here
  15. *
  16. * @author Mark Scherer
  17. * @license http://opensource.org/licenses/mit-license.php MIT
  18. */
  19. class HttpSocketLib {
  20. // First tries with curl, then cake, then php
  21. public $use = ['curl' => true, 'cake' => true, 'php' => true];
  22. public $debug = null;
  23. public $timeout = 5;
  24. public $cacheUsed = null;
  25. public $error = [];
  26. public $allowRedirects = [301];
  27. public function __construct($use = []) {
  28. if (is_array($use)) {
  29. foreach ($use as $key => $value) {
  30. if (array_key_exists($key, $this->use)) {
  31. $this->use[$key] = $value;
  32. }
  33. }
  34. } elseif (array_key_exists($use, $this->use)) {
  35. $this->use[$use] = true;
  36. if ($use === 'cake') {
  37. $this->use['curl'] = false;
  38. } elseif ($use === 'php') {
  39. $this->use['curl'] = $this->use['cake'] = false;
  40. }
  41. }
  42. }
  43. /**
  44. * @param string $error
  45. */
  46. public function setError($error) {
  47. if (empty($error)) {
  48. return;
  49. }
  50. $this->error[] = $error;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function error($asString = true, $separator = ', ') {
  56. return implode(', ', $this->error);
  57. }
  58. public function reset() {
  59. $this->error = [];
  60. $this->debug = null;
  61. }
  62. /**
  63. * Fetches url with curl if available
  64. * fallbacks: cake and php
  65. * note: expects url with json encoded content
  66. *
  67. * @param string $url
  68. * @param array $options
  69. * @return string Response or false on failure
  70. */
  71. public function fetch($url, $options = []) {
  72. if (!is_array($options)) {
  73. $options = ['agent' => $options];
  74. }
  75. $defaults = [
  76. 'agent' => 'cakephp http socket lib',
  77. 'cache' => false,
  78. 'clearCache' => false,
  79. 'use' => $this->use,
  80. 'timeout' => $this->timeout,
  81. ];
  82. $options += $defaults;
  83. // cached?
  84. if ($options['cache']) {
  85. $cacheName = md5($url);
  86. $cacheConfig = $options['cache'] === true ? null : $options['cache'];
  87. $cacheConfig = !Cache::isInitialized($cacheConfig) ? null : $cacheConfig;
  88. if ($options['clearCache']) {
  89. Cache::delete('http_' . $cacheName, $cacheConfig);
  90. } elseif (($res = Cache::read('http_' . $cacheName, $cacheConfig)) !== false && $res !== null) {
  91. $this->cacheUsed = true;
  92. return $res;
  93. }
  94. }
  95. $res = $this->_fetch($url, $options);
  96. if ($options['cache']) {
  97. Cache::write('http_' . $cacheName, $res, $cacheConfig);
  98. }
  99. return $res;
  100. }
  101. /**
  102. * @param string $url
  103. * @param array $options
  104. * @return string Response or false on failure
  105. */
  106. public function _fetch($url, $options) {
  107. $allowedCodes = array_merge($this->allowRedirects, [200, 201, 202, 203, 204, 205, 206]);
  108. if ($options['use']['curl'] && function_exists('curl_init')) {
  109. $this->debug = 'curl';
  110. $Ch = new CurlLib();
  111. $Ch->setUserAgent($options['agent']);
  112. $data = $Ch->get($url);
  113. $response = $data[0];
  114. $statusCode = $data[1]['http_code'];
  115. if (!in_array($statusCode, $allowedCodes)) {
  116. $this->setError('Error ' . $statusCode);
  117. return false;
  118. }
  119. $response = $this->_assertEncoding($response);
  120. return $response;
  121. }
  122. if ($options['use']['cake']) {
  123. $this->debug = 'cake';
  124. $HttpSocket = new HttpSocket(['timeout' => $options['timeout']]);
  125. $response = $HttpSocket->get($url);
  126. if (!in_array($response->code, $allowedCodes)) {
  127. return false;
  128. }
  129. $response = $this->_assertEncoding($response);
  130. return $response;
  131. }
  132. if ($options['use']['php']) {
  133. $this->debug = 'php';
  134. $opts = [
  135. 'http' => [
  136. 'method' => 'GET',
  137. 'header' => ['Connection: close'],
  138. 'timeout' => $options['timeout']
  139. ]
  140. ];
  141. if (isset($options['http'])) {
  142. $opts['http'] = array_merge($opts['http'], $options['http']);
  143. }
  144. if (is_array($opts['http']['header'])) {
  145. $opts['http']['header'] = implode(PHP_EOL, $opts['http']['header']);
  146. }
  147. $context = stream_context_create($opts);
  148. $response = file_get_contents($url, false, $context);
  149. if (!isset($httpResponseHeader)) {
  150. return false;
  151. }
  152. preg_match('/^HTTP.*\s([0-9]{3})/', $httpResponseHeader[0], $matches);
  153. $statusCode = (int)$matches[1];
  154. if (!in_array($statusCode, $allowedCodes)) {
  155. return false;
  156. }
  157. $response = $this->_assertEncoding($response);
  158. return $response;
  159. }
  160. throw new CakeException('no protocol given');
  161. }
  162. /**
  163. * It seems all three methods have encoding issues if not run through this method
  164. *
  165. * @param string $response
  166. * @param string Correctly encoded response
  167. */
  168. protected function _assertEncoding($response) {
  169. if (!defined('WINDOWS') || !WINDOWS) {
  170. return $response;
  171. }
  172. $x = mb_detect_encoding($response, 'auto', true);
  173. if ($x !== 'UTF-8') {
  174. $response = iconv(null, "utf-8", $response);
  175. }
  176. return $response;
  177. }
  178. }