Skip to content

Expose HTTP_HOST in GlobalState::enrichServerVars()#34

Merged
roxblnfk merged 4 commits intoroadrunner-php:4.xfrom
root-aza:http-host
Feb 26, 2026
Merged

Expose HTTP_HOST in GlobalState::enrichServerVars()#34
roxblnfk merged 4 commits intoroadrunner-php:4.xfrom
root-aza:http-host

Conversation

@root-aza
Copy link
Contributor

@root-aza root-aza commented Feb 26, 2026

Q A
Bugfix? ✔️
Breaks BC?
New feature? ✔️
Issues #...

Summary by CodeRabbit

  • Bug Fixes

    • Request handling now derives the HTTP Host (including port when present) from incoming request URIs so server context is accurate.
  • Tests

    • Updated fixtures to expect the HTTP Host header in server-state validations.
    • Added unit tests covering enrichment of server variables for URIs with/without host and port.

@coderabbitai
Copy link

coderabbitai bot commented Feb 26, 2026

📝 Walkthrough

Walkthrough

Added 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

Cohort / File(s) Summary
Global state enrichment
src/GlobalState.php
Parse request URI to extract host (and port when present) and populate HTTP_HOST in the enriched server vars.
Unit tests — new and updated
tests/Unit/GlobalStateTest.php, tests/Unit/PSR7WorkerTest.php
Added tests for HTTP_HOST derivation (including host+port cases) and updated expected server/header structures in PSR7 worker tests to include HTTP_HOST: 'localhost'.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Added GlobalState #28: Modifies GlobalState::enrichServerVars to derive and set HTTP_HOST from the request URI.

Suggested reviewers

  • roxblnfk

Poem

🐇 I hop through URIs at dawn,
I sniff the host till it is drawn,
With port or none, I place the boast,
A header born: the HTTP_HOST.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Added parse http host' is directly related to the main change, which adds HTTP_HOST parsing logic to GlobalState.php.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
tests/Unit/PSR7WorkerTest.php (1)

34-63: Add one fixture covering URI with explicit port.

A case like http://localhost:8080 would validate the host:port path 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: Consolidate HTTP_HOST assignment into one conditional path.

Current logic sets HTTP_HOST twice 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.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e24106 and 0831487.

📒 Files selected for processing (2)
  • src/GlobalState.php
  • tests/Unit/PSR7WorkerTest.php

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0831487 and 198a1b9.

📒 Files selected for processing (1)
  • src/GlobalState.php

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 198a1b9 and de02802.

📒 Files selected for processing (2)
  • src/GlobalState.php
  • tests/Unit/GlobalStateTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/GlobalState.php

Comment on lines +59 to +62
$_SERVER = [];

$server = GlobalState::enrichServerVars($request);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

Suggested change
$_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.

@roxblnfk
Copy link
Member

+---+------------------+-------+---------+--------+-------------------+--------+
| # | Name             | Iters | Revs    | Memory | Avg Time          | RStDev |
+---+------------------+-------+---------+--------+-------------------+--------+
| 3 | ?? false         | 10    | 2000000 | 0      | 383.072µs (-0.3%) | ±0.86% |
| 2 | array_key_exists | 10    | 2000000 | 0      | 364.215µs (-5.2%) | ±0.89% |
| 1 | isset            | 10    | 2000000 | 0      | 359.467µs (-6.4%) | ±0.91% |
+---+------------------+-------+---------+--------+-------------------+--------+

@roxblnfk roxblnfk merged commit b69cf62 into roadrunner-php:4.x Feb 26, 2026
12 checks passed
@roxblnfk roxblnfk changed the title Added parse http host Expose HTTP_HOST in GlobalState::enrichServerVars() Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants