You are here

public function PrepareBodyMiddleware::__invoke in Lockr 7.3

Parameters

RequestInterface $request:

array $options:

Return value

PromiseInterface

File

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

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 ($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 (!$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);
}