Skip to content

Instantly share code, notes, and snippets.

@brenttheisen
Forked from mironov/rack.rb
Created September 23, 2011 06:07
Show Gist options
  • Save brenttheisen/1236839 to your computer and use it in GitHub Desktop.
Save brenttheisen/1236839 to your computer and use it in GitHub Desktop.
Fix for Rack requests showing up on ports 81 and 444 for apps hosted on a EngineYard AppCloud cluster
module Rack
class Request
def scheme
if @env['HTTPS'] == 'on'
'https'
elsif @env['HTTP_X_FORWARDED_SSL'] == 'on'
'https'
elsif @env['HTTP_X_FORWARDED_PROTO']
@env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
else
@env["rack.url_scheme"]
end
end
def ey_haproxy_port
return if @env['HTTP_HOST']
host_parts = @env['HTTP_HOST'].split(':')
return if host_parts.length != 2
port = host_parts.last
if port == '81'
return 80
elsif port == '444'
return 443
end
end
def ssl?
scheme == 'https'
end
def host_with_port
if forwarded = @env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
elsif ey_haproxy_port
@env['HTTP_HOST'].split(':').first
else
@env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}"
end
end
def port
if port = host_with_port.split(/:/)[1]
port.to_i
elsif port = @env['HTTP_X_FORWARDED_PORT']
port.to_i
elsif ssl?
443
elsif @env.has_key?("HTTP_X_FORWARDED_HOST")
80
elsif port = ey_haproxy_port
port
else
@env["SERVER_PORT"].to_i
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment