-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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