Skip to content

Commit c5b79ad

Browse files
authored
Fix parsing host from HTTP_HOST header (#4)
1 parent f4211cb commit c5b79ad

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/SapiNormalizer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function explode;
1212
use function in_array;
1313
use function is_string;
14+
use function preg_match;
1415
use function preg_replace;
1516
use function strpos;
1617
use function str_replace;
@@ -59,16 +60,18 @@ public function normalizeUri(array $server): UriInterface
5960
$uri = $uri->withScheme((string) $scheme);
6061
}
6162

63+
if (isset($server['SERVER_PORT'])) {
64+
$uri = $uri->withPort((int) $server['SERVER_PORT']);
65+
}
66+
6267
if ($host = $server['HTTP_X_FORWARDED_HOST'] ?? $server['HTTP_HOST'] ?? '') {
63-
$uri = $uri->withHost((string) $host);
68+
$uri = preg_match('/^(.+):(\d+)$/', (string) $host, $matches) === 1
69+
? $uri->withHost($matches[1])->withPort((int) $matches[2])
70+
: $uri->withHost((string) $host);
6471
} elseif ($host = $server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? '') {
6572
$uri = $uri->withHost((string) $host);
6673
}
6774

68-
if (isset($server['SERVER_PORT'])) {
69-
$uri = $uri->withPort((int) $server['SERVER_PORT']);
70-
}
71-
7275
if ($path = $server['REQUEST_URI'] ?? $server['ORIG_PATH_INFO'] ?? '') {
7376
$uri = $uri->withPath(explode('?', preg_replace('/^[^\/:]+:\/\/[^\/]+/', '', (string) $path), 2)[0]);
7477
}

tests/SapiNormalizerTest.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testNormalizeUri(): void
9898
$this->assertSame($this->server['HTTP_HOST'], $uri->getAuthority());
9999
$this->assertSame('', $uri->getUserInfo());
100100
$this->assertSame($this->server['HTTP_HOST'], $uri->getHost());
101-
$this->assertSame(null, $uri->getPort());
101+
$this->assertNull($uri->getPort());
102102
$this->assertSame('/path', $uri->getPath());
103103
$this->assertSame($this->server['QUERY_STRING'], $uri->getQuery());
104104
$this->assertSame('https://example.com/path?name=value', (string) $uri);
@@ -111,21 +111,74 @@ public function testNormalizeUri(): void
111111
$this->assertSame($this->server['SERVER_NAME'], $uri->getAuthority());
112112
$this->assertSame('', $uri->getUserInfo());
113113
$this->assertSame($this->server['SERVER_NAME'], $uri->getHost());
114-
$this->assertSame(null, $uri->getPort());
114+
$this->assertNull($uri->getPort());
115115
$this->assertSame('/path', $uri->getPath());
116116
$this->assertSame($this->server['QUERY_STRING'], $uri->getQuery());
117117
$this->assertSame('https://example.org/path?name=value', (string) $uri);
118118
}
119119

120+
public function testNormalizeUriIfHttpHostHeaderWithStandardPort(): void
121+
{
122+
$uri = $this->normalizer->normalizeUri([
123+
'SERVER_PORT' => '443',
124+
'REQUEST_SCHEME' => 'https',
125+
'HTTP_HOST' => 'example.com:443',
126+
]);
127+
128+
$this->assertSame('https', $uri->getScheme());
129+
$this->assertSame('example.com', $uri->getAuthority());
130+
$this->assertSame('', $uri->getUserInfo());
131+
$this->assertSame('example.com', $uri->getHost());
132+
$this->assertNull($uri->getPort());
133+
$this->assertSame('', $uri->getPath());
134+
$this->assertSame('', $uri->getQuery());
135+
$this->assertSame('https://example.com', (string) $uri);
136+
}
137+
138+
public function testNormalizeUriIfHttpHostHeaderWithNotStandardPort(): void
139+
{
140+
$uri = $this->normalizer->normalizeUri([
141+
'SERVER_PORT' => '443',
142+
'REQUEST_SCHEME' => 'https',
143+
'HTTP_HOST' => 'example.com:8080',
144+
]);
145+
146+
$this->assertSame('https', $uri->getScheme());
147+
$this->assertSame('example.com:8080', $uri->getAuthority());
148+
$this->assertSame('', $uri->getUserInfo());
149+
$this->assertSame('example.com', $uri->getHost());
150+
$this->assertSame(8080, $uri->getPort());
151+
$this->assertSame('', $uri->getPath());
152+
$this->assertSame('', $uri->getQuery());
153+
$this->assertSame('https://example.com:8080', (string) $uri);
154+
}
155+
156+
public function testNormalizeUriIfHttpHostHeaderWithNotEqualStandardPortWithScheme(): void
157+
{
158+
$uri = $this->normalizer->normalizeUri([
159+
'SERVER_PORT' => '443',
160+
'REQUEST_SCHEME' => 'https',
161+
'HTTP_HOST' => 'example.com:80',
162+
]);
163+
164+
$this->assertSame('https', $uri->getScheme());
165+
$this->assertSame('example.com:80', $uri->getAuthority());
166+
$this->assertSame('', $uri->getUserInfo());
167+
$this->assertSame('example.com', $uri->getHost());
168+
$this->assertSame(80, $uri->getPort());
169+
$this->assertSame('', $uri->getPath());
170+
$this->assertSame('', $uri->getQuery());
171+
$this->assertSame('https://example.com:80', (string) $uri);
172+
}
173+
120174
public function testNormalizeUriIfServerIsEmpty(): void
121175
{
122176
$uri = $this->normalizer->normalizeUri([]);
123-
$this->assertInstanceOf(UriInterface::class, $uri);
124177
$this->assertSame('', $uri->getScheme());
125178
$this->assertSame('', $uri->getAuthority());
126179
$this->assertSame('', $uri->getUserInfo());
127180
$this->assertSame('', $uri->getHost());
128-
$this->assertSame(null, $uri->getPort());
181+
$this->assertNull($uri->getPort());
129182
$this->assertSame('', $uri->getPath());
130183
$this->assertSame('', $uri->getQuery());
131184
$this->assertSame('', (string) $uri);

0 commit comments

Comments
 (0)