Skip to content

Instantly share code, notes, and snippets.

@RichMorin
Created Dec 15, 2016
Embed
What would you like to do?
#!/usr/bin/env ruby
#
# test_rack_ssl - minimal example of rack-ssl snafu, for testing
#
# We're able to respond to HTTPS requests on port 44567, but so far, we're
# not getting automagical redirection from the server. Specifically, if we
# browse to localhost:12345, Chrome gives us a nastygram. However, if we
# browse to https://localhost:12345, we see our cheery greeting. And, after
# that, Chrome sends us to https://localhost:12345 automagically.
ext_libs = %w[ rack rack/ssl sinatra thin ]
ext_libs.each {|ext_lib| require ext_lib }
class SSL_Backend < ::Thin::Backends::TcpServer
#
# Support HTTPS
def initialize(host, port, options)
super(host, port)
@ssl = true
@ssl_options = options
end
end
configure do
use Rack::SSL
set :bind => '0.0.0.0',
:environment => :production,
:port => '12345', # '443' requires root; might conflict.
:server => 'thin'
class << settings
def server_settings
full_path = File.expand_path('..', __FILE__)
dir_path = File.dirname(full_path)
adm_path = "#{ dir_path }/../../admin"
crt_path = "#{ adm_path }/server.crt"
key_path = "#{ adm_path }/server.key"
preface = "Whoops! I can't find the SSL"
unless File.exist?(crt_path)
abort "#{ preface } certificate file (#{ crt_path })."
end
unless File.exist?(key_path)
abort "#{ preface } key file (#{ key_path })."
end
{
:backend => SSL_Backend,
:private_key_file => key_path,
:cert_chain_file => crt_path,
:verify_peer => false
}
end
end
get '/' do
'Hello, HTTPS client!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment