diff --git a/src/HTTP/Requests/RemoteAddress.php b/src/HTTP/Requests/RemoteAddress.php index 1a05a62..19bc653 100644 --- a/src/HTTP/Requests/RemoteAddress.php +++ b/src/HTTP/Requests/RemoteAddress.php @@ -1,87 +1,87 @@ remoteAddress = $remoteAddress; } public static function fromServer () : self { return new self(self::extractRemoteAddressesFromHeaders()); } /// /// Format methods /// public function has () : bool { return $this->remoteAddress !== ""; } - public function getOne () : string { + public function getClientAddress () : string { if (strpos($this->remoteAddress, ',') === false) { // We only have one value, it's the IP return $this->remoteAddress; } // Header contains 'clientIP, proxyIP, anotherProxyIP' // The first value is so the one to return. // See draft-ietf-appsawg-http-forwarded-10. $ips = explode(',', $this->remoteAddress, 2); return trim($ips[0]); } public function getAll () : string { return $this->remoteAddress; } /// /// Helper methods to determine the remote address /// /** * Allows to get all the remote addresses from relevant headers */ public static function extractRemoteAddressesFromHeaders () : string { foreach (self::listRemoteAddressHeaders() as $candidate) { if (isset($_SERVER[$candidate])) { return $_SERVER[$candidate]; } } return ""; } /// /// Data sources /// public static function listRemoteAddressHeaders () : array { return [ // Standard header provided by draft-ietf-appsawg-http-forwarded-10 'HTTP_X_FORWARDED_FOR', // Legacy headers 'HTTP_CLIENT_IP', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED', // Default header if no proxy information could be detected 'REMOTE_ADDR', ]; } } diff --git a/src/HTTP/Requests/WithRemoteAddress.php b/src/HTTP/Requests/WithRemoteAddress.php index 5524add..b6bb13c 100644 --- a/src/HTTP/Requests/WithRemoteAddress.php +++ b/src/HTTP/Requests/WithRemoteAddress.php @@ -1,25 +1,25 @@ getOne(); + return RemoteAddress::fromServer()->getClientAddress(); } } diff --git a/tests/HTTP/Requests/RemoteAddressTest.php b/tests/HTTP/Requests/RemoteAddressTest.php index 7fb2491..60aa490 100644 --- a/tests/HTTP/Requests/RemoteAddressTest.php +++ b/tests/HTTP/Requests/RemoteAddressTest.php @@ -1,72 +1,72 @@ assertEmpty($address->getOne()); + $this->assertEmpty($address->getClientAddress()); $this->assertEmpty($address->getAll()); $this->assertFalse($address->has()); } /** - * @covers \Keruald\OmniTools\HTTP\Requests\RemoteAddress::getOne + * @covers \Keruald\OmniTools\HTTP\Requests\RemoteAddress::getClientAddress * @dataProvider provideTenZeroZeroThreeHeaderValues */ public function testGetOne (string $value) : void { $address = new RemoteAddress($value); - $this->assertEquals('10.0.0.3', $address->getOne()); + $this->assertEquals('10.0.0.3', $address->getClientAddress()); } /** - * @covers \Keruald\OmniTools\HTTP\Requests\RemoteAddress::getOne + * @covers \Keruald\OmniTools\HTTP\Requests\RemoteAddress::getClientAddress * @dataProvider provideTenZeroZeroThreeHeaderValues */ public function testGetAll (string $value) : void { $address = new RemoteAddress($value); $this->assertEquals($value, $address->getAll()); } /** * @covers \Keruald\OmniTools\HTTP\Requests\RemoteAddress::has * @dataProvider provideTenZeroZeroThreeHeaderValues */ public function testHas (string $value) : void { $address = new RemoteAddress($value); $this->assertTrue($address->has()); } /// /// Data provider /// public function provideTenZeroZeroThreeHeaderValues () : iterable { return [ // Each value should return 10.0.0.3 ['10.0.0.3'], ['10.0.0.3,10.0.0.4'], ['10.0.0.3, 10.0.0.4'], ['10.0.0.3, 10.0.0.4, lorem ipsum dolor'], ]; } }