Created
May 26, 2010 17:48
-
-
Save mrship/414800 to your computer and use it in GitHub Desktop.
This file contains 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 EnsureSsl | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
ssl_request?(env) ? @app.call(env) : ssl_redirect(env) | |
end | |
private | |
def ssl_request?(env) | |
env["HTTP_X_FORWARDED_PROTO"] == "https" | |
end | |
def ssl_location(env) | |
"https://" + env['HTTP_HOST'] + env['PATH_INFO'] | |
end | |
def ssl_redirect(env) | |
[ | |
301, | |
{ | |
'Content-Type' => 'text/html', | |
'Location' => ssl_location(env) | |
}, | |
%{ | |
<html> | |
<head> | |
<title>SSL Redirect</title> | |
</head> | |
<body> | |
<p>The requested path must be requested via SSL. You are now being redirected.</p> | |
</body> | |
</html> | |
} | |
] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment