Skip to content

Commit

Permalink
GetIp#to_s should never return nil. That's icky.
Browse files Browse the repository at this point in the history
  • Loading branch information
indirect committed Nov 15, 2011
1 parent c7ab43f commit d743954
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 2 additions & 3 deletions actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -155,10 +155,9 @@ def ip
@ip ||= super @ip ||= super
end end


# Originating IP address, usually set by the RemoteIp middleware. # Originating IP address from the RemoteIp middleware.
def remote_ip def remote_ip
# Coerce the remote_ip object into a string, because to_s could return nil @remote_ip ||= @env["action_dispatch.remote_ip"]
@remote_ip ||= @env["action_dispatch.remote_ip"].to_s || ip
end end


# Returns the unique request id, which is based off either the X-Request-Id header that can # Returns the unique request id, which is based off either the X-Request-Id header that can
Expand Down
9 changes: 6 additions & 3 deletions actionpack/lib/action_dispatch/middleware/remote_ip.rb
Expand Up @@ -55,7 +55,10 @@ def calculate_ip
"HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}" "HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}"
end 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 end


def to_s def to_s
Expand All @@ -66,9 +69,9 @@ def to_s


protected protected


def ips_from(header) def ips_from(header, allow_proxies = false)
ips = @env[header] ? @env[header].strip.split(/[,\s]+/) : [] 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
end end


Expand Down

0 comments on commit d743954

Please sign in to comment.