Permalink
Browse files

Merge pull request #3649 from indirect/remote_ip

Fixes for Request#remote_ip (no middleware, all proxies)
  • Loading branch information...
2 parents 4c1a193 + a9044d0 commit 70f9b74a4988891b6cd824065bb06b7e6e19d43f @tenderlove tenderlove committed Nov 17, 2011
View
3 actionpack/lib/action_dispatch/http/request.rb
@@ -157,8 +157,7 @@ def ip
# Originating IP address, usually set by the RemoteIp middleware.
def remote_ip
- # Coerce the remote_ip object into a string, because to_s could return nil
- @remote_ip ||= @env["action_dispatch.remote_ip"].to_s || ip
+ @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
end
# Returns the unique request id, which is based off either the X-Request-Id header that can
View
9 actionpack/lib/action_dispatch/middleware/remote_ip.rb
@@ -55,7 +55,10 @@ def calculate_ip
"HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}"
end
- client_ip || forwarded_ips.last || remote_addrs.first
+ not_proxy = client_ip || forwarded_ips.last || remote_addrs.first
+
+ # Return first REMOTE_ADDR if there are no other options
+ not_proxy || ips_from('REMOTE_ADDR', :allow_proxies).first
end
def to_s
@@ -66,9 +69,9 @@ def to_s
protected
- def ips_from(header)
+ def ips_from(header, allow_proxies = false)
ips = @env[header] ? @env[header].strip.split(/[,\s]+/) : []
- ips.reject{|ip| ip =~ @middleware.proxies }
+ allow_proxies ? ips : ips.reject{|ip| ip =~ @middleware.proxies }
end
end

0 comments on commit 70f9b74

Please sign in to comment.