Cookie
2 years ago
Exception
2 years ago
Handler
2 years ago
BodySummarizer.php
2 years ago
BodySummarizerInterface.php
2 years ago
Client.php
2 years ago
ClientInterface.php
2 years ago
ClientTrait.php
2 years ago
HandlerStack.php
2 years ago
MessageFormatter.php
2 years ago
MessageFormatterInterface.php
2 years ago
Middleware.php
2 years ago
Pool.php
2 years ago
PrepareBodyMiddleware.php
2 years ago
RedirectMiddleware.php
2 years ago
RequestOptions.php
2 years ago
RetryMiddleware.php
2 years ago
TransferStats.php
2 years ago
Utils.php
2 years ago
functions.php
2 years ago
functions_include.php
2 years ago
PrepareBodyMiddleware.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace YoastSEO_Vendor\GuzzleHttp; |
| 4 | |
| 5 | use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface; |
| 6 | use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; |
| 7 | /** |
| 8 | * Prepares requests that contain a body, adding the Content-Length, |
| 9 | * Content-Type, and Expect headers. |
| 10 | * |
| 11 | * @final |
| 12 | */ |
| 13 | class PrepareBodyMiddleware |
| 14 | { |
| 15 | /** |
| 16 | * @var callable(RequestInterface, array): PromiseInterface |
| 17 | */ |
| 18 | private $nextHandler; |
| 19 | /** |
| 20 | * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
| 21 | */ |
| 22 | public function __construct(callable $nextHandler) |
| 23 | { |
| 24 | $this->nextHandler = $nextHandler; |
| 25 | } |
| 26 | public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface |
| 27 | { |
| 28 | $fn = $this->nextHandler; |
| 29 | // Don't do anything if the request has no body. |
| 30 | if ($request->getBody()->getSize() === 0) { |
| 31 | return $fn($request, $options); |
| 32 | } |
| 33 | $modify = []; |
| 34 | // Add a default content-type if possible. |
| 35 | if (!$request->hasHeader('Content-Type')) { |
| 36 | if ($uri = $request->getBody()->getMetadata('uri')) { |
| 37 | if (\is_string($uri) && ($type = \YoastSEO_Vendor\GuzzleHttp\Psr7\MimeType::fromFilename($uri))) { |
| 38 | $modify['set_headers']['Content-Type'] = $type; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | // Add a default content-length or transfer-encoding header. |
| 43 | if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) { |
| 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(\YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::modifyRequest($request, $modify), $options); |
| 54 | } |
| 55 | /** |
| 56 | * Add expect header |
| 57 | */ |
| 58 | private function addExpectHeader(\YoastSEO_Vendor\Psr\Http\Message\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 | } |
| 87 |