/** * Load contents of remote URI. Behaves pretty much like * `@file_get_contents($uri)` but doesn't require `allow_url_fopen`. * * @param string * @param float * @return string|false */ public static function loadContent($uri, $timeout = null) { $future = new HTTPSFuture($uri); if ($timeout !== null) { $future->setTimeout($timeout); } try { list($body) = $future->resolvex(); return $body; } catch (HTTPFutureResponseStatus $ex) { return false; } }
public function callMethod($method, array $params) { $meta = array(); if ($this->sessionKey) { $meta['sessionKey'] = $this->sessionKey; } if ($this->connectionID) { $meta['connectionID'] = $this->connectionID; } if ($method == 'conduit.connect') { $certificate = idx($params, 'certificate'); if ($certificate) { $token = time(); $params['authToken'] = $token; $params['authSignature'] = sha1($token . $certificate); } unset($params['certificate']); } if ($meta) { $params['__conduit__'] = $meta; } $uri = id(clone $this->uri)->setPath('/api/' . $method); $data = array('params' => json_encode($params), 'output' => 'json', '__conduit__' => true); // Always use the cURL-based HTTPSFuture, for proxy support and other // protocol edge cases that HTTPFuture does not support. $core_future = new HTTPSFuture($uri, $data); $core_future->setMethod('POST'); $core_future->setTimeout($this->timeout); if ($this->username !== null) { $core_future->setHTTPBasicAuthCredentials($this->username, $this->password); } $conduit_future = new ConduitFuture($core_future); $conduit_future->setClient($this, $method); $conduit_future->beginProfile($data); $conduit_future->isReady(); return $conduit_future; }
private function executeRequest($path, array $data, $is_write = false) { $uri = new PhutilURI($this->uri); $data = json_encode($data); $uri->setPath($path); $future = new HTTPSFuture($uri, $data); if ($is_write) { $future->setMethod('PUT'); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($is_write) { return null; } $body = json_decode($body, true); if (!is_array($body)) { throw new Exception("elasticsearch server returned invalid JSON!"); } return $body; }
private function executeRequest($path, array $data, $method = 'GET') { $uri = new PhutilURI($this->uri); $uri->setPath($this->index); $uri->appendPath($path); $data = json_encode($data); $future = new HTTPSFuture($uri, $data); if ($method != 'GET') { $future->setMethod($method); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($method != 'GET') { return null; } try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('ElasticSearch server returned invalid JSON!'), $ex); } }
public function processRequest() { $provider = $this->provider; $auth_enabled = $provider->isProviderEnabled(); $client_id = $provider->getClientID(); $client_secret = $provider->getClientSecret(); $key = $provider->getProviderKey(); $name = phutil_escape_html($provider->getProviderName()); $res_ok = '<strong style="color: #00aa00;">OK</strong>'; $res_no = '<strong style="color: #aa0000;">NO</strong>'; $res_na = '<strong style="color: #999999;">N/A</strong>'; $results = array(); $auth_key = $key . '.auth-enabled'; if (!$auth_enabled) { $results[$auth_key] = array($res_no, 'false', $name . ' authentication is disabled in the configuration. Edit the ' . 'Phabricator configuration to enable "' . $auth_key . '".'); } else { $results[$auth_key] = array($res_ok, 'true', $name . ' authentication is enabled.'); } $client_id_key = $key . '.application-id'; if (!$client_id) { $results[$client_id_key] = array($res_no, null, 'No ' . $name . ' Application ID is configured. Edit the Phabricator ' . 'configuration to specify an application ID in ' . '"' . $client_id_key . '". ' . $provider->renderGetClientIDHelp()); } else { $results[$client_id_key] = array($res_ok, $client_id, 'Application ID is set.'); } $client_secret_key = $key . '.application-secret'; if (!$client_secret) { $results[$client_secret_key] = array($res_no, null, 'No ' . $name . ' Application secret is configured. Edit the ' . 'Phabricator configuration to specify an Application Secret, in ' . '"' . $client_secret_key . '". ' . $provider->renderGetClientSecretHelp()); } else { $results[$client_secret_key] = array($res_ok, "It's a secret!", 'Application secret is set.'); } $timeout = 5; $internet = HTTPSFuture::loadContent("http://google.com/", $timeout); if ($internet === false) { $results['internet'] = array($res_no, null, 'Unable to make an HTTP request to Google. Check your outbound ' . 'internet connection and firewall/filtering settings.'); } else { $results['internet'] = array($res_ok, null, 'Internet seems OK.'); } $test_uris = $provider->getTestURIs(); foreach ($test_uris as $uri) { $success = HTTPSFuture::loadContent($uri, $timeout); if ($success === false) { $results[$uri] = array($res_no, null, "Unable to make an HTTP request to {$uri}. {$name} may be " . 'down or inaccessible.'); } else { $results[$uri] = array($res_ok, null, 'Made a request to ' . $uri . '.'); } } if ($provider->shouldDiagnoseAppLogin()) { $test_uri = new PhutilURI($provider->getTokenURI()); $test_uri->setQueryParams(array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials')); $future = new HTTPSFuture($test_uri); $future->setTimeout($timeout); try { list($body) = $future->resolvex(); $results['App Login'] = array($res_ok, '(A Valid Token)', "Raw application login to {$name} works."); } catch (Exception $ex) { if ($ex instanceof HTTPFutureResponseStatusCURL) { $results['App Login'] = array($res_no, null, "Unable to perform an application login with your Application ID " . "and Application Secret. You may have mistyped or misconfigured " . "them; {$name} may have revoked your authorization; or {$name} " . "may be having technical problems."); } else { $data = json_decode($token_value, true); if (!is_array($data)) { $results['App Login'] = array($res_no, $token_value, "Application Login failed but the provider did not respond " . "with valid JSON error information. {$name} may be experiencing " . "technical problems."); } else { $results['App Login'] = array($res_no, null, "Application Login failed with error: " . $token_value); } } } } return $this->renderResults($results); }
#!/usr/bin/env php <?php $root = dirname(dirname(dirname(__FILE__))); require_once $root . '/scripts/__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->parseStandardArguments(); $args->parse(array(array('name' => 'attach', 'param' => 'file', 'help' => pht('Attach a file to the request.')), array('name' => 'url', 'wildcard' => true))); $uri = $args->getArg('url'); if (count($uri) !== 1) { throw new PhutilArgumentUsageException("Specify exactly one URL to retrieve."); } $uri = head($uri); $method = 'GET'; $data = ''; $timeout = 30; $future = new HTTPSFuture($uri, $data); $future->setMethod($method); $future->setTimeout($timeout); $attach_file = $args->getArg('attach'); if ($attach_file !== null) { $future->attachFileData('file', Filesystem::readFile($attach_file), basename($attach_file), Filesystem::getMimeType($attach_file)); } print_r($future->resolve());