Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws;
3
 
4
use Aws\Api\Service;
5
use Aws\Exception\IncalculablePayloadException;
6
use Psr\Http\Message\RequestInterface;
7
 
8
/**
9
 * @internal
10
 */
11
class StreamRequestPayloadMiddleware
12
{
13
    private $nextHandler;
14
    private $service;
15
 
16
    /**
17
     * Create a middleware wrapper function
18
     *
19
     * @param Service $service
20
     * @return \Closure
21
     */
22
    public static function wrap(Service $service)
23
    {
24
        return function (callable $handler) use ($service) {
25
            return new self($handler, $service);
26
        };
27
    }
28
 
29
    public function __construct(callable $nextHandler, Service $service)
30
    {
31
        $this->nextHandler = $nextHandler;
32
        $this->service = $service;
33
    }
34
 
35
    public function __invoke(CommandInterface $command, RequestInterface $request)
36
    {
37
        $nextHandler = $this->nextHandler;
38
 
39
        $operation = $this->service->getOperation($command->getName());
40
        $contentLength = $request->getHeader('content-length');
41
        $hasStreaming = false;
42
        $requiresLength = false;
43
 
44
        // Check if any present input member is a stream and requires the
45
        // content length
46
        foreach ($operation->getInput()->getMembers() as $name => $member) {
47
            if (!empty($member['streaming']) && isset($command[$name])) {
48
                $hasStreaming = true;
49
                if (!empty($member['requiresLength'])) {
50
                    $requiresLength = true;
51
                }
52
            }
53
        }
54
 
55
        if ($hasStreaming) {
56
 
57
            // Add 'transfer-encoding' header if payload size not required to
58
            // to be calculated and not already known
59
            if (empty($requiresLength)
60
                && empty($contentLength)
61
                && isset($operation['authtype'])
62
                && $operation['authtype'] == 'v4-unsigned-body'
63
            ) {
64
                $request = $request->withHeader('transfer-encoding', 'chunked');
65
 
66
            // Otherwise, make sure 'content-length' header is added
67
            } else {
68
                if (empty($contentLength)) {
69
                    $size = $request->getBody()->getSize();
70
                    if (is_null($size)) {
71
                        throw new IncalculablePayloadException('Payload'
72
                            . ' content length is required and can not be'
73
                            . ' calculated.');
74
                    }
75
                    $request = $request->withHeader(
76
                        'content-length',
77
                        $size
78
                    );
79
                }
80
            }
81
        }
82
 
83
        return $nextHandler($command, $request);
84
    }
85
}