summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/StreamRequestPayloadMiddleware.php
blob: dd63d3a5f68838afce73c478802c215681f7beee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Aws;

use Aws\Api\Service;
use Aws\Exception\IncalculablePayloadException;
use Psr\Http\Message\RequestInterface;

/**
 * @internal
 */
class StreamRequestPayloadMiddleware
{
    private $nextHandler;
    private $service;

    /**
     * Create a middleware wrapper function
     *
     * @param Service $service
     * @return \Closure
     */
    public static function wrap(Service $service)
    {
        return function (callable $handler) use ($service) {
            return new self($handler, $service);
        };
    }

    public function __construct(callable $nextHandler, Service $service)
    {
        $this->nextHandler = $nextHandler;
        $this->service = $service;
    }

    public function __invoke(CommandInterface $command, RequestInterface $request)
    {
        $nextHandler = $this->nextHandler;

        $operation = $this->service->getOperation($command->getName());
        $contentLength = $request->getHeader('content-length');
        $hasStreaming = false;
        $requiresLength = false;

        // Check if any present input member is a stream and requires the
        // content length
        foreach ($operation->getInput()->getMembers() as $name => $member) {
            if (!empty($member['streaming']) && isset($command[$name])) {
                $hasStreaming = true;
                if (!empty($member['requiresLength'])) {
                    $requiresLength = true;
                }
            }
        }

        if ($hasStreaming) {

            // Add 'transfer-encoding' header if payload size not required to
            // to be calculated and not already known
            if (empty($requiresLength)
                && empty($contentLength)
                && isset($operation['authtype'])
                && $operation['authtype'] == 'v4-unsigned-body'
            ) {
                $request = $request->withHeader('transfer-encoding', 'chunked');

            // Otherwise, make sure 'content-length' header is added
            } else {
                if (empty($contentLength)) {
                    $size = $request->getBody()->getSize();
                    if (is_null($size)) {
                        throw new IncalculablePayloadException('Payload'
                            . ' content length is required and can not be'
                            . ' calculated.');
                    }
                    $request = $request->withHeader(
                        'content-length',
                        $size
                    );
                }
            }
        }

        return $nextHandler($command, $request);
    }
}