Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c352cf

Browse files
committed
Merge pull request #189 from eitoball/encode_to_binary_before_writing_with_ssl_socket
Encode to binary before writing with OpenSSL::SSL::SSLSocket
2 parents d573a3b + 24a2594 commit 5c352cf

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

ext/openssl/lib/openssl/buffering.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def readpartial(maxlen, buf=nil)
115115
# underlying IO is writable.
116116
#
117117
# So OpenSSL::Buffering#read_nonblock needs two rescue clause as follows.
118-
#
118+
#
119119
# # emulates blocking read (readpartial).
120120
# begin
121121
# result = ssl.read_nonblock(maxlen)
@@ -225,6 +225,7 @@ def eof?
225225
def do_write(s)
226226
@wbuffer = "" unless defined? @wbuffer
227227
@wbuffer << s
228+
@wbuffer.force_encoding(Encoding::BINARY)
228229
@sync ||= false
229230
if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
230231
remain = idx ? idx + $/.size : @wbuffer.length
@@ -247,7 +248,7 @@ def do_write(s)
247248

248249
def write(s)
249250
do_write(s)
250-
s.length
251+
s.bytesize
251252
end
252253

253254
# Writes _str_ in the non-blocking manner.
@@ -270,7 +271,7 @@ def write(s)
270271
# underlying IO is writable.
271272
#
272273
# So OpenSSL::Buffering#write_nonblock needs two rescue clause as follows.
273-
#
274+
#
274275
# # emulates blocking write.
275276
# begin
276277
# result = ssl.write_nonblock(str)

test-mri/test/openssl/test_buffering.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ def @io.syswrite *a
6363
refute @io.sync, 'sync must not change'
6464
end
6565

66+
def test_print
67+
str = "123\r\n".force_encoding(Encoding::UTF_8)
68+
@io.print(str)
69+
assert(Encoding::BINARY, @io.string.encoding)
70+
end
71+
6672
end if defined?(OpenSSL)

test-mri/test/openssl/test_ssl.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,36 @@ def test_unset_OP_ALL
685685
ssl.close
686686
}
687687
end
688+
689+
def test_multibyte_read_write
690+
#German a umlaut
691+
auml = [%w{ C3 A4 }.join('')].pack('H*')
692+
auml.force_encoding(Encoding::UTF_8)
693+
694+
str = nil
695+
num_written = nil
696+
697+
server_proc = Proc.new {|ctx, ssl|
698+
cmp = ssl.read
699+
raw_size = cmp.size
700+
cmp.force_encoding(Encoding::UTF_8)
701+
assert_equal(str, cmp)
702+
assert_equal(num_written, raw_size)
703+
ssl.close
704+
}
705+
706+
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :server_proc => server_proc){|server, port|
707+
[10, 1000, 100000].each {|i|
708+
sock = TCPSocket.new("127.0.0.1", port)
709+
ssl = OpenSSL::SSL::SSLSocket.new(sock)
710+
ssl.sync_close = true
711+
ssl.connect
712+
str = auml * i
713+
num_written = ssl.write(str)
714+
ssl.close
715+
}
716+
}
717+
end
688718
end
689719

690720
end

0 commit comments

Comments
 (0)