Skip to content

Commit dd87110

Browse files
thekuwayamaioquatix
authored andcommitted
add SSLSocket.open
1 parent b9e0c13 commit dd87110

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/openssl/ssl.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,25 @@ def session_get_cb
442442
end
443443

444444
class << self
445-
##
446-
# open is an alias to ::new
447-
alias open new
445+
446+
# call-seq:
447+
# open(remote_host, remote_port, context=nil, local_host=nil, local_port=nil)
448+
#
449+
# Creates a new instance of SSLSocket.
450+
# _remote\_host_ and _remote_port_ are used to open TCPSocket.
451+
# If _context_ is provided,
452+
# the SSL Sockets initial params will be taken from the context.
453+
# If _local\_host_ and _local\_port_ are specified,
454+
# then those parameters are used on the local end to establish the connection.
455+
#
456+
# === Example
457+
# ctx = OpenSSL::SSL::SSLContext.new
458+
# sock = OpenSSL::SSL::SSLSocket.open('localhost', 443, ctx)
459+
# sock.connect # Initiates a connection to localhost:443
460+
def open(remote_host, remote_port, context=nil, local_host=nil, local_port=nil)
461+
sock = ::TCPSocket.open(remote_host, remote_port, local_host, local_port)
462+
OpenSSL::SSL::SSLSocket.new(sock, context)
463+
end
448464
end
449465
end
450466

test/test_ssl.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ def test_ssl_with_server_cert
5656
}
5757
end
5858

59+
def test_ssl_socket_open
60+
start_server { |port|
61+
begin
62+
ctx = OpenSSL::SSL::SSLContext.new
63+
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, ctx)
64+
ssl.sync_close = true
65+
ssl.connect
66+
67+
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
68+
ensure
69+
ssl&.close
70+
end
71+
}
72+
end
73+
5974
def test_add_certificate
6075
ctx_proc = -> ctx {
6176
# Unset values set by start_server
@@ -1592,17 +1607,6 @@ def test_fileno
15921607
sock2.close
15931608
end
15941609

1595-
def test_ssl_socket_new_alias
1596-
mn = OpenSSL::SSL::SSLSocket.method(:new)
1597-
mo = OpenSSL::SSL::SSLSocket.method(:open)
1598-
1599-
if mn.inspect == "#<Method: Class#new>"
1600-
assert_equal mn.hash, mo.hash
1601-
else
1602-
assert_equal mn, mo
1603-
end
1604-
end
1605-
16061610
private
16071611

16081612
def start_server_version(version, ctx_proc = nil,

0 commit comments

Comments
 (0)