1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMiddlewares; |
4
|
|
|
|
5
|
|
|
use Neomerx\Cors\Analyzer; |
6
|
|
|
use Neomerx\Cors\Contracts\AnalysisResultInterface; |
7
|
|
|
use Neomerx\Cors\Contracts\AnalyzerInterface; |
8
|
|
|
use Neomerx\Cors\Contracts\Strategies\SettingsStrategyInterface; |
9
|
|
|
use Neomerx\Cors\Strategies\Settings; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CorsMiddleware |
16
|
|
|
* |
17
|
|
|
* @package Thruster\Component\HttpMiddlewares |
18
|
|
|
* @author Aurimas Niekis <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class CorsMiddleware |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var AnalyzerInterface |
24
|
|
|
*/ |
25
|
|
|
private $analyzer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SettingsStrategyInterface |
29
|
|
|
*/ |
30
|
|
|
private $settings; |
31
|
|
|
|
32
|
|
|
public function __construct(SettingsStrategyInterface $settings = null, LoggerInterface $logger = null) |
33
|
|
|
{ |
34
|
|
|
$this->settings = $settings ?? new Settings(); |
35
|
|
|
|
36
|
|
|
$this->analyzer = Analyzer::instance($this->settings); |
37
|
|
|
$this->analyzer->setLogger($logger); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
41
|
|
|
{ |
42
|
|
|
$cors = $this->analyzer->analyze($request); |
43
|
|
|
|
44
|
|
|
switch ($cors->getRequestType()) { |
45
|
|
|
case AnalysisResultInterface::ERR_NO_HOST_HEADER: |
|
|
|
|
46
|
|
|
case AnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED: |
|
|
|
|
47
|
|
|
case AnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED: |
48
|
|
|
case AnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED: |
49
|
|
|
return $response->withStatus(403); |
50
|
|
|
case AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE: |
51
|
|
|
return $next($request, $response); |
52
|
|
|
case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST: |
53
|
|
|
foreach ($cors->getResponseHeaders() as $name => $value) { |
54
|
|
|
$response = $response->withHeader($name, $value); |
55
|
|
|
} |
56
|
|
|
return $response->withStatus(200); |
57
|
|
|
default: |
58
|
|
|
/** @var ResponseInterface $response */ |
59
|
|
|
$response = $next($request, $response); |
60
|
|
|
|
61
|
|
|
foreach ($cors->getResponseHeaders() as $name => $value) { |
62
|
|
|
$response = $response->withHeader($name, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $response; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return SettingsStrategyInterface |
71
|
|
|
*/ |
72
|
|
|
public function getSettings() |
73
|
|
|
{ |
74
|
|
|
return $this->settings; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param AnalyzerInterface $analyzer |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setAnalyzer($analyzer) |
83
|
|
|
{ |
84
|
|
|
$this->analyzer = $analyzer; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):