Skip to content

Commit 3237ef4

Browse files
authored
Feature detect to make net/http usable with JRuby
Handle missing session_new_cb= and do not call session_cache_mode=, as JRuby SSL does not support these methods.
1 parent 4d47e34 commit 3237ef4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/net/http.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,14 @@ def connect
10511051
end
10521052
end
10531053
@ssl_context.set_params(ssl_parameters)
1054-
@ssl_context.session_cache_mode =
1055-
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
1056-
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
1057-
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
1054+
unless @ssl_context.session_cache_mode.nil? # a dummy method on JRuby
1055+
@ssl_context.session_cache_mode =
1056+
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
1057+
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
1058+
end
1059+
if @ssl_context.respond_to?(:session_new_cb) # not implemented under JRuby
1060+
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
1061+
end
10581062

10591063
# Still do the post_connection_check below even if connecting
10601064
# to IP address

test/net/http/test_https.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,14 @@ def test_session_reuse
152152
end
153153

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

159160
http.start
160-
assert_equal true, http.instance_variable_get(:@socket).io.session_reused?
161+
session_reused = http.instance_variable_get(:@socket).io.session_reused?
162+
assert_true session_reused unless session_reused.nil? # can not detect re-use under JRuby
161163
assert_equal $test_net_http_data, http.get("/").body
162164
http.finish
163165
end
@@ -301,7 +303,7 @@ def test_max_version
301303
ex = assert_raise(OpenSSL::SSL::SSLError){
302304
http.request_get("/") {|res| }
303305
}
304-
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version/
306+
re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version|No appropriate protocol/
305307
assert_match(re_msg, ex.message)
306308
end
307309

0 commit comments

Comments
 (0)