Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 33299) +++ lib/net/http.rb (working copy) @@ -586,6 +586,7 @@ module Net #:nodoc: @debug_output = nil @use_ssl = false @ssl_context = nil + @ssl_session = nil @enable_post_connection_check = true @compression = nil @sspi_enabled = false @@ -771,9 +772,14 @@ module Net #:nodoc: ssl_parameters[name] = value end end - @ssl_context = OpenSSL::SSL::SSLContext.new - @ssl_context.set_params(ssl_parameters) + + unless @ssl_context then + @ssl_context = OpenSSL::SSL::SSLContext.new + @ssl_context.set_params(ssl_parameters) + end + s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context) + s.session = @ssl_session if @ssl_session s.sync_close = true end @socket = BufferedIO.new(s) @@ -800,6 +806,7 @@ module Net #:nodoc: if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE s.post_connection_check(@address) end + @ssl_session = s.session rescue => exception D "Conn close because of connect error #{exception}" @socket.close if @socket and not @socket.closed? Index: test/net/http/test_https.rb =================================================================== --- test/net/http/test_https.rb (revision 33299) +++ test/net/http/test_https.rb (working copy) @@ -59,6 +59,29 @@ class TestNetHTTPS < Test::Unit::TestCas skip $! end + def test_session_reuse + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + http.verify_callback = Proc.new do |preverify_ok, store_ctx| + store_ctx.current_cert.to_der == config('ssl_certificate').to_der + end + + http.start + http.get("/") + http.finish + + http.start + http.get("/") + http.finish # three times due to possible bug in OpenSSL 0.9.8 + + http.start + http.get("/") + + socket = http.instance_variable_get(:@socket).io + + assert socket.session_reused? + end + if ENV["RUBY_OPENSSL_TEST_ALL"] def test_verify http = Net::HTTP.new("ssl.netlab.jp", 443)