123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- if (!defined('GNUSOCIAL')) {
- exit(1);
- }
- require_once 'HTTP/Request2.php';
- require_once 'HTTP/Request2/Response.php';
- 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())
- {
- $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)
- {
- $client = new HTTPClient();
- if (!is_null($accept)) {
- $client->setHeader('Accept', $accept);
- }
- $response = $client->get($url);
- if (!$response->isOk()) {
-
- throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
- }
- return $response->getBody();
- }
-
- public function get($url, $headers=array())
- {
- return $this->doRequest($url, self::METHOD_GET, $headers);
- }
-
- public function head($url, $headers=array())
- {
- return $this->doRequest($url, self::METHOD_HEAD, $headers);
- }
-
- 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, $headers)
- {
- $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);
- if ($headers) {
- foreach ($headers as $header) {
- $this->setHeader($header);
- }
- }
- $response = $this->send();
- 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['follow_redirects'])) {
- $maxRedirs = 0;
- }
- $redirs = 0;
- do {
- try {
- $response = parent::send();
- } catch (HTTP_Request2_Exception $e) {
- $this->log(LOG_ERR, $e->getMessage());
- throw $e;
- }
- $code = $response->getStatus();
- 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);
- }
- }
|