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

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