示例#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));
 }
示例#2
0
 public function __invoke(array $request)
 {
     Core::doSleep($request);
     $response = is_callable($this->result) ? call_user_func($this->result, $request) : $this->result;
     if (is_array($response)) {
         $response = new CompletedFutureArray($response + array('status' => null, 'body' => null, 'headers' => array(), 'reason' => null, 'effective_url' => null));
     } elseif (!$response instanceof FutureArrayInterface) {
         throw new \InvalidArgumentException('Response must be an array or FutureArrayInterface. Found ' . Core::describeType($request));
     }
     return $response;
 }
示例#3
0
 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 (RingException $e) {
         return $this->createErrorResponse($url, $e);
     }
 }
示例#4
0
 public function __invoke(array $request)
 {
     $factory = $this->factory;
     // Ensure headers are by reference. They're updated elsewhere.
     $result = $factory($request, $this->checkoutEasyHandle());
     $h = $result[0];
     $hd =& $result[1];
     $bd = $result[2];
     Core::doSleep($request);
     curl_exec($h);
     $response = ['transfer_stats' => curl_getinfo($h)];
     $response['curl']['error'] = curl_error($h);
     $response['curl']['errno'] = curl_errno($h);
     $this->releaseEasyHandle($h);
     return new CompletedFutureArray(CurlFactory::createResponse($this, $request, $response, $hd, $bd));
 }
示例#5
0
 public function __invoke(array $request)
 {
     Core::doSleep($request);
     $response = is_callable($this->result) ? call_user_func($this->result, $request) : $this->result;
     $data = "'" . Stream::factory($request['body']) . "'";
     $headers = '';
     foreach ($request['headers'] as $key => $val) {
         $headers .= '-H "' . $key . ': ' . $val[0] . '" ';
     }
     $exec = 'curl -X POST ' . trim($headers) . ' -d ' . $data . ' "' . $request['url'] . '"';
     $exec .= " >/dev/null 2>&1 &";
     exec($exec, $output, $return_var);
     if (is_array($response)) {
         $response = new CompletedFutureArray($response + ['status' => 200, 'body' => null, 'headers' => [], 'reason' => null, 'effective_url' => null]);
     } elseif (!$response instanceof FutureArrayInterface) {
         throw new \InvalidArgumentException('Response must be an array or FutureArrayInterface. Found ' . Core::describeType($request));
     }
     return $response;
 }
示例#6
0
 public function testDoesSleep()
 {
     $t = microtime(true);
     $expected = $t + 100 / 1000;
     Core::doSleep(['client' => ['delay' => 100]]);
     $this->assertGreaterThanOrEqual($expected, microtime(true));
 }