Method: Rack::Request::Helpers#ip

Defined in:
lib/rack/request.rb

#ipObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/rack/request.rb', line 419

def ip
  remote_addresses = split_header(get_header('REMOTE_ADDR'))

  remote_addresses.reverse_each do |ip|
    return ip unless trusted_proxy?(ip)
  end

  if (forwarded_for = self.forwarded_for) && !forwarded_for.empty?
    # The forwarded for addresses are ordered: client, proxy1, proxy2.
    # So we reject all the trusted addresses (proxy*) and return the
    # last client. Or if we trust everyone, we just return the first
    # address.
    forwarded_for.reverse_each do |ip|
      return ip unless trusted_proxy?(ip)
    end
    return forwarded_for.first
  end

  # If all the addresses are trusted, and we aren't forwarded, just return
  # the first remote address, which represents the source of the request.
  remote_addresses.first
end