Page MenuHomeDevCentral

D2279.id5743.diff
No OneTemporary

D2279.id5743.diff

diff --git a/src/HTTP/Requests/RemoteAddress.php b/src/HTTP/Requests/RemoteAddress.php
--- a/src/HTTP/Requests/RemoteAddress.php
+++ b/src/HTTP/Requests/RemoteAddress.php
@@ -26,10 +26,6 @@
/// Format methods
///
- public function has () : bool {
- return $this->remoteAddress !== "";
- }
-
public function getClientAddress () : string {
// Header contains 'clientIP, proxyIP, anotherProxyIP'
// or 'clientIP proxyIP anotherProxyIP'
@@ -39,10 +35,23 @@
return trim($ips[0]);
}
+ public function isFromLocalHost () : bool {
+ return IP::isLoopBack($this->getClientAddress());
+ }
+
public function getAll () : string {
return $this->remoteAddress;
}
+
+ public function has () : bool {
+ return $this->remoteAddress !== "";
+ }
+
+ ///
+ /// Information methods
+ ///
+
///
/// Helper methods to determine the remote address
///
diff --git a/src/HTTP/Requests/WithRemoteAddress.php b/src/HTTP/Requests/WithRemoteAddress.php
--- a/src/HTTP/Requests/WithRemoteAddress.php
+++ b/src/HTTP/Requests/WithRemoteAddress.php
@@ -22,4 +22,8 @@
return RemoteAddress::fromServer()->getClientAddress();
}
+ public static function isFromLocalHost () : bool {
+ return RemoteAddress::fromServer()->isFromLocalHost();
+ }
+
}
diff --git a/src/Network/IP.php b/src/Network/IP.php
--- a/src/Network/IP.php
+++ b/src/Network/IP.php
@@ -17,4 +17,15 @@
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}
+ public static function isLoopback (string $ip) : bool {
+ $ranges = IPRange::getLoopbackRanges();
+ foreach ($ranges as $range) {
+ if ($range->contains($ip)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
}
diff --git a/tests/Network/IPTest.php b/tests/Network/IPTest.php
--- a/tests/Network/IPTest.php
+++ b/tests/Network/IPTest.php
@@ -64,6 +64,19 @@
yield ["1.2.3.4:1111:2222:3333:4444::5555"];
}
+ public function provideValidLoopblackIP () : iterable {
+ yield ["127.0.0.1"];
+ yield ["127.0.0.3"];
+ yield ["::1"];
+ }
+
+ public function provideInvalidLoopblackIP () : iterable {
+ yield ["0.0.0.0"];
+ yield ["1.2.3.4"];
+ yield ["192.168.1.1"];
+ yield ["::2"];
+ }
+
///
/// Test cases
///
@@ -116,5 +129,20 @@
$this->assertFalse(IP::isIPv6($ip), $message);
}
+ /**
+ * @covers \Keruald\OmniTools\Network\IP::isLoopback
+ * @dataProvider provideValidLoopblackIP
+ */
+ public function testIsLoopback (string $ip) : void {
+ $this->assertTrue(IP::isLoopback($ip));
+ }
+
+ /**
+ * @covers \Keruald\OmniTools\Network\IP::isLoopback
+ * @dataProvider provideInvalidLoopblackIP
+ */
+ public function testIsLoopbackWhenItIsNot (string $ip) : void {
+ $this->assertFalse(IP::isLoopback($ip));
+ }
}

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 04:33 (20 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250140
Default Alt Text
D2279.id5743.diff (2 KB)

Event Timeline