Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

memoize the relatively expensive remote IP code

  • Loading branch information...
commit cda1a5d5fe002ca92aca01586e8a60439605f960 1 parent 00a0a4d
@indirect indirect authored
Showing with 7 additions and 1 deletion.
  1. +7 −1 actionpack/lib/action_dispatch/middleware/remote_ip.rb
View
8 actionpack/lib/action_dispatch/middleware/remote_ip.rb
@@ -42,7 +42,7 @@ def initialize(env, middleware)
# HTTP_X_FORWARDED_FOR may be a comma-delimited list in the case of
# multiple chained proxies. The last address which is not a known proxy
# will be the originating IP.
- def to_s
+ def calculate_ip
client_ip = @env['HTTP_CLIENT_IP']
forwarded_ips = ips_from('HTTP_X_FORWARDED_FOR')
remote_addrs = ips_from('REMOTE_ADDR')
@@ -58,6 +58,12 @@ def to_s
client_ip || forwarded_ips.last || remote_addrs.first
end
+ def to_s
+ return @ip if @calculated_ip
+ @calculated_ip = true
+ @ip = calculate_ip
+ end
+
protected
def ips_from(header)
Please sign in to comment.
Something went wrong with that request. Please try again.