Class: ActionPack::CloudfrontViewerAddress::RemoteIp

Inherits:
Object
  • Object
show all
Defined in:
lib/action_pack/cloudfront_viewer_address/remote_ip.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RemoteIp

Returns a new instance of RemoteIp.



8
9
10
# File 'lib/action_pack/cloudfront_viewer_address/remote_ip.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/action_pack/cloudfront_viewer_address/remote_ip.rb', line 12

def call(env)
  req = ::ActionDispatch::Request.new(env)
  if env["HTTP_CLOUDFRONT_VIEWER_ADDRESS"].present?
    # IPv4 "HTTP_CLOUDFRONT_VIEWER_ADDRESS" = "1.1.1.1:3000"
    # IPV6 "HTTP_CLOUDFRONT_VIEWER_ADDRESS" = "0000:0000:0000:0000:0000:0000:0000:0000:3000"
    req.remote_ip = req.env["HTTP_CLOUDFRONT_VIEWER_ADDRESS"].sub(/:\d+\z/, "") # remove `port(:0000)` part
  end
  app.call(req.env)
end