Example #1
0
 /**
  * @internal
  *
  * @param array $request
  *
  * @return array
  */
 public function _invokeAsArray(array $request)
 {
     $factory = $this->factory;
     // Ensure headers are by reference. They're updated elsewhere.
     $result = $factory($request, curl_init());
     $h = $result[0];
     $hd =& $result[1];
     $body = $result[2];
     Core::doSleep($request);
     try {
         // override the default body stream with the request response
         $safecurl = new SafeCurl($h);
         $body = $safecurl->execute(Core::url($request));
     } catch (Exception $e) {
         // URL wasn't safe, return empty content
         $body = '';
         $safeCurlError = $e->getMessage();
     }
     $response = ['transfer_stats' => curl_getinfo($h)];
     $response['curl']['error'] = curl_error($h);
     $response['curl']['errno'] = curl_errno($h);
     $response['transfer_stats'] = array_merge($response['transfer_stats'], $response['curl']);
     curl_close($h);
     // override default error message in case of SafeCurl error
     if (isset($safeCurlError)) {
         $response['err_message'] = $safeCurlError;
     }
     return CurlFactory::createResponse([$this, '_invokeAsArray'], $request, $response, $hd, Stream::factory($body));
 }
 public function __invoke(array $request)
 {
     $url = Core::url($request);
     Core::doSleep($request);
     try {
         // Does not support the expect header.
         $request = Core::removeHeader($request, 'Expect');
         $stream = $this->createStream($url, $request, $headers);
         return $this->createResponse($request, $url, $headers, $stream);
     } catch (\Exception $e) {
         return $this->createErrorResponse($url, $e);
     }
 }
Example #3
0
 /**
  * @param callable $handler
  * @param string   $clientKey
  * @param string   $clientSecret
  * @return callable
  */
 public static function signRequest(callable $handler, $clientKey, $clientSecret)
 {
     return function (array $request) use($handler, $clientKey, $clientSecret) {
         $now = time();
         // Client and time stamp
         $request['headers']['HM-Client'] = [$clientKey];
         $request['headers']['HM-Timestamp'] = [$now];
         // Sign and add
         $body = isset($request['body']) ? $request['body'] : '';
         $signature = Security::signRequest($clientSecret, $request['http_method'], Core::url($request), $body, $now);
         $request['headers']['HM-Signature'] = [$signature];
         // Send the request using the handler and return the response.
         return $handler($request);
     };
 }
Example #4
0
 private function add_debug(array $request, &$options, $value, &$params)
 {
     if ($value === false) {
         return;
     }
     static $map = array(STREAM_NOTIFY_CONNECT => 'CONNECT', STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED', STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT', STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS', STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS', STREAM_NOTIFY_REDIRECTED => 'REDIRECTED', STREAM_NOTIFY_PROGRESS => 'PROGRESS', STREAM_NOTIFY_FAILURE => 'FAILURE', STREAM_NOTIFY_COMPLETED => 'COMPLETED', STREAM_NOTIFY_RESOLVE => 'RESOLVE');
     static $args = array('severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max');
     $value = Core::getDebugResource($value);
     $ident = $request['http_method'] . ' ' . Core::url($request);
     $fn = function () use($ident, $value, $map, $args) {
         $passed = func_get_args();
         $code = array_shift($passed);
         fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
         foreach (array_filter($passed) as $i => $v) {
             fwrite($value, $args[$i] . ': "' . $v . '" ');
         }
         fwrite($value, "\n");
     };
     // Wrap the existing function if needed.
     $params['notification'] = isset($params['notification']) ? Core::callArray([$params['notification'], $fn]) : $fn;
 }
Example #5
0
 private function getDefaultOptions(array $request, array &$headers)
 {
     $url = Core::url($request);
     $startingResponse = false;
     $options = ['_headers' => $request['headers'], CURLOPT_CUSTOMREQUEST => $request['http_method'], CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_HEADERFUNCTION => function ($ch, $h) use(&$headers, &$startingResponse) {
         $value = trim($h);
         if ($value === '') {
             $startingResponse = true;
         } elseif ($startingResponse) {
             $startingResponse = false;
             $headers = [$value];
         } else {
             $headers[] = $value;
         }
         return strlen($h);
     }];
     if (isset($request['version'])) {
         if ($request['version'] == 2.0) {
             $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0;
         } else {
             if ($request['version'] == 1.1) {
                 $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
             } else {
                 $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
             }
         }
     }
     if (defined('CURLOPT_PROTOCOLS')) {
         $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     return $options;
 }
Example #6
0
 public function testUsesUrlIfSet()
 {
     $req = ['url' => 'http://foo.com'];
     $this->assertEquals('http://foo.com', Core::url($req));
 }
Example #7
0
 private function getDefaultOptions(array $request, array &$headers)
 {
     $url = Core::url($request);
     $options = ['_headers' => $request['headers'], CURLOPT_CUSTOMREQUEST => $request['http_method'], CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_HEADERFUNCTION => function ($ch, $h) use(&$headers) {
         $headers[] = trim($h);
         return strlen($h);
     }];
     if (isset($request['version'])) {
         $options[CURLOPT_HTTP_VERSION] = $request['version'] == 1.1 ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
     }
     if (defined('CURLOPT_PROTOCOLS')) {
         $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     return $options;
 }
Example #8
0
 /**
  * Creates a Guzzle request object using a ring request array.
  *
  * @param array $request Ring request
  *
  * @return Request
  * @throws \InvalidArgumentException for incomplete requests.
  */
 public static function fromRingRequest(array $request)
 {
     $options = [];
     if (isset($request['version'])) {
         $options['protocol_version'] = $request['version'];
     }
     if (!isset($request['http_method'])) {
         throw new \InvalidArgumentException('No http_method');
     }
     return new Request($request['http_method'], Core::url($request), isset($request['headers']) ? $request['headers'] : [], isset($request['body']) ? Stream::factory($request['body']) : null, $options);
 }
Example #9
0
 private function getDefaultOptions(array $request, array &$headers)
 {
     $url = Core::url($request);
     $options = ['_headers' => $request['headers'], CURLOPT_CUSTOMREQUEST => $request['http_method'], CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_HEADERFUNCTION => function ($ch, $h) use(&$headers) {
         $length = strlen($h);
         if ($value = trim($h)) {
             $headers[] = trim($h);
         }
         return $length;
     }];
     if (defined('CURLOPT_PROTOCOLS')) {
         $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     return $options;
 }