From 827db6c90ea3fb623a0c480c6a4c8ae46c655cf8 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 1 Jul 2022 10:28:31 +0300 Subject: [PATCH] =?UTF-8?q?Revert=20"Fix=20#19290:=20Fix=20`Request::getHo?= =?UTF-8?q?stInfo()`=20doesn=E2=80=99t=20return=20the=20port=20if=20a=20Ho?= =?UTF-8?q?st=20header=20is=20used"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 8046d3a50f207e1033e1b23ff571da5bced0c822. --- framework/CHANGELOG.md | 1 - framework/web/Request.php | 21 ++++------ tests/framework/web/RequestTest.php | 64 ----------------------------- 3 files changed, 7 insertions(+), 79 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 966a13627d..07ee7c4c79 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -13,7 +13,6 @@ Yii Framework 2 Change Log - Enh #19270: Replace deprecated `scss` converter in `yii\web\AssetConverter::$commands` (WinterSilence) - Enh #19254: Support specifying custom characters for `yii.validation.trim()` and replace deprecated `jQuery.trim()` (WinterSilence) - Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence) -- Bug #19290: Fix `Request::getHostInfo()` doesn’t return the port if a Host header is used (lesha724) - Enh #19295: Added alias `text/rtf` for mime-type `application/rtf` (lesha724) - Enh #19308: Add `yii\web\UploadedFile::$fullPath` represents 'full_path' key added in PHP 8.1 (WinterSilence) - Bug #19303: Fix serialization in `yii\caching\Dependency::generateReusableHash()` (WinterSilence) diff --git a/framework/web/Request.php b/framework/web/Request.php index fb9be9d31b..ecbb0dd9e8 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -764,20 +764,13 @@ class Request extends \yii\base\Request $this->_hostInfo = $http . '://' . trim(explode(',', $this->headers->get('X-Forwarded-Host'))[0]); } elseif ($this->headers->has('X-Original-Host')) { $this->_hostInfo = $http . '://' . trim(explode(',', $this->headers->get('X-Original-Host'))[0]); - } else { - if ($this->headers->has('Host')) { - $this->_hostInfo = $http . '://' . $this->headers->get('Host'); - } elseif (filter_has_var(INPUT_SERVER, 'SERVER_NAME')) { - $this->_hostInfo = $http . '://' . filter_input(INPUT_SERVER, 'SERVER_NAME'); - } elseif (isset($_SERVER['SERVER_NAME'])) { - $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; - } - - if ($this->_hostInfo !== null && !preg_match('/:\d+$/', $this->_hostInfo)) { - $port = $secure ? $this->getSecurePort() : $this->getPort(); - if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) { - $this->_hostInfo .= ':' . $port; - } + } elseif ($this->headers->has('Host')) { + $this->_hostInfo = $http . '://' . $this->headers->get('Host'); + } elseif (isset($_SERVER['SERVER_NAME'])) { + $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; + $port = $secure ? $this->getSecurePort() : $this->getPort(); + if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) { + $this->_hostInfo .= ':' . $port; } } } diff --git a/tests/framework/web/RequestTest.php b/tests/framework/web/RequestTest.php index f2253f8216..15a39557cc 100644 --- a/tests/framework/web/RequestTest.php +++ b/tests/framework/web/RequestTest.php @@ -286,48 +286,6 @@ class RequestTest extends TestCase 'example1.com', ] ], - // HTTP header missing with port 80 - [ - [ - 'HTTP_HOST' => 'example1.com', - 'SERVER_PORT' => 80, - ], - [ - 'http://example1.com', - 'example1.com', - ] - ], - // normal with nonstandart port 8080 - [ - [ - 'HTTP_HOST' => 'example1.com', - 'SERVER_PORT' => 8080, - ], - [ - 'http://example1.com:8080', - 'example1.com', - ] - ], - [ - [ - 'HTTP_HOST' => 'example1.com:8081', - 'SERVER_PORT' => 8080, - ], - [ - 'http://example1.com:8081', - 'example1.com', - ] - ], - [ - [ - 'HTTP_HOST' => 'example1.com:8080', - 'SERVER_PORT' => 8080, - ], - [ - 'http://example1.com:8080', - 'example1.com', - ] - ], // HTTP header missing [ [ @@ -338,28 +296,6 @@ class RequestTest extends TestCase 'example2.com', ] ], - // HTTP header missing with nonstandart port 8080 - [ - [ - 'SERVER_NAME' => 'example1.com', - 'SERVER_PORT' => 8080, - ], - [ - 'http://example1.com:8080', - 'example1.com', - ] - ], - // HTTP header missing with port 80 - [ - [ - 'SERVER_NAME' => 'example1.com', - 'SERVER_PORT' => 80, - ], - [ - 'http://example1.com', - 'example1.com', - ] - ], // forwarded from untrusted server [ [