/**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $body = new Body();
     $body->append($this->prepareBody($internalRequest));
     $request = new Request($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $body);
     $httpVersion = $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0 ? \http\Client\Curl\HTTP_VERSION_1_0 : \http\Client\Curl\HTTP_VERSION_1_1;
     $request->setOptions(array('protocol' => $httpVersion, 'timeout' => $this->getConfiguration()->getTimeout()));
     try {
         $this->client->reset()->enqueue($request)->send();
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     $response = $this->client->getResponse();
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->getResponseCode(), $response->getHttpVersion(), $response->getHeaders(), $response->getBody()->getResource());
 }
Example #2
0
 /**
  * Gets configured http Request
  *
  * @return Request
  */
 private function getRequest()
 {
     if (!$this->request) {
         $opt = ['timeout' => 15];
         if (\Scalr::config('scalr.aws.use_proxy')) {
             $proxy = \Scalr::config('scalr.connections.proxy');
             if (in_array($proxy['use_on'], array('both', 'scalr'))) {
                 $opt['proxyhost'] = $proxy['host'];
                 $opt['proxyport'] = $proxy['port'];
                 $opt['proxytype'] = $proxy['type'];
                 if (!empty($proxy['pass']) && !empty($proxy['user'])) {
                     $opt['proxyauth'] = "{$proxy['user']}:{$proxy['pass']}";
                     $opt['proxyauthtype'] = $proxy['authtype'];
                 }
             }
         }
         $this->request = new Request("GET");
         $this->request->setOptions($opt);
     } else {
         $this->request->setOptions(['cookiesession' => true]);
     }
     return $this->request;
 }
Example #3
0
 public function getContent($params = array())
 {
     /* @var $endpoint Endpoint */
     $endpoint = $this->getEnvironment()->cloudCredentials(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE] == Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_GOV_CLOUD ? new GovEndpoint() : new Endpoint();
     $data = [];
     $services = ['Amazon Elastic Compute Cloud', 'Amazon Relational Database Service', 'Amazon Simple Storage Service'];
     $awsCachePath = CACHEPATH . "/{$endpoint->cacheFile}";
     if (empty($params['locations'])) {
         $neededLocations = $this->getUsedLocations();
         $params['locations'] = $neededLocations;
     } else {
         $neededLocations = $params['locations'];
     }
     if (file_exists($awsCachePath) && time() - filemtime($awsCachePath) < 3600) {
         clearstatcache();
         $time = filemtime($awsCachePath);
         $data = (array) json_decode(file_get_contents($awsCachePath));
     } else {
         $req = new Request();
         $req->setOptions(array('redirect' => 10, 'verifypeer' => false, 'verifyhost' => false, 'timeout' => 30, 'connecttimeout' => 30, 'cookiesession' => true));
         if (\Scalr::config('scalr.aws.use_proxy') && in_array(\Scalr::config('scalr.connections.proxy.use_on'), array('both', 'scalr'))) {
             $proxySettings = \Scalr::config('scalr.connections.proxy');
         }
         if (!empty($proxySettings)) {
             $req->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]);
             if ($proxySettings['user']) {
                 $req->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]);
             }
         }
         $req->setRequestMethod("GET");
         $req->setRequestUrl($endpoint->statUrl);
         try {
             $response = \Scalr::getContainer()->http->sendRequest($req);
             if ($response->getResponseCode() == 200) {
                 $html = $response->getBody()->toString();
             } else {
                 return [];
             }
         } catch (\http\Exception $e) {
             return [];
         }
         //$html = @file_get_contents($endpoint->statUrl);
         if ($html) {
             $dom = new DOMDocument();
             $dom->validateOnParse = false;
             @$dom->loadHTML($html);
             $dom->preserveWhiteSpace = false;
             foreach ($endpoint->compliance as $compKey => $compValue) {
                 $div = $dom->getElementById($compValue['name']);
                 $tables = $div->getElementsByTagName('table');
                 $rows = $tables->item(0)->getElementsByTagName('tr');
                 foreach ($rows as $row) {
                     $cols = $row->getElementsByTagName('td');
                     if ($cols->length == 0) {
                         continue;
                     }
                     if (preg_match('/(.*)(' . implode('|', $services) . ')(.*)/', $cols->item(1)->nodeValue)) {
                         $regionFilter = $compValue['filter'];
                         if (is_array($compValue['filter'])) {
                             $regionFilter = implode('|', $compValue['filter']);
                         }
                         if (preg_match('/(.*)(' . $regionFilter . ')(.*)/', $cols->item(1)->nodeValue)) {
                             $img = '';
                             $message = '';
                             if ($cols->item(0)->getElementsByTagName('img')->item(0)->getAttribute('src') == '/images/status0.gif') {
                                 $img = 'normal.png';
                             } else {
                                 $img = 'disruption.png';
                                 $message = $cols->item(2)->nodeValue;
                             }
                             $data[$compKey][substr(str_replace($services, array('EC2', 'RDS', 'S3'), $cols->item(1)->nodeValue), 0, strpos(str_replace($services, array('EC2', 'RDS', 'S3'), $cols->item(1)->nodeValue), ' ('))] = array('img' => $img, 'status' => $cols->item(2)->nodeValue, 'message' => $message);
                             $data[$compKey]['locations'] = $compKey;
                         }
                     }
                 }
             }
             file_put_contents($awsCachePath, json_encode($data));
         } else {
             return [];
         }
     }
     $retval = array('locations' => json_encode($neededLocations));
     foreach ($neededLocations as $value) {
         $retval['data'][] = $data[$value];
     }
     return $retval;
 }
Example #4
0
 protected function sendRequest($uri, $method, $data = null)
 {
     try {
         $url = new \http\Url("https://{$this->active_domain}.chargify.com{$uri}", null, \http\Url::PARSE_MBLOC | \http\Url::PARSE_MBUTF8 | \http\Url::PARSE_TOPCT);
         $request = new Request($method, $url);
         $request->setHeaders(array('Content-Type' => 'application/xml'));
         $request->setOptions(array('httpauth' => "{$this->active_api_key}:x", 'timeout' => 45, 'connecttimeout' => 45));
         if ($data) {
             $request->getBody()->append($data);
         }
     } catch (Exception $e) {
         //TODO:
         throw $e;
     }
     $response = (new \http\Client())->enqueue($request)->send()->getResponse($request);
     return $response;
 }
Example #5
0
 protected function sendRequest($uri, $method, $data = null)
 {
     try {
         $url = new \http\Url("https://{$this->domain}.chargify.com{$uri}", null, \http\Url::PARSE_MBLOC | \http\Url::PARSE_MBUTF8 | \http\Url::PARSE_TOPCT);
         $request = new Request($method, $url);
         $request->setHeaders(array('Content-Type' => 'application/json', 'Authorization' => 'Basic ' . base64_encode("{$this->apiKey}:x")));
         $request->setOptions(array('httpauth' => "{$this->apiKey}:x", 'timeout' => 45, 'connecttimeout' => 45, 'ssl' => array('version' => 6)));
         $request->getBody()->append($data);
     } catch (Exception $e) {
         throw $e;
     }
     $response = (new \http\Client())->enqueue($request)->send()->getResponse($request);
     if ($response->getResponseCode() == 500) {
         throw new Exception("Unable to proceed your request at the moment. Please try again later.");
     }
     if ($response->getResponseCode() == 404) {
         throw new Exception("Unable to proceed your request. Please contact billing@scalr.net to get help.");
     }
     return $response;
 }