Skip to content

Commit

Permalink
Feature detect to make net/http usable with JRuby
Browse files Browse the repository at this point in the history
Handle missing session_new_cb= and do not call
session_cache_mode=, as JRuby SSL does not support
these methods.
  • Loading branch information
kares authored Apr 20, 2022
1 parent 4d47e34 commit 3237ef4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,14 @@ def connect
end
end
@ssl_context.set_params(ssl_parameters)
@ssl_context.session_cache_mode =
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
unless @ssl_context.session_cache_mode.nil? # a dummy method on JRuby
@ssl_context.session_cache_mode =
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
end
if @ssl_context.respond_to?(:session_new_cb) # not implemented under JRuby
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
end

# Still do the post_connection_check below even if connecting
# to IP address
Expand Down
8 changes: 5 additions & 3 deletions test/net/http/test_https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ def test_session_reuse
end

http.start
assert_equal false, http.instance_variable_get(:@socket).io.session_reused?
session_reused = http.instance_variable_get(:@socket).io.session_reused?
assert_false session_reused unless session_reused.nil? # can not detect re-use under JRuby
http.get("/")
http.finish

http.start
assert_equal true, http.instance_variable_get(:@socket).io.session_reused?
session_reused = http.instance_variable_get(:@socket).io.session_reused?
assert_true session_reused unless session_reused.nil? # can not detect re-use under JRuby
assert_equal $test_net_http_data, http.get("/").body
http.finish
end
Expand Down Expand Up @@ -301,7 +303,7 @@ def test_max_version
ex = assert_raise(OpenSSL::SSL::SSLError){
http.request_get("/") {|res| }
}
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version/
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version|No appropriate protocol/
assert_match(re_msg, ex.message)
end

Expand Down

0 comments on commit 3237ef4

Please sign in to comment.