Skip to content

Instantly share code, notes, and snippets.

@arturoleon
Created April 17, 2013 20:56
Show Gist options
  • Save arturoleon/5407715 to your computer and use it in GitHub Desktop.
Save arturoleon/5407715 to your computer and use it in GitHub Desktop.
Sinatra over SSL
#from http://blog.divebomb.org/2012/01/ruby-sinatra-and-ssl/
require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'
name = "/C=US/ST=SomeState/L=SomeCity/O=Organization/OU=Unit/CN=localhost"
ca = OpenSSL::X509::Name.parse(name)
key = OpenSSL::PKey::RSA.new(1024)
crt = OpenSSL::X509::Certificate.new
crt.version = 2
crt.serial = 1
crt.subject = ca
crt.issuer = ca
crt.public_key = key.public_key
crt.not_before = Time.now
crt.not_after = Time.now + 1 * 365 * 24 * 60 * 60 # 1 year
webrick_options = {
:port => 4567,
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLCertificate => crt,
:SSLPrivateKey => key,
:SSLCertName => [[ "CN", WEBrick::Utils::getservername ]],
}
def agg_lines(data)
data.each_line.collect{|x| " >> #{x}" }.join
end
class MyServer < Sinatra::Base
get '/' do
"Hello World!\n"
end
get '*' do |x|
"GET of URI #{x}\n"
end
post "*" do |x|
request.body.rewind
"POST to URI #{x}\n#{agg_lines(request.body.read)}\n"
end
end
server = ::Rack::Handler::WEBrick
trap(:INT) do
server.shutdown
end
server.run(MyServer, webrick_options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment