Last active
August 29, 2015 14:22
-
-
Save jamesmartin/15b5debbd73c9db3f0d8 to your computer and use it in GitHub Desktop.
Rack middleware to redirect HTTPS to HTTP
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
| require 'sinatra' | |
| require 'only_http' | |
| use OnlyHttp | |
| get '/' do | |
| "We'll never see this over HTTPS!" | |
| end |
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
| require 'rack/request' | |
| require 'uri' | |
| class OnlyHttp | |
| def initialize(app) | |
| @app = app | |
| end | |
| def call(env) | |
| req = Rack::Request.new(env) | |
| if req.ssl? | |
| location = URI(req.url) | |
| location.scheme = 'http' | |
| [301, {"Location" => location.to_s, "Content-Type" => 'text/html', "Content-Length" => '0'}, []] | |
| else | |
| @app.call(env) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment