1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http\Aggregator; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\Http\HttpMessageHeader; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* HttpRequestUriAggregator |
31
|
|
|
* |
32
|
|
|
* Implements a resolver for a http request uri. |
33
|
|
|
* @author Celestino Diaz <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class HttpRequestUriAggregator implements UriAggregator { |
36
|
|
|
|
37
|
|
|
/** @var \Brickoo\Component\Http\HttpMessageHeader */ |
38
|
|
|
private $header; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
private $serverValues; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class constructor. |
45
|
|
|
* @param \Brickoo\Component\Http\HttpMessageHeader $header |
46
|
|
|
* @param array $serverValues the server variables as key/value pairs |
47
|
|
|
*/ |
48
|
3 |
|
public function __construct(HttpMessageHeader $header, array $serverValues = []) { |
49
|
3 |
|
$this->header = $header; |
50
|
3 |
|
$this->serverValues = $serverValues; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** {@inheritDoc} */ |
54
|
2 |
|
public function getScheme() { |
55
|
2 |
|
if (!($isSecure = $this->isForwardedFromHttps())) { |
56
|
1 |
|
$isSecure = $this->isHttpsMode(); |
57
|
1 |
|
} |
58
|
2 |
|
return "http".($isSecure ? "s" : ""); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** {@inheritDoc} */ |
62
|
2 |
|
public function getHostname() { |
63
|
2 |
|
if ($this->header->contains("Host")) { |
64
|
1 |
|
return $this->header->getField("Host")->getValue(); |
65
|
|
|
} |
66
|
1 |
|
return $this->getServerVar("SERVER_NAME", $this->getServerVar("SERVER_ADDR", "localhost")); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritDoc} */ |
70
|
2 |
|
public function getPort() { |
71
|
2 |
|
if ($this->header->contains("X-Forwarded-Port")) { |
72
|
1 |
|
return (int)$this->header->getField("X-Forwarded-Port")->getValue(); |
73
|
|
|
} |
74
|
1 |
|
return (int)$this->getServerVar("SERVER_PORT", 80); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** {@inheritDoc} */ |
78
|
5 |
|
public function getPath() { |
79
|
5 |
|
if ((!$requestPath = $this->getServerVar("REQUEST_URI")) |
80
|
5 |
|
&& (!$requestPath = $this->getServerVar("ORIG_PATH_INFO"))) { |
81
|
3 |
|
$requestPath = $this->getIisRequestUri(); |
82
|
3 |
|
} |
83
|
5 |
|
return "/".trim(rawurldecode(strval(parse_url($requestPath, PHP_URL_PATH))), "/"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** {@inheritDoc} */ |
87
|
2 |
|
public function getQueryString() { |
88
|
2 |
|
if (!$queryString = $this->getServerVar("QUERY_STRING")) { |
89
|
1 |
|
$queryArray = []; |
90
|
1 |
|
foreach ($_GET as $key => $value) { |
91
|
1 |
|
$queryArray[] = $key."=".$value; |
92
|
1 |
|
} |
93
|
1 |
|
$queryString = implode("&", $queryArray); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
2 |
|
return urldecode($queryString); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** {@inheritDoc} */ |
100
|
1 |
|
public function getFragment() { |
101
|
1 |
|
return ""; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if the request was forwarded from a https connection. |
106
|
|
|
* @return boolean check result |
107
|
|
|
*/ |
108
|
2 |
|
private function isForwardedFromHttps() { |
109
|
2 |
|
$isSecure = false; |
110
|
2 |
|
if ($this->header->contains("X-Forwarded-Proto")) { |
111
|
1 |
|
$httpsForwarded = $this->header->getField("X-Forwarded-Proto")->getValue(); |
112
|
1 |
|
$isSecure = (strtolower($httpsForwarded) == "https"); |
113
|
1 |
|
} |
114
|
2 |
|
return $isSecure; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the server provides a https connection. |
119
|
|
|
* @return boolean check result |
120
|
|
|
*/ |
121
|
1 |
|
private function isHttpsMode() { |
122
|
1 |
|
$isSecure = false; |
123
|
1 |
|
if ($httpsMode = $this->getServerVar("HTTPS", "off")) { |
124
|
1 |
|
$isSecure = in_array(strtolower($httpsMode), ["on", "1"]); |
125
|
1 |
|
} |
126
|
1 |
|
return $isSecure; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the IIS request ur assigned if available. |
131
|
|
|
* @return string|null the request uri or null on unavailable |
132
|
|
|
*/ |
133
|
3 |
|
private function getIisRequestUri() { |
134
|
3 |
|
if ($this->header->contains("X-Original-Url")) { |
135
|
1 |
|
return $this->header->getField("X-Original-Url")->getValue(); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
if ($this->header->contains("X-Rewrite-Url")) { |
139
|
1 |
|
return $this->header->getField("X-Rewrite-Url")->getValue(); |
140
|
|
|
} |
141
|
1 |
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return a server variable or the default value if it does not exist. |
146
|
|
|
* @param string $key the key of the server variable |
147
|
|
|
* @param string|null $defaultValue the default value to return |
148
|
|
|
* @return string|null the value of the server variable otherwise the default value |
149
|
|
|
*/ |
150
|
7 |
|
private function getServerVar($key, $defaultValue = null) { |
151
|
7 |
|
if (isset($this->serverValues[$key])) { |
152
|
6 |
|
return $this->serverValues[$key]; |
153
|
|
|
} |
154
|
3 |
|
return $defaultValue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|