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