Skip to content

Commit b69cf62

Browse files
authored
Merge pull request #34 expose HTTP_HOST
2 parents 8e24106 + de02802 commit b69cf62

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/GlobalState.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public static function enrichServerVars(Request $request): array
3535
$server['REQUEST_METHOD'] = $request->method;
3636
$server['HTTP_USER_AGENT'] = '';
3737

38+
$parts = \parse_url($request->uri);
39+
40+
if (isset($parts['host'])) {
41+
$server['HTTP_HOST'] = isset($parts['port'])
42+
? $parts['host'] . ':' . $parts['port']
43+
: $parts['host'];
44+
}
3845
foreach ($request->headers as $key => $value) {
3946
$key = \strtoupper(\str_replace('-', '_', $key));
4047

tests/Unit/GlobalStateTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunner\Tests\Http\Unit;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
use Spiral\RoadRunner\Http\GlobalState;
10+
use Spiral\RoadRunner\Http\Request;
11+
12+
final class GlobalStateTest extends TestCase
13+
{
14+
/**
15+
* @return iterable<array{0: Request, 1: array<array-key, mixed>}>
16+
*/
17+
public static function enrichServerVarsDataProvider(): iterable
18+
{
19+
yield [
20+
new Request(),
21+
[
22+
'REQUEST_URI' => 'http://localhost',
23+
'REMOTE_ADDR' => '127.0.0.1',
24+
'REQUEST_METHOD' => 'GET',
25+
'HTTP_USER_AGENT' => '',
26+
'HTTP_HOST' => 'localhost',
27+
],
28+
];
29+
30+
yield [
31+
new Request(uri: 'https://roadrunner.dev'),
32+
[
33+
'REQUEST_URI' => 'https://roadrunner.dev',
34+
'REMOTE_ADDR' => '127.0.0.1',
35+
'REQUEST_METHOD' => 'GET',
36+
'HTTP_USER_AGENT' => '',
37+
'HTTP_HOST' => 'roadrunner.dev',
38+
],
39+
];
40+
41+
yield [
42+
new Request(uri: 'https://roadrunner.dev:8080'),
43+
[
44+
'REQUEST_URI' => 'https://roadrunner.dev:8080',
45+
'REMOTE_ADDR' => '127.0.0.1',
46+
'REQUEST_METHOD' => 'GET',
47+
'HTTP_USER_AGENT' => '',
48+
'HTTP_HOST' => 'roadrunner.dev:8080',
49+
],
50+
];
51+
}
52+
53+
/**
54+
* @param array<array-key, mixed> $expected
55+
*/
56+
#[DataProvider('enrichServerVarsDataProvider')]
57+
public function testEnrichServerVars(Request $request, array $expected): void
58+
{
59+
$_SERVER = [];
60+
61+
$server = GlobalState::enrichServerVars($request);
62+
63+
$this->assertArrayHasKey('REQUEST_TIME', $server);
64+
$this->assertArrayHasKey('REQUEST_TIME_FLOAT', $server);
65+
66+
unset($server['REQUEST_TIME']);
67+
unset($server['REQUEST_TIME_FLOAT']);
68+
69+
$this->assertEquals($server, $expected);
70+
}
71+
}

tests/Unit/PSR7WorkerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testStateServerLeak(): void
4444
'HTTP_USER_AGENT' => '',
4545
'CONTENT_TYPE' => 'application/html',
4646
'HTTP_CONNECTION' => 'keep-alive',
47+
'HTTP_HOST' => 'localhost',
4748
],
4849
],
4950
[
@@ -56,6 +57,7 @@ public function testStateServerLeak(): void
5657
'REQUEST_METHOD' => 'GET',
5758
'HTTP_USER_AGENT' => '',
5859
'CONTENT_TYPE' => 'application/json',
60+
'HTTP_HOST' => 'localhost',
5961
],
6062
],
6163
];

0 commit comments

Comments
 (0)