1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShiftOneLabs\LaravelCors\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use ShiftOneLabs\LaravelCors\CorsPolicyManager; |
9
|
|
|
|
10
|
|
|
class ApplyCorsPolicy |
11
|
|
|
{ |
12
|
|
|
/** @var \ShiftOneLabs\LaravelCors\CorsPolicyManager $corsPolicyManager */ |
13
|
|
|
protected $corsPolicyManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create a new middleware instance. |
17
|
|
|
* |
18
|
|
|
* @param \ShiftOneLabs\LaravelCors\CorsPolicyManager $corsPolicyManager |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
46 |
|
public function __construct(CorsPolicyManager $corsPolicyManager) |
23
|
|
|
{ |
24
|
46 |
|
$this->corsPolicyManager = $corsPolicyManager; |
25
|
46 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Handle an incoming request. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Http\Request $request |
31
|
|
|
* @param \Closure $next |
32
|
|
|
* @param string|null $profile |
33
|
|
|
* |
34
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
35
|
|
|
*/ |
36
|
46 |
|
public function handle($request, Closure $next, $profile = null) |
37
|
|
|
{ |
38
|
46 |
|
$cors = $this->corsPolicyManager->make($profile); |
39
|
|
|
|
40
|
46 |
|
if (!$cors->isCorsRequest($request)) { |
41
|
46 |
|
return $next($request); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($cors->isPreflightRequest($request)) { |
45
|
|
|
$preflightResponse = $cors->handlePreflightRequest($request); |
46
|
|
|
|
47
|
|
|
// Stop the call stack once a rejected preflight response is made. |
48
|
|
|
if ($cors->isPreflightRejected($preflightResponse)) { |
49
|
|
|
return $this->prepareResponse($preflightResponse); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// If this preflight was successful, move on to the next. |
53
|
|
|
$response = $next($request); |
54
|
|
|
|
55
|
|
|
// The destination core is plain text, not a response object. If we |
56
|
|
|
// hit the core, or the last middleware failed, return the current |
57
|
|
|
// preflight response. If we're on the way down the stack, |
58
|
|
|
// continue to return the last preflight response. |
59
|
|
|
return $this->prepareResponse($response instanceof Response && $response->isSuccessful() ? $response : $preflightResponse); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Stop the call stack once a CORS request is rejected. |
63
|
|
|
if (!$cors->isActualRequestAllowed($request)) { |
64
|
|
|
return $this->prepareResponse($cors->createNotAllowedResponse($request)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$response = Router::toResponse($request, $next($request)); |
68
|
|
|
|
69
|
|
|
// CORS policy already applied. |
70
|
|
|
if ($response->headers->has('X-S1L-CORS-HANDLED')) { |
71
|
|
|
return $this->prepareResponse($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->prepareResponse($cors->addActualRequestHeaders($response, $request)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Prepare the response that has been handled by the CORS middleware. |
79
|
|
|
* |
80
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response |
81
|
|
|
* |
82
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
83
|
|
|
*/ |
84
|
|
|
protected function prepareResponse(Response $response) |
85
|
|
|
{ |
86
|
|
|
$response->headers->set('X-S1L-CORS-HANDLED', true); |
87
|
|
|
|
88
|
|
|
return $response; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|