Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StreamHandler.php

function StreamHandler::__invoke

Sends an HTTP request.

Parameters

RequestInterface $request Request to send.:

array $options Request transfer options.:

File

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

Class

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

Namespace

GuzzleHttp\Handler

Code

public function __invoke(RequestInterface $request, array $options) : PromiseInterface {
    // Sleep if there is a delay specified.
    if (isset($options['delay'])) {
        \usleep($options['delay'] * 1000);
    }
    $protocolVersion = $request->getProtocolVersion();
    if ('1.0' !== $protocolVersion && '1.1' !== $protocolVersion) {
        throw new ConnectException(sprintf('HTTP/%s is not supported by the stream handler.', $protocolVersion), $request);
    }
    $startTime = isset($options['on_stats']) ? Utils::currentTime() : 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 (false !== \strpos($message, 'getaddrinfo') || false !== \strpos($message, 'Connection refused') || false !== \strpos($message, "couldn't connect to host") || false !== \strpos($message, 'connection attempt failed')) {
            $e = new ConnectException($e->getMessage(), $request, $e);
        }
        else {
            $e = RequestException::wrapException($request, $e);
        }
        $this->invokeStats($options, $request, $startTime, null, $e);
        return P\Create::rejectionFor($e);
    }
}
RSS feed
Powered by Drupal