Skip to content

Commit 2d87b25

Browse files
committed
Request::getRemoteHost() is deprecated and returns null [Closes #218]
1 parent 4080942 commit 2d87b25

6 files changed

Lines changed: 12 additions & 32 deletions

File tree

readme.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ getRemoteAddress(): ?string
169169
---------------------------
170170
Returns the user's IP address. You may need to [set up a proxy|configuring#HTTP proxy] for proper functionality.
171171

172-
getRemoteHost(): ?string
173-
------------------------
174-
Returns DNS translation of the user's IP address. You may need to [set up a proxy|configuring#HTTP proxy] for proper functionality.
175-
176172
getRawBody(): ?string
177173
---------------------
178174
Returns the body of the HTTP request:

src/Http/IRequest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ function isAjax(): bool;
129129
*/
130130
function getRemoteAddress(): ?string;
131131

132-
/**
133-
* Returns the host of the remote client.
134-
*/
132+
#[\Deprecated]
135133
function getRemoteHost(): ?string;
136134

137135
/**

src/Http/Request.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\Http;
99

1010
use Nette;
11-
use function array_change_key_case, base64_decode, count, explode, func_num_args, gethostbyaddr, implode, in_array, preg_match, preg_match_all, rsort, strcasecmp, strtr;
11+
use function array_change_key_case, base64_decode, count, explode, implode, in_array, preg_match, preg_match_all, rsort, strcasecmp, strtr;
1212

1313

1414
/**
@@ -25,7 +25,7 @@
2525
* @property-read bool $secured
2626
* @property-read bool $ajax
2727
* @property-read ?string $remoteAddress
28-
* @property-read ?string $remoteHost
28+
* @property-deprecated ?string $remoteHost
2929
* @property-read ?string $rawBody
3030
*/
3131
class Request implements IRequest
@@ -278,16 +278,11 @@ public function getRemoteAddress(): ?string
278278
}
279279

280280

281-
/**
282-
* Returns the host of the remote client.
283-
*/
281+
#[\Deprecated]
284282
public function getRemoteHost(): ?string
285283
{
286-
if ($this->remoteHost === null && $this->remoteAddress !== null) {
287-
$this->remoteHost = gethostbyaddr($this->remoteAddress) ?: null;
288-
}
289-
290-
return $this->remoteHost;
284+
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
285+
return null;
291286
}
292287

293288

src/Http/RequestFactory.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function fromGlobals(): Request
7979
$this->getServer($url);
8080
$this->getPathAndQuery($url);
8181
[$post, $cookies] = $this->getGetPostCookie($url);
82-
[$remoteAddr, $remoteHost] = $this->getClient($url);
82+
$remoteAddr = $this->getClient($url);
8383
if ($this->forceHttps) {
8484
$url->setScheme('https');
8585
}
@@ -92,7 +92,7 @@ public function fromGlobals(): Request
9292
$this->getHeaders(),
9393
$this->getMethod(),
9494
$remoteAddr,
95-
$remoteHost,
95+
null,
9696
fn() => (string) file_get_contents('php://input'),
9797
);
9898
}
@@ -295,24 +295,19 @@ private function getMethod(): string
295295
}
296296

297297

298-
/** @return array{?string, ?string} [remoteAddr, remoteHost] */
299-
private function getClient(Url $url): array
298+
private function getClient(Url $url): ?string
300299
{
301300
$remoteAddr = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
302301

303-
// use real client address and host if trusted proxy is used
302+
// use real client address if trusted proxy is used
304303
$usingTrustedProxy = $remoteAddr && Arrays::some($this->proxies, fn(string $proxy): bool => Helpers::ipMatch($remoteAddr, $proxy));
305304
if ($usingTrustedProxy) {
306-
$remoteHost = null;
307-
$remoteAddr = empty($_SERVER['HTTP_FORWARDED'])
305+
return empty($_SERVER['HTTP_FORWARDED'])
308306
? $this->useNonstandardProxy($url)
309307
: $this->useForwardedProxy($url);
310-
311-
} else {
312-
$remoteHost = !empty($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null;
313308
}
314309

315-
return [$remoteAddr, $remoteHost];
310+
return $remoteAddr;
316311
}
317312

318313

tests/Http/RequestFactory.proxy.forwarded.phpt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ test('forwarded header handling with proxy', function () {
1919
$factory = new RequestFactory;
2020
$factory->setProxy('127.0.0.1');
2121
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
22-
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());
2322

2423
$factory->setProxy('127.0.0.1/8');
2524
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
@@ -58,7 +57,6 @@ test('IPv6 addresses in Forwarded header', function () {
5857

5958
$factory->setProxy('127.0.0.3');
6059
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
61-
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteHost());
6260

6361
$url = $factory->fromGlobals()->getUrl();
6462
Assert::same('[2001:db8:cafe::18]', $url->getHost());
@@ -75,7 +73,6 @@ test('IPv6 addresses and ports in Forwarded header', function () {
7573

7674
$factory->setProxy('127.0.0.3');
7775
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
78-
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteHost());
7976

8077
$url = $factory->fromGlobals()->getUrl();
8178
Assert::same(47832, $url->getPort());

tests/Http/RequestFactory.proxy.x-forwarded.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ test('X-Forwarded headers handling with proxy', function () {
2121
$factory = new RequestFactory;
2222
$factory->setProxy('127.0.0.1');
2323
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
24-
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());
2524

2625
$factory->setProxy('127.0.0.1/8');
2726
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());

0 commit comments

Comments
 (0)