Skip to content

Commit 1680a13

Browse files
committed
Fix Net::Protocol::BufferedIO#write when sending large multi-byte string
This commit should fix Net::Protocol::BufferedIO#write when sending large multi-byte string like following example. ``` $ ruby -rnet/http -rjson -v -e "Net::HTTP.post(URI('http://httpbin.org/post'), { text: 'あ'*100_000 }.to_json, 'Content-Type' => 'application/json')" ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux] Traceback (most recent call last): 19: from -e:1:in `<main>' 18: from lib/ruby/2.6.0/net/http.rb:500:in `post' 17: from lib/ruby/2.6.0/net/http.rb:605:in `start' 16: from lib/ruby/2.6.0/net/http.rb:920:in `start' 15: from lib/ruby/2.6.0/net/http.rb:502:in `block in post' 14: from lib/ruby/2.6.0/net/http.rb:1281:in `post' 13: from lib/ruby/2.6.0/net/http.rb:1493:in `send_entity' 12: from lib/ruby/2.6.0/net/http.rb:1479:in `request' 11: from lib/ruby/2.6.0/net/http.rb:1506:in `transport_request' 10: from lib/ruby/2.6.0/net/http.rb:1506:in `catch' 9: from lib/ruby/2.6.0/net/http.rb:1507:in `block in transport_request' 8: from lib/ruby/2.6.0/net/http/generic_request.rb:123:in `exec' 7: from lib/ruby/2.6.0/net/http/generic_request.rb:189:in `send_request_with_body' 6: from lib/ruby/2.6.0/net/protocol.rb:247:in `write' 5: from lib/ruby/2.6.0/net/protocol.rb:265:in `writing' 4: from lib/ruby/2.6.0/net/protocol.rb:248:in `block in write' 3: from lib/ruby/2.6.0/net/protocol.rb:275:in `write0' 2: from lib/ruby/2.6.0/net/protocol.rb:275:in `each_with_index' 1: from lib/ruby/2.6.0/net/protocol.rb:275:in `each' lib/ruby/2.6.0/net/protocol.rb:280:in `block in write0': undefined method `bytesize' for nil:NilClass (NoMethodError) ``` [Fix rubyGH-2058] From: Eito Katagiri <eitoball@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66582 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 1f866dd commit 1680a13

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/net/protocol.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def write0(*strs)
286286
# next string
287287
end
288288
elsif len < 0
289-
str = str[len, -len]
289+
str = str.byteslice(len, -len)
290290
else # len > 0
291291
need_retry = false
292292
# next string

test/net/protocol/test_protocol.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def test_each_crlf_line
2727
end
2828
end
2929

30-
def create_mockio
30+
def create_mockio(capacity: 100)
3131
mockio = Object.new
3232
mockio.instance_variable_set(:@str, +'')
33-
mockio.instance_variable_set(:@capacity, 100)
33+
mockio.instance_variable_set(:@capacity, capacity)
3434
def mockio.string; @str; end
3535
def mockio.to_io; self; end
3636
def mockio.wait_writable(sec); sleep sec; false; end
@@ -46,7 +46,7 @@ def mockio.write_nonblock(*strs, exception: true)
4646
strs.each do |str|
4747
len1 = @str.bytesize
4848
break if @capacity <= len1
49-
@str << str[0, @capacity - @str.bytesize]
49+
@str << str.byteslice(0, @capacity - @str.bytesize)
5050
len2 = @str.bytesize
5151
len += len2 - len1
5252
end
@@ -55,6 +55,16 @@ def mockio.write_nonblock(*strs, exception: true)
5555
mockio
5656
end
5757

58+
def test_write0_multibyte
59+
mockio = create_mockio(capacity: 1)
60+
def mockio.write_nonblock(str, *strs, **kw)
61+
@str << str.byteslice(0, 1)
62+
1
63+
end
64+
io = Net::BufferedIO.new(mockio)
65+
assert_equal(3, io.write("\u3042"))
66+
end
67+
5868
def test_write0_timeout
5969
mockio = create_mockio
6070
io = Net::BufferedIO.new(mockio)

0 commit comments

Comments
 (0)