You are here

private function StreamHandler::createStream in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php \GuzzleHttp\Handler\StreamHandler::createStream()
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 228

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, $options);
  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']);
  }
  $context = $this
    ->createResource(function () use ($context, $params) {
    return stream_context_create($context, $params);
  });
  return $this
    ->createResource(function () use ($request, &$http_response_header, $context) {
    $resource = fopen($request
      ->getUri(), 'r', null, $context);
    $this->lastHeaders = $http_response_header;
    return $resource;
  });
}