Skip to content

Commit b9fe288

Browse files
committed
Reduce Array object allocations
1 parent 768a2c6 commit b9fe288

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

actionpack/lib/action_dispatch/middleware/remote_ip.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def calculate_ip
125125
remote_addr = ips_from(@req.remote_addr).last
126126

127127
# Could be a CSV list and/or repeated headers that were concatenated.
128-
client_ips = ips_from(@req.client_ip).reverse
129-
forwarded_ips = ips_from(@req.x_forwarded_for).reverse
128+
client_ips = ips_from(@req.client_ip).reverse!
129+
forwarded_ips = ips_from(@req.x_forwarded_for).reverse!
130130

131131
# +Client-Ip+ and +X-Forwarded-For+ should not, generally, both be set.
132132
# If they are both set, it means that either:
@@ -154,7 +154,8 @@ def calculate_ip
154154
# - X-Forwarded-For will be a list of IPs, one per proxy, or blank
155155
# - Client-Ip is propagated from the outermost proxy, or is blank
156156
# - REMOTE_ADDR will be the IP that made the request to Rack
157-
ips = [forwarded_ips, client_ips].flatten.compact
157+
ips = forwarded_ips + client_ips
158+
ips.compact!
158159

159160
# If every single IP option is in the trusted list, return the IP
160161
# that's furthest away
@@ -172,14 +173,15 @@ def ips_from(header) # :doc:
172173
return [] unless header
173174
# Split the comma-separated list into an array of strings.
174175
ips = header.strip.split(/[,\s]+/)
175-
ips.select do |ip|
176+
ips.select! do |ip|
176177
# Only return IPs that are valid according to the IPAddr#new method.
177178
range = IPAddr.new(ip).to_range
178179
# We want to make sure nobody is sneaking a netmask in.
179180
range.begin == range.end
180181
rescue ArgumentError
181182
nil
182183
end
184+
ips
183185
end
184186

185187
def filter_proxies(ips) # :doc:

0 commit comments

Comments
 (0)