diff options
author | Andre Arko <andre@arko.net> | 2011-11-14 11:20:57 -1000 |
---|---|---|
committer | Andre Arko <andre@arko.net> | 2011-11-14 11:20:57 -1000 |
commit | cda1a5d5fe002ca92aca01586e8a60439605f960 (patch) | |
tree | 866fd700f880d07841a5c82e609f02a43252328f | |
parent | 00a0a4ddebe0160f851d28e29d5fb7e8e7a2a5dc (diff) | |
download | rails-cda1a5d5fe002ca92aca01586e8a60439605f960.tar.gz rails-cda1a5d5fe002ca92aca01586e8a60439605f960.tar.bz2 rails-cda1a5d5fe002ca92aca01586e8a60439605f960.zip |
memoize the relatively expensive remote IP code
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/remote_ip.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/remote_ip.rb b/actionpack/lib/action_dispatch/middleware/remote_ip.rb index 446fcce823..ee0d19a50d 100644 --- a/actionpack/lib/action_dispatch/middleware/remote_ip.rb +++ b/actionpack/lib/action_dispatch/middleware/remote_ip.rb @@ -42,7 +42,7 @@ module ActionDispatch # 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 @@ module ActionDispatch 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) |