Skip to content
Snippets Groups Projects
Unverified Commit d49ef479 authored by Alex Reisner's avatar Alex Reisner Committed by GitHub
Browse files

Merge pull request #1252 from abevoelker/ipv6-fix

Handle IPv6 addresses when removing port numbers in request.location. Closes #1210
parents b00d68bf 650add80
No related branches found
No related tags found
No related merge requests found
...@@ -81,7 +81,18 @@ module Geocoder ...@@ -81,7 +81,18 @@ module Geocoder
end end
def geocoder_remove_port_from_addresses(ip_addresses) def geocoder_remove_port_from_addresses(ip_addresses)
ip_addresses.map { |ip| ip.split(':').first } ip_addresses.map do |ip|
# IPv4
if ip.count('.') > 0
ip.split(':').first
# IPv6 bracket notation
elsif match = ip.match(/\[(\S+)\]/)
match.captures.first
# IPv6 bare notation
else
ip
end
end
end end
def geocoder_reject_non_ipv4_addresses(ip_addresses) def geocoder_reject_non_ipv4_addresses(ip_addresses)
......
...@@ -72,6 +72,12 @@ class RequestTest < GeocoderTestCase ...@@ -72,6 +72,12 @@ class RequestTest < GeocoderTestCase
req = MockRequest.new() req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips) assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end end
def test_geocoder_remove_port_from_ipv6_addresses_with_port
expected_ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '2001:db8:1f70::999:de8:7648:6e8', '10.128.0.2']
ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '[2001:db8:1f70::999:de8:7648:6e8]:100', '10.128.0.2']
req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end
def test_geocoder_remove_port_from_addresses_without_port def test_geocoder_remove_port_from_addresses_without_port
expected_ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3'] expected_ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3'] ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
......
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