PrepareBodyMiddleware.php in Zircon Profile 8.0
File
vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php
View source
<?php
namespace GuzzleHttp;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
class PrepareBodyMiddleware {
private $nextHandler;
private static $skipMethods = [
'GET' => true,
'HEAD' => true,
];
public function __construct(callable $nextHandler) {
$this->nextHandler = $nextHandler;
}
public function __invoke(RequestInterface $request, array $options) {
$fn = $this->nextHandler;
if (isset(self::$skipMethods[$request
->getMethod()]) || $request
->getBody()
->getSize() === 0) {
return $fn($request, $options);
}
$modify = [];
if (!$request
->hasHeader('Content-Type')) {
if ($uri = $request
->getBody()
->getMetadata('uri')) {
if ($type = Psr7\mimetype_from_filename($uri)) {
$modify['set_headers']['Content-Type'] = $type;
}
}
}
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';
}
}
$this
->addExpectHeader($request, $options, $modify);
return $fn(Psr7\modify_request($request, $modify), $options);
}
private function addExpectHeader(RequestInterface $request, array $options, array &$modify) {
if ($request
->hasHeader('Expect')) {
return;
}
$expect = isset($options['expect']) ? $options['expect'] : null;
if ($expect === false || $request
->getProtocolVersion() < 1.1) {
return;
}
if ($expect === true) {
$modify['set_headers']['Expect'] = '100-Continue';
return;
}
if ($expect === null) {
$expect = 1048576;
}
$body = $request
->getBody();
$size = $body
->getSize();
if ($size === null || $size >= (int) $expect || !$body
->isSeekable()) {
$modify['set_headers']['Expect'] = '100-Continue';
}
}
}
Classes
Name |
Description |
PrepareBodyMiddleware |
Prepares requests that contain a body, adding the Content-Length,
Content-Type, and Expect headers. |