function PrepareBodyMiddleware::__invoke
File
-
vendor/
guzzlehttp/ guzzle/ src/ PrepareBodyMiddleware.php, line 29
Class
- PrepareBodyMiddleware
- Prepares requests that contain a body, adding the Content-Length, Content-Type, and Expect headers.
Namespace
GuzzleHttpCode
public function __invoke(RequestInterface $request, array $options) : PromiseInterface {
$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 (is_string($uri) && ($type = Psr7\MimeType::fromFilename($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\Utils::modifyRequest($request, $modify), $options);
}