diff options
author | Andre Arko <andre@arko.net> | 2011-11-14 16:43:21 -1000 |
---|---|---|
committer | Andre Arko <andre@arko.net> | 2011-11-14 16:43:21 -1000 |
commit | d743954792ccf5975a11ee88cdd690e8f1915728 (patch) | |
tree | 0eee69ec80417dde9bc8c08822bb83f0e9c28100 /actionpack/lib/action_dispatch | |
parent | c7ab43ff062390205f1508b6476cd354a16eb362 (diff) | |
download | rails-d743954792ccf5975a11ee88cdd690e8f1915728.tar.gz rails-d743954792ccf5975a11ee88cdd690e8f1915728.tar.bz2 rails-d743954792ccf5975a11ee88cdd690e8f1915728.zip |
GetIp#to_s should never return nil. That's icky.
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r-- | actionpack/lib/action_dispatch/http/request.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/remote_ip.rb | 9 |
2 files changed, 8 insertions, 6 deletions
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index b10f6b48c7..0e8bd0bd6d 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -155,10 +155,9 @@ module ActionDispatch @ip ||= super end - # Originating IP address, usually set by the RemoteIp middleware. + # Originating IP address from 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"] end # Returns the unique request id, which is based off either the X-Request-Id header that can diff --git a/actionpack/lib/action_dispatch/middleware/remote_ip.rb b/actionpack/lib/action_dispatch/middleware/remote_ip.rb index ee0d19a50d..77aa4e743e 100644 --- a/actionpack/lib/action_dispatch/middleware/remote_ip.rb +++ b/actionpack/lib/action_dispatch/middleware/remote_ip.rb @@ -55,7 +55,10 @@ module ActionDispatch "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', :all).first end def to_s @@ -66,9 +69,9 @@ module ActionDispatch 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 |