diff --git a/extensions/debug/panels/RequestPanel.php b/extensions/debug/panels/RequestPanel.php index 74d0f6a6d6..17db55f3c1 100644 --- a/extensions/debug/panels/RequestPanel.php +++ b/extensions/debug/panels/RequestPanel.php @@ -36,13 +36,15 @@ class RequestPanel extends Panel public function save() { - if (function_exists('apache_request_headers')) { - $requestHeaders = apache_request_headers(); - } elseif (function_exists('http_get_request_headers')) { - $requestHeaders = http_get_request_headers(); - } else { - $requestHeaders = []; + $headers = Yii::$app->getRequest()->getHeaders(); + foreach ($headers as $name => $value) { + if (is_array($value) && count($value) == 1) { + $requestHeaders[$name] = current($value); + } else { + $requestHeaders[$name] = $value; + } } + $responseHeaders = []; foreach (headers_list() as $header) { if (($pos = strpos($header, ':')) !== false) { diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b60e949035..8d4939c277 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -93,6 +93,7 @@ Yii Framework 2 Change Log - Enh #1839: Added support for getting file extension and basename from uploaded file (anfrantic) - Enh: yii\codeception\TestCase now supports loading and using fixtures via Yii fixture framework (qiangxue) - Enh: Added support to parse json request data to Request::getRestParams() (cebe) +- Enh: Added ability to get incoming headers (dizews) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) - Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue) diff --git a/framework/web/Request.php b/framework/web/Request.php index f9563dac2e..f82688b015 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -151,6 +151,10 @@ class Request extends \yii\base\Request private $_cookies; + /** + * @var array the headers in this collection (indexed by the header names) + */ + private $_headers; /** * Resolves the current request into a route and the associated parameters. @@ -169,6 +173,37 @@ class Request extends \yii\base\Request } } + /** + * Returns the header collection. + * The header collection contains incoming HTTP headers. + * @return HeaderCollection the header collection + */ + public function getHeaders() + { + if ($this->_headers === null) { + $this->_headers = new HeaderCollection; + $headers = []; + if (function_exists('getallheaders')) { + $headers = getallheaders(); + } elseif (function_exists('http_get_request_headers')) { + $headers = http_get_request_headers(); + } else { + foreach ($_SERVER as $name => $value) { + if (substr($name, 0, 5) == 'HTTP_') { + $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); + $this->_headers->add($name, $value); + } + } + return $this->_headers; + } + foreach ($headers as $name => $value) { + $this->_headers->add($name, $value); + } + } + + return $this->_headers; + } + /** * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE.