Skip to content

Commit

Permalink
Ensure that binary buffer is used at all times.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 5, 2020
1 parent bc50396 commit 0aaf274
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
31 changes: 27 additions & 4 deletions lib/openssl/buffering.rb
Expand Up @@ -22,6 +22,29 @@
module OpenSSL::Buffering
include Enumerable

# A buffer which will retain binary encoding.
class Buffer < String
BINARY = Encoding::BINARY

def initialize
super

force_encoding(BINARY)
end

def << string
if string.encoding == BINARY
super(string)
else
super(string.b)
end

return self
end

alias concat <<
end

##
# The "sync mode" of the SSLSocket.
#
Expand All @@ -40,7 +63,7 @@ module OpenSSL::Buffering
def initialize(*)
super
@eof = false
@rbuffer = String.new
@rbuffer = Buffer.new
@sync = @io.sync
end

Expand Down Expand Up @@ -312,7 +335,7 @@ def eof?
# buffer is flushed to the underlying socket.

def do_write(s)
@wbuffer = String.new unless defined? @wbuffer
@wbuffer = Buffer.new unless defined? @wbuffer
@wbuffer << s
@wbuffer.force_encoding(Encoding::BINARY)
@sync ||= false
Expand Down Expand Up @@ -398,7 +421,7 @@ def <<(s)
# See IO#puts for full details.

def puts(*args)
s = String.new
s = Buffer.new
if args.empty?
s << "\n"
end
Expand All @@ -416,7 +439,7 @@ def puts(*args)
# See IO#print for full details.

def print(*args)
s = String.new
s = Buffer.new
args.each{ |arg| s << arg.to_s }
do_write(s)
nil
Expand Down
9 changes: 8 additions & 1 deletion test/test_buffering.rb
Expand Up @@ -10,7 +10,7 @@ class IO
attr_accessor :sync

def initialize
@io = String.new
@io = Buffer.new
def @io.sync
true
end
Expand Down Expand Up @@ -41,6 +41,13 @@ def setup
@io = IO.new
end

def test_encoding
@io.write '😊'
@io.flush

assert_equal @io.string.encoding, Encoding::BINARY
end

def test_flush
@io.write 'a'

Expand Down

0 comments on commit 0aaf274

Please sign in to comment.