|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
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
|
|
|
namespace Surfnet\StepupBundle\EventListener; |
|
20
|
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Request\RequestId; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
23
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* When receiving a kernel request, reads the request ID from the X-Stepup-Request-Id header, if present, and sets it on |
|
27
|
|
|
* a RequestId instance. |
|
28
|
|
|
*/ |
|
29
|
|
|
class RequestIdRequestResponseListener |
|
30
|
|
|
{ |
|
31
|
|
|
const HEADER_NAME = 'X-Stepup-Request-Id'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RequestId |
|
35
|
|
|
*/ |
|
36
|
|
|
private $requestId; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param RequestId $requestId |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(RequestId $requestId) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->requestId = $requestId; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* If present, reads the request ID from the appropriate header and sets it on a RequestId instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @param GetResponseEvent $event |
|
50
|
|
|
*/ |
|
51
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
|
52
|
|
|
{ |
|
53
|
|
|
$headers = $event->getRequest()->headers; |
|
54
|
|
|
|
|
55
|
|
|
if (!$headers->has(self::HEADER_NAME)) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->requestId->set($headers->get(self::HEADER_NAME, null, true)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* If enabled, sets the request ID on the appropriate response header. |
|
64
|
|
|
* |
|
65
|
|
|
* @param FilterResponseEvent $event |
|
66
|
|
|
*/ |
|
67
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
|
68
|
|
|
{ |
|
69
|
|
|
$event->getResponse()->headers->set(self::HEADER_NAME, $this->requestId->get()); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|