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

Breadcrumb

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

function Client::applyOptions

Applies the array of request options to a request.

1 call to Client::applyOptions()
Client::transfer in vendor/guzzlehttp/guzzle/src/Client.php
Transfers the given request and applies request options.

File

vendor/guzzlehttp/guzzle/src/Client.php, line 342

Class

Client
@final

Namespace

GuzzleHttp

Code

private function applyOptions(RequestInterface $request, array &$options) : RequestInterface {
    $modify = [
        'set_headers' => [],
    ];
    if (isset($options['headers'])) {
        if (array_keys($options['headers']) === range(0, count($options['headers']) - 1)) {
            throw new InvalidArgumentException('The headers array must have header name as keys.');
        }
        $modify['set_headers'] = $options['headers'];
        unset($options['headers']);
    }
    if (isset($options['form_params'])) {
        if (isset($options['multipart'])) {
            throw new InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.');
        }
        $options['body'] = \http_build_query($options['form_params'], '', '&');
        unset($options['form_params']);
        // Ensure that we don't have the header in different case and set the new value.
        $options['_conditional'] = Psr7\Utils::caselessRemove([
            'Content-Type',
        ], $options['_conditional']);
        $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
    }
    if (isset($options['multipart'])) {
        $options['body'] = new Psr7\MultipartStream($options['multipart']);
        unset($options['multipart']);
    }
    if (isset($options['json'])) {
        $options['body'] = Utils::jsonEncode($options['json']);
        unset($options['json']);
        // Ensure that we don't have the header in different case and set the new value.
        $options['_conditional'] = Psr7\Utils::caselessRemove([
            'Content-Type',
        ], $options['_conditional']);
        $options['_conditional']['Content-Type'] = 'application/json';
    }
    if (!empty($options['decode_content']) && $options['decode_content'] !== true) {
        // Ensure that we don't have the header in different case and set the new value.
        $options['_conditional'] = Psr7\Utils::caselessRemove([
            'Accept-Encoding',
        ], $options['_conditional']);
        $modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
    }
    if (isset($options['body'])) {
        if (\is_array($options['body'])) {
            throw $this->invalidBody();
        }
        $modify['body'] = Psr7\Utils::streamFor($options['body']);
        unset($options['body']);
    }
    if (!empty($options['auth']) && \is_array($options['auth'])) {
        $value = $options['auth'];
        $type = isset($value[2]) ? \strtolower($value[2]) : 'basic';
        switch ($type) {
            case 'basic':
                // Ensure that we don't have the header in different case and set the new value.
                $modify['set_headers'] = Psr7\Utils::caselessRemove([
                    'Authorization',
                ], $modify['set_headers']);
                $modify['set_headers']['Authorization'] = 'Basic ' . \base64_encode("{$value[0]}:{$value[1]}");
                break;
            case 'digest':
                // @todo: Do not rely on curl
                $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST;
                $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}";
                break;
            case 'ntlm':
                $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM;
                $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}";
                break;
        }
    }
    if (isset($options['query'])) {
        $value = $options['query'];
        if (\is_array($value)) {
            $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986);
        }
        if (!\is_string($value)) {
            throw new InvalidArgumentException('query must be a string or array');
        }
        $modify['query'] = $value;
        unset($options['query']);
    }
    // Ensure that sink is not an invalid value.
    if (isset($options['sink'])) {
        // TODO: Add more sink validation?
        if (\is_bool($options['sink'])) {
            throw new InvalidArgumentException('sink must not be a boolean');
        }
    }
    if (isset($options['version'])) {
        $modify['version'] = $options['version'];
    }
    $request = Psr7\Utils::modifyRequest($request, $modify);
    if ($request->getBody() instanceof Psr7\MultipartStream) {
        // Use a multipart/form-data POST if a Content-Type is not set.
        // Ensure that we don't have the header in different case and set the new value.
        $options['_conditional'] = Psr7\Utils::caselessRemove([
            'Content-Type',
        ], $options['_conditional']);
        $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()
            ->getBoundary();
    }
    // Merge in conditional headers if they are not present.
    if (isset($options['_conditional'])) {
        // Build up the changes so it's in a single clone of the message.
        $modify = [];
        foreach ($options['_conditional'] as $k => $v) {
            if (!$request->hasHeader($k)) {
                $modify['set_headers'][$k] = $v;
            }
        }
        $request = Psr7\Utils::modifyRequest($request, $modify);
        // Don't pass this internal value along to middleware/handlers.
        unset($options['_conditional']);
    }
    return $request;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal