3 * StatusNet, the distributed open-source microblogging tool
5 * Utility for doing HTTP-related things
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
33 * Useful structure for HTTP responses
35 * We make HTTP calls in several places, and we have several different
36 * ways of doing them. This class hides the specifics of what underlying
37 * library (curl or PHP-HTTP or whatever) that's used.
39 * This extends the HTTP_Request2_Response class with methods to get info
40 * about any followed redirects.
42 * Originally used the name 'HTTPResponse' to match earlier code, but
43 * this conflicts with a class in in the PECL HTTP extension.
47 * @author Evan Prodromou <evan@status.net>
48 * @author Brion Vibber <brion@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 class GNUsocial_HTTPResponse extends HTTP_Request2_Response
54 function __construct(HTTP_Request2_Response $response, $url, $redirects=0)
56 foreach (get_object_vars($response) as $key => $val) {
59 $this->url = strval($url);
60 $this->redirectCount = intval($redirects);
64 * Get the count of redirects that have been followed, if any.
67 function getRedirectCount()
69 return $this->redirectCount;
73 * Gets the target URL, before any redirects. Use getEffectiveUrl() for final target.
82 * Check if the response is OK, generally a 200 or other 2xx status code.
87 $status = $this->getStatus();
88 return ($status >= 200 && $status < 300);
93 * Utility class for doing HTTP client stuff
95 * We make HTTP calls in several places, and we have several different
96 * ways of doing them. This class hides the specifics of what underlying
97 * library (curl or PHP-HTTP or whatever) that's used.
99 * This extends the PEAR HTTP_Request2 package:
100 * - sends StatusNet-specific User-Agent header
101 * - 'follow_redirects' config option, defaulting on
102 * - 'max_redirs' config option, defaulting to 10
103 * - extended response class adds getRedirectCount() and getUrl() methods
104 * - get() and post() convenience functions return body content directly
108 * @author Evan Prodromou <evan@status.net>
109 * @author Brion Vibber <brion@status.net>
110 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
111 * @link http://status.net/
114 class HTTPClient extends HTTP_Request2
117 function __construct($url=null, $method=self::METHOD_GET, $config=array())
119 if (is_int(common_config('http', 'timeout'))) {
120 // Reasonably you shouldn't set http/timeout to 0 because of
121 // malicious remote servers that can cause infinitely long
122 // responses... But the default in HTTP_Request2 is 0 for
123 // some reason and should probably be considered a valid value.
124 $this->config['timeout'] = common_config('http', 'timeout');
125 common_debug('Using HTTPClient timeout value of '._ve($this->config['timeout']));
127 common_log(LOG_ERR, 'config option http/timeout is not an integer value: '._ve(common_config('http', 'timeout')));
129 $this->config['connect_timeout'] = common_config('http', 'connect_timeout') ?: $this->config['connect_timeout'];
130 $this->config['max_redirs'] = 10;
131 $this->config['follow_redirects'] = true;
133 // We've had some issues with keepalive breaking with
134 // HEAD requests, such as to youtube which seems to be
135 // emitting chunked encoding info for an empty body
136 // instead of not emitting anything. This may be a
137 // bug on YouTube's end, but the upstream libray
138 // ought to be investigated to see if we can handle
139 // it gracefully in that case as well.
140 $this->config['protocol_version'] = '1.0';
142 // Default state of OpenSSL seems to have no trusted
143 // SSL certificate authorities, which breaks hostname
144 // verification and means we have a hard time communicating
145 // with other sites' HTTPS interfaces.
147 // Turn off verification unless we've configured a CA bundle.
148 if (common_config('http', 'ssl_cafile')) {
149 $this->config['ssl_cafile'] = common_config('http', 'ssl_cafile');
151 $this->config['ssl_verify_peer'] = false;
154 // This means "verify the cert hostname against what we connect to", it does not
155 // imply CA trust or anything like that. Just the hostname.
156 $this->config['ssl_verify_host'] = common_config('http', 'ssl_verify_host');
158 if (common_config('http', 'curl') && extension_loaded('curl')) {
159 $this->config['adapter'] = 'HTTP_Request2_Adapter_Curl';
162 foreach (array('host', 'port', 'user', 'password', 'auth_scheme') as $cf) {
164 $v = common_config('http', $k);
166 $this->config[$k] = $v;
170 parent::__construct($url, $method, $config);
171 $this->setHeader('User-Agent', self::userAgent());
175 * Convenience/back-compat instantiator
178 public static function start()
180 return new HTTPClient();
184 * Quick static function to GET a URL
186 public static function quickGet($url, $accept=null, array $params=array(), array $headers=array())
188 if (!empty($params)) {
189 $params = http_build_query($params, null, '&');
190 if (strpos($url, '?') === false) {
191 $url .= '?' . $params;
193 $url .= '&' . $params;
197 $client = new HTTPClient();
198 if (!is_null($accept)) {
199 $client->setHeader('Accept', $accept);
201 $response = $client->get($url, $headers);
202 if (!$response->isOk()) {
203 // TRANS: Exception. %s is the URL we tried to GET.
204 throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
206 return $response->getBody();
209 public static function quickGetJson($url, $params=array())
211 $data = json_decode(self::quickGet($url, null, $params));
212 if (is_null($data)) {
213 common_debug('Could not decode JSON data from URL: '.$url);
214 throw new ServerException('Could not decode JSON data from URL');
220 * If you want an Accept header, put it in $headers
222 public static function quickHead($url, array $params=array(), array $headers=array())
224 if (!empty($params)) {
225 $params = http_build_query($params, null, '&');
226 if (strpos($url, '?') === false) {
227 $url .= '?' . $params;
229 $url .= '&' . $params;
233 $client = new HTTPClient();
234 $response = $client->head($url, $headers);
235 if (!$response->isOk()) {
236 // TRANS: Exception. %s is the URL we tried to GET.
237 throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
239 return $response->getHeader();
243 * Convenience function to run a GET request.
245 * @return GNUsocial_HTTPResponse
246 * @throws HTTP_Request2_Exception
248 public function get($url, $headers=array())
250 return $this->doRequest($url, self::METHOD_GET, $headers);
254 * Convenience function to run a HEAD request.
256 * NOTE: Will probably turn into a GET request if you let it follow redirects!
257 * That option is only there to be flexible and may be removed in the future!
259 * @return GNUsocial_HTTPResponse
260 * @throws HTTP_Request2_Exception
262 public function head($url, $headers=array(), $follow_redirects=false)
264 // Save the configured value for follow_redirects
265 $old_follow = $this->config['follow_redirects'];
267 // Temporarily (possibly) override the follow_redirects setting
268 $this->config['follow_redirects'] = $follow_redirects;
269 return $this->doRequest($url, self::METHOD_HEAD, $headers);
270 } catch (Exception $e) {
271 // Let the exception go on its merry way.
274 // reset to the old value
275 $this->config['follow_redirects'] = $old_follow;
277 //we've either returned or thrown exception here
281 * Convenience function to POST form data.
284 * @param array $headers optional associative array of HTTP headers
285 * @param array $data optional associative array or blob of form data to submit
286 * @return GNUsocial_HTTPResponse
287 * @throws HTTP_Request2_Exception
289 public function post($url, $headers=array(), $data=array())
292 $this->addPostParameter($data);
294 return $this->doRequest($url, self::METHOD_POST, $headers);
298 * @param string $url The URL including possible querystring
299 * @param string $method The HTTP method to use
300 * @param array $headers List of already formatted strings
301 * (not an associative array, to allow
302 * multiple same-named headers)
304 * @return GNUsocial_HTTPResponse
305 * @throws HTTP_Request2_Exception
307 protected function doRequest($url, $method, array $headers=array())
311 // Workaround for HTTP_Request2 not setting up SNI in socket contexts;
312 // This fixes cert validation for SSL virtual hosts using SNI.
313 // Requires PHP 5.3.2 or later and OpenSSL with SNI support.
314 if ($this->url->getScheme() == 'https' && defined('OPENSSL_TLSEXT_SERVER_NAME')) {
315 $this->config['ssl_SNI_enabled'] = true;
316 $this->config['ssl_SNI_server_name'] = $this->url->getHost();
319 $this->setMethod($method);
320 foreach ($headers as $header) {
321 $this->setHeader($header);
323 $response = $this->send();
324 if (is_null($response)) {
325 // TRANS: Failed to retrieve a remote web resource, %s is the target URL.
326 throw new NoHttpResponseException($url);
331 protected function log($level, $detail) {
332 $method = $this->getMethod();
333 $url = $this->getUrl();
334 common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
338 * Pulls up GNU Social's customized user-agent string, so services
339 * we hit can track down the responsible software.
343 static public function userAgent()
345 return GNUSOCIAL_ENGINE . '/' . GNUSOCIAL_VERSION
346 . ' (' . GNUSOCIAL_CODENAME . ')';
350 * Actually performs the HTTP request and returns a
351 * GNUsocial_HTTPResponse object with response body and header info.
353 * Wraps around parent send() to add logging and redirection processing.
355 * @return GNUsocial_HTTPResponse
356 * @throw HTTP_Request2_Exception
358 public function send()
360 $maxRedirs = intval($this->config['max_redirs']);
361 if (empty($this->config['max_redirs'])) {
365 $redirUrls = array();
368 $response = parent::send();
369 } catch (Exception $e) {
370 $this->log(LOG_ERR, $e->getMessage());
373 $code = $response->getStatus();
374 $effectiveUrl = $response->getEffectiveUrl();
375 $redirUrls[] = $effectiveUrl;
376 $response->redirUrls = $redirUrls;
377 if ($code >= 200 && $code < 300) {
378 $reason = $response->getReasonPhrase();
379 $this->log(LOG_INFO, "$code $reason");
380 } elseif ($code >= 300 && $code < 400) {
381 $url = $this->getUrl();
382 $target = $response->getHeader('Location');
384 if (++$redirs >= $maxRedirs) {
385 common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
389 $this->setUrl($target);
390 $this->setHeader('Referer', $url);
391 common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
393 } catch (HTTP_Request2_Exception $e) {
394 common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
397 $reason = $response->getReasonPhrase();
398 $this->log(LOG_ERR, "$code $reason");
401 } while ($maxRedirs);
402 return new GNUsocial_HTTPResponse($response, $this->getUrl(), $redirs);