You are here

public function StreamHandler::__invoke in Lockr 7.3

Sends an HTTP request.

Parameters

RequestInterface $request Request to send.:

array $options Request transfer options.:

Return value

PromiseInterface

File

vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php, line 29

Class

StreamHandler
HTTP handler that uses PHP's HTTP stream wrapper.

Namespace

GuzzleHttp\Handler

Code

public function __invoke(RequestInterface $request, array $options) {

  // Sleep if there is a delay specified.
  if (isset($options['delay'])) {
    usleep($options['delay'] * 1000);
  }
  $startTime = isset($options['on_stats']) ? microtime(true) : null;
  try {

    // Does not support the expect header.
    $request = $request
      ->withoutHeader('Expect');

    // Append a content-length header if body size is zero to match
    // cURL's behavior.
    if (0 === $request
      ->getBody()
      ->getSize()) {
      $request = $request
        ->withHeader('Content-Length', 0);
    }
    return $this
      ->createResponse($request, $options, $this
      ->createStream($request, $options), $startTime);
  } catch (\InvalidArgumentException $e) {
    throw $e;
  } catch (\Exception $e) {

    // Determine if the error was a networking error.
    $message = $e
      ->getMessage();

    // This list can probably get more comprehensive.
    if (strpos($message, 'getaddrinfo') || strpos($message, 'Connection refused') || strpos($message, "couldn't connect to host") || strpos($message, "connection attempt failed")) {
      $e = new ConnectException($e
        ->getMessage(), $request, $e);
    }
    $e = RequestException::wrapException($request, $e);
    $this
      ->invokeStats($options, $request, $startTime, null, $e);
    return \GuzzleHttp\Promise\rejection_for($e);
  }
}