Skip to content

Commit d60a5bf

Browse files
author
Greg Bowler
authored
Build up request URI in parts for greater compatibility (#31)
1 parent 8d05ab1 commit d60a5bf

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ServerInfo.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,36 @@ public function getScriptName():string {
136136
* The URI which was given in order to access this page.
137137
*/
138138
public function getRequestUri():UriInterface {
139-
return new Uri($this->server["REQUEST_URI"]);
139+
$uri = new Uri();
140+
141+
if(isset($this->server["HTTPS"])) {
142+
$uri = $uri->withScheme("https");
143+
}
144+
else {
145+
$uri = $uri->withScheme("http");
146+
}
147+
148+
if(isset($this->server["HTTP_HOST"])) {
149+
$uri = $uri->withHost(
150+
strtok($this->server["HTTP_HOST"], ":")
151+
);
152+
}
153+
154+
if(isset($this->server["SERVER_PORT"])) {
155+
$uri = $uri->withPort((int)$this->server["SERVER_PORT"]);
156+
}
157+
158+
if(isset($this->server["REQUEST_URI"])) {
159+
$uri = $uri->withPath(
160+
strtok($this->server["REQUEST_URI"], "?")
161+
);
162+
}
163+
164+
if(isset($this->server["QUERY_STRING"])) {
165+
$uri = $uri->withQuery($this->server["QUERY_STRING"]);
166+
}
167+
168+
return $uri;
140169
}
141170

142171
public function getFullUri():UriInterface {

test/unit/ServerInfoTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ public function testGetScriptName() {
117117

118118
public function testGetRequestUri() {
119119
$sut = new ServerInfo(self::getServerArray());
120-
self::assertEquals("/example?key1=value1&key2=value2", $sut->getRequestUri());
120+
121+
self::assertEquals(
122+
"http://localhost/example?key1=value1&key2=value2",
123+
$sut->getRequestUri()
124+
);
121125
}
122126

123127
public function testNullFields() {

0 commit comments

Comments
 (0)