You are here

public function PrepareBodyMiddleware::__invoke in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php \GuzzleHttp\PrepareBodyMiddleware::__invoke()

Parameters

RequestInterface $request:

array $options:

Return value

PromiseInterface

File

vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php, line 34

Class

PrepareBodyMiddleware
Prepares requests that contain a body, adding the Content-Length, Content-Type, and Expect headers.

Namespace

GuzzleHttp

Code

public function __invoke(RequestInterface $request, array $options) {
  $fn = $this->nextHandler;

  // Don't do anything if the request has no body.
  if (isset(self::$skipMethods[$request
    ->getMethod()]) || $request
    ->getBody()
    ->getSize() === 0) {
    return $fn($request, $options);
  }
  $modify = [];

  // Add a default content-type if possible.
  if (!$request
    ->hasHeader('Content-Type')) {
    if ($uri = $request
      ->getBody()
      ->getMetadata('uri')) {
      if ($type = Psr7\mimetype_from_filename($uri)) {
        $modify['set_headers']['Content-Type'] = $type;
      }
    }
  }

  // Add a default content-length or transfer-encoding header.
  if (!isset(self::$skipMethods[$request
    ->getMethod()]) && !$request
    ->hasHeader('Content-Length') && !$request
    ->hasHeader('Transfer-Encoding')) {
    $size = $request
      ->getBody()
      ->getSize();
    if ($size !== null) {
      $modify['set_headers']['Content-Length'] = $size;
    }
    else {
      $modify['set_headers']['Transfer-Encoding'] = 'chunked';
    }
  }

  // Add the expect header if needed.
  $this
    ->addExpectHeader($request, $options, $modify);
  return $fn(Psr7\modify_request($request, $modify), $options);
}