Completed
Push — master ( b2599f...653121 )
by Alexander
14:59 queued 04:10
created

BasicNetworkResolver::withoutProtocolHeaders()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
4
namespace Yiisoft\Yii\Web\Middleware;
5
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
12
/**
13
 * Basic network resolver updates an instance of server request with protocol from special headers.
14
 *
15
 * It can be used in the following cases:
16
 * - not required IP resolve to access the user's IP
17
 * - user's IP is already resolved (eg `ngx_http_realip_module` or similar)
18
 */
19
class BasicNetworkResolver implements MiddlewareInterface
20
{
21
    private const DEFAULT_PROTOCOL_AND_ACCEPTABLE_VALUES = [
22
        'http' => ['http'],
23
        'https' => ['https', 'on'],
24
    ];
25
26
    private $protocolHeaders = [];
27
28
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
29
    {
30
        $newScheme = null;
31
        foreach ($this->protocolHeaders as $header => $data) {
32
            if (!$request->hasHeader($header)) {
33
                continue;
34
            }
35
            $headerValues = $request->getHeader($header);
36
            if (is_callable($data)) {
37
                $newScheme = $data($headerValues, $header, $request);
38
                if ($newScheme === null) {
39
                    continue;
40
                }
41
                if (!is_string($newScheme)) {
42
                    throw new \RuntimeException('The scheme is neither string nor null!');
43
                }
44
                if ($newScheme === '') {
45
                    throw new \RuntimeException('The scheme cannot be an empty string!');
46
                }
47
                break;
48
            }
49
            $headerValue = strtolower($headerValues[0]);
50
            foreach ($data as $protocol => $acceptedValues) {
51
                if (!in_array($headerValue, $acceptedValues, true)) {
52
                    continue;
53
                }
54
                $newScheme = $protocol;
55
                break 2;
56
            }
57
        }
58
        $uri = $request->getUri();
59
        if ($newScheme !== null && $newScheme !== $uri->getScheme()) {
60
            $request = $request->withUri($uri->withScheme($newScheme));
61
        }
62
        return $handler->handle($request);
63
    }
64
65
    /**
66
     * With added header to check for determining whether the connection is made via HTTP or HTTPS (or any protocol).
67
     *
68
     * The match of header names and values is case-insensitive.
69
     * It's not advisable to put insecure/untrusted headers here.
70
     *
71
     * Accepted types of values:
72
     * - NULL (default): {{DEFAULT_PROTOCOL_AND_ACCEPTABLE_VALUES}}
73
     * - callable: custom function for getting the protocol
74
     * ```php
75
     * ->withProtocolHeader('x-forwarded-proto', function(array $values, string $header, ServerRequestInterface $request) {
76
     *   return $values[0] === 'https' ? 'https' : 'http';
77
     *   return null;     // If it doesn't make sense.
78
     * });
79
     * ```
80
     * - array: The array keys are protocol string and the array value is a list of header values that indicate the protocol.
81
     * ```php
82
     * ->withProtocolHeader('x-forwarded-proto', [
83
     *   'http' => ['http'],
84
     *   'https' => ['https']
85
     * ]);
86
     * ```
87
     * @param string $header
88
     * @param callable|array|null $values
89
     * @see DEFAULT_PROTOCOL_AND_ACCEPTABLE_VALUES
90
     */
91
    public function withAddedProtocolHeader(string $header, $values = null): self
92
    {
93
        $new = clone $this;
94
        $header = strtolower($header);
95
        if ($values === null) {
96
            $new->protocolHeaders[$header] = self::DEFAULT_PROTOCOL_AND_ACCEPTABLE_VALUES;
97
            return $new;
98
        }
99
        if (is_callable($values)) {
100
            $new->protocolHeaders[$header] = $values;
101
            return $new;
102
        }
103
        if (!is_array($values)) {
104
            throw new \RuntimeException('Accepted values is not array nor callable!');
105
        }
106
        if (count($values) === 0) {
107
            throw new \RuntimeException('Accepted values cannot be an empty array!');
108
        }
109
        $new->protocolHeaders[$header] = [];
110
        foreach ($values as $protocol => $acceptedValues) {
111
            if (!is_string($protocol)) {
112
                throw new \RuntimeException('The protocol must be type of string!');
113
            }
114
            if ($protocol === '') {
115
                throw new \RuntimeException('The protocol cannot be an empty string!');
116
            }
117
            $new->protocolHeaders[$header][$protocol] = array_map('strtolower', (array)$acceptedValues);
118
        }
119
        return $new;
120
    }
121
122
    public function withoutProtocolHeader(string $header): self
123
    {
124
        $new = clone $this;
125
        unset($new->protocolHeaders[strtolower($header)]);
126
        return $new;
127
    }
128
129
    public function withoutProtocolHeaders(?array $headers = null): self
130
    {
131
        $new = clone $this;
132
        if ($headers === null) {
133
            $new->protocolHeaders = [];
134
        } else {
135
            foreach ($headers as $header) {
136
                $new = $new->withoutProtocolHeader($header);
137
            }
138
        }
139
        return $new;
140
    }
141
}
142