module ActionDispatch::Http::URL

def raw_host_with_port

req.raw_host_with_port # => "example.com:8080"
req = ActionDispatch::Request.new 'HTTP_HOST' => 'example.com:8080'

req.raw_host_with_port # => "example.com:80"
req = ActionDispatch::Request.new 'HTTP_HOST' => 'example.com:80'

req.raw_host_with_port # => "example.com"
req = ActionDispatch::Request.new 'HTTP_HOST' => 'example.com'

Returns the \host and port for this request, such as "example.com:8080".
def raw_host_with_port
  if forwarded = x_forwarded_host.presence
    forwarded.split(/,\s?/).last
  else
    get_header("HTTP_HOST") || "#{server_name}:#{get_header('SERVER_PORT')}"
  end
end