|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Helper; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
|
17
|
|
|
use Silverback\ApiComponentsBundle\Exception\OutOfBoundsException; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Daniel West <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class RefererUrlResolver |
|
24
|
|
|
{ |
|
25
|
|
|
private RequestStack $requestStack; |
|
26
|
|
|
|
|
27
|
14 |
|
public function __construct(RequestStack $requestStack) |
|
28
|
|
|
{ |
|
29
|
14 |
|
$this->requestStack = $requestStack; |
|
30
|
14 |
|
} |
|
31
|
|
|
|
|
32
|
14 |
|
public function getAbsoluteUrl($path): string |
|
33
|
|
|
{ |
|
34
|
14 |
|
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) { |
|
35
|
1 |
|
return $path; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
13 |
|
$request = $this->requestStack->getMasterRequest(); |
|
39
|
13 |
|
if (!$request) { |
|
40
|
1 |
|
throw new InvalidArgumentException('To generate an absolute URL to the referrer, there must be a valid master request'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
12 |
|
if (null !== ($origin = $request->headers->get('origin', null))) { |
|
|
|
|
|
|
44
|
3 |
|
return $this->concatPathToUrl($origin, $path); |
|
45
|
|
|
} |
|
46
|
9 |
|
if (null !== ($referer = $request->headers->get('referer', null))) { |
|
47
|
8 |
|
return $this->concatPathToUrl($referer, $path); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
throw new OutOfBoundsException('To generate an absolute URL to the referrer, the request must have a `origin` or `referer` header present'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
private function concatPathToUrl(string $origin, string $path): string |
|
54
|
|
|
{ |
|
55
|
11 |
|
return $this->getUrlPrefix($origin) . '/' . ltrim($path, '/'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
11 |
|
private function getUrlPrefix(string $url): string |
|
59
|
|
|
{ |
|
60
|
|
|
$defaults = [ |
|
61
|
11 |
|
'host' => null, |
|
62
|
|
|
'scheme' => null, |
|
63
|
|
|
'port' => null, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
[ |
|
67
|
11 |
|
'host' => $host, |
|
68
|
11 |
|
'scheme' => $scheme, |
|
69
|
11 |
|
'port' => $port |
|
70
|
11 |
|
] = array_merge($defaults, parse_url($url) ?: []); |
|
71
|
|
|
|
|
72
|
11 |
|
if (null === $host) { |
|
73
|
3 |
|
throw new OutOfBoundsException('Could not extract `host` while parsing the `referer` header'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
8 |
|
if (null === $scheme) { |
|
77
|
1 |
|
throw new OutOfBoundsException('Could not extract `scheme` while parsing the `referer` header'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
7 |
|
$url = $scheme . '://' . $host; |
|
81
|
7 |
|
if ($port && (('https' === $scheme && 443 !== $port) || ('http' === $scheme && 80 !== $port))) { |
|
82
|
1 |
|
$url .= ':' . $port; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
7 |
|
return $url; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|