Skip to content

Commit

Permalink
Split ServerRequest::env() into getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Apr 10, 2017
1 parent b891683 commit 1013625
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/Auth/BasicAuthenticate.php
Expand Up @@ -73,8 +73,8 @@ public function authenticate(ServerRequest $request, Response $response)
*/
public function getUser(ServerRequest $request)
{
$username = $request->env('PHP_AUTH_USER');
$pass = $request->env('PHP_AUTH_PW');
$username = $request->getEnv('PHP_AUTH_USER');
$pass = $request->getEnv('PHP_AUTH_PW');

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

return sprintf('WWW-Authenticate: Basic realm="%s"', $realm);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Auth/DigestAuthenticate.php
Expand Up @@ -120,7 +120,7 @@ public function getUser(ServerRequest $request)
$password = $user[$field];
unset($user[$field]);

$hash = $this->generateResponseHash($digest, $password, $request->env('ORIGINAL_REQUEST_METHOD'));
$hash = $this->generateResponseHash($digest, $password, $request->getEnv('ORIGINAL_REQUEST_METHOD'));
if ($digest['response'] === $hash) {
return $user;
}
Expand All @@ -136,7 +136,7 @@ public function getUser(ServerRequest $request)
*/
protected function _getDigest(ServerRequest $request)
{
$digest = $request->env('PHP_AUTH_DIGEST');
$digest = $request->getEnv('PHP_AUTH_DIGEST');
if (empty($digest) && function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (!empty($headers['Authorization']) && substr($headers['Authorization'], 0, 7) === 'Digest ') {
Expand Down Expand Up @@ -215,7 +215,7 @@ public static function password($username, $password, $realm)
*/
public function loginHeaders(ServerRequest $request)
{
$realm = $this->_config['realm'] ?: $request->env('SERVER_NAME');
$realm = $this->_config['realm'] ?: $request->getEnv('SERVER_NAME');

$options = [
'realm' => $realm,
Expand Down
2 changes: 1 addition & 1 deletion src/Error/BaseErrorHandler.php
Expand Up @@ -329,7 +329,7 @@ protected function _requestContext($request)
{
$message = "\nRequest URL: " . $request->getRequestTarget();

$referer = $request->env('HTTP_REFERER');
$referer = $request->getEnv('HTTP_REFERER');
if ($referer) {
$message .= "\nReferer URL: " . $referer;
}
Expand Down
89 changes: 62 additions & 27 deletions src/Http/ServerRequest.php
Expand Up @@ -351,7 +351,7 @@ protected function _setConfig($config)
*/
protected function _processPost($data)
{
$method = $this->env('REQUEST_METHOD');
$method = $this->getEnv('REQUEST_METHOD');
$override = false;

if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
Expand Down Expand Up @@ -506,12 +506,12 @@ protected function _normalizeNestedFiles(array $files = [])
*/
public function contentType()
{
$type = $this->env('CONTENT_TYPE');
$type = $this->getEnv('CONTENT_TYPE');
if ($type) {
return $type;
}

return $this->env('HTTP_CONTENT_TYPE');
return $this->getEnv('HTTP_CONTENT_TYPE');
}

/**
Expand Down Expand Up @@ -539,12 +539,12 @@ public function session(Session $session = null)
*/
public function clientIp()
{
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_FOR')) {
$ipaddr = preg_replace('/(?:,.*)/', '', $this->env('HTTP_X_FORWARDED_FOR'));
} elseif ($this->trustProxy && $this->env('HTTP_CLIENT_IP')) {
$ipaddr = $this->env('HTTP_CLIENT_IP');
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
$ipaddr = preg_replace('/(?:,.*)/', '', $this->getEnv('HTTP_X_FORWARDED_FOR'));
} elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
$ipaddr = $this->getEnv('HTTP_CLIENT_IP');
} else {
$ipaddr = $this->env('REMOTE_ADDR');
$ipaddr = $this->getEnv('REMOTE_ADDR');
}

return trim($ipaddr);
Expand All @@ -559,7 +559,7 @@ public function clientIp()
*/
public function referer($local = false)
{
$ref = $this->env('HTTP_REFERER');
$ref = $this->getEnv('HTTP_REFERER');

$base = Configure::read('App.fullBaseUrl') . $this->webroot;
if (!empty($ref) && !empty($base)) {
Expand Down Expand Up @@ -719,7 +719,7 @@ protected function _is($type, $args)
*/
protected function _acceptHeaderDetector($detect)
{
$acceptHeaders = explode(',', $this->env('HTTP_ACCEPT'));
$acceptHeaders = explode(',', $this->getEnv('HTTP_ACCEPT'));
foreach ($detect['accept'] as $header) {
if (in_array($header, $acceptHeaders)) {
return true;
Expand All @@ -738,7 +738,7 @@ protected function _acceptHeaderDetector($detect)
protected function _headerDetector($detect)
{
foreach ($detect['header'] as $header => $value) {
$header = $this->env('http_' . $header);
$header = $this->getEnv('http_' . $header);
if ($header !== null) {
if (!is_string($value) && !is_bool($value) && is_callable($value)) {
return call_user_func($value, $header);
Expand Down Expand Up @@ -782,15 +782,15 @@ protected function _environmentDetector($detect)
{
if (isset($detect['env'])) {
if (isset($detect['value'])) {
return $this->env($detect['env']) == $detect['value'];
return $this->getEnv($detect['env']) == $detect['value'];
}
if (isset($detect['pattern'])) {
return (bool)preg_match($detect['pattern'], $this->env($detect['env']));
return (bool)preg_match($detect['pattern'], $this->getEnv($detect['env']));
}
if (isset($detect['options'])) {
$pattern = '/' . implode('|', $detect['options']) . '/i';

return (bool)preg_match($pattern, $this->env($detect['env']));
return (bool)preg_match($pattern, $this->getEnv($detect['env']));
}
}

Expand Down Expand Up @@ -966,7 +966,7 @@ public function header($name)
{
$name = $this->normalizeHeaderName($name);

return $this->env($name);
return $this->getEnv($name);
}

/**
Expand Down Expand Up @@ -1117,7 +1117,7 @@ public function withoutHeader($name)
*/
public function method()
{
return $this->env('REQUEST_METHOD');
return $this->getEnv('REQUEST_METHOD');
}

/**
Expand All @@ -1136,7 +1136,7 @@ public function method()
*/
public function getMethod()
{
return $this->env('REQUEST_METHOD');
return $this->getEnv('REQUEST_METHOD');
}

/**
Expand Down Expand Up @@ -1211,11 +1211,11 @@ public function withQueryParams(array $query)
*/
public function host()
{
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_HOST')) {
return $this->env('HTTP_X_FORWARDED_HOST');
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_HOST')) {
return $this->getEnv('HTTP_X_FORWARDED_HOST');
}

return $this->env('HTTP_HOST');
return $this->getEnv('HTTP_HOST');
}

/**
Expand All @@ -1225,11 +1225,11 @@ public function host()
*/
public function port()
{
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_PORT')) {
return $this->env('HTTP_X_FORWARDED_PORT');
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PORT')) {
return $this->getEnv('HTTP_X_FORWARDED_PORT');
}

return $this->env('SERVER_PORT');
return $this->getEnv('SERVER_PORT');
}

/**
Expand All @@ -1241,11 +1241,11 @@ public function port()
*/
public function scheme()
{
if ($this->trustProxy && $this->env('HTTP_X_FORWARDED_PROTO')) {
return $this->env('HTTP_X_FORWARDED_PROTO');
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PROTO')) {
return $this->getEnv('HTTP_X_FORWARDED_PROTO');
}

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

/**
Expand Down Expand Up @@ -1712,7 +1712,7 @@ public function getProtocolVersion()
}

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

/**
* Get a value from the request's environment data.
* Fallback to using env() if the key is not set in the $environment property.
*
* @param string $key The key you want to read from.
* @param string|null $default Default value when trying to retrieve an environment
* variable's value that does not exist.
* @return string|null Either the environment value, or null if the value doesn't exist.
*/
public function getEnv($key, $default = null)
{
$key = strtoupper($key);
if (!array_key_exists($key, $this->_environment)) {
$this->_environment[$key] = env($key);
}

return $this->_environment[$key] !== null ? $this->_environment[$key] : $default;
}

/**
* Set a value to the request's environment data.
*
* @param string $key The key you want to write to.
* @param string|null $value Value to set. Default null.
* @return $this
*/
public function setEnv($key, $value = null)
{
$this->_environment[$key] = $value;
$this->clearDetectorCache();

return $this;
}

/**
* Get/Set value from the request's environment data.
* Fallback to using env() if key not set in $environment property.
*
* @deprecated 3.5.0 Use getEnv()/setEnv() instead.
* @param string $key The key you want to read/write from/to.
* @param string|null $value Value to set. Default null.
* @param string|null $default Default value when trying to retrieve an environment
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Filter/AssetFilter.php
Expand Up @@ -133,7 +133,7 @@ protected function _deliverAsset(ServerRequest $request, Response $response, $as
$compressionEnabled = $response->compress();
if ($response->type($ext) === $ext) {
$contentType = 'application/octet-stream';
$agent = $request->env('HTTP_USER_AGENT');
$agent = $request->getEnv('HTTP_USER_AGENT');
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
$contentType = 'application/octetstream';
}
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Http/ServerRequestTest.php
Expand Up @@ -3519,6 +3519,30 @@ public function testWithRequestTarget()
$this->assertEquals('/articles/view/3', $new->getRequestTarget(), 'reflects method call');
}

/**
* Test setEnv() and getEnv()
*
* @return void
*/
public function testGetSetEnv()
{
$request = new ServerRequest();

$request->setEnv('HTTP_HOST', 'cakephp.org');
$this->assertSame($request->getEnv('HTTP_HOST'), 'cakephp.org');

//Test default null
$request->setEnv('HTTP_HOST');
$this->assertSame($request->getEnv('HTTP_HOST'), null);

//Test default set
$this->assertSame($request->getEnv('Foo', 'Bar'), 'Bar');

//Test env() fallback
$_SERVER['TEST'] = 'ing';
$this->assertSame($request->getEnv('test'), 'ing');
}

/**
* Data provider for emulated property tests.
*
Expand Down

0 comments on commit 1013625

Please sign in to comment.