Skip to content
Snippets Groups Projects
Commit 0c08fb63 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #717 from sethherr/ipv6_matching

Add IPv6 format detection
parents 497b197d 5cb5cf78
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,14 @@ module Geocoder ...@@ -6,7 +6,14 @@ module Geocoder
end end
def valid? def valid?
!!self.match(/\A(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/) ipregex = %r{
\A( # String Starts
((::ffff:)?((\d{1,3})\.){3}\d{1,3}) # Check for IPv4
| # .... Or
(\S+?(:\S+?){6}\S+) # Check for IPv6
)\z
}x
!!self.match(ipregex)
end end
end end
end end
...@@ -8,6 +8,7 @@ class IpAddressTest < GeocoderTestCase ...@@ -8,6 +8,7 @@ class IpAddressTest < GeocoderTestCase
assert Geocoder::IpAddress.new("232.65.123.94").valid? assert Geocoder::IpAddress.new("232.65.123.94").valid?
assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid
assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid? assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid?
assert Geocoder::IpAddress.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").valid?
assert !Geocoder::IpAddress.new("232.65.123.94.43").valid? assert !Geocoder::IpAddress.new("232.65.123.94.43").valid?
assert !Geocoder::IpAddress.new("232.65.123").valid? assert !Geocoder::IpAddress.new("232.65.123").valid?
assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid? assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid?
......
...@@ -6,6 +6,7 @@ class QueryTest < GeocoderTestCase ...@@ -6,6 +6,7 @@ class QueryTest < GeocoderTestCase
def test_ip_address_detection def test_ip_address_detection
assert Geocoder::Query.new("232.65.123.94").ip_address? assert Geocoder::Query.new("232.65.123.94").ip_address?
assert Geocoder::Query.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").ip_address?
assert !Geocoder::Query.new("232.65.123.94.43").ip_address? assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
assert !Geocoder::Query.new("::ffff:123.456.789").ip_address? assert !Geocoder::Query.new("::ffff:123.456.789").ip_address?
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment