Permalink
Browse files

Refactoring ip method

  • Loading branch information...
1 parent 08e0eb0 commit 42f4b95118f277eb3baa1aff02b24cfdbb114802 Rodrigo Flores committed Jan 10, 2012
Showing with 12 additions and 4 deletions.
  1. +12 −4 lib/rack/request.rb
View
@@ -313,23 +313,31 @@ def trusted_proxy?(ip)
end
def ip
- remote_addrs = @env['REMOTE_ADDR'] ? @env['REMOTE_ADDR'].split(/[,\s]+/) : []
- remote_addrs.reject! { |addr| trusted_proxy?(addr) }
+ remote_addrs = split_ip_addresses(@env['REMOTE_ADDR'])
+ remote_addrs = filter_trusted_ip_addresses(remote_addrs)
return remote_addrs.first if remote_addrs.any?
- forwarded_ips = @env['HTTP_X_FORWARDED_FOR'] ? @env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : []
+ forwarded_ips = split_ip_addresses(@env['HTTP_X_FORWARDED_FOR'])
if client_ip = @env['HTTP_CLIENT_IP']
# If forwarded_ips doesn't include the client_ip, it might be an
# ip spoofing attempt, so we ignore HTTP_CLIENT_IP
return client_ip if forwarded_ips.include?(client_ip)
end
- return forwarded_ips.reject { |ip| trusted_proxy?(ip) }.last || @env["REMOTE_ADDR"]
+ return filter_trusted_ip_addresses(forwarded_ips).last || @env["REMOTE_ADDR"]
end
protected
+ def split_ip_addresses(ip_addresses)
+ ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
+ end
+
+ def filter_trusted_ip_addresses(ip_addresses)
+ ip_addresses.reject { |ip| trusted_proxy?(ip) }
+ end
+
def parse_query(qs)
Utils.parse_nested_query(qs)
end

0 comments on commit 42f4b95

Please sign in to comment.