Convert client options into curl options.

@param \Cake\Http\Client\Request $request The request.
@param array $options The client options
@return array
551597
public function buildOptions(Request $request, array $options)
    {
        $headers = [];
        foreach ($request->getHeaders() as $key => $values) {
            $headers[] = $key . ': ' . implode(', ', $values);
        }

        $out = [
            CURLOPT_URL => (string)$request->getUri(),
            CURLOPT_HTTP_VERSION => $request->getProtocolVersion(),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => true,
            CURLOPT_HTTPHEADER => $headers
        ];
        switch ($request->getMethod()) {
            case Request::METHOD_GET:
                $out[CURLOPT_HTTPGET] = true;
                break;

            case Request::METHOD_POST:
                $out[CURLOPT_POST] = true;
                break;

            default:
                $out[CURLOPT_POST] = true;
                $out[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
                break;
        }

        $body = $request->getBody();
        if ($body) {
            $body->rewind();
            $out[CURLOPT_POSTFIELDS] = $body->getContents();
        }

        if (empty($options['ssl_cafile'])) {
            $options['ssl_cafile'] = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem';
        }
        if (!empty($options['ssl_verify_host'])) {
            // Value of 1 or true is deprecated. Only 2 or 0 should be used now.
            $options['ssl_verify_host'] = 2;
        }
        $optionMap = [
            'timeout' => CURLOPT_TIMEOUT,
            'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER,
            'ssl_verify_host' => CURLOPT_SSL_VERIFYHOST,
            'ssl_cafile' => CURLOPT_CAINFO,
            'ssl_local_cert' => CURLOPT_SSLCERT,
            'ssl_passphrase' => CURLOPT_SSLCERTPASSWD,
        ];
        foreach ($optionMap as $option => $curlOpt) {
            if (isset($options[$option])) {
                $out[$curlOpt] = $options[$option];
            }
        }
        if (isset($options['proxy']['proxy'])) {
            $out[CURLOPT_PROXY] = $options['proxy']['proxy'];
        }
        if (isset($options['proxy']['username'])) {
            $password = !empty($options['proxy']['password']) ? $options['proxy']['password'] : '';
            $out[CURLOPT_PROXYUSERPWD] = $options['proxy']['username'] . ':' . $password;
        }
        if (isset($options['curl']) && is_array($options['curl'])) {
            // Can't use array_merge() because keys will be re-ordered.
            foreach ($options['curl'] as $key => $value) {
                $out[$key] = $value;
            }
        }

        return $out;
    }
455567
public function request($options)
    {
        $request = $this->getRequest($options);
        $requestOptions = isset($options['query']) ? ['query' => $options['query']] : [];

        // Try to perform request
        try {
            $response = $this->httpClient->send($request, $requestOptions);
        } catch (GuzzleRequestException $err) {
            $hasResponse = $err->hasResponse();
            if (!$hasResponse) {
                // @todo how do we handle, wrap guzzle err
                throw $err;
            }

            $response = $err->getResponse();
            $code = $response->getStatusCode();

            if ($code >= 500) {
                throw new ServerException($response);
            } elseif ($code >= 400) {
                throw new ClientException($response);
            }
        }

        $contentType = $response->getHeader('Content-Type')[0];
        $isJson = stripos($contentType, 'application/json') !== false;
        $rawBody = (string) $response->getBody();
        $body = $isJson ? json_decode($rawBody, true) : $rawBody;

        return $body;
    }
336776
protected function prepareOptions(array $options = [])
    {
        $cookieName = time()."_".substr(md5(microtime()), 0, 5).".txt";
                
        $defaultConfig = array(
            'curl' => array(
                CURLOPT_COOKIEJAR      => $cookieName,
                CURLOPT_COOKIEFILE     => $cookieName,
            ),
        );
        $configOptions = array_merge_recursive($options, $defaultConfig);
        
        return $configOptions;
    }
446404
public function withOptions($options)
    {
        if (is_array($options)) {
            $options = new RequestOptions($options);
        }

        $this->options = $options;

        return $this;
    }
146904
public function setCurlOptions(array $options) {
        return new Request([
            'method'              => $this->method,
            'url'                 => $this->url,
            'curlOptions'         => $options,
            'query'               => $this->query,
            'payload'             => $this->payload,
            'expectedStatusCodes' => $this->expectedStatusCodes
        ]);
    }
45958
protected function getOptions(Request $request)
    {
        $configs = $this->getCorsConfigs();
        $uri = $request->getPathInfo() ?: '/';

        /** @var CorsConfig $bestMatch */
        $bestMatch = null;
        foreach ($configs as $config) {
            $path = $config->path;
            if ($request->is($path) || (Str::startsWith($path, '^') && preg_match('{' . $path . '}i', $uri))) {
                if ($bestMatch) {
                    // simple compare path lengths for accuracy
                    if (strlen($path) > strlen($bestMatch->path)) {
                        $bestMatch = $config;
                    }
                } else {
                    $bestMatch = $config;
                }
            }
        }

        if ($bestMatch) {
            return [
                "allowedOrigins"      => explode(',', $bestMatch->origin),
                "allowedHeaders"      => explode(',', $bestMatch->header),
                "exposedHeaders"      => explode(',', $bestMatch->exposed_header),
                "allowedMethods"      => $bestMatch->method,
                "maxAge"              => $bestMatch->max_age,
                "supportsCredentials" => $bestMatch->supports_credentials,
            ];
        }

        return [];
    }
905129
public static function get(string $url, array $options = []) : array {
		$result = [
			'success' => false,
			'error' => [],
			'data' => null
		];
		if (strpos($url, '?') !== false) {
			$url.= '&';
		} else {
			$url.= '?';
		}
		$url.= http_build_query2($options['params']);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$result['data'] = curl_exec($ch);
		if (!empty($options['json'])) {
			$result['data'] = json_decode($result['data'], true);
		}
		curl_close($ch);
		$result['success'] = true;
		return $result;
	}
33623
public function curlOpts( array $options )
      : Request
   {

      $this->_curlOpts = \array_merge( $this->_curlOpts, $options );

      return $this;

   }
1237880
protected function setOptionsOnRequest(CurlRequest $request, $options) {
        foreach($options as $option => $value) {
            $request->setOption($option, $value);
        }
    }
690485
protected function _makeOptions(ApiRequestInterface $request)
  {
    $headers = $options = [];

    if($request->getVerb() == HttpVerb::POST)
    {
      $options['json'] = $request->getPostData();
    }

    if($this->_accessToken !== null)
    {
      $headers['Authorization'] = 'Bearer ' . $this->_accessToken;
    }

    if($this->_orgFid !== null)
    {
      $headers['X-Fortifi-Org'] = $this->_orgFid;
    }

    if($this->_disableNesting)
    {
      $headers['Disable-Nesting'] = "true";
    }

    if($this->_disableLogging)
    {
      $headers['Disable-Logging'] = "true";
    }

    if($this->_shortNesting)
    {
      $headers['Short-Nesting'] = "true";
    }

    if(!empty($headers))
    {
      $options['headers'] = $headers;
    }

    return $options;
  }
900299
protected function filterRequestOption(array &$options)
    {
        if (isset($options['json'])) {
            $options['format'] = 'json';
            $options['body'] = json_encode($options['json']);
        } elseif (!isset($options['format'])) {
            // Make sure we always have a format
            $options['format'] = $this->getFormat();
        }

        // Set correct headers for this format
        switch ($options['format']) {
            case 'xml':
                $options['headers']['Content-Type'] = 'text/xml';
                break;
            case 'json':
                $options['headers']['Content-Type'] = 'application/json';
                $options['headers']['x-li-format'] = 'json';
                $options['query']['format'] = 'json';
                break;
            default:
                // Do nothing
        }

        return $options['format'];
    }
139884
protected function prepareOptions(RequestInterface $request)
    {
        $this->options[CURLOPT_URL] = (string)$request->getUri();
        $this->options[CURLOPT_HTTP_VERSION] = $request->getProtocolVersion();

        // Set request method
        if (strtoupper($request->getMethod()) === 'GET') {
            $this->options[CURLOPT_HTTPGET] = true;
        } elseif (strtoupper($request->getMethod()) === 'HEAD') {
            $this->options[CURLOPT_NOBODY] = true;
        } else {
            $this->options[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
        }

        // Prepare and set request headers
        foreach ($request->getHeaders() as $name => $values) {
            foreach ($values as $value) {
                $this->options[CURLOPT_HTTPHEADER][] = sprintf('%s: %s', $name, $value);
            }
        }

        curl_setopt_array($this->handle, $this->options);
    }
585642
protected function ipResolve(RequestInterface $request, array &$options = []): array
    {
        $host = $request->getUri()->getHost();
        if (isset($options['port']) && is_numeric($options['port'])) {
            $port = (int)$options['port'];
        } elseif ($request->getUri()->getPort()) {
            $port = $request->getUri()->getPort();
        } elseif ($request->getUri() instanceof Uri) {
            /** @var Uri $uri */
            $uri = $request->getUri();
            $port = $uri->isDefaultPort() ? $uri->getDefaultPort() : 80;
        } else {
            $port = 80;
        }

        return [$host, $port];
    }
249774
public function getOptions(Request $request)
    {
        $routes = [];

        foreach ($this->routes as $route) {
            if ($route->matchesPattern($request)) {
                $routes[] = $route;
            }
        }

        return $routes;
    }
527327
protected function _buildSslContext(Request $request, $options)
    {
        $sslOptions = [
            'ssl_verify_peer',
            'ssl_verify_peer_name',
            'ssl_verify_depth',
            'ssl_allow_self_signed',
            'ssl_cafile',
            'ssl_local_cert',
            'ssl_passphrase',
        ];
        if (empty($options['ssl_cafile'])) {
            $options['ssl_cafile'] = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem';
        }
        if (!empty($options['ssl_verify_host'])) {
            $url = $request->getUri();
            $host = parse_url($url, PHP_URL_HOST);
            $this->_sslContextOptions['peer_name'] = $host;
        }
        foreach ($sslOptions as $key) {
            if (isset($options[$key])) {
                $name = substr($key, 4);
                $this->_sslContextOptions[$name] = $options[$key];
            }
        }
    }
738305
protected function createHeaders(RequestInterface $request, array $options)
	{
		$headers = [];
		$body = $request->getBody();
		$size = $body->getSize();

		foreach ($request->getHeaders() as $header => $values)
		{
			foreach ((array) $values as $value)
			{
				$headers[] = sprintf('%s: %s', $header, $value);
			}
		}

		if ( ! $request->hasHeader('Transfer-Encoding') && $size === null)
		{
			$headers[] = 'Transfer-Encoding: chunked';
		}

		if ( ! $request->hasHeader('Expect') && in_array($request->getMethod(), ['POST', 'PUT']))
		{
			if ($request->getProtocolVersion() < 2.0 && ! $body->isSeekable() || $size === null || $size > 1048576)
			{
				$headers[] = 'Expect: 100-Continue';
			}
			else
			{
				$headers[] = 'Expect: ';
			}
		}

		return $headers;
	}
1198459
private function transformRequest(Request $request)
    {
        $method = $request->getMethod();
        $url = (string) $request->getUrl();
        $options = [
            'version' => $request->getProtocolVersion(),
            'headers' => $request->getHeaders(),
        ];

        if ($body = $request->getBody()) {
            $options['body'] = $body->detach();
        }

        return $this->client->createRequest($method, $url, $options);
    }