Skip to content

Commit 1013625

Browse files
author
Michael Hoffmann
committed
Split ServerRequest::env() into getter/setter
1 parent b891683 commit 1013625

File tree

6 files changed

+94
-35
lines changed

6 files changed

+94
-35
lines changed

src/Auth/BasicAuthenticate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public function authenticate(ServerRequest $request, Response $response)
7373
*/
7474
public function getUser(ServerRequest $request)
7575
{
76-
$username = $request->env('PHP_AUTH_USER');
77-
$pass = $request->env('PHP_AUTH_PW');
76+
$username = $request->getEnv('PHP_AUTH_USER');
77+
$pass = $request->getEnv('PHP_AUTH_PW');
7878

7979
if (!is_string($username) || $username === '' || !is_string($pass) || $pass === '') {
8080
return false;
@@ -106,7 +106,7 @@ public function unauthenticated(ServerRequest $request, Response $response)
106106
*/
107107
public function loginHeaders(ServerRequest $request)
108108
{
109-
$realm = $this->getConfig('realm') ?: $request->env('SERVER_NAME');
109+
$realm = $this->getConfig('realm') ?: $request->getEnv('SERVER_NAME');
110110

111111
return sprintf('WWW-Authenticate: Basic realm="%s"', $realm);
112112
}

src/Auth/DigestAuthenticate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getUser(ServerRequest $request)
120120
$password = $user[$field];
121121
unset($user[$field]);
122122

123-
$hash = $this->generateResponseHash($digest, $password, $request->env('ORIGINAL_REQUEST_METHOD'));
123+
$hash = $this->generateResponseHash($digest, $password, $request->getEnv('ORIGINAL_REQUEST_METHOD'));
124124
if ($digest['response'] === $hash) {
125125
return $user;
126126
}
@@ -136,7 +136,7 @@ public function getUser(ServerRequest $request)
136136
*/
137137
protected function _getDigest(ServerRequest $request)
138138
{
139-
$digest = $request->env('PHP_AUTH_DIGEST');
139+
$digest = $request->getEnv('PHP_AUTH_DIGEST');
140140
if (empty($digest) && function_exists('apache_request_headers')) {
141141
$headers = apache_request_headers();
142142
if (!empty($headers['Authorization']) && substr($headers['Authorization'], 0, 7) === 'Digest ') {
@@ -215,7 +215,7 @@ public static function password($username, $password, $realm)
215215
*/
216216
public function loginHeaders(ServerRequest $request)
217217
{
218-
$realm = $this->_config['realm'] ?: $request->env('SERVER_NAME');
218+
$realm = $this->_config['realm'] ?: $request->getEnv('SERVER_NAME');
219219

220220
$options = [
221221
'realm' => $realm,

src/Error/BaseErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ protected function _requestContext($request)
329329
{
330330
$message = "\nRequest URL: " . $request->getRequestTarget();
331331

332-
$referer = $request->env('HTTP_REFERER');
332+
$referer = $request->getEnv('HTTP_REFERER');
333333
if ($referer) {
334334
$message .= "\nReferer URL: " . $referer;
335335
}

src/Http/ServerRequest.php

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ protected function _setConfig($config)
351351
*/
352352
protected function _processPost($data)
353353
{
354-
$method = $this->env('REQUEST_METHOD');
354+
$method = $this->getEnv('REQUEST_METHOD');
355355
$override = false;
356356

357357
if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
@@ -506,12 +506,12 @@ protected function _normalizeNestedFiles(array $files = [])
506506
*/
507507
public function contentType()
508508
{
509-
$type = $this->env('CONTENT_TYPE');
509+
$type = $this->getEnv('CONTENT_TYPE');
510510
if ($type) {
511511
return $type;
512512
}
513513

514-
return $this->env('HTTP_CONTENT_TYPE');
514+
return $this->getEnv('HTTP_CONTENT_TYPE');
515515
}
516516

517517
/**
@@ -539,12 +539,12 @@ public function session(Session $session = null)
539539
*/
540540
public function clientIp()
541541
{
542-
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_FOR')) {
543-
$ipaddr = preg_replace('/(?:,.*)/', '', $this->env('HTTP_X_FORWARDED_FOR'));
544-
} elseif ($this->trustProxy && $this->env('HTTP_CLIENT_IP')) {
545-
$ipaddr = $this->env('HTTP_CLIENT_IP');
542+
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
543+
$ipaddr = preg_replace('/(?:,.*)/', '', $this->getEnv('HTTP_X_FORWARDED_FOR'));
544+
} elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
545+
$ipaddr = $this->getEnv('HTTP_CLIENT_IP');
546546
} else {
547-
$ipaddr = $this->env('REMOTE_ADDR');
547+
$ipaddr = $this->getEnv('REMOTE_ADDR');
548548
}
549549

550550
return trim($ipaddr);
@@ -559,7 +559,7 @@ public function clientIp()
559559
*/
560560
public function referer($local = false)
561561
{
562-
$ref = $this->env('HTTP_REFERER');
562+
$ref = $this->getEnv('HTTP_REFERER');
563563

564564
$base = Configure::read('App.fullBaseUrl') . $this->webroot;
565565
if (!empty($ref) && !empty($base)) {
@@ -719,7 +719,7 @@ protected function _is($type, $args)
719719
*/
720720
protected function _acceptHeaderDetector($detect)
721721
{
722-
$acceptHeaders = explode(',', $this->env('HTTP_ACCEPT'));
722+
$acceptHeaders = explode(',', $this->getEnv('HTTP_ACCEPT'));
723723
foreach ($detect['accept'] as $header) {
724724
if (in_array($header, $acceptHeaders)) {
725725
return true;
@@ -738,7 +738,7 @@ protected function _acceptHeaderDetector($detect)
738738
protected function _headerDetector($detect)
739739
{
740740
foreach ($detect['header'] as $header => $value) {
741-
$header = $this->env('http_' . $header);
741+
$header = $this->getEnv('http_' . $header);
742742
if ($header !== null) {
743743
if (!is_string($value) && !is_bool($value) && is_callable($value)) {
744744
return call_user_func($value, $header);
@@ -782,15 +782,15 @@ protected function _environmentDetector($detect)
782782
{
783783
if (isset($detect['env'])) {
784784
if (isset($detect['value'])) {
785-
return $this->env($detect['env']) == $detect['value'];
785+
return $this->getEnv($detect['env']) == $detect['value'];
786786
}
787787
if (isset($detect['pattern'])) {
788-
return (bool)preg_match($detect['pattern'], $this->env($detect['env']));
788+
return (bool)preg_match($detect['pattern'], $this->getEnv($detect['env']));
789789
}
790790
if (isset($detect['options'])) {
791791
$pattern = '/' . implode('|', $detect['options']) . '/i';
792792

793-
return (bool)preg_match($pattern, $this->env($detect['env']));
793+
return (bool)preg_match($pattern, $this->getEnv($detect['env']));
794794
}
795795
}
796796

@@ -966,7 +966,7 @@ public function header($name)
966966
{
967967
$name = $this->normalizeHeaderName($name);
968968

969-
return $this->env($name);
969+
return $this->getEnv($name);
970970
}
971971

972972
/**
@@ -1117,7 +1117,7 @@ public function withoutHeader($name)
11171117
*/
11181118
public function method()
11191119
{
1120-
return $this->env('REQUEST_METHOD');
1120+
return $this->getEnv('REQUEST_METHOD');
11211121
}
11221122

11231123
/**
@@ -1136,7 +1136,7 @@ public function method()
11361136
*/
11371137
public function getMethod()
11381138
{
1139-
return $this->env('REQUEST_METHOD');
1139+
return $this->getEnv('REQUEST_METHOD');
11401140
}
11411141

11421142
/**
@@ -1211,11 +1211,11 @@ public function withQueryParams(array $query)
12111211
*/
12121212
public function host()
12131213
{
1214-
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_HOST')) {
1215-
return $this->env('HTTP_X_FORWARDED_HOST');
1214+
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_HOST')) {
1215+
return $this->getEnv('HTTP_X_FORWARDED_HOST');
12161216
}
12171217

1218-
return $this->env('HTTP_HOST');
1218+
return $this->getEnv('HTTP_HOST');
12191219
}
12201220

12211221
/**
@@ -1225,11 +1225,11 @@ public function host()
12251225
*/
12261226
public function port()
12271227
{
1228-
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_PORT')) {
1229-
return $this->env('HTTP_X_FORWARDED_PORT');
1228+
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PORT')) {
1229+
return $this->getEnv('HTTP_X_FORWARDED_PORT');
12301230
}
12311231

1232-
return $this->env('SERVER_PORT');
1232+
return $this->getEnv('SERVER_PORT');
12331233
}
12341234

12351235
/**
@@ -1241,11 +1241,11 @@ public function port()
12411241
*/
12421242
public function scheme()
12431243
{
1244-
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_PROTO')) {
1245-
return $this->env('HTTP_X_FORWARDED_PROTO');
1244+
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PROTO')) {
1245+
return $this->getEnv('HTTP_X_FORWARDED_PROTO');
12461246
}
12471247

1248-
return $this->env('HTTPS') ? 'https' : 'http';
1248+
return $this->getEnv('HTTPS') ? 'https' : 'http';
12491249
}
12501250

12511251
/**
@@ -1712,7 +1712,7 @@ public function getProtocolVersion()
17121712
}
17131713

17141714
// Lazily populate this data as it is generally not used.
1715-
preg_match('/^HTTP\/([\d.]+)$/', $this->env('SERVER_PROTOCOL'), $match);
1715+
preg_match('/^HTTP\/([\d.]+)$/', $this->getEnv('SERVER_PROTOCOL'), $match);
17161716
$protocol = '1.1';
17171717
if (isset($match[1])) {
17181718
$protocol = $match[1];
@@ -1742,10 +1742,45 @@ public function withProtocolVersion($version)
17421742
return $new;
17431743
}
17441744

1745+
/**
1746+
* Get a value from the request's environment data.
1747+
* Fallback to using env() if the key is not set in the $environment property.
1748+
*
1749+
* @param string $key The key you want to read from.
1750+
* @param string|null $default Default value when trying to retrieve an environment
1751+
* variable's value that does not exist.
1752+
* @return string|null Either the environment value, or null if the value doesn't exist.
1753+
*/
1754+
public function getEnv($key, $default = null)
1755+
{
1756+
$key = strtoupper($key);
1757+
if (!array_key_exists($key, $this->_environment)) {
1758+
$this->_environment[$key] = env($key);
1759+
}
1760+
1761+
return $this->_environment[$key] !== null ? $this->_environment[$key] : $default;
1762+
}
1763+
1764+
/**
1765+
* Set a value to the request's environment data.
1766+
*
1767+
* @param string $key The key you want to write to.
1768+
* @param string|null $value Value to set. Default null.
1769+
* @return $this
1770+
*/
1771+
public function setEnv($key, $value = null)
1772+
{
1773+
$this->_environment[$key] = $value;
1774+
$this->clearDetectorCache();
1775+
1776+
return $this;
1777+
}
1778+
17451779
/**
17461780
* Get/Set value from the request's environment data.
17471781
* Fallback to using env() if key not set in $environment property.
17481782
*
1783+
* @deprecated 3.5.0 Use getEnv()/setEnv() instead.
17491784
* @param string $key The key you want to read/write from/to.
17501785
* @param string|null $value Value to set. Default null.
17511786
* @param string|null $default Default value when trying to retrieve an environment

src/Routing/Filter/AssetFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected function _deliverAsset(ServerRequest $request, Response $response, $as
133133
$compressionEnabled = $response->compress();
134134
if ($response->type($ext) === $ext) {
135135
$contentType = 'application/octet-stream';
136-
$agent = $request->env('HTTP_USER_AGENT');
136+
$agent = $request->getEnv('HTTP_USER_AGENT');
137137
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
138138
$contentType = 'application/octetstream';
139139
}

tests/TestCase/Http/ServerRequestTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,6 +3519,30 @@ public function testWithRequestTarget()
35193519
$this->assertEquals('/articles/view/3', $new->getRequestTarget(), 'reflects method call');
35203520
}
35213521

3522+
/**
3523+
* Test setEnv() and getEnv()
3524+
*
3525+
* @return void
3526+
*/
3527+
public function testGetSetEnv()
3528+
{
3529+
$request = new ServerRequest();
3530+
3531+
$request->setEnv('HTTP_HOST', 'cakephp.org');
3532+
$this->assertSame($request->getEnv('HTTP_HOST'), 'cakephp.org');
3533+
3534+
//Test default null
3535+
$request->setEnv('HTTP_HOST');
3536+
$this->assertSame($request->getEnv('HTTP_HOST'), null);
3537+
3538+
//Test default set
3539+
$this->assertSame($request->getEnv('Foo', 'Bar'), 'Bar');
3540+
3541+
//Test env() fallback
3542+
$_SERVER['TEST'] = 'ing';
3543+
$this->assertSame($request->getEnv('test'), 'ing');
3544+
}
3545+
35223546
/**
35233547
* Data provider for emulated property tests.
35243548
*

0 commit comments

Comments
 (0)