PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 18.2, at vendor_prefixed/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php

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