Expose HTTP_HOST in GlobalState::enrichServerVars()#34
Expose HTTP_HOST in GlobalState::enrichServerVars()#34roxblnfk merged 4 commits intoroadrunner-php:4.xfrom
HTTP_HOST in GlobalState::enrichServerVars()#34Conversation
📝 WalkthroughWalkthroughAdded logic in GlobalState::enrichServerVars to derive and set HTTP_HOST from the request URI; updated unit tests and fixtures to expect this header in enriched server variables. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/Unit/PSR7WorkerTest.php (1)
34-63: Add one fixture covering URI with explicit port.A case like
http://localhost:8080would validate thehost:portpath and lock in the new behavior.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/Unit/PSR7WorkerTest.php` around lines 34 - 63, Add a third fixture to the $fixtures array that uses a URI containing an explicit port (e.g., 'REQUEST_URI' => 'http://localhost:8080') so the test covers host:port handling; ensure the corresponding server/HTTP header values in that fixture match the port-aware host (e.g., 'HTTP_HOST' => 'localhost:8080') and include the same Content-Type and other headers structure as the existing fixtures to validate PSR7Worker/related request-parsing logic against a host:port input.src/GlobalState.php (1)
38-47: ConsolidateHTTP_HOSTassignment into one conditional path.Current logic sets
HTTP_HOSTtwice through overlapping conditions. You can keep the same behavior with a single assignment branch for better readability.♻️ Suggested simplification
- $host = parse_url($request->uri, PHP_URL_HOST); - $port = parse_url($request->uri, PHP_URL_PORT); - - if ($host != null) { - $server['HTTP_HOST'] = $host; - } - - if ($host != null && $port != null) { - $server['HTTP_HOST'] = $host . ':' . $port; - } + $host = parse_url($request->uri, PHP_URL_HOST); + $port = parse_url($request->uri, PHP_URL_PORT); + + if ($host != null) { + $server['HTTP_HOST'] = $port != null ? $host . ':' . $port : $host; + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/GlobalState.php` around lines 38 - 47, The code sets $server['HTTP_HOST'] twice via overlapping conditionals; consolidate into a single conditional that computes $host and $port using parse_url($request->uri, PHP_URL_HOST) and parse_url($request->uri, PHP_URL_PORT) and then sets $server['HTTP_HOST'] to either "$host:$port" when $port is present or just $host when only $host is present (i.e., replace the two if blocks with one that checks $host and builds the string accordingly).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/GlobalState.php`:
- Around line 38-47: The code sets $server['HTTP_HOST'] twice via overlapping
conditionals; consolidate into a single conditional that computes $host and
$port using parse_url($request->uri, PHP_URL_HOST) and parse_url($request->uri,
PHP_URL_PORT) and then sets $server['HTTP_HOST'] to either "$host:$port" when
$port is present or just $host when only $host is present (i.e., replace the two
if blocks with one that checks $host and builds the string accordingly).
In `@tests/Unit/PSR7WorkerTest.php`:
- Around line 34-63: Add a third fixture to the $fixtures array that uses a URI
containing an explicit port (e.g., 'REQUEST_URI' => 'http://localhost:8080') so
the test covers host:port handling; ensure the corresponding server/HTTP header
values in that fixture match the port-aware host (e.g., 'HTTP_HOST' =>
'localhost:8080') and include the same Content-Type and other headers structure
as the existing fixtures to validate PSR7Worker/related request-parsing logic
against a host:port input.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/GlobalState.php`:
- Around line 38-44: The code calls parse_url($uri) but $uri is undefined;
change the call to parse_url($request->uri) so it uses the existing $request
object already referenced earlier, and update any related references (e.g., the
$parts handling and subsequent $server['HTTP_HOST'] assignment) to rely on
$parts derived from $request->uri.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/Unit/GlobalStateTest.php`:
- Around line 59-62: The test clears $_SERVER but calls
GlobalState::enrichServerVars($request) which reads the static
GlobalState::$cachedServer and can leak state between tests; update the test
setup to initialize the GlobalState cache by invoking
GlobalState::cacheServerVars() (or explicitly reset GlobalState::$cachedServer)
before calling GlobalState::enrichServerVars() so the cached server vars are
deterministic for the test.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/GlobalState.phptests/Unit/GlobalStateTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
- src/GlobalState.php
| $_SERVER = []; | ||
|
|
||
| $server = GlobalState::enrichServerVars($request); | ||
|
|
There was a problem hiding this comment.
Initialize GlobalState cache in test setup to avoid cross-test leakage
This test resets $_SERVER, but GlobalState::enrichServerVars() uses GlobalState::$cachedServer (static), not $_SERVER directly. Without calling GlobalState::cacheServerVars(), this can become order-dependent and flaky.
Proposed fix
$_SERVER = [];
+ GlobalState::cacheServerVars();
$server = GlobalState::enrichServerVars($request);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $_SERVER = []; | |
| $server = GlobalState::enrichServerVars($request); | |
| $_SERVER = []; | |
| GlobalState::cacheServerVars(); | |
| $server = GlobalState::enrichServerVars($request); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/Unit/GlobalStateTest.php` around lines 59 - 62, The test clears
$_SERVER but calls GlobalState::enrichServerVars($request) which reads the
static GlobalState::$cachedServer and can leak state between tests; update the
test setup to initialize the GlobalState cache by invoking
GlobalState::cacheServerVars() (or explicitly reset GlobalState::$cachedServer)
before calling GlobalState::enrichServerVars() so the cached server vars are
deterministic for the test.
|
HTTP_HOST in GlobalState::enrichServerVars()
Summary by CodeRabbit
Bug Fixes
Tests