Fix host priority in getUriFromGlobals() for Swoole 6.2.0 compatibility#7745
Conversation
There was a problem hiding this comment.
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_host→header['host']→server_name→server_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 beforeserver_name/server_addr, an empty Host header value (e.g.'') will now callparseHost('')and throwInvalidArgumentException, whereas previously the code could fall back toserver_name/server_addr. Consider guarding with a non-empty check (e.g.isset(...) && $header['host'] !== '') or falling back whenparseHost()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.
| // 模拟 Swoole 6.2.0 在 $request->server 中设置 server_addr 的场景 | ||
| // header['host'] 应优先于 server_addr |
There was a problem hiding this comment.
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.
| // 模拟 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. |
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_host→server_name→server_addr→header['host']这与 PHP-FPM / nginx 的行为不一致——
$_SERVER['HTTP_HOST']始终优先于SERVER_ADDR。修复
调整优先级为:
http_host→header['host']→server_name→server_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仍可作为兜底