From 650add809d3e252c899255cafe186759e2189714 Mon Sep 17 00:00:00 2001 From: Abe Voelker <abe@abevoelker.com> Date: Tue, 2 Jan 2018 12:05:52 -0600 Subject: [PATCH] Handle IPv6 addresses when removing port numbers in request.location --- lib/geocoder/request.rb | 13 ++++++++++++- test/unit/request_test.rb | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/geocoder/request.rb b/lib/geocoder/request.rb index 5e2eb6c9..0180cf95 100644 --- a/lib/geocoder/request.rb +++ b/lib/geocoder/request.rb @@ -81,7 +81,18 @@ module Geocoder end 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 def geocoder_reject_non_ipv4_addresses(ip_addresses) diff --git a/test/unit/request_test.rb b/test/unit/request_test.rb index 127f5f4f..09e8ac9a 100644 --- a/test/unit/request_test.rb +++ b/test/unit/request_test.rb @@ -72,6 +72,12 @@ class RequestTest < GeocoderTestCase req = MockRequest.new() assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips) 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 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'] -- GitLab