httpclient.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Utility for doing HTTP-related things
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Action
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) {
  30. exit(1);
  31. }
  32. require_once 'HTTP/Request2.php';
  33. require_once 'HTTP/Request2/Response.php';
  34. /**
  35. * Useful structure for HTTP responses
  36. *
  37. * We make HTTP calls in several places, and we have several different
  38. * ways of doing them. This class hides the specifics of what underlying
  39. * library (curl or PHP-HTTP or whatever) that's used.
  40. *
  41. * This extends the HTTP_Request2_Response class with methods to get info
  42. * about any followed redirects.
  43. *
  44. * Originally used the name 'HTTPResponse' to match earlier code, but
  45. * this conflicts with a class in in the PECL HTTP extension.
  46. *
  47. * @category HTTP
  48. * @package StatusNet
  49. * @author Evan Prodromou <evan@status.net>
  50. * @author Brion Vibber <brion@status.net>
  51. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  52. * @link http://status.net/
  53. */
  54. class GNUsocial_HTTPResponse extends HTTP_Request2_Response
  55. {
  56. function __construct(HTTP_Request2_Response $response, $url, $redirects=0)
  57. {
  58. foreach (get_object_vars($response) as $key => $val) {
  59. $this->$key = $val;
  60. }
  61. $this->url = strval($url);
  62. $this->redirectCount = intval($redirects);
  63. }
  64. /**
  65. * Get the count of redirects that have been followed, if any.
  66. * @return int
  67. */
  68. function getRedirectCount()
  69. {
  70. return $this->redirectCount;
  71. }
  72. /**
  73. * Gets the final target URL, after any redirects have been followed.
  74. * @return string URL
  75. */
  76. function getUrl()
  77. {
  78. return $this->url;
  79. }
  80. /**
  81. * Check if the response is OK, generally a 200 or other 2xx status code.
  82. * @return bool
  83. */
  84. function isOk()
  85. {
  86. $status = $this->getStatus();
  87. return ($status >= 200 && $status < 300);
  88. }
  89. }
  90. /**
  91. * Utility class for doing HTTP client stuff
  92. *
  93. * We make HTTP calls in several places, and we have several different
  94. * ways of doing them. This class hides the specifics of what underlying
  95. * library (curl or PHP-HTTP or whatever) that's used.
  96. *
  97. * This extends the PEAR HTTP_Request2 package:
  98. * - sends StatusNet-specific User-Agent header
  99. * - 'follow_redirects' config option, defaulting off
  100. * - 'max_redirs' config option, defaulting to 10
  101. * - extended response class adds getRedirectCount() and getUrl() methods
  102. * - get() and post() convenience functions return body content directly
  103. *
  104. * @category HTTP
  105. * @package StatusNet
  106. * @author Evan Prodromou <evan@status.net>
  107. * @author Brion Vibber <brion@status.net>
  108. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  109. * @link http://status.net/
  110. */
  111. class HTTPClient extends HTTP_Request2
  112. {
  113. function __construct($url=null, $method=self::METHOD_GET, $config=array())
  114. {
  115. $this->config['max_redirs'] = 10;
  116. $this->config['follow_redirects'] = true;
  117. // We've had some issues with keepalive breaking with
  118. // HEAD requests, such as to youtube which seems to be
  119. // emitting chunked encoding info for an empty body
  120. // instead of not emitting anything. This may be a
  121. // bug on YouTube's end, but the upstream libray
  122. // ought to be investigated to see if we can handle
  123. // it gracefully in that case as well.
  124. $this->config['protocol_version'] = '1.0';
  125. // Default state of OpenSSL seems to have no trusted
  126. // SSL certificate authorities, which breaks hostname
  127. // verification and means we have a hard time communicating
  128. // with other sites' HTTPS interfaces.
  129. //
  130. // Turn off verification unless we've configured a CA bundle.
  131. if (common_config('http', 'ssl_cafile')) {
  132. $this->config['ssl_cafile'] = common_config('http', 'ssl_cafile');
  133. } else {
  134. $this->config['ssl_verify_peer'] = false;
  135. }
  136. // This means "verify the cert hostname against what we connect to", it does not
  137. // imply CA trust or anything like that. Just the hostname.
  138. $this->config['ssl_verify_host'] = common_config('http', 'ssl_verify_host');
  139. if (common_config('http', 'curl') && extension_loaded('curl')) {
  140. $this->config['adapter'] = 'HTTP_Request2_Adapter_Curl';
  141. }
  142. foreach (array('host', 'port', 'user', 'password', 'auth_scheme') as $cf) {
  143. $k = 'proxy_'.$cf;
  144. $v = common_config('http', $k);
  145. if (!empty($v)) {
  146. $this->config[$k] = $v;
  147. }
  148. }
  149. parent::__construct($url, $method, $config);
  150. $this->setHeader('User-Agent', self::userAgent());
  151. }
  152. /**
  153. * Convenience/back-compat instantiator
  154. * @return HTTPClient
  155. */
  156. public static function start()
  157. {
  158. return new HTTPClient();
  159. }
  160. /**
  161. * Quick static function to GET a URL
  162. */
  163. public static function quickGet($url, $accept=null)
  164. {
  165. $client = new HTTPClient();
  166. if (!is_null($accept)) {
  167. $client->setHeader('Accept', $accept);
  168. }
  169. $response = $client->get($url);
  170. if (!$response->isOk()) {
  171. // TRANS: Exception. %s is a profile URL.
  172. throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
  173. }
  174. return $response->getBody();
  175. }
  176. /**
  177. * Convenience function to run a GET request.
  178. *
  179. * @return GNUsocial_HTTPResponse
  180. * @throws HTTP_Request2_Exception
  181. */
  182. public function get($url, $headers=array())
  183. {
  184. return $this->doRequest($url, self::METHOD_GET, $headers);
  185. }
  186. /**
  187. * Convenience function to run a HEAD request.
  188. *
  189. * @return GNUsocial_HTTPResponse
  190. * @throws HTTP_Request2_Exception
  191. */
  192. public function head($url, $headers=array())
  193. {
  194. return $this->doRequest($url, self::METHOD_HEAD, $headers);
  195. }
  196. /**
  197. * Convenience function to POST form data.
  198. *
  199. * @param string $url
  200. * @param array $headers optional associative array of HTTP headers
  201. * @param array $data optional associative array or blob of form data to submit
  202. * @return GNUsocial_HTTPResponse
  203. * @throws HTTP_Request2_Exception
  204. */
  205. public function post($url, $headers=array(), $data=array())
  206. {
  207. if ($data) {
  208. $this->addPostParameter($data);
  209. }
  210. return $this->doRequest($url, self::METHOD_POST, $headers);
  211. }
  212. /**
  213. * @return GNUsocial_HTTPResponse
  214. * @throws HTTP_Request2_Exception
  215. */
  216. protected function doRequest($url, $method, $headers)
  217. {
  218. $this->setUrl($url);
  219. // Workaround for HTTP_Request2 not setting up SNI in socket contexts;
  220. // This fixes cert validation for SSL virtual hosts using SNI.
  221. // Requires PHP 5.3.2 or later and OpenSSL with SNI support.
  222. if ($this->url->getScheme() == 'https' && defined('OPENSSL_TLSEXT_SERVER_NAME')) {
  223. $this->config['ssl_SNI_enabled'] = true;
  224. $this->config['ssl_SNI_server_name'] = $this->url->getHost();
  225. }
  226. $this->setMethod($method);
  227. if ($headers) {
  228. foreach ($headers as $header) {
  229. $this->setHeader($header);
  230. }
  231. }
  232. $response = $this->send();
  233. return $response;
  234. }
  235. protected function log($level, $detail) {
  236. $method = $this->getMethod();
  237. $url = $this->getUrl();
  238. common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
  239. }
  240. /**
  241. * Pulls up GNU Social's customized user-agent string, so services
  242. * we hit can track down the responsible software.
  243. *
  244. * @return string
  245. */
  246. static public function userAgent()
  247. {
  248. return GNUSOCIAL_ENGINE . '/' . GNUSOCIAL_VERSION
  249. . ' (' . GNUSOCIAL_CODENAME . ')';
  250. }
  251. /**
  252. * Actually performs the HTTP request and returns a
  253. * GNUsocial_HTTPResponse object with response body and header info.
  254. *
  255. * Wraps around parent send() to add logging and redirection processing.
  256. *
  257. * @return GNUsocial_HTTPResponse
  258. * @throw HTTP_Request2_Exception
  259. */
  260. public function send()
  261. {
  262. $maxRedirs = intval($this->config['max_redirs']);
  263. if (empty($this->config['follow_redirects'])) {
  264. $maxRedirs = 0;
  265. }
  266. $redirs = 0;
  267. do {
  268. try {
  269. $response = parent::send();
  270. } catch (HTTP_Request2_Exception $e) {
  271. $this->log(LOG_ERR, $e->getMessage());
  272. throw $e;
  273. }
  274. $code = $response->getStatus();
  275. if ($code >= 200 && $code < 300) {
  276. $reason = $response->getReasonPhrase();
  277. $this->log(LOG_INFO, "$code $reason");
  278. } elseif ($code >= 300 && $code < 400) {
  279. $url = $this->getUrl();
  280. $target = $response->getHeader('Location');
  281. if (++$redirs >= $maxRedirs) {
  282. common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
  283. break;
  284. }
  285. try {
  286. $this->setUrl($target);
  287. $this->setHeader('Referer', $url);
  288. common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
  289. continue;
  290. } catch (HTTP_Request2_Exception $e) {
  291. common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
  292. }
  293. } else {
  294. $reason = $response->getReasonPhrase();
  295. $this->log(LOG_ERR, "$code $reason");
  296. }
  297. break;
  298. } while ($maxRedirs);
  299. return new GNUsocial_HTTPResponse($response, $this->getUrl(), $redirs);
  300. }
  301. }