GuzzleHttpPrepareBodyMiddleware

Prepares requests that contain a body, adding the Content-Length, Content-Type, and Expect headers.

Defined (1)

The class is defined in the following location(s).

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