Skip to content

Instantly share code, notes, and snippets.

@mrship
Created May 26, 2010 17:48
Show Gist options
  • Save mrship/414800 to your computer and use it in GitHub Desktop.
Save mrship/414800 to your computer and use it in GitHub Desktop.
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