Skip to content

Commit

Permalink
Merge pull request #438 from ruby/getbyte
Browse files Browse the repository at this point in the history
Add SSLSocket#getbyte
  • Loading branch information
rhenium authored Apr 17, 2021
2 parents d172036 + ac1490b commit 03cb3d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/openssl/buffering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,27 @@ def consume_rbuff(size=nil)
end
end

if "".respond_to?(:unpack1)
def unpack_byte(str)
str.unpack1("C")
end
else
def unpack_byte(str)
str.unpack("C").first
end
end

public

# call-seq:
# ssl.getbyte => 81
#
# Get the next 8bit byte from `ssl`. Returns `nil` on EOF
def getbyte
byte = read(1)
byte && unpack_byte(byte)
end

##
# Reads _size_ bytes from the stream. If _buf_ is provided it must
# reference a string which will receive the data.
Expand Down
13 changes: 13 additions & 0 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ def test_sysread_and_syswrite
}
end

def test_getbyte
start_server { |port|
server_connect(port) { |ssl|
str = +("x" * 100 + "\n")
ssl.syswrite(str)
newstr = str.bytesize.times.map { |i|
ssl.getbyte
}.pack("C*")
assert_equal(str, newstr)
}
}
end

def test_sync_close
start_server do |port|
begin
Expand Down
4 changes: 4 additions & 0 deletions test/openssl/ut_eof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
if defined?(OpenSSL)

module OpenSSL::TestEOF
def test_getbyte_eof
open_file("") {|f| assert_nil f.getbyte }
end

def test_eof_0
open_file("") {|f|
assert_equal("", f.read(0))
Expand Down

0 comments on commit 03cb3d5

Please sign in to comment.