PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / PrepareBodyMiddleware.php

PrepareBodyMiddleware.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php

87 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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'] = (string) $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 using HTTP/1.0
66 if ($expect === \false || $request->getProtocolVersion() === '1.0') {
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