vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php line 35

Open in your IDE?
  1. <?php
  2. namespace GuzzleHttp;
  3. use GuzzleHttp\Promise\PromiseInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. /**
  6.  * Prepares requests that contain a body, adding the Content-Length,
  7.  * Content-Type, and Expect headers.
  8.  *
  9.  * @final
  10.  */
  11. class PrepareBodyMiddleware
  12. {
  13.     /**
  14.      * @var callable(RequestInterface, array): PromiseInterface
  15.      */
  16.     private $nextHandler;
  17.     /**
  18.      * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke.
  19.      */
  20.     public function __construct(callable $nextHandler)
  21.     {
  22.         $this->nextHandler $nextHandler;
  23.     }
  24.     public function __invoke(RequestInterface $request, array $options): PromiseInterface
  25.     {
  26.         $fn $this->nextHandler;
  27.         // Don't do anything if the request has no body.
  28.         if ($request->getBody()->getSize() === 0) {
  29.             return $fn($request$options);
  30.         }
  31.         $modify = [];
  32.         // Add a default content-type if possible.
  33.         if (!$request->hasHeader('Content-Type')) {
  34.             if ($uri $request->getBody()->getMetadata('uri')) {
  35.                 if (is_string($uri) && $type Psr7\MimeType::fromFilename($uri)) {
  36.                     $modify['set_headers']['Content-Type'] = $type;
  37.                 }
  38.             }
  39.         }
  40.         // Add a default content-length or transfer-encoding header.
  41.         if (!$request->hasHeader('Content-Length')
  42.             && !$request->hasHeader('Transfer-Encoding')
  43.         ) {
  44.             $size $request->getBody()->getSize();
  45.             if ($size !== null) {
  46.                 $modify['set_headers']['Content-Length'] = $size;
  47.             } else {
  48.                 $modify['set_headers']['Transfer-Encoding'] = 'chunked';
  49.             }
  50.         }
  51.         // Add the expect header if needed.
  52.         $this->addExpectHeader($request$options$modify);
  53.         return $fn(Psr7\Utils::modifyRequest($request$modify), $options);
  54.     }
  55.     /**
  56.      * Add expect header
  57.      */
  58.     private function addExpectHeader(RequestInterface $request, array $options, array &$modify): void
  59.     {
  60.         // Determine if the Expect header should be used
  61.         if ($request->hasHeader('Expect')) {
  62.             return;
  63.         }
  64.         $expect $options['expect'] ?? null;
  65.         // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0
  66.         if ($expect === false || $request->getProtocolVersion() < 1.1) {
  67.             return;
  68.         }
  69.         // The expect header is unconditionally enabled
  70.         if ($expect === true) {
  71.             $modify['set_headers']['Expect'] = '100-Continue';
  72.             return;
  73.         }
  74.         // By default, send the expect header when the payload is > 1mb
  75.         if ($expect === null) {
  76.             $expect 1048576;
  77.         }
  78.         // Always add if the body cannot be rewound, the size cannot be
  79.         // determined, or the size is greater than the cutoff threshold
  80.         $body $request->getBody();
  81.         $size $body->getSize();
  82.         if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
  83.             $modify['set_headers']['Expect'] = '100-Continue';
  84.         }
  85.     }
  86. }