Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F43123556
D2279.id5742.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D2279.id5742.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Wed, Aug 12, 02:01 (16 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3986648
Default Alt Text
D2279.id5742.diff (2 KB)
Attached To
Mode
D2279: Allow to determine if a request is from loopback address
Attached
Detach File
Event Timeline
Log In to Comment