예제 #1
0
 public function send()
 {
     if (is_null($this->getBody())) {
         $body = null;
     } else {
         $body = new Body();
         $body->append($this->getBody());
     }
     $request = new Request($this->getMethod(), 'https://' . $this->getUrl(), $this->getHeaders(), $body);
     $request->addQuery($this->getQuery());
     $client = new Client();
     $client->enqueue($request)->send();
     $this->response = $client->getResponse();
 }
예제 #2
0
 /**
  * {@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());
 }
예제 #3
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;
 }
예제 #4
0
 /**
  * {@inheritdoc}
  * @see \Scalr\Service\Azure\Client\ClientResponseInterface::hasError()
  */
 public function hasError()
 {
     if (!isset($this->errorData)) {
         $this->errorData = false;
         $code = $this->getResponseCode();
         if ($code < 200 || $code > 299) {
             $this->errorData = new ErrorData();
             $responseObj = json_decode($this->getContent());
             if (empty($responseObj)) {
                 $this->errorData->message = 'Bad server response: ' . $this->getResponseStatus();
                 $this->errorData->code = $code;
             }
             $errFieldName = 'odata.error';
             if (isset($responseObj->error)) {
                 $this->errorData->message = $responseObj->error->message;
                 $this->errorData->code = $responseObj->error->code;
             } else {
                 if (isset($responseObj->{$errFieldName}) && $responseObj->{$errFieldName}->message->value) {
                     $this->errorData->message = $responseObj->{$errFieldName}->message->value;
                     $this->errorData->code = $responseObj->{$errFieldName}->code;
                 } else {
                     if (isset($responseObj->message)) {
                         $this->errorData->message = $responseObj->message;
                         $this->errorData->code = $responseObj->code;
                     }
                 }
             }
             throw AzureResponseErrorFactory::make($this->errorData, $code);
         } else {
             if ($code == 204 && $this->request->getRequestMethod() == 'DELETE') {
                 throw new NotFoundException('Azure error. The Resource was not found.', 404);
             }
         }
     }
     return $this->errorData instanceof ErrorData;
 }
예제 #5
0
파일: Request.php 프로젝트: mheydt/scalr
 /**
  * {@inheritdoc}
  * @see \http\Client::setRequestUrl()
  */
 public function setRequestUrl($url)
 {
     return parent::setRequestUrl($this->parseUrl($url));
 }
예제 #6
0
파일: Status.php 프로젝트: mheydt/scalr
 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;
 }
예제 #7
0
<?php

use http\Client, http\Client\Request;
use http\QueryString;
//$params = new QueryString;
$params["foo"] = "bar";
$params["bar"] = "foo";
$request = new Request("POST", "http://www.google.com");
$request->getBody()->append($params);
$request->setContentType("application/x-www-form-urlencoded");
$client = new Client();
$client->enqueue($request);
$client->send();
$response = $client->getResponse($request);
printf("Sent:\n%s\n\n", $response->getParentMessage());
printf("%s returned '%s'\n%s\n", $response->getTransferInfo("effective_url"), $response->getInfo(), $response->getBody());
?>
 
Sent:
POST http://example.com HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 7
 
bar=foo
 
http://example.com/ returned 'HTTP/1.1 200 OK'
<!doctype html>
<html>
<head>
    <title>Example Domain</title>
 
예제 #8
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;
 }
예제 #9
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;
 }