Skip to content

Commit ac1490b

Browse files
committed
Add SSLSocket#getbyte
Normal sockets respond to `getbyte`, so we should make SSLSocket respond to `getbyte` as well. This way we can substitute SSLSockets for regular sockets.
1 parent d172036 commit ac1490b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/openssl/buffering.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,27 @@ def consume_rbuff(size=nil)
9999
end
100100
end
101101

102+
if "".respond_to?(:unpack1)
103+
def unpack_byte(str)
104+
str.unpack1("C")
105+
end
106+
else
107+
def unpack_byte(str)
108+
str.unpack("C").first
109+
end
110+
end
111+
102112
public
103113

114+
# call-seq:
115+
# ssl.getbyte => 81
116+
#
117+
# Get the next 8bit byte from `ssl`. Returns `nil` on EOF
118+
def getbyte
119+
byte = read(1)
120+
byte && unpack_byte(byte)
121+
end
122+
104123
##
105124
# Reads _size_ bytes from the stream. If _buf_ is provided it must
106125
# reference a string which will receive the data.

test/openssl/test_ssl.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ def test_sysread_and_syswrite
185185
}
186186
end
187187

188+
def test_getbyte
189+
start_server { |port|
190+
server_connect(port) { |ssl|
191+
str = +("x" * 100 + "\n")
192+
ssl.syswrite(str)
193+
newstr = str.bytesize.times.map { |i|
194+
ssl.getbyte
195+
}.pack("C*")
196+
assert_equal(str, newstr)
197+
}
198+
}
199+
end
200+
188201
def test_sync_close
189202
start_server do |port|
190203
begin

test/openssl/ut_eof.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
if defined?(OpenSSL)
55

66
module OpenSSL::TestEOF
7+
def test_getbyte_eof
8+
open_file("") {|f| assert_nil f.getbyte }
9+
end
10+
711
def test_eof_0
812
open_file("") {|f|
913
assert_equal("", f.read(0))

0 commit comments

Comments
 (0)