Skip to content

Commit 03b90dc

Browse files
committed
Releasing 1.1.1
2 parents 92e2844 + bf06626 commit 03b90dc

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/TgUtils/Request.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static function getRequest() {
3838
public $uri;
3939
/** The path of the request. Does not include parameters */
4040
public $path;
41+
/** The path of the original request (requested at proxy). Does not include parameters */
42+
public $originalPath;
4143
/** The path split in its elements */
4244
public $pathElements;
4345
/** The parameters as a string */
@@ -89,6 +91,7 @@ public function __construct() {
8991
$this->body = NULL;
9092
$this->documentRoot = $this->initDocumentRoot();
9193
$this->webRoot = $this->initWebRoot(TRUE);
94+
$this->originalPath = $this->initOriginalPath();
9295
$this->localWebRoot = $this->initWebRoot(FALSE);
9396
$this->webRootUri = $this->initWebRootUri();
9497
$this->appRoot = $this->documentRoot;
@@ -111,7 +114,10 @@ public function __construct() {
111114
protected function initHost() {
112115
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
113116
$forwarded = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
114-
return trim($forwarded[count($forwarded)-1]);
117+
$last = trim($forwarded[count($forwarded)-1]);
118+
$first = trim($forwarded[0]);
119+
if ($first != $this->httpHost) return $first;
120+
if ($last != $this->httpHost) return $last;
115121
}
116122
return $this->httpHost;
117123
}
@@ -282,7 +288,24 @@ protected function initDocumentRoot() {
282288
}
283289
return $_SERVER['DOCUMENT_ROOT'];
284290
}
285-
291+
292+
/**
293+
* Returns the original path as request by the end user.
294+
* The path might be different from $this->path as
295+
* a webroot mapping might be involved.
296+
*/
297+
protected function initOriginalPath() {
298+
$rc = $this->path;
299+
$rootDef = $_SERVER['HTTP_X_FORWARDED_ROOT'];
300+
if ($rootDef) {
301+
$arr = explode(',', $rootDef);
302+
if (strpos($rc, $arr[0]) === 0) {
303+
$rc = $arr[1].substr($rc, strlen($arr[0]));
304+
}
305+
}
306+
return $rc;
307+
}
308+
286309
/**
287310
* Returns the web root - that is the web path where the current
288311
* script is rooted and usually the base path for an application.
@@ -295,7 +318,9 @@ protected function initWebRoot($considerForwarding = TRUE) {
295318
$rootDef = $_SERVER['HTTP_X_FORWARDED_ROOT'];
296319
if ($rootDef) {
297320
$arr = explode(',', $rootDef);
298-
return $arr[1];
321+
$rc = $arr[1];
322+
if ((strlen($rc) > 0) && (substr($rc, -1) == '/')) $rc = substr($rc, 0, strlen($rc)-1);
323+
return $rc;
299324
}
300325
}
301326
$docRoot = $this->documentRoot;
@@ -304,6 +329,7 @@ protected function initWebRoot($considerForwarding = TRUE) {
304329
if (isset($_SERVER['CONTEXT'])) {
305330
$webRoot = $_SERVER['CONTEXT'].$webRoot;
306331
}
332+
if ((strlen($webRoot) > 0) && (substr($webRoot, -1) == '/')) $webRoot = substr($rc, 0, strlen($webRoot)-1);
307333
return $webRoot;
308334
}
309335

0 commit comments

Comments
 (0)