Skip to content

Instantly share code, notes, and snippets.

@jamesmartin
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save jamesmartin/15b5debbd73c9db3f0d8 to your computer and use it in GitHub Desktop.

Select an option

Save jamesmartin/15b5debbd73c9db3f0d8 to your computer and use it in GitHub Desktop.
Rack middleware to redirect HTTPS to HTTP
require 'sinatra'
require 'only_http'
use OnlyHttp
get '/' do
"We'll never see this over HTTPS!"
end
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