Skip to content

Commit

Permalink
Merge pull request #743 from ruby/close-read-write
Browse files Browse the repository at this point in the history
Introduce basic support for `close_read` and `close_write`.
  • Loading branch information
rhenium authored Apr 30, 2024
2 parents 362a69a + 0697f2f commit a7f2d22
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,32 @@ def session
nil
end

# Close the stream for reading.
# This method is ignored by OpenSSL as there is no reasonable way to
# implement it, but exists for compatibility with IO.
def close_read
# Unsupported and ignored.
# Just don't read any more.
end

# Closes the stream for writing. The behavior of this method depends on
# the version of OpenSSL and the TLS protocol in use.
#
# - Sends a 'close_notify' alert to the peer.
# - Does not wait for the peer's 'close_notify' alert in response.
#
# In TLS 1.2 and earlier:
# - On receipt of a 'close_notify' alert, responds with a 'close_notify'
# alert of its own and close down the connection immediately,
# discarding any pending writes.
#
# Therefore, on TLS 1.2, this method will cause the connection to be
# completely shut down. On TLS 1.3, the connection will remain open for
# reading only.
def close_write
stop
end

private

def using_anon_cipher?
Expand Down
24 changes: 24 additions & 0 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ def test_socket_open_with_local_address_port_context
}
end

def test_socket_close_write
server_proc = proc do |ctx, ssl|
message = ssl.read
ssl.write(message)
ssl.close_write
ensure
ssl.close
end

start_server(server_proc: server_proc) do |port|
ctx = OpenSSL::SSL::SSLContext.new
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, context: ctx)
ssl.sync_close = true
ssl.connect

message = "abc"*1024
ssl.write message
ssl.close_write
assert_equal message, ssl.read
ensure
ssl&.close
end
end

def test_add_certificate
ctx_proc = -> ctx {
# Unset values set by start_server
Expand Down

0 comments on commit a7f2d22

Please sign in to comment.