Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sinatra + WEBrick + HTTPS
require 'sinatra'
require 'webrick/https'
set :server_settings,
SSLEnable: true,
SSLCertName: [['CN', WEBrick::Utils.getservername]]
get '/' do
'https page'
end
@k-ta-yamada

This comment has been minimized.

Show comment Hide comment
@k-ta-yamada

k-ta-yamada Aug 23, 2017

https://github.com/ruby/ruby/blob/trunk/lib/webrick/ssl.rb#L20-L22

    # WEBrick can automatically create a self-signed certificate if
    # <code>:SSLCertName</code> is set.  For more information on the various
    # SSL options see OpenSSL::SSL::SSLContext.
Owner

k-ta-yamada commented Aug 23, 2017

https://github.com/ruby/ruby/blob/trunk/lib/webrick/ssl.rb#L20-L22

    # WEBrick can automatically create a self-signed certificate if
    # <code>:SSLCertName</code> is set.  For more information on the various
    # SSL options see OpenSSL::SSL::SSLContext.
@k-ta-yamada

This comment has been minimized.

Show comment Hide comment
@k-ta-yamada

k-ta-yamada Aug 23, 2017

https://github.com/ruby/ruby/blob/202bbda2bf5f25343e286099140fb9282880ecba/lib/webrick.rb#L88-L121

# == HTTPS
#
# To create an HTTPS server you only need to enable SSL and provide an SSL
# certificate name:
#
#   require 'webrick'
#   require 'webrick/https'
#
#   cert_name = [
#     %w[CN localhost],
#   ]
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertName => cert_name)
#
# This will start the server with a self-generated self-signed certificate.
# The certificate will be changed every time the server is restarted.
#
# To create a server with a pre-determined key and certificate you can provide
# them:
#
#   require 'webrick'
#   require 'webrick/https'
#   require 'openssl'
#
#   cert = OpenSSL::X509::Certificate.new File.read '/path/to/cert.pem'
#   pkey = OpenSSL::PKey::RSA.new File.read '/path/to/pkey.pem'
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertificate => cert,
#                                    :SSLPrivateKey => pkey)
#
Owner

k-ta-yamada commented Aug 23, 2017

https://github.com/ruby/ruby/blob/202bbda2bf5f25343e286099140fb9282880ecba/lib/webrick.rb#L88-L121

# == HTTPS
#
# To create an HTTPS server you only need to enable SSL and provide an SSL
# certificate name:
#
#   require 'webrick'
#   require 'webrick/https'
#
#   cert_name = [
#     %w[CN localhost],
#   ]
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertName => cert_name)
#
# This will start the server with a self-generated self-signed certificate.
# The certificate will be changed every time the server is restarted.
#
# To create a server with a pre-determined key and certificate you can provide
# them:
#
#   require 'webrick'
#   require 'webrick/https'
#   require 'openssl'
#
#   cert = OpenSSL::X509::Certificate.new File.read '/path/to/cert.pem'
#   pkey = OpenSSL::PKey::RSA.new File.read '/path/to/pkey.pem'
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertificate => cert,
#                                    :SSLPrivateKey => pkey)
#
@k-ta-yamada

This comment has been minimized.

Show comment Hide comment
@k-ta-yamada

k-ta-yamada Aug 23, 2017

Sinatra(WEBrick)でsslサーバをたてる
https://gist.github.com/suruseas/3489236

Owner

k-ta-yamada commented Aug 23, 2017

Sinatra(WEBrick)でsslサーバをたてる
https://gist.github.com/suruseas/3489236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment