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
|
|
|
|
38
|
|
|
if (null !== $logger) { |
39
|
|
|
$this->analyzer->setLogger($logger); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
44
|
|
|
{ |
45
|
|
|
$cors = $this->analyzer->analyze($request); |
46
|
|
|
|
47
|
|
|
switch ($cors->getRequestType()) { |
48
|
|
|
case AnalysisResultInterface::ERR_NO_HOST_HEADER: |
|
|
|
|
49
|
|
|
case AnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED: |
|
|
|
|
50
|
|
|
case AnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED: |
51
|
|
|
case AnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED: |
52
|
|
|
return $response->withStatus(403); |
53
|
|
|
case AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE: |
54
|
|
|
return $next($request, $response); |
55
|
|
|
case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST: |
56
|
|
|
foreach ($cors->getResponseHeaders() as $name => $value) { |
57
|
|
|
$response = $response->withHeader($name, $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $response->withStatus(200); |
61
|
|
|
default: |
62
|
|
|
foreach ($cors->getResponseHeaders() as $name => $value) { |
63
|
|
|
$response = $response->withHeader($name, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $next($request, $response); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return SettingsStrategyInterface |
72
|
|
|
*/ |
73
|
|
|
public function getSettings() |
74
|
|
|
{ |
75
|
|
|
return $this->settings; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param AnalyzerInterface $analyzer |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setAnalyzer($analyzer) |
84
|
|
|
{ |
85
|
|
|
$this->analyzer = $analyzer; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.