You are here

private function CurlFactory::applyHandlerOptions in Lockr 7.3

1 call to CurlFactory::applyHandlerOptions()
CurlFactory::create in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php
Creates a cURL handle resource.

File

vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php, line 323

Class

CurlFactory
Creates curl resources from a request

Namespace

GuzzleHttp\Handler

Code

private function applyHandlerOptions(EasyHandle $easy, array &$conf) {
  $options = $easy->options;
  if (isset($options['verify'])) {
    if ($options['verify'] === false) {
      unset($conf[CURLOPT_CAINFO]);
      $conf[CURLOPT_SSL_VERIFYHOST] = 0;
      $conf[CURLOPT_SSL_VERIFYPEER] = false;
    }
    else {
      $conf[CURLOPT_SSL_VERIFYHOST] = 2;
      $conf[CURLOPT_SSL_VERIFYPEER] = true;
      if (is_string($options['verify'])) {

        // Throw an error if the file/folder/link path is not valid or doesn't exist.
        if (!file_exists($options['verify'])) {
          throw new \InvalidArgumentException("SSL CA bundle not found: {$options['verify']}");
        }

        // If it's a directory or a link to a directory use CURLOPT_CAPATH.
        // If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
        if (is_dir($options['verify']) || is_link($options['verify']) && is_dir(readlink($options['verify']))) {
          $conf[CURLOPT_CAPATH] = $options['verify'];
        }
        else {
          $conf[CURLOPT_CAINFO] = $options['verify'];
        }
      }
    }
  }
  if (!empty($options['decode_content'])) {
    $accept = $easy->request
      ->getHeaderLine('Accept-Encoding');
    if ($accept) {
      $conf[CURLOPT_ENCODING] = $accept;
    }
    else {
      $conf[CURLOPT_ENCODING] = '';

      // Don't let curl send the header over the wire
      $conf[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:';
    }
  }
  if (isset($options['sink'])) {
    $sink = $options['sink'];
    if (!is_string($sink)) {
      $sink = \GuzzleHttp\Psr7\stream_for($sink);
    }
    elseif (!is_dir(dirname($sink))) {

      // Ensure that the directory exists before failing in curl.
      throw new \RuntimeException(sprintf('Directory %s does not exist for sink value of %s', dirname($sink), $sink));
    }
    else {
      $sink = new LazyOpenStream($sink, 'w+');
    }
    $easy->sink = $sink;
    $conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink) {
      return $sink
        ->write($write);
    };
  }
  else {

    // Use a default temp stream if no sink was set.
    $conf[CURLOPT_FILE] = fopen('php://temp', 'w+');
    $easy->sink = Psr7\stream_for($conf[CURLOPT_FILE]);
  }
  $timeoutRequiresNoSignal = false;
  if (isset($options['timeout'])) {
    $timeoutRequiresNoSignal |= $options['timeout'] < 1;
    $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
  }

  // CURL default value is CURL_IPRESOLVE_WHATEVER
  if (isset($options['force_ip_resolve'])) {
    if ('v4' === $options['force_ip_resolve']) {
      $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
    }
    elseif ('v6' === $options['force_ip_resolve']) {
      $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
    }
  }
  if (isset($options['connect_timeout'])) {
    $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1;
    $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
  }
  if ($timeoutRequiresNoSignal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
    $conf[CURLOPT_NOSIGNAL] = true;
  }
  if (isset($options['proxy'])) {
    if (!is_array($options['proxy'])) {
      $conf[CURLOPT_PROXY] = $options['proxy'];
    }
    else {
      $scheme = $easy->request
        ->getUri()
        ->getScheme();
      if (isset($options['proxy'][$scheme])) {
        $host = $easy->request
          ->getUri()
          ->getHost();
        if (!isset($options['proxy']['no']) || !\GuzzleHttp\is_host_in_noproxy($host, $options['proxy']['no'])) {
          $conf[CURLOPT_PROXY] = $options['proxy'][$scheme];
        }
      }
    }
  }
  if (isset($options['cert'])) {
    $cert = $options['cert'];
    if (is_array($cert)) {
      $conf[CURLOPT_SSLCERTPASSWD] = $cert[1];
      $cert = $cert[0];
    }
    if (!file_exists($cert)) {
      throw new \InvalidArgumentException("SSL certificate not found: {$cert}");
    }
    $conf[CURLOPT_SSLCERT] = $cert;
  }
  if (isset($options['ssl_key'])) {
    $sslKey = $options['ssl_key'];
    if (is_array($sslKey)) {
      $conf[CURLOPT_SSLKEYPASSWD] = $sslKey[1];
      $sslKey = $sslKey[0];
    }
    if (!file_exists($sslKey)) {
      throw new \InvalidArgumentException("SSL private key not found: {$sslKey}");
    }
    $conf[CURLOPT_SSLKEY] = $sslKey;
  }
  if (isset($options['progress'])) {
    $progress = $options['progress'];
    if (!is_callable($progress)) {
      throw new \InvalidArgumentException('progress client option must be callable');
    }
    $conf[CURLOPT_NOPROGRESS] = false;
    $conf[CURLOPT_PROGRESSFUNCTION] = function () use ($progress) {
      $args = func_get_args();

      // PHP 5.5 pushed the handle onto the start of the args
      if (is_resource($args[0])) {
        array_shift($args);
      }
      call_user_func_array($progress, $args);
    };
  }
  if (!empty($options['debug'])) {
    $conf[CURLOPT_STDERR] = \GuzzleHttp\debug_resource($options['debug']);
    $conf[CURLOPT_VERBOSE] = true;
  }
}