1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Packages\Cors; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Closure; |
22
|
|
|
use Limoncello\Contracts\Application\MiddlewareInterface; |
23
|
|
|
use Neomerx\Cors\Contracts\AnalysisResultInterface; |
24
|
|
|
use Neomerx\Cors\Contracts\AnalyzerInterface; |
25
|
|
|
use Psr\Container\ContainerExceptionInterface; |
26
|
|
|
use Psr\Container\ContainerInterface; |
27
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
28
|
|
|
use Psr\Http\Message\ResponseInterface; |
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
30
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
31
|
|
|
use function assert; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @package Limoncello\Application |
35
|
|
|
*/ |
36
|
|
|
class CorsMiddleware implements MiddlewareInterface |
37
|
|
|
{ |
38
|
|
|
/** Middleware handler */ |
39
|
|
|
const CALLABLE_HANDLER = [self::class, self::MIDDLEWARE_METHOD_NAME]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
* |
44
|
7 |
|
* @throws ContainerExceptionInterface |
45
|
|
|
* @throws NotFoundExceptionInterface |
46
|
|
|
*/ |
47
|
|
|
public static function handle( |
48
|
|
|
ServerRequestInterface $request, |
49
|
|
|
Closure $next, |
50
|
7 |
|
ContainerInterface $container |
51
|
7 |
|
): ResponseInterface { |
52
|
|
|
/** @var AnalyzerInterface $analyzer */ |
53
|
7 |
|
$analyzer = $container->get(AnalyzerInterface::class); |
54
|
|
|
$analysis = $analyzer->analyze($request); |
55
|
7 |
|
|
56
|
|
|
$requestType = $analysis->getRequestType(); |
57
|
1 |
|
switch ($requestType) { |
58
|
|
|
case AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE: |
59
|
6 |
|
// call next middleware handler |
60
|
|
|
return $next($request); |
61
|
1 |
|
|
62
|
|
|
case AnalysisResultInterface::TYPE_ACTUAL_REQUEST: |
63
|
|
|
// actual CORS request |
64
|
1 |
|
$corsHeaders = $analysis->getResponseHeaders(); |
65
|
|
|
|
66
|
|
|
/** @var ResponseInterface $response */ |
67
|
1 |
|
$response = $next($request); |
68
|
1 |
|
|
69
|
|
|
// add CORS headers to Response $response |
70
|
|
|
foreach ($corsHeaders as $name => $value) { |
71
|
1 |
|
$response = $response->withHeader($name, $value); |
72
|
|
|
} |
73
|
5 |
|
|
74
|
|
|
return $response; |
75
|
1 |
|
|
76
|
|
|
case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST: |
77
|
4 |
|
// return 200 HTTP with $corsHeaders |
78
|
1 |
|
return new EmptyResponse(200, $analysis->getResponseHeaders()); |
79
|
|
|
|
80
|
3 |
|
case AnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED: |
81
|
1 |
|
return static::getErrorOriginNotAllowedResponse($analysis); |
82
|
|
|
|
83
|
2 |
|
case AnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED: |
84
|
1 |
|
return static::getErrorMethodNotSupportedResponse($analysis); |
85
|
|
|
|
86
|
1 |
|
case AnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED: |
87
|
|
|
return static::getErrorHeadersNotSupportedResponse($analysis); |
88
|
1 |
|
|
89
|
|
|
case AnalysisResultInterface::ERR_NO_HOST_HEADER: |
90
|
1 |
|
default: |
91
|
|
|
assert($requestType === AnalysisResultInterface::ERR_NO_HOST_HEADER); |
92
|
|
|
|
93
|
|
|
return static::getErrorNoHostHeaderResponse($analysis); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param AnalysisResultInterface $analysis |
99
|
|
|
* |
100
|
|
|
* @return ResponseInterface |
101
|
1 |
|
* |
102
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
103
|
|
|
*/ |
104
|
1 |
|
protected static function getErrorNoHostHeaderResponse( |
105
|
|
|
/** @noinspection PhpUnusedParameterInspection */ AnalysisResultInterface $analysis |
|
|
|
|
106
|
|
|
): ResponseInterface { |
107
|
|
|
return new EmptyResponse(400); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param AnalysisResultInterface $analysis |
112
|
|
|
* |
113
|
|
|
* @return ResponseInterface |
114
|
1 |
|
* |
115
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
116
|
|
|
*/ |
117
|
1 |
|
protected static function getErrorOriginNotAllowedResponse( |
118
|
|
|
/** @noinspection PhpUnusedParameterInspection */ AnalysisResultInterface $analysis |
|
|
|
|
119
|
|
|
): ResponseInterface { |
120
|
|
|
return new EmptyResponse(400); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param AnalysisResultInterface $analysis |
125
|
|
|
* |
126
|
|
|
* @return ResponseInterface |
127
|
1 |
|
* |
128
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
129
|
|
|
*/ |
130
|
1 |
|
protected static function getErrorMethodNotSupportedResponse( |
131
|
|
|
/** @noinspection PhpUnusedParameterInspection */ AnalysisResultInterface $analysis |
|
|
|
|
132
|
|
|
): ResponseInterface { |
133
|
|
|
return new EmptyResponse(400); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param AnalysisResultInterface $analysis |
138
|
|
|
* |
139
|
|
|
* @return ResponseInterface |
140
|
1 |
|
* |
141
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
142
|
|
|
*/ |
143
|
1 |
|
protected static function getErrorHeadersNotSupportedResponse( |
144
|
|
|
/** @noinspection PhpUnusedParameterInspection */ AnalysisResultInterface $analysis |
|
|
|
|
145
|
|
|
): ResponseInterface { |
146
|
|
|
return new EmptyResponse(400); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.