Skip to content

Instantly share code, notes, and snippets.

@mrship
Created May 26, 2010 17:48

Revisions

  1. mrship created this gist May 26, 2010.
    41 changes: 41 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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