Created
February 14, 2011 11:52
-
-
Save mironov/825769 to your computer and use it in GitHub Desktop.
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 ssl? | |
scheme == 'https' | |
end | |
def host_with_port | |
if forwarded = @env["HTTP_X_FORWARDED_HOST"] | |
forwarded.split(/,\s?/).last | |
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 | |
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
rack/rack#287