mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
webrick: add Server Name Indication (SNI)
* lib/webrick/https.rb: servername_cb implementation. * lib/webrick/ssl.rb: abstract servername_cb. * test/webrick/test_https.rb: test. [ruby-dev:50165] [Feature #13729] Author: Tietew <tietew@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59281 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4d3d6f6898
commit
08bdbef5ca
3 changed files with 143 additions and 0 deletions
84
test/webrick/test_https.rb
Normal file
84
test/webrick/test_https.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
# frozen_string_literal: false
|
||||
require "test/unit"
|
||||
require "net/http"
|
||||
require "webrick"
|
||||
require "webrick/https"
|
||||
require "webrick/utils"
|
||||
require_relative "utils"
|
||||
|
||||
class TestWEBrickHTTPS < Test::Unit::TestCase
|
||||
empty_log = Object.new
|
||||
def empty_log.<<(str)
|
||||
assert_equal('', str)
|
||||
self
|
||||
end
|
||||
NoLog = WEBrick::Log.new(empty_log, WEBrick::BasicLog::WARN)
|
||||
|
||||
class HTTPSNITest < ::Net::HTTP
|
||||
attr_accessor :sni_hostname
|
||||
|
||||
def ssl_socket_connect(s, timeout)
|
||||
s.hostname = sni_hostname
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def teardown
|
||||
WEBrick::Utils::TimeoutHandler.terminate
|
||||
super
|
||||
end
|
||||
|
||||
def https_get(addr, port, hostname, path)
|
||||
http = HTTPSNITest.new(addr, port)
|
||||
http.use_ssl = true
|
||||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
http.sni_hostname = hostname
|
||||
req = Net::HTTP::Get.new(path)
|
||||
req["Host"] = "#{hostname}:#{port}"
|
||||
http.request(req).body
|
||||
end
|
||||
|
||||
def test_sni
|
||||
config = {
|
||||
:ServerName => "localhost",
|
||||
:SSLEnable => true,
|
||||
:SSLCertName => "/CN=locahost",
|
||||
}
|
||||
TestWEBrick.start_httpserver(config){|server, addr, port, log|
|
||||
server.mount_proc("/") {|req, res| res.body = "master" }
|
||||
|
||||
vhost_config1 = {
|
||||
:ServerName => "vhost1",
|
||||
:Port => port,
|
||||
:DoNotListen => true,
|
||||
:Logger => NoLog,
|
||||
:AccessLog => [],
|
||||
:SSLEnable => true,
|
||||
:SSLCertName => "/CN=vhost1",
|
||||
}
|
||||
vhost1 = WEBrick::HTTPServer.new(vhost_config1)
|
||||
vhost1.mount_proc("/") {|req, res| res.body = "vhost1" }
|
||||
server.virtual_host(vhost1)
|
||||
|
||||
vhost_config2 = {
|
||||
:ServerName => "vhost2",
|
||||
:ServerAlias => ["vhost2alias"],
|
||||
:Port => port,
|
||||
:DoNotListen => true,
|
||||
:Logger => NoLog,
|
||||
:AccessLog => [],
|
||||
:SSLEnable => true,
|
||||
:SSLCertName => "/CN=vhost2",
|
||||
}
|
||||
vhost2 = WEBrick::HTTPServer.new(vhost_config2)
|
||||
vhost2.mount_proc("/") {|req, res| res.body = "vhost2" }
|
||||
server.virtual_host(vhost2)
|
||||
|
||||
assert_equal("master", https_get(addr, port, "localhost", "/localhost"))
|
||||
assert_equal("master", https_get(addr, port, "unknown", "/unknown"))
|
||||
assert_equal("vhost1", https_get(addr, port, "vhost1", "/vhost1"))
|
||||
assert_equal("vhost2", https_get(addr, port, "vhost2", "/vhost2"))
|
||||
assert_equal("vhost2", https_get(addr, port, "vhost2alias", "/vhost2alias"))
|
||||
}
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue