123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class GNUsocial_HTTPResponse extends HTTP_Request2_Response
- {
- function __construct(HTTP_Request2_Response $response, $url, $redirects=0)
- {
- foreach (get_object_vars($response) as $key => $val) {
- $this->$key = $val;
- }
- $this->url = strval($url);
- $this->redirectCount = intval($redirects);
- }
-
- function getRedirectCount()
- {
- return $this->redirectCount;
- }
-
- function getUrl()
- {
- return $this->url;
- }
-
- function isOk()
- {
- $status = $this->getStatus();
- return ($status >= 200 && $status < 300);
- }
- }
- class HTTPClient extends HTTP_Request2
- {
- function __construct($url=null, $method=self::METHOD_GET, $config=array())
- {
- if (is_int(common_config('http', 'timeout'))) {
-
-
-
-
- $this->config['timeout'] = common_config('http', 'timeout');
- } else {
- common_log(LOG_ERR, 'config option http/timeout is not an integer value: '._ve(common_config('http', 'timeout')));
- }
- if (!empty(common_config('http', 'connect_timeout'))) {
- $this->config['connect_timeout'] = common_config('http', 'connect_timeout');
- }
- $this->config['max_redirs'] = 10;
- $this->config['follow_redirects'] = true;
-
-
-
-
-
-
-
- $this->config['protocol_version'] = '1.0';
-
-
-
-
-
-
- if (common_config('http', 'ssl_cafile')) {
- $this->config['ssl_cafile'] = common_config('http', 'ssl_cafile');
- } else {
- $this->config['ssl_verify_peer'] = false;
- }
-
-
- $this->config['ssl_verify_host'] = common_config('http', 'ssl_verify_host');
- if (common_config('http', 'curl') && extension_loaded('curl')) {
- $this->config['adapter'] = 'HTTP_Request2_Adapter_Curl';
- }
- foreach (array('host', 'port', 'user', 'password', 'auth_scheme') as $cf) {
- $k = 'proxy_'.$cf;
- $v = common_config('http', $k);
- if (!empty($v)) {
- $this->config[$k] = $v;
- }
- }
- parent::__construct($url, $method, $config);
- $this->setHeader('User-Agent', self::userAgent());
- }
-
- public static function start()
- {
- return new HTTPClient();
- }
-
- public static function quickGet($url, $accept=null, array $params=array(), array $headers=array())
- {
- if (!empty($params)) {
- $params = http_build_query($params, null, '&');
- if (strpos($url, '?') === false) {
- $url .= '?' . $params;
- } else {
- $url .= '&' . $params;
- }
- }
- $client = new HTTPClient();
- if (!is_null($accept)) {
- $client->setHeader('Accept', $accept);
- }
- $response = $client->get($url, $headers);
- if (!$response->isOk()) {
-
- throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
- }
- return $response->getBody();
- }
- public static function quickGetJson($url, $params=array())
- {
- $data = json_decode(self::quickGet($url, null, $params));
- if (is_null($data)) {
- common_debug('Could not decode JSON data from URL: '.$url);
- throw new ServerException('Could not decode JSON data from URL');
- }
- return $data;
- }
-
- public static function quickHead($url, array $params=array(), array $headers=array())
- {
- if (!empty($params)) {
- $params = http_build_query($params, null, '&');
- if (strpos($url, '?') === false) {
- $url .= '?' . $params;
- } else {
- $url .= '&' . $params;
- }
- }
- $client = new HTTPClient();
- $response = $client->head($url, $headers);
- if (!$response->isOk()) {
-
- throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
- }
- return $response->getHeader();
- }
-
- public function get($url, $headers=array())
- {
- return $this->doRequest($url, self::METHOD_GET, $headers);
- }
-
- public function head($url, $headers=array(), $follow_redirects=false)
- {
-
- $old_follow = $this->config['follow_redirects'];
- try {
-
- $this->config['follow_redirects'] = $follow_redirects;
- return $this->doRequest($url, self::METHOD_HEAD, $headers);
- } catch (Exception $e) {
-
- throw $e;
- } finally {
-
- $this->config['follow_redirects'] = $old_follow;
- }
-
- }
-
- public function post($url, $headers=array(), $data=array())
- {
- if ($data) {
- $this->addPostParameter($data);
- }
- return $this->doRequest($url, self::METHOD_POST, $headers);
- }
-
- protected function doRequest($url, $method, array $headers=array())
- {
- $this->setUrl($url);
-
-
-
- if ($this->url->getScheme() == 'https' && defined('OPENSSL_TLSEXT_SERVER_NAME')) {
- $this->config['ssl_SNI_enabled'] = true;
- $this->config['ssl_SNI_server_name'] = $this->url->getHost();
- }
- $this->setMethod($method);
- foreach ($headers as $header) {
- $this->setHeader($header);
- }
- $response = $this->send();
- if (is_null($response)) {
-
- throw new NoHttpResponseException($url);
- }
- return $response;
- }
- protected function log($level, $detail) {
- $method = $this->getMethod();
- $url = $this->getUrl();
- common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
- }
-
- static public function userAgent()
- {
- return GNUSOCIAL_ENGINE . '/' . GNUSOCIAL_VERSION
- . ' (' . GNUSOCIAL_CODENAME . ')';
- }
-
- public function send()
- {
- $maxRedirs = intval($this->config['max_redirs']);
- if (empty($this->config['max_redirs'])) {
- $maxRedirs = 0;
- }
- $redirs = 0;
- $redirUrls = array();
- do {
- try {
- $response = parent::send();
- } catch (Exception $e) {
- $this->log(LOG_ERR, $e->getMessage());
- throw $e;
- }
- $code = $response->getStatus();
- $effectiveUrl = $response->getEffectiveUrl();
- $redirUrls[] = $effectiveUrl;
- $response->redirUrls = $redirUrls;
- if ($code >= 200 && $code < 300) {
- $reason = $response->getReasonPhrase();
- $this->log(LOG_INFO, "$code $reason");
- } elseif ($code >= 300 && $code < 400) {
- $url = $this->getUrl();
- $target = $response->getHeader('Location');
- if (++$redirs >= $maxRedirs) {
- common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
- break;
- }
- try {
- $this->setUrl($target);
- $this->setHeader('Referer', $url);
- common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
- continue;
- } catch (HTTP_Request2_Exception $e) {
- common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
- }
- } else {
- $reason = $response->getReasonPhrase();
- $this->log(LOG_ERR, "$code $reason");
- }
- break;
- } while ($maxRedirs);
- return new GNUsocial_HTTPResponse($response, $this->getUrl(), $redirs);
- }
- }
|