Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

! handle multiple IPs in X-Forwarde-For

  • Loading branch information...
commit 246f7984cfd4d20947d11cb71216e4c8ae8ab0a0 1 parent 44893ae
@niko authored
View
8 README.textile
@@ -66,11 +66,11 @@ h2. The database file
The direct link to the free version of database is "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz":http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
-To specify the database file to be used, you have two possibilities:
+To specify the database file to be used, you have three possibilities:
-* First you set an environment variable:" @MMGeoipDatabase=/your/database.dat mm_geoip 134.34.3.2"@
-* Or you pass the location as a second argument:" @mm_geoip 134.34.3.2 /your/database.dat"@
-* Lastly you can put or link one into the gem so mm_geoip finds it automatically:" @cp /your/database.dat /location/of/the/gem/data/GeoLiteCity.dat"@
+* First you set an environment variable: @MMGeoipDatabase=/your/database.dat mm_geoip 134.34.3.2@
+* Or you pass the location as a second argument: @mm_geoip 134.34.3.2 /your/database.dat@
+* Lastly you can put or link one into the gem so mm_geoip finds it automatically: @cp /your/database.dat /location/of/the/gem/data/GeoLiteCity.dat@
By default, the database is no longer included in the gem. It has made the gem huge and slow to install. In production use you will always want to update the database independently of the gem anyway.
View
2  bin/mm_geoip
@@ -35,7 +35,7 @@ When runing this tool you have three possibilities to define the database locati
Download:
curl http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gunzip > GeoLiteCity.dat
-
+
}
end
View
5 lib/mm_geoip.rb
@@ -19,7 +19,10 @@ class NoDatabaseFile < StandardError; end
def initialize(env)
# May be a Rack @env or any hash containing initial data. Or just an IP.
@env = env.is_a?(Hash) ? env.dup : {:ip => env}
- @ip = @env[:ip] || @env["HTTP_X_REAL_IP"] || @env["HTTP_X_FORWARDED_FOR"] || @env["REMOTE_ADDR"]
+ @ip = @env[:ip] ||
+ @env["HTTP_X_REAL_IP"] ||
+ (@env["HTTP_X_FORWARDED_FOR"] && @env["HTTP_X_FORWARDED_FOR"].split(/[ ,]+/).first) ||
+ @env["REMOTE_ADDR"]
raise NoIpGiven.new("No IP in env hash") unless @ip
raise NoDatabaseFile.new("No database file: #{self.class.db_path}") unless File.exists? self.class.db_path
View
2  lib/mm_geoip/version.rb
@@ -1,3 +1,3 @@
class MMGeoip
- VERSION = '0.1.0'
+ VERSION = '0.1.1'
end
View
19 spec/mm_geoip_spec.rb
@@ -27,19 +27,28 @@
describe "#initialize" do
it "works with the IP as :ip field" do
- MMGeoip.new '134.34.3.2'
+ g = MMGeoip.new '134.34.3.2'
+ g.lat
end
it "works with just the IP" do
- MMGeoip.new :ip => '134.34.3.2'
+ g = MMGeoip.new :ip => '134.34.3.2'
+ g.lat
end
it "works with the IP as 'REMOTE_ADDR' field" do
- MMGeoip.new 'REMOTE_ADDR' => '134.34.3.2'
+ g = MMGeoip.new 'REMOTE_ADDR' => '134.34.3.2'
+ g.lat
end
it "works with the IP as 'HTTP_X_REAL_IP' field" do
- MMGeoip.new 'HTTP_X_REAL_IP' => '134.34.3.2'
+ g = MMGeoip.new 'HTTP_X_REAL_IP' => '134.34.3.2'
+ g.lat
end
it "works with the IP as 'HTTP_X_FORWARDED_FOR' field" do
- MMGeoip.new 'HTTP_X_FORWARDED_FOR' => '134.34.3.2'
+ g = MMGeoip.new 'HTTP_X_FORWARDED_FOR' => '134.34.3.2'
+ g.lat
+ end
+ it "works with multiple IPs as 'HTTP_X_FORWARDED_FOR' field" do
+ g = MMGeoip.new 'HTTP_X_FORWARDED_FOR' => '134.34.3.2, 134.34.3.3'
+ g.lat
end
it "raises, if not :ip or 'REMOTE_ADDR' is given" do
lambda{ MMGeoip.new :whatelse => 'something' }.should raise_error(MMGeoip::NoIpGiven)
Please sign in to comment.
Something went wrong with that request. Please try again.