diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 26765815..aa5f5fe3 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -639,15 +639,13 @@ module Puma fast_write client, lines.to_s return keep_alive end - - unless response_hijack - if content_length - lines.append CONTENT_LENGTH_S, content_length.to_s, line_ending - chunked = false - elsif allow_chunked - lines << TRANSFER_ENCODING_CHUNKED - chunked = true - end + + if content_length + lines.append CONTENT_LENGTH_S, content_length.to_s, line_ending + chunked = false + elsif allow_chunked + lines << TRANSFER_ENCODING_CHUNKED + chunked = true end lines << line_ending diff --git a/test/test_puma_server.rb b/test/test_puma_server.rb index 4184ef6a..2c9cd4e9 100644 --- a/test/test_puma_server.rb +++ b/test/test_puma_server.rb @@ -369,7 +369,30 @@ class TestPumaServer < Test::Unit::TestCase assert_equal "HTTP/1.0 200 OK\r\nContent-Type: plain/text\r\nContent-Length: 5\r\n\r\nhello", data end + + def test_http_10_partial_hijack_with_content_length + body_parts = ['abc', 'de'] + + @server.app = proc do |env| + hijack_lambda = proc do | io | + io.write(body_parts[0]) + io.write(body_parts[1]) + io.close + end + [200, {"Content-Length" => "5", 'rack.hijack' => hijack_lambda}, nil] + end + @server.add_tcp_listener @host, @port + @server.run + + sock = TCPSocket.new @host, @port + sock << "GET / HTTP/1.0\r\nConnection: close\r\n\r\n" + + data = sock.read + + assert_equal "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nabcde", data + end + def test_http_10_keep_alive_without_body @server.app = proc { |env| [204, {}, []] }