Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 343367f

Browse files
committed
ADD support to header X-Forwarded-Prefix
1 parent 60323fe commit 343367f

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

‎src/utils/UriHelper.php‎

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,62 @@ public static function getPathInfo(?string $requestUri = null, ?string $scriptNa
205205
return '/' . ltrim($pathInfo, '/');
206206
}
207207

208+
private static function buildStringFromParts(array $uriParts): string
209+
{
210+
$join[] = $scheme = $uriParts['scheme'] ?? 'http';
211+
$join[] = '://';
212+
if (isset($uriParts['user'])) {
213+
$join[] = $uriParts['user'];
214+
if (isset($uriParts['pass'])) {
215+
$join[] = ":{$uriParts['pass']}";
216+
}
217+
$join[] = '@';
218+
}
219+
$join[] = $uriParts['host'] ?? 'localhost';
220+
if (isset($uriParts['port'])) {
221+
$port = $uriParts['port'];
222+
if (
223+
$scheme == 'http' && $port != 80 ||
224+
$scheme == 'https' && $port != 443
225+
) {
226+
$join[] = ":{$port}";
227+
}
228+
}
229+
$join[] = '/';
230+
if (isset($uriParts['path'])) {
231+
$join[] = ltrim($uriParts['path'], '/');
232+
}
233+
if (isset($uriParts['query'])) {
234+
$join[] = "?{$uriParts['query']}";
235+
}
236+
if (isset($uriParts['fragment'])) {
237+
$join[] .= "#{$uriParts['fragment']}";
238+
}
239+
return join('', $join);
240+
}
241+
208242
public static function getSiteUrl(?string $path = null, array $sapiVars = [], bool $cached = false)
209243
{
210244
$sapiVars = array_merge($_SERVER, $sapiVars);
211245
$sapiVars['REQUEST_URI'] = $sapiVars['SCRIPT_NAME'] ?? '';
212246
$uri = static::getCurrentString($sapiVars, $cached);
213-
if (isset($path)) {
214-
$uri = rtrim($uri, '/') . '/' . ltrim($path, '/');
247+
$uriParts = parse_url($uri);
248+
if (isset($sapiVars['X_FORWARDED_PREFIX'])) {
249+
$uriParts['path'] =
250+
'/' . ltrim($sapiVars['X_FORWARDED_PREFIX'], '/') .
251+
$uriParts['path'];
215252
}
216-
return $uri;
253+
if (!empty($path)) {
254+
$uriParts['path'] = ($uriParts['path'] ?? '') . '/' . ltrim($path, '/');
255+
}
256+
return static::buildStringFromParts($uriParts);
217257
}
218258

219259
public static function getBaseUrl(?string $path = null, array $sapiVars = [], bool $cached = false)
220260
{
221261
$sapiVars = array_merge($_SERVER, $sapiVars);
222262
$scriptName = $sapiVars['SCRIPT_NAME'] ?? '';
223-
$sapiVars['SCRIPT_NAME'] = substr_replace($scriptName, '', strrpos($scriptName, '/'));
263+
$sapiVars['SCRIPT_NAME'] = '/' . ltrim(strtr(dirname($scriptName), '\\', '/'), '/');
224264
return static::getSiteUrl($path, $sapiVars, $cached);
225265
}
226266

@@ -233,25 +273,18 @@ public static function getCurrentString(array $sapiVars = [], bool $cached = fal
233273

234274
$sapiVars = array_merge($_SERVER, $sapiVars);
235275

236-
$scheme = 'http';
237-
$defaultPort = 80;
238-
if (!empty($sapiVars['HTTPS']) && $sapiVars['HTTPS'] != 'off') {
239-
$scheme = 'https';
240-
$defaultPort = 443;
276+
$uriParts['scheme'] = 'http';
277+
$uriParts['port'] = 80;
278+
if (isset($sapiVars['HTTPS']) && $sapiVars['HTTPS'] != 'off') {
279+
$uriParts['scheme'] = 'https';
280+
$uriParts['port'] = 443;
241281
}
242-
243-
$host = $sapiVars['HTTP_HOST'] ?? null;
244-
if (!isset($host)) {
245-
$host = $sapiVars['SERVER_NAME'];
246-
// Detecting port and comparing with default
247-
if (isset($sapiVars['SERVER_PORT']) && ($port = $sapiVars['SERVER_PORT']) !== $defaultPort) {
248-
$host .= ":{$port}";
249-
}
282+
if (isset($sapiVars['SERVER_PORT'])) {
283+
$uriParts['port'] = $sapiVars['SERVER_PORT'];
250284
}
251-
252-
$uri = "{$scheme}://{$host}";
253-
$uri = rtrim($uri, '/') . '/' . ltrim($sapiVars['REQUEST_URI'], '/');
254-
return $uri;
285+
$uriParts['host'] = $sapiVars['HTTP_HOST'] ?? $sapiVars['SERVER_NAME'] ?? null;
286+
$uriParts['path'] = '/' . ltrim($sapiVars['REQUEST_URI'], '/');
287+
return static::buildStringFromParts($uriParts);
255288
}
256289

257290
public static function getCurrent(UriFactoryInterface $uriFactory): UriInterface
@@ -315,7 +348,7 @@ public static function getUser(UriInterface $uri): string
315348
return $explode[0];
316349
}
317350

318-
public static function getPassword(UriInterface $uri): string
351+
public static function getPassword(UriInterface $uri): ?string
319352
{
320353
$explode = explode(':', $uri->getUserInfo());
321354
if (isset($explode[1])) {

‎test/UriHelperTest.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ public function testGetBaseUrl()
6666
$this->assertEquals(
6767
'https://localhost/assets/css/style.css',
6868
UriHelper::getBaseUrl('/assets/css/style.css', [
69-
'HTTP_HOST' => 'localhost',
7069
'HTTPS' => 'on',
70+
'HTTP_HOST' => 'localhost',
7171
'SCRIPT_NAME' => '/index.php'
7272
], false)
7373
);
7474
$this->assertEquals(
7575
'https://localhost/public/assets/css/style.css',
7676
UriHelper::getBaseUrl('/assets/css/style.css', [
77-
'HTTP_HOST' => 'localhost',
7877
'HTTPS' => 'on',
78+
'HTTP_HOST' => 'localhost',
7979
'SCRIPT_NAME' => '/public/index.php'
8080
], false)
8181
);
@@ -110,12 +110,23 @@ public function testGetSiteUrl()
110110
'SCRIPT_NAME' => '/public/index.php'
111111
], false)
112112
);
113+
114+
$this->assertEquals(
115+
'https://localhost:3000/public/index.php/some/path',
116+
siteUrl('/some/path', [
117+
'HTTPS' => 'on',
118+
'SERVER_NAME' => 'localhost',
119+
'SERVER_PORT' => 3000,
120+
'SCRIPT_NAME' => 'index.php',
121+
'X_FORWARDED_PREFIX' => '/public'
122+
], false)
123+
);
113124
}
114125

115126
public function testGetPathParams()
116127
{
117128
$matches = UriHelper::getPathParams('/a/1/b/2', '/a/{a}/b/{b}');
118-
var_dump($matches);
129+
// var_dump($matches);
119130
$this->assertTrue(true);
120131
}
121132
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /