Skip to content

Commit

Permalink
Merge pull request elastic#1058 from jordansissel/LOGSTASH-1354
Browse files Browse the repository at this point in the history
Fix string encoding problems from the geoip library
  • Loading branch information
jordansissel committed Feb 13, 2014
2 parents 97b9ce7 + 75eca3c commit d550f21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/logstash/filters/geoip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,17 @@ def filter(event)
geo_data_hash.each do |key, value|
next if value.nil? || (value.is_a?(String) && value.empty?)
if @fields.nil? || @fields.empty? || @fields.include?(key.to_s)
# no fields requested, so add all geoip hash items to
# the event's fields.
# convert key to string (normally a Symbol)
if value.is_a?(String)
# Some strings from GeoIP don't have the correct encoding...
value = case value.encoding
# I have found strings coming from GeoIP that are ASCII-8BIT are actually
# ISO-8859-1...
when Encoding::ASCII_8BIT; value.force_encoding("ISO-8859-1").encode("UTF-8")
when Encoding::ISO_8859_1; value.encode("UTF-8")
else; value
end
end
event[@target][key.to_s] = value
end
end # geo_data_hash.each
Expand Down
8 changes: 8 additions & 0 deletions spec/filters/geoip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,22 @@
dma_code area_code timezone)

sample("ip" => "1.1.1.1") do
checked = 0
expected_fields.each do |f|
next unless subject["geoip"][f]
checked += 1
insist { subject["geoip"][f].encoding } == Encoding::UTF_8
end
insist { checked } > 0
end
sample("ip" => "189.2.0.0") do
checked = 0
expected_fields.each do |f|
next unless subject["geoip"][f]
checked += 1
insist { subject["geoip"][f].encoding } == Encoding::UTF_8
end
insist { checked } > 0
end

end
Expand Down

0 comments on commit d550f21

Please sign in to comment.