Skip to content

Commit 17244ee

Browse files
committed
Fix socket connections over ssl
1 parent 843123a commit 17244ee

File tree

1 file changed

+5
-57
lines changed

1 file changed

+5
-57
lines changed

lib/mongo/socket/ssl.rb

+5-57
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ class SSL < Socket
3838
# @return [ Float ] timeout The connection timeout.
3939
attr_reader :timeout
4040

41-
# Close the socket.
42-
#
43-
# @example Close the socket.
44-
# socket.close
45-
#
46-
# @return [ true ] Always true.
47-
#
48-
# @since 2.0.0
49-
def close
50-
socket.sysclose rescue true
51-
true
52-
end
53-
5441
# Establishes a socket connection.
5542
#
5643
# @example Connect the socket.
@@ -91,29 +78,10 @@ def initialize(host, port, timeout, family, options = {})
9178
@family = family
9279
@tcp_socket = ::Socket.new(family, SOCK_STREAM, 0)
9380
@tcp_socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
94-
end
95-
96-
# Will read all data from the socket for the provided number of bytes.
97-
# If less data is returned than requested, an exception will be raised.
98-
#
99-
# @example Read all the requested data from the socket.
100-
# socket.read(4096)
101-
#
102-
# @param [ Integer ] length The number of bytes to read.
103-
#
104-
# @raise [ Mongo::SocketError ] If not all data is returned.
105-
#
106-
# @return [ Object ] The data from the socket.
107-
#
108-
# @since 2.0.0
109-
def read(length)
110-
handle_errors do
111-
data = read_from_socket(length)
112-
while data.length < length
113-
data << read_from_socket(length - data.length)
114-
end
115-
data
116-
end
81+
encoded_timeout = [ timeout, 0 ].pack(TIMEOUT_PACK)
82+
@tcp_socket.set_encoding(BSON::BINARY)
83+
@tcp_socket.setsockopt(SOL_SOCKET, SO_RCVTIMEO, encoded_timeout)
84+
@tcp_socket.setsockopt(SOL_SOCKET, SO_SNDTIMEO, encoded_timeout)
11785
end
11886

11987
# Read a single byte from the socket.
@@ -125,21 +93,7 @@ def read(length)
12593
#
12694
# @since 2.0.0
12795
def readbyte
128-
handle_errors { read_from_socket(1) }
129-
end
130-
131-
# Writes data to the socket instance.
132-
#
133-
# @example Write to the socket.
134-
# socket.write(data)
135-
#
136-
# @param [ Object ] data The data to be written.
137-
#
138-
# @return [ Integer ] The length of bytes written to the socket.
139-
#
140-
# @since 2.0.0
141-
def write(data)
142-
handle_errors { socket.syswrite(data) }
96+
handle_errors { socket.read(1) }
14397
end
14498

14599
private
@@ -159,12 +113,6 @@ def create_context(options)
159113
context
160114
end
161115

162-
def read_from_socket(length)
163-
Timeout::timeout(timeout, Error::SocketTimeoutError) do
164-
socket.sysread(length) || String.new
165-
end
166-
end
167-
168116
def verify_certificate!(socket)
169117
if context.verify_mode == OpenSSL::SSL::VERIFY_PEER
170118
unless OpenSSL::SSL.verify_certificate_identity(socket.peer_cert, host)

0 commit comments

Comments
 (0)