Skip to content

Fix host priority in getUriFromGlobals() for Swoole 6.2.0 compatibility#7745

Merged
limingxinleo merged 2 commits into
hyperf:masterfrom
yansongda:fix/uri-host-priority-swoole-6.2
Apr 28, 2026
Merged

Fix host priority in getUriFromGlobals() for Swoole 6.2.0 compatibility#7745
limingxinleo merged 2 commits into
hyperf:masterfrom
yansongda:fix/uri-host-priority-swoole-6.2

Conversation

@yansongda
Copy link
Copy Markdown
Contributor

@yansongda yansongda commented Apr 27, 2026

Fix #7744 #7735

问题

Swoole 6.2.0 在 $swooleRequest->server 中新增了 server_addr 字段(服务器本地 IP)。当前代码中 server_addr 的优先级高于 header['host'],导致 getUri()->getHost() 返回服务器本地 IP(如 127.0.0.1)而非 HTTP Host 请求头中的域名(如 hyperf.example.com)。

修复前优先级:http_hostserver_nameserver_addrheader['host']

这与 PHP-FPM / nginx 的行为不一致——$_SERVER['HTTP_HOST'] 始终优先于 SERVER_ADDR

Related: Swoole v6.2.0 Release Note — "在 Swoole 的 HTTP 服务器回调函数中,Swoole\Http\Request 对象的 server 属性新增了 server_addr 字段,该字段用于标识服务端的 IP 地址"

修复

调整优先级为:http_hostheader['host']server_nameserver_addr

变更

  • src/http-message/src/Server/Request.php:调整 getUriFromGlobals()elseif 链的顺序
  • src/http-message/tests/ServerRequestTest.php:新增 2 个测试用例
    • testGetUriFromGlobalsHeaderHostPriorityOverServerAddr:验证 server_addr 存在时 header['host'] 仍优先(Swoole 6.2.0 场景)
    • testGetUriFromGlobalsServerAddrFallbackWhenNoHostHeader:验证无 Host 请求头时 server_addr 仍可作为兜底

Copilot AI review requested due to automatic review settings April 27, 2026 15:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

修复在 Swoole 6.2.0 新增 server_addr 字段后,getUriFromGlobals() 解析 host 时 server_addr 覆盖 header['host'] 导致 getUri()->getHost() 返回服务器本地 IP 的问题(Fix #7744),使其行为与 PHP-FPM/nginx 更一致。

Changes:

  • 调整 getUriFromGlobals() 中 host 的解析优先级:http_hostheader['host']server_nameserver_addr
  • 新增测试覆盖:header['host'] 优先于 server_addr,以及无 Host 头时回退到 server_addr

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/http-message/src/Server/Request.php 调整 getUriFromGlobals() 的 host fallback 顺序,避免 Swoole 6.2.0 的 server_addr 覆盖客户端 Host 头
src/http-message/tests/ServerRequestTest.php 增加两个测试用例覆盖 Swoole 6.2.0 的 server_addr 场景与无 Host 的回退行为
Comments suppressed due to low confidence (1)

src/http-message/src/Server/Request.php:599

  • Because the header['host'] branch is now evaluated before server_name/server_addr, an empty Host header value (e.g. '') will now call parseHost('') and throw InvalidArgumentException, whereas previously the code could fall back to server_name/server_addr. Consider guarding with a non-empty check (e.g. isset(...) && $header['host'] !== '') or falling back when parseHost() fails, to avoid a behavioral regression for malformed/empty Host headers.
        } elseif (isset($header['host'])) {
            $hasPort = true;
            [$host, $port] = self::parseHost($header['host']);
            if (isset($port) && $port !== $uri->getDefaultPort()) {
                $uri = $uri->withPort($port);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +168 to +169
// 模拟 Swoole 6.2.0 在 $request->server 中设置 server_addr 的场景
// header['host'] 应优先于 server_addr
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

New inline comments are written in Chinese, while the rest of the tests in this package use English-only docblocks/identifiers. For consistency and easier collaboration across the project, consider translating these comments to English or removing them if they’re not essential.

Suggested change
// 模拟 Swoole 6.2.0 $request->server 中设置 server_addr 的场景
// header['host'] 应优先于 server_addr
// Simulate the Swoole 6.2.0 scenario where server_addr is set in $request->server.
// header['host'] should take precedence over server_addr.

Copilot uses AI. Check for mistakes.
@yansongda yansongda changed the title 修复 Swoole 6.2.0 下 getUriFromGlobals() 的 host 解析优先级问题 Fix host priority in getUriFromGlobals() for Swoole 6.2.0 compatibility Apr 27, 2026
@limingxinleo limingxinleo merged commit 16c72e2 into hyperf:master Apr 28, 2026
80 checks passed
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.

[BUG] getUri()->getHost() returns server IP instead of domain when using swoole 6.2.0 (server_addr priority issue)

3 participants