Cookie
3 years ago
Exception
3 years ago
Handler
3 years ago
Client.php
3 years ago
ClientInterface.php
3 years ago
HandlerStack.php
3 years ago
MessageFormatter.php
3 years ago
Middleware.php
3 years ago
Pool.php
3 years ago
PrepareBodyMiddleware.php
3 years ago
RedirectMiddleware.php
3 years ago
RequestOptions.php
3 years ago
RetryMiddleware.php
3 years ago
TransferStats.php
3 years ago
UriTemplate.php
3 years ago
Utils.php
3 years ago
functions.php
3 years ago
functions_include.php
3 years ago
PrepareBodyMiddleware.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Vendor\GuzzleHttp; |
| 4 | |
| 5 | use WPMailSMTP\Vendor\GuzzleHttp\Promise\PromiseInterface; |
| 6 | use WPMailSMTP\Vendor\GuzzleHttp\Psr7; |
| 7 | use WPMailSMTP\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(\WPMailSMTP\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 = \WPMailSMTP\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(\WPMailSMTP\Vendor\GuzzleHttp\Psr7\modify_request($request, $modify), $options); |
| 57 | } |
| 58 | /** |
| 59 | * Add expect header |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | private function addExpectHeader(\WPMailSMTP\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 |