You are here

private function StreamHandler::createStream in Lockr 7.3

1 call to StreamHandler::createStream()
StreamHandler::__invoke in vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php
Sends an HTTP request.

File

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

Class

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

Namespace

GuzzleHttp\Handler

Code

private function createStream(RequestInterface $request, array $options) {
  static $methods;
  if (!$methods) {
    $methods = array_flip(get_class_methods(__CLASS__));
  }

  // HTTP/1.1 streams using the PHP stream wrapper require a
  // Connection: close header
  if ($request
    ->getProtocolVersion() == '1.1' && !$request
    ->hasHeader('Connection')) {
    $request = $request
      ->withHeader('Connection', 'close');
  }

  // Ensure SSL is verified by default
  if (!isset($options['verify'])) {
    $options['verify'] = true;
  }
  $params = [];
  $context = $this
    ->getDefaultContext($request);
  if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
    throw new \InvalidArgumentException('on_headers must be callable');
  }
  if (!empty($options)) {
    foreach ($options as $key => $value) {
      $method = "add_{$key}";
      if (isset($methods[$method])) {
        $this
          ->{$method}($request, $context, $value, $params);
      }
    }
  }
  if (isset($options['stream_context'])) {
    if (!is_array($options['stream_context'])) {
      throw new \InvalidArgumentException('stream_context must be an array');
    }
    $context = array_replace_recursive($context, $options['stream_context']);
  }

  // Microsoft NTLM authentication only supported with curl handler
  if (isset($options['auth']) && is_array($options['auth']) && isset($options['auth'][2]) && 'ntlm' == $options['auth'][2]) {
    throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
  }
  $uri = $this
    ->resolveHost($request, $options);
  $context = $this
    ->createResource(function () use ($context, $params) {
    return stream_context_create($context, $params);
  });
  return $this
    ->createResource(function () use ($uri, &$http_response_header, $context, $options) {
    $resource = fopen((string) $uri, 'r', null, $context);
    $this->lastHeaders = $http_response_header;
    if (isset($options['read_timeout'])) {
      $readTimeout = $options['read_timeout'];
      $sec = (int) $readTimeout;
      $usec = ($readTimeout - $sec) * 100000;
      stream_set_timeout($resource, $sec, $usec);
    }
    return $resource;
  });
}