Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request.rb - fix chunked body assembly for ascii incompatible encodings #2585

Merged
merged 1 commit into from Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/puma/request.rb
Expand Up @@ -148,8 +148,9 @@ def handle_request(client, lines)
res_body.each do |part|
next if part.bytesize.zero?
if chunked
str = part.bytesize.to_s(16) << line_ending << part << line_ending
fast_write io, str
fast_write io, (part.bytesize.to_s(16) << line_ending)
fast_write io, part # part may have different encoding
fast_write io, line_ending
else
fast_write io, part
end
Expand Down
26 changes: 25 additions & 1 deletion test/test_puma_server.rb
Expand Up @@ -839,6 +839,31 @@ def test_chunked_keep_alive_two_back_to_back_with_set_remote_address
sock.close
end

def test_chunked_encoding
enc = Encoding::UTF_16LE
str = "──иї_テスト──\n".encode enc

server_run app: ->(env) {
hdrs = {}
hdrs['Content-Type'] = "text; charset=#{enc.to_s.downcase}"

body = Enumerator.new do |yielder|
100.times do |entry|
yielder << str
end
yielder << "\nHello World\n".encode(enc)
end

[200, hdrs, body]
}

body = Net::HTTP.start @host, @port do |http|
http.request(Net::HTTP::Get.new '/').body.force_encoding(enc)
end
assert_includes body, str
assert_equal enc, body.encoding
end

def test_empty_header_values
server_run app: ->(env) { [200, {"X-Empty-Header" => ""}, []] }

Expand Down Expand Up @@ -1180,7 +1205,6 @@ def test_custom_io_selector

@server = Puma::Server.new @app, @events, {:io_selector_backend => backend}
@server.run
@server.stop

selector = @server.instance_variable_get(:@reactor).instance_variable_get(:@selector)

Expand Down