@@ -38,6 +38,8 @@ public static function getRequest() {
38
38
public $ uri ;
39
39
/** The path of the request. Does not include parameters */
40
40
public $ path ;
41
+ /** The path of the original request (requested at proxy). Does not include parameters */
42
+ public $ originalPath ;
41
43
/** The path split in its elements */
42
44
public $ pathElements ;
43
45
/** The parameters as a string */
@@ -89,6 +91,7 @@ public function __construct() {
89
91
$ this ->body = NULL ;
90
92
$ this ->documentRoot = $ this ->initDocumentRoot ();
91
93
$ this ->webRoot = $ this ->initWebRoot (TRUE );
94
+ $ this ->originalPath = $ this ->initOriginalPath ();
92
95
$ this ->localWebRoot = $ this ->initWebRoot (FALSE );
93
96
$ this ->webRootUri = $ this ->initWebRootUri ();
94
97
$ this ->appRoot = $ this ->documentRoot ;
@@ -111,7 +114,10 @@ public function __construct() {
111
114
protected function initHost () {
112
115
if (isset ($ _SERVER ['HTTP_X_FORWARDED_HOST ' ])) {
113
116
$ 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 ;
115
121
}
116
122
return $ this ->httpHost ;
117
123
}
@@ -282,7 +288,24 @@ protected function initDocumentRoot() {
282
288
}
283
289
return $ _SERVER ['DOCUMENT_ROOT ' ];
284
290
}
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
+
286
309
/**
287
310
* Returns the web root - that is the web path where the current
288
311
* script is rooted and usually the base path for an application.
@@ -295,7 +318,9 @@ protected function initWebRoot($considerForwarding = TRUE) {
295
318
$ rootDef = $ _SERVER ['HTTP_X_FORWARDED_ROOT ' ];
296
319
if ($ rootDef ) {
297
320
$ 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 ;
299
324
}
300
325
}
301
326
$ docRoot = $ this ->documentRoot ;
@@ -304,6 +329,7 @@ protected function initWebRoot($considerForwarding = TRUE) {
304
329
if (isset ($ _SERVER ['CONTEXT ' ])) {
305
330
$ webRoot = $ _SERVER ['CONTEXT ' ].$ webRoot ;
306
331
}
332
+ if ((strlen ($ webRoot ) > 0 ) && (substr ($ webRoot , -1 ) == '/ ' )) $ webRoot = substr ($ rc , 0 , strlen ($ webRoot )-1 );
307
333
return $ webRoot ;
308
334
}
309
335
0 commit comments